add config change username card allow outside alphabets

This commit is contained in:
xiaomlove
2022-08-11 18:26:11 +08:00
parent 9332b676b4
commit 8071efd487
5 changed files with 30 additions and 2 deletions

View File

@@ -400,7 +400,10 @@ class UserRepository extends BaseRepository
$user = User::query()->findOrFail($uid, User::$commonFields);
if ($metaKey == UserMeta::META_KEY_CHANGE_USERNAME) {
NexusDB::transaction(function () use ($user, $meta, $params) {
$this->changeUsername($user, UsernameChangeLog::CHANGE_TYPE_USER, $user, $params['username']);
$this->changeUsername(
$user, UsernameChangeLog::CHANGE_TYPE_USER, $user, $params['username'],
Setting::get('system.change_username_card_allow_characters_outside_the_alphabets') == 'yes'
);
$meta->delete();
clear_user_cache($user->id, $user->passkey);
});
@@ -410,7 +413,7 @@ class UserRepository extends BaseRepository
throw new \InvalidArgumentException("Invalid meta_key: $metaKey");
}
private function changeUsername($operator, $changeType, $targetUser, $newUsername): bool
private function changeUsername($operator, $changeType, $targetUser, $newUsername, $allowOutsideAlphabets = false): bool
{
$operator = $this->getUser($operator);
$targetUser = $this->getUser($targetUser);
@@ -422,6 +425,9 @@ class UserRepository extends BaseRepository
if ($strWidth < 4 || $strWidth > 20) {
throw new \InvalidArgumentException("Invalid username, maybe too long or too short");
}
if (!$allowOutsideAlphabets && !validusername($newUsername)) {
throw new \InvalidArgumentException("Invalid username, only support alphabets");
}
if (User::query()->where('username', $newUsername)->where('id', '!=', $targetUser->id)->exists()) {
throw new \RuntimeException("Username: $newUsername already exists !");
}