From c1d4680a02142e1c0c36951e13bb9aea9fa9a4e5 Mon Sep 17 00:00:00 2001 From: gr-dv Date: Thu, 12 Oct 2023 08:27:14 +0300 Subject: [PATCH] MSP-2243 update interface --- service/auth.go | 12 ++++++------ service/role.go | 20 +++++++++++--------- service/user.go | 4 ++++ service/worker/inactive.go | 5 +++-- 4 files changed, 24 insertions(+), 17 deletions(-) diff --git a/service/auth.go b/service/auth.go index e584688..f4373fb 100644 --- a/service/auth.go +++ b/service/auth.go @@ -24,7 +24,7 @@ type AuthTransactionRunner interface { } type auditService interface { - SaveAuditAsync(ctx context.Context, userId int64, message string) + SaveAuditAsync(ctx context.Context, userId int64, message string, event string) } type userRepository interface { @@ -103,13 +103,13 @@ func (a Auth) Login(ctx context.Context, request domain.LoginRequest) (*domain.L } if user.Blocked { - a.auditService.SaveAuditAsync(ctx, user.Id, "Неуспешный вход. Пользователь заблокирован") + a.auditService.SaveAuditAsync(ctx, user.Id, "Неуспешный вход. Пользователь заблокирован", entity.EventErrorLogin) return errors.WithMessagef(domain.ErrUnauthenticated, "user '%d' is blocked", user.Id) } err = bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(request.Password)) if err != nil { - a.auditService.SaveAuditAsync(ctx, user.Id, "Неуспешный вход. Неверный пароль") + a.auditService.SaveAuditAsync(ctx, user.Id, "Неуспешный вход. Неверный пароль", entity.EventErrorLogin) return errors.WithMessage(domain.ErrUnauthenticated, "wrong password") } @@ -118,7 +118,7 @@ func (a Auth) Login(ctx context.Context, request domain.LoginRequest) (*domain.L return errors.WithMessage(err, "generate token") } - a.auditService.SaveAuditAsync(ctx, user.Id, "Успешный вход через форму входа") + a.auditService.SaveAuditAsync(ctx, user.Id, "Успешный вход через форму входа", entity.EventSuccessLogin) updateEntity := entity.UpdateUser{ FirstName: user.FirstName, @@ -211,7 +211,7 @@ func (a Auth) LoginWithSudir(ctx context.Context, request domain.LoginSudirReque return nil, err } - a.auditService.SaveAuditAsync(ctx, user.Id, "Успешный вход через СУДИР") + a.auditService.SaveAuditAsync(ctx, user.Id, "Успешный вход через СУДИР", entity.EventSuccessLogin) return &domain.LoginResponse{ Token: tokenString, @@ -226,7 +226,7 @@ func (a Auth) Logout(ctx context.Context, adminId int64) error { return errors.WithMessage(err, "revoke all tokens by user id") } - a.auditService.SaveAuditAsync(ctx, adminId, "Выход") + a.auditService.SaveAuditAsync(ctx, adminId, "Выход", entity.EventSuccessLogout) return nil } diff --git a/service/role.go b/service/role.go index 084ee00..d931aa7 100644 --- a/service/role.go +++ b/service/role.go @@ -65,10 +65,10 @@ func (u Role) Create(ctx context.Context, req domain.CreateRoleRequest, adminId return nil, errors.WithMessage(err, "create role") } - u.auditService.SaveAuditAsync(ctx, adminId, fmt.Sprintf("Роль. Создание новой роли %s. Причина: %s", - req.Name, - req.ChangeMessage, - )) + u.auditService.SaveAuditAsync(ctx, adminId, + fmt.Sprintf("Роль. Создание новой роли %s. Причина: %s", req.Name, req.ChangeMessage), + entity.EventRoleChanged, + ) result := u.toDomain(*role) return &result, nil @@ -104,10 +104,10 @@ func (u Role) Update(ctx context.Context, req domain.UpdateRoleRequest, adminId return nil, errors.WithMessage(err, "update role") } - u.auditService.SaveAuditAsync(ctx, adminId, fmt.Sprintf("Роль. Изменение роли %s. Причина: %s", - req.Name, - req.ChangeMessage, - )) + u.auditService.SaveAuditAsync(ctx, adminId, + fmt.Sprintf("Роль. Изменение роли %s. Причина: %s", req.Name, req.ChangeMessage), + entity.EventRoleChanged, + ) result := u.toDomain(*role) return &result, nil } @@ -118,7 +118,9 @@ func (u Role) Delete(ctx context.Context, req domain.DeleteRoleRequest, adminId return errors.WithMessage(err, "delete role") } - u.auditService.SaveAuditAsync(ctx, adminId, fmt.Sprintf("Роль. Удаление роли. ID: %d", req.Id)) + u.auditService.SaveAuditAsync(ctx, adminId, fmt.Sprintf("Роль. Удаление роли. ID: %d", req.Id), + entity.EventRoleChanged, + ) return nil } diff --git a/service/user.go b/service/user.go index 1141547..9fdcf68 100644 --- a/service/user.go +++ b/service/user.go @@ -198,6 +198,7 @@ func (u User) CreateUser(ctx context.Context, req domain.CreateUserRequest, admi u.auditService.SaveAuditAsync(ctx, adminId, fmt.Sprintf("Пользователь. Создание пользователя %s.", usr.Email), + entity.EventUserChanged, ) result := u.toDomain(usr, req.Roles, nil) @@ -261,6 +262,7 @@ func (u User) UpdateUser(ctx context.Context, req domain.UpdateUserRequest, admi u.auditService.SaveAuditAsync(ctx, adminId, fmt.Sprintf("Пользователь. Изменение пользователя %s.", user.Email), + entity.EventUserChanged, ) result := u.toDomain(*user, req.Roles, lastSessionCreatedAt) @@ -278,6 +280,7 @@ func (u User) DeleteUsers(ctx context.Context, ids []int64, adminId int64) (int, u.auditService.SaveAuditAsync(ctx, adminId, fmt.Sprintf("Пользователь. Удаление пользователей %d.", ids), + entity.EventUserChanged, ) return count, err @@ -331,6 +334,7 @@ func (u User) Block(ctx context.Context, adminId int64, userId int) error { u.auditService.SaveAuditAsync(ctx, adminId, fmt.Sprintf("Пользователь. %s пользователя ID %d.", userBlocked, userId), + entity.EventUserChanged, ) return nil diff --git a/service/worker/inactive.go b/service/worker/inactive.go index b6df901..a326c58 100644 --- a/service/worker/inactive.go +++ b/service/worker/inactive.go @@ -6,10 +6,11 @@ import ( "github.com/integration-system/isp-kit/log" "github.com/pkg/errors" + "msp-admin-service/entity" ) type AuditRepo interface { - SaveAuditAsync(ctx context.Context, userId int64, message string) + SaveAuditAsync(ctx context.Context, userId int64, message string, event string) } type TokenRepo interface { @@ -72,7 +73,7 @@ func (w InactiveBlocker) do(ctx context.Context) error { if err != nil { return errors.WithMessagef(err, "block user %d", userId) } - w.auditRepo.SaveAuditAsync(ctx, userId, "Блокировка неактивной УЗ") + w.auditRepo.SaveAuditAsync(ctx, userId, "Блокировка неактивной УЗ", entity.EventUserChanged) } return nil