From da3ae217661362f41433a882d1ebc832afb0ec06 Mon Sep 17 00:00:00 2001 From: riabovvo Date: Tue, 23 May 2023 15:13:10 +0300 Subject: [PATCH] MSP-1756 update audit --- controller/user.go | 9 ++++++-- service/role.go | 51 +++++++++++++++++++++++++++++++++++----------- service/user.go | 13 ++++++++++-- tests/user_test.go | 1 + 4 files changed, 58 insertions(+), 16 deletions(-) diff --git a/controller/user.go b/controller/user.go index 14c8ad2..4748064 100644 --- a/controller/user.go +++ b/controller/user.go @@ -16,7 +16,7 @@ type userService interface { CreateUser(ctx context.Context, req domain.CreateUserRequest, adminId int64) (*domain.User, error) UpdateUser(ctx context.Context, req domain.UpdateUserRequest, adminId int64) (*domain.User, error) DeleteUsers(ctx context.Context, ids []int64, adminId int64) (int, error) - Block(ctx context.Context, userId int) error + Block(ctx context.Context, adminId int64, userId int) error GetById(ctx context.Context, userId int) (*domain.User, error) } @@ -185,7 +185,12 @@ func (u User) DeleteUser(ctx context.Context, authData grpc.AuthData, identities // @Failure 500 {object} domain.GrpcError // @Router /user/block_user [POST] func (u User) Block(ctx context.Context, authData grpc.AuthData, identities domain.IdRequest) error { - err := u.userService.Block(ctx, identities.UserId) + adminId, err := getUserToken(authData) + if err != nil { + return err + } + + err = u.userService.Block(ctx, adminId, identities.UserId) if err != nil { return errors.WithMessage(err, "block") } diff --git a/service/role.go b/service/role.go index 2d0a60b..4a2314f 100644 --- a/service/role.go +++ b/service/role.go @@ -14,6 +14,8 @@ type roleRoleRepo interface { InsertRole(ctx context.Context, role entity.Role) (*entity.Role, error) Update(ctx context.Context, role entity.Role) (*entity.Role, error) Delete(ctx context.Context, id int) error + GetRoleByName(ctx context.Context, name string) (*entity.Role, error) + GetRoleByIds(ctx context.Context, id []int) ([]entity.Role, error) } type Role struct { @@ -43,20 +45,29 @@ func (u Role) All(ctx context.Context) ([]domain.Role, error) { } func (u Role) Create(ctx context.Context, req domain.CreateRoleRequest, adminId int64) (*domain.Role, error) { - role, err := u.roleRepo.InsertRole(ctx, entity.Role{ + role, err := u.roleRepo.GetRoleByName(ctx, req.Name) + switch { + case errors.Is(err, domain.ErrNotFound): + break + case err != nil: + return nil, errors.WithMessagef(err, "get role by name") + case role != nil: + return nil, domain.ErrAlreadyExists + } + + role, err = u.roleRepo.InsertRole(ctx, entity.Role{ Name: req.Name, ExternalGroup: req.ExternalGroup, - ChangeMessage: req.ChangeMessage, Permissions: req.Permissions, }) if err != nil { - return nil, errors.WithMessage(err, "create user") + return nil, errors.WithMessage(err, "create role") } - u.auditService.SaveAuditAsync(ctx, adminId, fmt.Sprintf("Роль. Создание новой роли %s. %s", - role.Name, - role.ChangeMessage, + u.auditService.SaveAuditAsync(ctx, adminId, fmt.Sprintf("Роль. Создание новой роли %s. Причина: %s", + req.Name, + req.ChangeMessage, )) result := u.toDomain(*role) @@ -64,21 +75,38 @@ func (u Role) Create(ctx context.Context, req domain.CreateRoleRequest, adminId } func (u Role) Update(ctx context.Context, req domain.UpdateRoleRequest, adminId int64) (*domain.Role, error) { + roleByName, err := u.roleRepo.GetRoleByName(ctx, req.Name) + switch { + case errors.Is(err, domain.ErrNotFound): + break + case err != nil: + return nil, errors.WithMessagef(err, "get role by id") + } + + roles, err := u.roleRepo.GetRoleByIds(ctx, []int{req.Id}) + switch { + case errors.Is(err, domain.ErrNotFound): + break + case err != nil: + return nil, errors.WithMessagef(err, "get role by id") + case roleByName != nil && roleByName.Id != roles[0].Id: + return nil, domain.ErrAlreadyExists + } + role, err := u.roleRepo.Update(ctx, entity.Role{ Id: req.Id, Name: req.Name, ExternalGroup: req.ExternalGroup, - ChangeMessage: req.ChangeMessage, Permissions: req.Permissions, }) if err != nil { - return nil, errors.WithMessage(err, "update user") + return nil, errors.WithMessage(err, "update role") } - u.auditService.SaveAuditAsync(ctx, adminId, fmt.Sprintf("Роль. Изменение роли %s. %s", - role.Name, - role.ChangeMessage, + u.auditService.SaveAuditAsync(ctx, adminId, fmt.Sprintf("Роль. Изменение роли %s. Причина: %s", + req.Name, + req.ChangeMessage, )) result := u.toDomain(*role) return &result, nil @@ -99,7 +127,6 @@ func (u Role) toDomain(role entity.Role) domain.Role { Id: role.Id, Name: role.Name, ExternalGroup: role.ExternalGroup, - ChangeMessage: role.ChangeMessage, Permissions: role.Permissions, CreatedAt: role.CreatedAt, UpdatedAt: role.UpdatedAt, diff --git a/service/user.go b/service/user.go index 4e641c5..3e535a6 100644 --- a/service/user.go +++ b/service/user.go @@ -293,7 +293,9 @@ func (u User) GetById(ctx context.Context, userId int) (*domain.User, error) { return &result, nil } -func (u User) Block(ctx context.Context, userId int) error { +func (u User) Block(ctx context.Context, adminId int64, userId int) error { + userBlocked := "блокировка" + err := u.txRunner.UserTransaction(ctx, func(ctx context.Context, tx UserTransaction) error { blocked, err := tx.ChangeBlockStatus(ctx, userId) if err != nil { @@ -307,13 +309,20 @@ func (u User) Block(ctx context.Context, userId int) error { } } + if !blocked { + userBlocked = "разблокировка" + } + return nil }) - if err != nil { return errors.WithMessage(err, "block user transaction") } + u.auditService.SaveAuditAsync(ctx, adminId, + fmt.Sprintf("Пользователь. %s пользователя ID %d.", userBlocked, userId), + ) + return nil } diff --git a/tests/user_test.go b/tests/user_test.go index 37228d1..0162a22 100644 --- a/tests/user_test.go +++ b/tests/user_test.go @@ -324,6 +324,7 @@ func (s *UserTestSuite) TestBlockUser() { }) err := s.grpcCli.Invoke("admin/user/block_user"). + AppendMetadata(domain.AdminAuthIdHeader, strconv.Itoa(int(id))). JsonRequestBody(domain.IdRequest{UserId: int(id)}). Do(context.Background()) s.Require().NoError(err)