From e39f88f6c6804fe261f0bb7e334deb93d142bc32 Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Sun, 23 Dec 2018 17:16:59 -0700 Subject: [PATCH] Bump version to 0.7.1 --- app/Console/Commands/FixUsernames.php | 110 ++++++++++++++++++ .../Controllers/Auth/RegisterController.php | 5 +- config/pixelfed.php | 2 +- 3 files changed, 115 insertions(+), 2 deletions(-) create mode 100644 app/Console/Commands/FixUsernames.php diff --git a/app/Console/Commands/FixUsernames.php b/app/Console/Commands/FixUsernames.php new file mode 100644 index 000000000..04f628edd --- /dev/null +++ b/app/Console/Commands/FixUsernames.php @@ -0,0 +1,110 @@ +info('This command is only for versions lower than 0.7.2'); + return; + } + + $this->info('Collecting data ...'); + + $affected = collect([]); + + $users = User::chunk(100, function($users) use($affected) { + foreach($users as $user) { + $val = str_replace(['-', '_'], '', $user->username); + if(!ctype_alnum($val)) { + $this->info('Found invalid username: ' . $user->username); + $affected->push($user); + } + } + }); + if($affected->count() > 0) { + $this->info('Found: ' . $affected->count() . ' affected usernames'); + + $opts = [ + 'Random replace (assigns random username)', + 'Best try replace (assigns alpha numeric username)', + 'Manual replace (manually set username)' + ]; + + foreach($affected as $u) { + $old = $u->username; + $opt = $this->choice('Select fix method:', $opts, 0); + + switch ($opt) { + case $opts[0]: + $new = "user_" . str_random(6); + $this->info('New username: ' . $new); + break; + + case $opts[1]: + $new = filter_var($old, FILTER_SANITIZE_STRING|FILTER_FLAG_STRIP_LOW); + if(strlen($new) < 6) { + $new = $new . '_' . str_random(4); + } + $this->info('New username: ' . $new); + break; + + case $opts[2]: + $new = $this->ask('Enter new username:'); + $this->info('New username: ' . $new); + break; + + default: + $new = "user_" . str_random(6); + break; + } + + DB::transaction(function() use($u, $new) { + $profile = $u->profile; + $profile->username = $new; + $u->username = $new; + $u->save(); + $profile->save(); + }); + $this->info('Selected: ' . $opt); + } + + $this->info('Fixed ' . $affected->count() . ' usernames!'); + } + } +} diff --git a/app/Http/Controllers/Auth/RegisterController.php b/app/Http/Controllers/Auth/RegisterController.php index 9a8ef07a1..d9354697a 100644 --- a/app/Http/Controllers/Auth/RegisterController.php +++ b/app/Http/Controllers/Auth/RegisterController.php @@ -55,7 +55,6 @@ class RegisterController extends Controller $this->validateUsername($data['username']); $usernameRules = [ 'required', - 'alpha_dash', 'min:2', 'max:15', 'unique:users', @@ -63,6 +62,10 @@ class RegisterController extends Controller if (!ctype_alpha($value[0])) { return $fail($attribute.' is invalid. Username must be alpha-numeric and start with a letter.'); } + $val = str_replace(['-', '_'], '', $value); + if(!ctype_alnum($val)) { + return $fail($attribute . ' is invalid. Username must be alpha-numeric.'); + } }, ]; diff --git a/config/pixelfed.php b/config/pixelfed.php index bfe7f17dd..3fa1c3690 100644 --- a/config/pixelfed.php +++ b/config/pixelfed.php @@ -23,7 +23,7 @@ return [ | This value is the version of your PixelFed instance. | */ - 'version' => '0.7.0', + 'version' => '0.7.1', /* |--------------------------------------------------------------------------