Skip to content

Commit 39cd778

Browse files
committed
Add support for additional replacement characers
1 parent 6ecdea1 commit 39cd778

File tree

4 files changed

+11
-10
lines changed

4 files changed

+11
-10
lines changed

custom/conf/app.example.ini

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1527,8 +1527,8 @@ LEVEL = Info
15271527
;; email = use the username part of the email attribute
15281528
;; Note: `nickname` and `email` options will normalize input strings using the following criteria:
15291529
;; - diacritics are removed
1530-
;; - single-quotes are removed
1531-
;; - the characters set `\s,` is replaced with `-`
1530+
;; - the characters set `[´\x60]` are removed
1531+
;; - the characters set `[\s~+]` is replaced with `-`
15321532
;USERNAME = nickname
15331533
;;
15341534
;; Update avatar if available from oauth2 provider.

docs/content/administration/config-cheat-sheet.en-us.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -600,8 +600,8 @@ And the following unique queues:
600600
- `email` - use the username part of the email attribute
601601
- Note: `nickname` and `email` options will normalize input strings using the following criteria:
602602
- diacritics are removed
603-
- single-quotes are removed
604-
- the characters set `\s,` is replaced with `-`
603+
- the characters set `[´\x60]` are removed
604+
- the characters set `[\s~+]` is replaced with `-`
605605
- `UPDATE_AVATAR`: **false**: Update avatar if available from oauth2 provider. Update will be performed on each login.
606606
- `ACCOUNT_LINKING`: **login**: How to handle if an account / email already exists:
607607
- disabled - show an error

models/user/user.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -523,20 +523,21 @@ func GetUserSalt() (string, error) {
523523
// Note: The set of characters here can safely expand without a breaking change,
524524
// but characters removed from this set can cause user account linking to break
525525
var (
526-
removeCharsRE = regexp.MustCompile(`['\x60]`)
526+
customCharsReplacement = strings.NewReplacer("Æ", "AE")
527+
removeCharsRE = regexp.MustCompile(`['´\x60]`)
527528
removeDiacriticsTransform = transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), norm.NFC)
528-
replaceCharsRE = regexp.MustCompile(`[\s,]`)
529+
replaceCharsHyphenRE = regexp.MustCompile(`[\s~+]`)
529530
)
530531

531532
// normalizeUserName returns a string with single-quotes and diacritics
532533
// removed, and any other non-supported username characters replaced with
533534
// a `-` character
534535
func NormalizeUserName(s string) (string, error) {
535-
strDiacriticsRemoved, n, err := transform.String(removeDiacriticsTransform, s)
536+
strDiacriticsRemoved, n, err := transform.String(removeDiacriticsTransform, customCharsReplacement.Replace(s))
536537
if err != nil {
537538
return "", fmt.Errorf("Failed to normalize character `%v` in provided username `%v`", s[n], s)
538539
}
539-
return replaceCharsRE.ReplaceAllLiteralString(removeCharsRE.ReplaceAllLiteralString(strDiacriticsRemoved, ""), "-"), nil
540+
return replaceCharsHyphenRE.ReplaceAllLiteralString(removeCharsRE.ReplaceAllLiteralString(strDiacriticsRemoved, ""), "-"), nil
540541
}
541542

542543
var (

models/user/user_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -553,13 +553,13 @@ func Test_NormalizeUserFromEmail(t *testing.T) {
553553
}{
554554
{"test", "test", true},
555555
{"Sinéad.O'Connor", "Sinead.OConnor", true},
556-
{"Æsir", "Æsir", false}, // Currently unsupported
556+
{"Æsir", "AEsir", true},
557557
// \u00e9\u0065\u0301
558558
{"éé", "ee", true},
559559
{"Awareness Hub", "Awareness-Hub", true},
560560
{"double__underscore", "double__underscore", false}, // We should consider squashing double non-alpha characters
561561
{".bad.", ".bad.", false},
562-
{"new😀user", "new😀user", false},
562+
{"new😀user", "new😀user", false}, // No plans to support
563563
}
564564
for _, testCase := range testCases {
565565
normalizedName, err := user_model.NormalizeUserName(testCase.Input)

0 commit comments

Comments
 (0)