2018-12-24 00:16:59 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
|
|
|
|
use Illuminate\Console\Command;
|
|
|
|
use App\{Profile, User};
|
|
|
|
use DB;
|
2019-01-04 06:08:23 +00:00
|
|
|
use App\Util\Lexer\RestrictedNames;
|
2018-12-24 00:16:59 +00:00
|
|
|
|
|
|
|
class FixUsernames extends Command
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* The name and signature of the console command.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $signature = 'fix:usernames';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The console command description.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $description = 'Fix invalid usernames';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new command instance.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the console command.
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
2020-04-22 05:13:38 +00:00
|
|
|
$this->line(' ');
|
2018-12-24 00:16:59 +00:00
|
|
|
$this->info('Collecting data ...');
|
2020-04-22 05:13:38 +00:00
|
|
|
$this->line(' ');
|
|
|
|
$this->restrictedCheck();
|
|
|
|
}
|
2018-12-24 00:16:59 +00:00
|
|
|
|
2020-04-22 05:13:38 +00:00
|
|
|
protected function restrictedCheck()
|
|
|
|
{
|
2018-12-24 00:16:59 +00:00
|
|
|
$affected = collect([]);
|
|
|
|
|
2019-01-04 06:08:23 +00:00
|
|
|
$restricted = RestrictedNames::get();
|
|
|
|
|
|
|
|
$users = User::chunk(100, function($users) use($affected, $restricted) {
|
2018-12-24 00:16:59 +00:00
|
|
|
foreach($users as $user) {
|
2019-11-14 06:30:02 +00:00
|
|
|
if($user->is_admin || $user->status == 'deleted') {
|
|
|
|
continue;
|
|
|
|
}
|
2020-11-26 17:30:40 +00:00
|
|
|
if(in_array(strtolower($user->username), array_map('strtolower', $restricted))) {
|
2019-01-04 06:08:23 +00:00
|
|
|
$affected->push($user);
|
|
|
|
}
|
2019-11-14 06:20:01 +00:00
|
|
|
$val = str_replace(['-', '_', '.'], '', $user->username);
|
2018-12-24 00:16:59 +00:00
|
|
|
if(!ctype_alnum($val)) {
|
|
|
|
$this->info('Found invalid username: ' . $user->username);
|
|
|
|
$affected->push($user);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2020-04-22 05:13:38 +00:00
|
|
|
|
2018-12-24 00:16:59 +00:00
|
|
|
if($affected->count() > 0) {
|
|
|
|
$this->info('Found: ' . $affected->count() . ' affected usernames');
|
|
|
|
|
|
|
|
$opts = [
|
|
|
|
'Random replace (assigns random username)',
|
|
|
|
'Best try replace (assigns alpha numeric username)',
|
2019-01-04 06:08:23 +00:00
|
|
|
'Manual replace (manually set username)',
|
|
|
|
'Skip (do not replace. Use at your own risk)'
|
2018-12-24 00:16:59 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
foreach($affected as $u) {
|
|
|
|
$old = $u->username;
|
2019-01-04 06:08:23 +00:00
|
|
|
$this->info("Found user: {$old}");
|
2019-06-11 04:33:35 +00:00
|
|
|
$opt = $this->choice('Select fix method:', $opts, 3);
|
2018-12-24 00:16:59 +00:00
|
|
|
|
|
|
|
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;
|
2019-01-04 06:08:23 +00:00
|
|
|
|
|
|
|
case $opts[3]:
|
|
|
|
$new = false;
|
|
|
|
break;
|
2018-12-24 00:16:59 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
$new = "user_" . str_random(6);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-01-04 06:08:23 +00:00
|
|
|
if($new) {
|
|
|
|
DB::transaction(function() use($u, $new) {
|
|
|
|
$profile = $u->profile;
|
|
|
|
$profile->username = $new;
|
|
|
|
$u->username = $new;
|
|
|
|
$u->save();
|
|
|
|
$profile->save();
|
|
|
|
});
|
|
|
|
}
|
2018-12-24 00:16:59 +00:00
|
|
|
$this->info('Selected: ' . $opt);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->info('Fixed ' . $affected->count() . ' usernames!');
|
2019-01-04 06:08:23 +00:00
|
|
|
} else {
|
2020-04-22 05:13:38 +00:00
|
|
|
$this->info('No restricted usernames found!');
|
2018-12-24 00:16:59 +00:00
|
|
|
}
|
2020-04-22 05:13:38 +00:00
|
|
|
$this->line(' ');
|
|
|
|
$this->versionZeroTenNineFix();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function versionZeroTenNineFix()
|
|
|
|
{
|
|
|
|
$profiles = Profile::whereNotNull('domain')
|
|
|
|
->whereNull('private_key')
|
|
|
|
->where('username', 'not like', '@%@%')
|
|
|
|
->get();
|
|
|
|
|
|
|
|
$count = $profiles->count();
|
|
|
|
|
|
|
|
if($count > 0) {
|
|
|
|
$this->info("Found {$count} remote usernames to fix ...");
|
|
|
|
$this->line(' ');
|
|
|
|
} else {
|
|
|
|
$this->info('No remote fixes found!');
|
|
|
|
$this->line(' ');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
foreach($profiles as $p) {
|
|
|
|
$this->info("Fixed $p->username => $p->webfinger");
|
|
|
|
$p->username = $p->webfinger ?? "@{$p->username}@{$p->domain}";
|
|
|
|
if(Profile::whereUsername($p->username)->exists()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$p->save();
|
|
|
|
}
|
|
|
|
if($count > 0) {
|
|
|
|
$this->line(' ');
|
|
|
|
}
|
|
|
|
|
2018-12-24 00:16:59 +00:00
|
|
|
}
|
|
|
|
}
|