Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix(admin): add restriction about admin modify their status
  • Loading branch information
LinkinStars committed Feb 23, 2023
1 parent 15390ad commit 4ca2429
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 4 deletions.
2 changes: 2 additions & 0 deletions i18n/en_US.yaml
Expand Up @@ -37,6 +37,8 @@ backend:
admin:
cannot_update_their_password:
other: You cannot modify your password.
cannot_modify_self_status:
other: You cannot modify your status.
email_or_password_wrong:
other: Email and password do not match.
answer:
Expand Down
1 change: 1 addition & 0 deletions internal/base/reason/reason.go
Expand Up @@ -65,4 +65,5 @@ const (
NotAllowedRegistration = "error.user.not_allowed_registration"
SMTPConfigFromNameCannotBeEmail = "error.smtp.config_from_name_cannot_be_email"
AdminCannotUpdateTheirPassword = "error.admin.cannot_update_their_password"
AdminCannotModifySelfStatus = "error.admin.cannot_modify_self_status"
)
2 changes: 2 additions & 0 deletions internal/controller_admin/user_backyard_controller.go
Expand Up @@ -34,6 +34,8 @@ func (uc *UserAdminController) UpdateUserStatus(ctx *gin.Context) {
return
}

req.LoginUserID = middleware.GetLoginUserIDFromContext(ctx)

err := uc.userService.UpdateUserStatus(ctx, req)
handler.HandleResponse(ctx, err, nil)
}
Expand Down
7 changes: 3 additions & 4 deletions internal/schema/backyard_user_schema.go
Expand Up @@ -2,10 +2,9 @@ package schema

// UpdateUserStatusReq update user request
type UpdateUserStatusReq struct {
// user id
UserID string `validate:"required" json:"user_id"`
// user status
Status string `validate:"required,oneof=normal suspended deleted inactive" json:"status" enums:"normal,suspended,deleted,inactive"`
UserID string `validate:"required" json:"user_id"`
Status string `validate:"required,oneof=normal suspended deleted inactive" json:"status" enums:"normal,suspended,deleted,inactive"`
LoginUserID string `json:"-"`
}

const (
Expand Down
4 changes: 4 additions & 0 deletions internal/service/user_admin/user_backyard.go
Expand Up @@ -61,6 +61,10 @@ func NewUserAdminService(

// UpdateUserStatus update user
func (us *UserAdminService) UpdateUserStatus(ctx context.Context, req *schema.UpdateUserStatusReq) (err error) {
// Admin cannot modify their status
if req.UserID == req.LoginUserID {
return errors.BadRequest(reason.AdminCannotModifySelfStatus)
}
userInfo, exist, err := us.userRepo.GetUserInfo(ctx, req.UserID)
if err != nil {
return
Expand Down

0 comments on commit 4ca2429

Please sign in to comment.