feat(user): user can't use reserved username

This commit is contained in:
LinkinStar 2022-12-28 17:11:24 +08:00
parent 81135192e1
commit 427753a161
4 changed files with 34 additions and 0 deletions

View File

@ -7,3 +7,6 @@ var Config []byte
//go:embed path_ignore.yaml
var PathIgnore []byte
//go:embed reserved-usernames.json
var ReservedUsernames []byte

File diff suppressed because one or more lines are too long

View File

@ -126,6 +126,10 @@ func (us *UserCommon) MakeUsername(ctx context.Context, displayName string) (use
return "", errors.BadRequest(reason.UsernameInvalid)
}
if checker.IsReservedUsername(username) {
return "", errors.BadRequest(reason.UsernameInvalid)
}
for {
_, has, err := us.userRepo.GetByUsername(ctx, username+suffix)
if err != nil {

View File

@ -0,0 +1,26 @@
package checker
import (
"encoding/json"
"github.com/answerdev/answer/configs"
"github.com/segmentfault/pacman/log"
)
var (
reservedUsernameMapping = make(map[string]bool)
)
func init() {
var usernames []string
_ = json.Unmarshal(configs.ReservedUsernames, &usernames)
log.Debugf("get reserved usernames %d", len(usernames))
for _, username := range usernames {
reservedUsernameMapping[username] = true
}
}
// IsReservedUsername checks whether the username is reserved
func IsReservedUsername(username string) bool {
return reservedUsernameMapping[username]
}