Better use of Nickname validation functions

Nickname verifications on registration and updates for profiles (not yet
groups) have been improved.

Minor bugs in RegisterAction were also fixed, where multiple forms would
be outputed because the function did not return after showForm(). This
will be solved more permanently with throwing exceptions in the future.
This commit is contained in:
Mikael Nordfeldth
2013-10-16 14:58:22 +02:00
parent 38a69b5597
commit db7ef52d13
10 changed files with 58 additions and 235 deletions

View File

@@ -519,24 +519,12 @@ class TwitterauthorizationAction extends Action
}
try {
$nickname = Nickname::normalize($this->trimmed('newname'));
$nickname = Nickname::normalize($this->trimmed('newname'), true);
} catch (NicknameException $e) {
$this->showForm($e->getMessage());
return;
}
if (!User::allowed_nickname($nickname)) {
// TRANS: Client error displayed when trying to create a new user with an invalid username.
$this->showForm(_m('Nickname not allowed.'));
return;
}
if (User::getKV('nickname', $nickname)) {
// TRANS: Client error displayed when trying to create a new user with a username that is already in use.
$this->showForm(_m('Nickname already in use. Try another one.'));
return;
}
$fullname = trim($this->tw_fields['fullname']);
$args = array('nickname' => $nickname, 'fullname' => $fullname);
@@ -688,36 +676,10 @@ class TwitterauthorizationAction extends Action
function bestNewNickname()
{
if (!empty($this->tw_fields['fullname'])) {
$nickname = $this->nicknamize($this->tw_fields['fullname']);
if ($this->isNewNickname($nickname)) {
return $nickname;
}
try {
return Nickname::normalize($this->tw_fields['fullname'], true);
} catch (NicknameException $e) {
return null
}
return null;
}
// Given a string, try to make it work as a nickname
function nicknamize($str)
{
$str = preg_replace('/\W/', '', $str);
$str = str_replace(array('-', '_'), '', $str);
return strtolower($str);
}
function isNewNickname($str)
{
if (!Nickname::isValid($str)) {
return false;
}
if (!User::allowed_nickname($str)) {
return false;
}
if (User::getKV('nickname', $str)) {
return false;
}
return true;
}
}