From 561a280865f5e80800c0c7ec359814d53c9df281 Mon Sep 17 00:00:00 2001 From: ildyria Date: Sat, 24 Dec 2022 15:56:56 +0100 Subject: [PATCH] In diagnostics inform what is required to do if no admins are found. --- app/Actions/Diagnostics/Errors.php | 2 ++ .../Pipes/Checks/AdminUserExistsCheck.php | 19 ++++++++++ app/Console/Commands/CreateUser.php | 21 +++-------- app/Console/Commands/ResetAdmin.php | 2 +- app/Console/Commands/UpdateUser.php | 35 ++++++++++--------- 5 files changed, 44 insertions(+), 35 deletions(-) create mode 100644 app/Actions/Diagnostics/Pipes/Checks/AdminUserExistsCheck.php diff --git a/app/Actions/Diagnostics/Errors.php b/app/Actions/Diagnostics/Errors.php index 05c2861a3a9..12326fbe8e7 100644 --- a/app/Actions/Diagnostics/Errors.php +++ b/app/Actions/Diagnostics/Errors.php @@ -2,6 +2,7 @@ namespace App\Actions\Diagnostics; +use App\Actions\Diagnostics\Pipes\Checks\AdminUserExistsCheck; use App\Actions\Diagnostics\Pipes\Checks\BasicPermissionCheck; use App\Actions\Diagnostics\Pipes\Checks\ConfigSanityCheck; use App\Actions\Diagnostics\Pipes\Checks\DBSupportCheck; @@ -23,6 +24,7 @@ class Errors * @var array */ private array $pipes = [ + AdminUserExistsCheck::class, BasicPermissionCheck::class, ConfigSanityCheck::class, DBSupportCheck::class, diff --git a/app/Actions/Diagnostics/Pipes/Checks/AdminUserExistsCheck.php b/app/Actions/Diagnostics/Pipes/Checks/AdminUserExistsCheck.php new file mode 100644 index 00000000000..1c4a5589bcd --- /dev/null +++ b/app/Actions/Diagnostics/Pipes/Checks/AdminUserExistsCheck.php @@ -0,0 +1,19 @@ +where('may_administrate', '=', true)->count(); + if ($numberOfAdmin === 0) { + $data[] = 'Error: User Admin not found in database. Please run: "php lychee:create_user {username} {password}"'; + } + + return $next($data); + } +} \ No newline at end of file diff --git a/app/Console/Commands/CreateUser.php b/app/Console/Commands/CreateUser.php index e96183ce9e8..100dca8cc33 100644 --- a/app/Console/Commands/CreateUser.php +++ b/app/Console/Commands/CreateUser.php @@ -18,8 +18,8 @@ class CreateUser extends Command */ protected $signature = 'lychee:create_user' . - '{--username= : username of the new user} ' . - '{--password= : password of the new user} ' . + '{username : username of the new user} ' . + '{password : password of the new user} ' . '{--may-edit-own-settings : user can edit own settings} ' . '{--may-upload : user may upload} ' . '{--may-administrate : user is an admin} '; @@ -49,21 +49,8 @@ public function __construct(Create $create) */ public function handle(): int { - /** @var string|null $username */ - $username = $this->option('username'); - /** @var string|null $password */ - $password = $this->option('password'); - - if ($username === null || $username === '') { - $this->error('Username is missing.'); - - return 1; - } - if ($password === null || $password === '') { - $this->error('Password is missing.'); - - return 1; - } + $username = strval($this->argument('username')); + $password = strval($this->argument('password')); $count = User::query()->count(); diff --git a/app/Console/Commands/ResetAdmin.php b/app/Console/Commands/ResetAdmin.php index e8ec7ae1839..74f61b069f3 100644 --- a/app/Console/Commands/ResetAdmin.php +++ b/app/Console/Commands/ResetAdmin.php @@ -27,7 +27,7 @@ class ResetAdmin extends Command */ public function handle(): int { - $this->line('Command deprecated. Instead, use php artisan lychee:update_user or lychee:create_user --may_administrate.'); + $this->line('Command deprecated. Instead, use php artisan lychee:update_user or lychee:create_user {username} {password} --may_administrate.'); return 1; } diff --git a/app/Console/Commands/UpdateUser.php b/app/Console/Commands/UpdateUser.php index 49613d40fcb..844de6f442d 100644 --- a/app/Console/Commands/UpdateUser.php +++ b/app/Console/Commands/UpdateUser.php @@ -16,15 +16,15 @@ class UpdateUser extends Command */ protected $signature = 'lychee:update_user' . - '{--username= : username of the user} ' . - '{--password= : password of the user} '; + '{username : username of the user} ' . + '{password : password of the user} '; /** * The console command description. * * @var string */ - protected $description = 'Update the user with the given username. If an option is not set, it is not changed.'; + protected $description = 'Update the user with the given username.'; /** * Execute the console command. @@ -35,29 +35,30 @@ class UpdateUser extends Command */ public function handle(): int { - /** @var string|null $username */ - $username = $this->option('username'); + $username = strval($this->argument('username')); - if ($username === null || $username === '') { - $this->error('Username is missing.'); + /** @var User|null $user */ + $user = User::query()->where('username', '=', $username)->first(); + + if ($user === null) { + $this->error('user not found'); return 1; } - /** @var User $user */ - $user = User::query()->where('username', '=', $username)->firstOrFail(); - - /** @var string|null $password */ - $password = $this->option('password'); + $password = strval($this->argument('password')); - if ($password !== null && $password !== '') { + if ($password !== '') { $user->password = Hash::make($password); - } + $user->save(); - $user->save(); + $this->line('Successfully updated user ' . $username); + + return 0; + } - $this->line('Successfully updated user ' . $username); + $this->error('wrong password'); - return 0; + return 1; } }