Skip to content

Commit

Permalink
In diagnostics inform what is required to do if no admins are found.
Browse files Browse the repository at this point in the history
  • Loading branch information
ildyria committed Dec 24, 2022
1 parent c0a2dad commit 561a280
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 35 deletions.
2 changes: 2 additions & 0 deletions app/Actions/Diagnostics/Errors.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -23,6 +24,7 @@ class Errors
* @var array<int,class-string>
*/
private array $pipes = [
AdminUserExistsCheck::class,
BasicPermissionCheck::class,
ConfigSanityCheck::class,
DBSupportCheck::class,
Expand Down
19 changes: 19 additions & 0 deletions app/Actions/Diagnostics/Pipes/Checks/AdminUserExistsCheck.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Actions\Diagnostics\Pipes\Checks;

use App\Contracts\DiagnosticPipe;
use App\Models\User;

class AdminUserExistsCheck implements DiagnosticPipe
{
public function handle(array &$data, \Closure $next): array
{
$numberOfAdmin = User::query()->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);
}
}
21 changes: 4 additions & 17 deletions app/Console/Commands/CreateUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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} ';
Expand Down Expand Up @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion app/Console/Commands/ResetAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
35 changes: 18 additions & 17 deletions app/Console/Commands/UpdateUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
}
}

0 comments on commit 561a280

Please sign in to comment.