Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions internal/service/user_common/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package usercommon

import (
"context"
"regexp"
"strings"

"github.com/apache/answer/internal/base/constant"
Expand All @@ -35,6 +36,7 @@ import (
"github.com/apache/answer/pkg/checker"
"github.com/apache/answer/pkg/random"
"github.com/mozillazg/go-pinyin"
"github.com/mozillazg/go-unidecode"
"github.com/segmentfault/pacman/errors"
"github.com/segmentfault/pacman/log"
)
Expand Down Expand Up @@ -190,6 +192,10 @@ func (us *UserCommon) FormatUserBasicInfo(ctx context.Context, userInfo *entity.
return userBasicInfo
}

// usernameDisallowed matches everything checker.IsInvalidUsername rejects.
// Kept in sync with pkg/checker/username.go.
var usernameDisallowed = regexp.MustCompile(`[^\w.\- ]`)

// MakeUsername
// Generate a unique Username based on the displayName
func (us *UserCommon) MakeUsername(ctx context.Context, displayName string) (username string, err error) {
Expand All @@ -198,6 +204,17 @@ func (us *UserCommon) MakeUsername(ctx context.Context, displayName string) (use
displayName = strings.Join(pinyin.LazyConvert(displayName, nil), "")
}

// Other non-Latin characters, such as German umlauts or accented
// letters, are transliterated the same way question URLs are (see
// pkg/htmltext). Without this, a display name like "Jürgen Müller"
// would produce an invalid username and registration would fail.
displayName = unidecode.Unidecode(displayName)

// Transliteration can emit characters a username may not contain:
// "Ольга" becomes "Ol'ga", for example. Drop those, so the result
// still satisfies checker.IsInvalidUsername below.
displayName = usernameDisallowed.ReplaceAllString(displayName, "")

username = strings.ReplaceAll(displayName, " ", "-")
username = strings.ToLower(username)
suffix := ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const Index: React.FC<Props> = ({ callback }) => {
});

const emailCaptcha = useCaptchaPlugin('email');
const nameRegex = /^[\w.-\s]{2,30}$/;
const nameRegex = /^[\p{L}\p{N}._\s-]{2,30}$/u;

const handleChange = (params: FormDataType) => {
setFormData({ ...formData, ...params });
Expand Down