MSP-5083 fix blocking user
This commit is contained in:
1 parent
40b37f8900
commit
558682da1e
10 files changed
+138
-92
No files matched your search
+30
-5
@@ -31,6 +31,7 @@ type userRepository interface {
|
||||
GetUserByEmail(ctx context.Context, email string) (*entity.User, error)
|
||||
UpsertBySudirUserId(ctx context.Context, user entity.User) (*entity.User, error)
|
||||
UpdateUser(ctx context.Context, id int64, user entity.UpdateUser) (*entity.User, error)
|
||||
UpdateLastActiveAt(ctx context.Context, userId int64, lastActiveAt time.Time) error
|
||||
}
|
||||
|
||||
type tokenService interface {
|
||||
@@ -118,6 +119,12 @@ func (a Auth) Login(ctx context.Context, request domain.LoginRequest) (*domain.L
|
||||
return errors.WithMessage(err, "generate token")
|
||||
}
|
||||
|
||||
lastActiveAt := time.Now().UTC()
|
||||
err = tx.UpdateLastActiveAt(ctx, user.Id, lastActiveAt)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "update user last_active_at")
|
||||
}
|
||||
|
||||
a.auditService.SaveAuditAsync(ctx, user.Id, "Успешный вход через форму входа", entity.EventSuccessLogin)
|
||||
|
||||
return nil
|
||||
@@ -185,6 +192,12 @@ func (a Auth) LoginWithSudir(ctx context.Context, request domain.LoginSudirReque
|
||||
return errors.WithMessage(err, "generate token")
|
||||
}
|
||||
|
||||
lastActiveAt := time.Now().UTC()
|
||||
err = tx.UpdateLastActiveAt(ctx, user.Id, lastActiveAt)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "update user last_active_at")
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
@@ -201,12 +214,24 @@ func (a Auth) LoginWithSudir(ctx context.Context, request domain.LoginSudirReque
|
||||
}
|
||||
|
||||
func (a Auth) Logout(ctx context.Context, adminId int64) error {
|
||||
err := a.tokenService.RevokeAllByUserId(ctx, adminId)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "revoke all tokens by user id")
|
||||
}
|
||||
err := a.txRunner.AuthTransaction(ctx, func(ctx context.Context, tx AuthTransaction) error {
|
||||
err := a.tokenService.RevokeAllByUserId(ctx, adminId)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "revoke all tokens by user id")
|
||||
}
|
||||
|
||||
a.auditService.SaveAuditAsync(ctx, adminId, "Выход", entity.EventSuccessLogout)
|
||||
lastActiveAt := time.Now().UTC()
|
||||
err = tx.UpdateLastActiveAt(ctx, adminId, lastActiveAt)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "update user last_active_at")
|
||||
}
|
||||
|
||||
a.auditService.SaveAuditAsync(ctx, adminId, "Выход", entity.EventSuccessLogout)
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "auth transaction")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -19,11 +19,8 @@ type AuditRepo interface {
|
||||
SaveAuditAsync(ctx context.Context, userId int64, message string, event string)
|
||||
}
|
||||
|
||||
type TokenRepo interface {
|
||||
LastAccessNotBlockedUsers(ctx context.Context) (map[int64]time.Time, error)
|
||||
}
|
||||
|
||||
type UserRepo interface {
|
||||
LastAccessNotBlockedUsers(ctx context.Context) (map[int64]time.Time, error)
|
||||
Block(ctx context.Context, userId int) (*entity.User, error)
|
||||
}
|
||||
|
||||
@@ -36,7 +33,6 @@ type UserRoleRepo interface {
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
tokenRepo TokenRepo
|
||||
userRepo UserRepo
|
||||
auditRepo AuditRepo
|
||||
userRoleRepo UserRoleRepo
|
||||
@@ -47,7 +43,6 @@ type Service struct {
|
||||
}
|
||||
|
||||
func NewInactiveBlocker(
|
||||
tokensRepo TokenRepo,
|
||||
userRepo UserRepo,
|
||||
auditRepo AuditRepo,
|
||||
userRoleRepo UserRoleRepo,
|
||||
@@ -56,7 +51,6 @@ func NewInactiveBlocker(
|
||||
logger log.Logger,
|
||||
) Service {
|
||||
return Service{
|
||||
tokenRepo: tokensRepo,
|
||||
userRepo: userRepo,
|
||||
auditRepo: auditRepo,
|
||||
userRoleRepo: userRoleRepo,
|
||||
@@ -79,7 +73,7 @@ func (w Service) Handle(ctx context.Context, _ bgjob.Job) bgjob.Result {
|
||||
}
|
||||
|
||||
func (w Service) do(ctx context.Context) error {
|
||||
lastAccess, err := w.tokenRepo.LastAccessNotBlockedUsers(ctx)
|
||||
lastAccess, err := w.userRepo.LastAccessNotBlockedUsers(ctx)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "get last access times")
|
||||
}
|
||||
|
||||
+44
-25
@@ -34,6 +34,7 @@ type UserRepo interface {
|
||||
Insert(ctx context.Context, user entity.User) (int, error)
|
||||
ChangeBlockStatus(ctx context.Context, userId int) (bool, error)
|
||||
ChangePassword(ctx context.Context, userId int64, newPassword string) error
|
||||
UpdateLastActiveAt(ctx context.Context, userId int64, lastActiveAt time.Time) error
|
||||
}
|
||||
|
||||
type TokenRepo interface {
|
||||
@@ -371,7 +372,7 @@ func (u User) Block(ctx context.Context, adminId int64, userId int) error {
|
||||
}
|
||||
|
||||
if blocked {
|
||||
err := tx.UpdateStatusByUserId(ctx, userId, entity.TokenStatusRevoked)
|
||||
err = tx.UpdateStatusByUserId(ctx, userId, entity.TokenStatusRevoked)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "revoke tokens")
|
||||
}
|
||||
@@ -379,6 +380,11 @@ func (u User) Block(ctx context.Context, adminId int64, userId int) error {
|
||||
|
||||
if !blocked {
|
||||
userBlocked = "разблокировка"
|
||||
lastActiveAt := time.Now().UTC()
|
||||
err = tx.UpdateLastActiveAt(ctx, int64(userId), lastActiveAt)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "update user last_active_at")
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -444,33 +450,46 @@ func diffToString(a map[string]any, b map[string]any) string {
|
||||
}
|
||||
|
||||
func (u User) ChangePassword(ctx context.Context, adminId int64, oldPassword string, newPassword string) error {
|
||||
admin, err := u.userRepo.GetUserById(ctx, adminId)
|
||||
err := u.txRunner.UserTransaction(ctx, func(ctx context.Context, tx UserTransaction) error {
|
||||
admin, err := tx.GetUserById(ctx, adminId)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "user.service.ChangePassword: get user by id")
|
||||
}
|
||||
|
||||
if err = bcrypt.CompareHashAndPassword([]byte(admin.Password), []byte(oldPassword)); err != nil {
|
||||
u.auditService.SaveAuditAsync(ctx, adminId, "Указан неверный старый пароль", entity.EventErrorPasswordChange)
|
||||
return domain.ErrInvalidPassword
|
||||
}
|
||||
|
||||
encryptedPassword, err := u.cryptPassword(newPassword)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "user.service.ChangePassword: crypt new password")
|
||||
}
|
||||
|
||||
err = tx.ChangePassword(ctx, adminId, encryptedPassword)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "user.service.ChangePassword: change password ")
|
||||
}
|
||||
|
||||
err = u.tokenService.RevokeAllByUserId(ctx, adminId)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "revoke all tokens by user id")
|
||||
}
|
||||
|
||||
lastActiveAt := time.Now().UTC()
|
||||
err = tx.UpdateLastActiveAt(ctx, adminId, lastActiveAt)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "update user last_active_at")
|
||||
}
|
||||
|
||||
u.auditService.SaveAuditAsync(ctx, adminId, "Сменил пароль", entity.EventUserPasswordChanged)
|
||||
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "user.service.ChangePassword: get user by id")
|
||||
return errors.WithMessage(err, "user transaction")
|
||||
}
|
||||
|
||||
if err := bcrypt.CompareHashAndPassword([]byte(admin.Password), []byte(oldPassword)); err != nil {
|
||||
u.auditService.SaveAuditAsync(ctx, adminId, "Указан неверный старый пароль", entity.EventErrorPasswordChange)
|
||||
return domain.ErrInvalidPassword
|
||||
}
|
||||
|
||||
encryptedPassword, err := u.cryptPassword(newPassword)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "user.service.ChangePassword: crypt new password")
|
||||
}
|
||||
|
||||
err = u.userRepo.ChangePassword(ctx, adminId, encryptedPassword)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "user.service.ChangePassword: change password ")
|
||||
}
|
||||
|
||||
err = u.tokenService.RevokeAllByUserId(ctx, adminId)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "revoke all tokens by user id")
|
||||
}
|
||||
|
||||
u.auditService.SaveAuditAsync(ctx, adminId, "Сменил пароль", entity.EventUserPasswordChanged)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user