A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.
feat(user): admin can use reserved username
LinkinStarsea614f019397
diff --git a/internal/controller/user_controller.go b/internal/controller/user_controller.goindex 4ad697cf..47a39416 100644--- a/internal/controller/user_controller.go+++ b/internal/controller/user_controller.go@@ -421,6 +421,7 @@ func (uc *UserController) UserUpdateInfo(ctx *gin.Context) {return}req.UserID = middleware.GetLoginUserIDFromContext(ctx)+ req.IsAdmin = middleware.GetUserIsAdminModerator(ctx)errFields, err := uc.userService.UpdateInfo(ctx, req)for _, field := range errFields {field.ErrorMsg = translator.Tr(handler.GetLang(ctx), field.ErrorMsg)diff --git a/internal/schema/user_schema.go b/internal/schema/user_schema.goindex 82a8ab2f..93b6291a 100644--- a/internal/schema/user_schema.go+++ b/internal/schema/user_schema.go@@ -208,22 +208,15 @@ func (u *UserModifyPasswordReq) Check() (errFields []*validator.FormErrorField,}type UpdateInfoRequest struct {- // display_name- DisplayName string `validate:"omitempty,gt=0,lte=30" json:"display_name"`- // username- Username string `validate:"omitempty,gt=3,lte=30" json:"username"`- // avatar- Avatar AvatarInfo `json:"avatar"`- // bio- Bio string `validate:"omitempty,gt=0,lte=4096" json:"bio"`- // bio- BioHTML string `json:"-"`- // website- Website string `validate:"omitempty,gt=0,lte=500" json:"website"`- // location- Location string `validate:"omitempty,gt=0,lte=100" json:"location"`- // user id- UserID string `json:"-"`+ DisplayName string `validate:"omitempty,gt=0,lte=30" json:"display_name"`+ Username string `validate:"omitempty,gt=3,lte=30" json:"username"`+ Avatar AvatarInfo `json:"avatar"`+ Bio string `validate:"omitempty,gt=0,lte=4096" json:"bio"`+ BioHTML string `json:"-"`+ Website string `validate:"omitempty,gt=0,lte=500" json:"website"`+ Location string `validate:"omitempty,gt=0,lte=100" json:"location"`+ UserID string `json:"-"`+ IsAdmin bool `json:"-"`}type AvatarInfo struct {diff --git a/internal/service/user_service.go b/internal/service/user_service.goindex c2553141..560ff7c2 100644--- a/internal/service/user_service.go+++ b/internal/service/user_service.go@@ -274,18 +274,22 @@ func (us *UserService) UpdateInfo(ctx context.Context, req *schema.UpdateInfoReqif siteUsers.AllowUpdateUsername && len(req.Username) > 0 {if checker.IsInvalidUsername(req.Username) {- errFields = append(errFields, &validator.FormErrorField{+ return append(errFields, &validator.FormErrorField{ErrorField: "username",ErrorMsg: reason.UsernameInvalid,- })- return errFields, errors.BadRequest(reason.UsernameInvalid)+ }), errors.BadRequest(reason.UsernameInvalid)}- if checker.IsReservedUsername(req.Username) {- errFields = append(errFields, &validator.FormErrorField{+ // admin can use reserved username+ if !req.IsAdmin && checker.IsReservedUsername(req.Username) {+ return append(errFields, &validator.FormErrorField{ErrorField: "username",ErrorMsg: reason.UsernameInvalid,- })- return errFields, errors.BadRequest(reason.UsernameInvalid)+ }), errors.BadRequest(reason.UsernameInvalid)+ } else if req.IsAdmin && checker.IsUsersIgnorePath(req.Username) {+ return append(errFields, &validator.FormErrorField{+ ErrorField: "username",+ ErrorMsg: reason.UsernameInvalid,+ }), errors.BadRequest(reason.UsernameInvalid)}userInfo, exist, err := us.userRepo.GetByUsername(ctx, req.Username)@@ -293,11 +297,10 @@ func (us *UserService) UpdateInfo(ctx context.Context, req *schema.UpdateInfoReqreturn nil, err}if exist && userInfo.ID != req.UserID {- errFields = append(errFields, &validator.FormErrorField{+ return append(errFields, &validator.FormErrorField{ErrorField: "username",ErrorMsg: reason.UsernameDuplicate,- })- return errFields, errors.BadRequest(reason.UsernameDuplicate)+ }), errors.BadRequest(reason.UsernameDuplicate)}}