Skip to content

Commit

Permalink
Properly clean the username when saving
Browse files Browse the repository at this point in the history
  • Loading branch information
nanaya committed Jan 22, 2018
1 parent a34c6d6 commit c037c07
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 4 deletions.
1 change: 0 additions & 1 deletion app/Libraries/UserRegistration.php
Expand Up @@ -34,7 +34,6 @@ class UserRegistration
public function __construct($params)
{
$this->user = new User(array_merge([
'username_clean' => $params['username'] ?? '',
'user_permissions' => '',
'user_interests' => '',
'user_occ' => '',
Expand Down
12 changes: 11 additions & 1 deletion app/Models/User.php
Expand Up @@ -191,7 +191,6 @@ private function tryUpdateUsername($try, $newUsername, $oldUsername, $type)
private function updateUsername($newUsername, $oldUsername, $type)
{
$this->username_previous = $oldUsername;
$this->username_clean = strtolower($newUsername);
$this->username = $newUsername;

DB::transaction(function () use ($newUsername, $oldUsername, $type) {
Expand All @@ -217,6 +216,11 @@ private function updateUsername($newUsername, $oldUsername, $type)
});
}

public static function cleanUsername($username)
{
return strtolower($username);
}

public static function findByUsernameForInactive($username)
{
return static::whereIn(
Expand Down Expand Up @@ -457,6 +461,12 @@ public function setOsuPlaystyleAttribute($value)
$this->attributes['osu_playstyle'] = $styles;
}

public function setUsernameAttribute($value)
{
$this->attributes['username'] = $value;
$this->username_clean = static::cleanUsername($value);
}

public function isSpecial()
{
return $this->user_id !== null && present($this->user_colour);
Expand Down
1 change: 0 additions & 1 deletion database/factories/UserFactory.php
Expand Up @@ -42,7 +42,6 @@

return [
'username' => $username,
'username_clean' => $username,
'user_id' => $userid,
'user_password' => password_hash(md5('password'), PASSWORD_BCRYPT),
'user_email' => $faker->safeEmail,
Expand Down
Expand Up @@ -131,7 +131,6 @@ public function testRunWhenUsernameIsTaken()
{
factory(User::class)->create([
'username' => 'new_username',
'username_clean' => 'new_username',
'user_lastvisit' => time(),
]);

Expand Down

0 comments on commit c037c07

Please sign in to comment.