admin: allow assigning a reserved username to a user via the admin console

Add users.Service.UpdateUsernameAdmin, which skips the
config.ReservedUsernames block that self-service UpdateUsername enforces.
Operators need to be able to hand a reserved word to a specific account
even though regular users can't claim it themselves.
This commit is contained in:
Astra 2026-09-15 12:04:26 +01:00
parent 2ffdb1beb5
commit d83034e8dc
4 changed files with 50 additions and 4 deletions

View file

@ -318,8 +318,21 @@ func (s *Service) checkUsernameAvailable(ctx context.Context, selfID int64, user
return !found || u.ID == selfID, nil
}
// UpdateUsername 修改当前用户的主 username。空字符串表示删除 username。
// UpdateUsername 修改当前用户的主 usernameself-service。空字符串表示删除 username。
func (s *Service) UpdateUsername(ctx context.Context, userID int64, username string) (domain.User, error) {
return s.updateUsername(ctx, userID, username, true)
}
// UpdateUsernameAdmin sets a user's username on the operator's behalf (via
// the admin console). Unlike UpdateUsername, it does not consult
// config.ReservedUsernames -- an operator who deliberately reserved a word
// still needs to be able to hand it to a specific account. Validity and
// availability (no collision with another account) are still enforced.
func (s *Service) UpdateUsernameAdmin(ctx context.Context, userID int64, username string) (domain.User, error) {
return s.updateUsername(ctx, userID, username, false)
}
func (s *Service) updateUsername(ctx context.Context, userID int64, username string, enforceReserved bool) (domain.User, error) {
self, err := s.loadSelf(ctx, userID)
if err != nil {
return domain.User{}, err
@ -334,7 +347,7 @@ func (s *Service) UpdateUsername(ctx context.Context, userID int64, username str
return s.projectOne(ctx, self.ID, self)
}
if username != "" {
if !validUsername(username) || s.reserved.Contains(username) {
if !validUsername(username) || (enforceReserved && s.reserved.Contains(username)) {
return domain.User{}, domain.ErrUsernameInvalid
}
ok, err := s.checkUsernameAvailable(ctx, self.ID, username)