Merge remote-tracking branch 'origin/feat/1.0.2/user' into test

This commit is contained in:
LinkinStar 2022-12-28 17:13:56 +08:00
commit 35dbd269f9
4 changed files with 34 additions and 0 deletions

View File

@ -7,3 +7,6 @@ var Config []byte
//go:embed path_ignore.yaml //go:embed path_ignore.yaml
var PathIgnore []byte 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) return "", errors.BadRequest(reason.UsernameInvalid)
} }
if checker.IsReservedUsername(username) {
return "", errors.BadRequest(reason.UsernameInvalid)
}
for { for {
_, has, err := us.userRepo.GetByUsername(ctx, username+suffix) _, has, err := us.userRepo.GetByUsername(ctx, username+suffix)
if err != nil { 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]
}