Update user:admin command, improve logic. Fixes #2465

This commit is contained in:
Daniel Supernault 2023-10-10 20:20:18 -06:00
parent 7bfe43095b
commit 01bac51104
No known key found for this signature in database
GPG key ID: 23740873EE6F76A1
2 changed files with 20 additions and 12 deletions

View file

@ -3,16 +3,17 @@
namespace App\Console\Commands; namespace App\Console\Commands;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use Illuminate\Contracts\Console\PromptsForMissingInput;
use App\User; use App\User;
class UserAdmin extends Command class UserAdmin extends Command implements PromptsForMissingInput
{ {
/** /**
* The name and signature of the console command. * The name and signature of the console command.
* *
* @var string * @var string
*/ */
protected $signature = 'user:admin {id}'; protected $signature = 'user:admin {username}';
/** /**
* The console command description. * The console command description.
@ -22,13 +23,15 @@ class UserAdmin extends Command
protected $description = 'Make a user an admin, or remove admin privileges.'; protected $description = 'Make a user an admin, or remove admin privileges.';
/** /**
* Create a new command instance. * Prompt for missing input arguments using the returned questions.
* *
* @return void * @return array
*/ */
public function __construct() protected function promptForMissingArgumentsUsing()
{ {
parent::__construct(); return [
'username' => 'Which username should we toggle admin privileges for?',
];
} }
/** /**
@ -38,16 +41,15 @@ class UserAdmin extends Command
*/ */
public function handle() public function handle()
{ {
$id = $this->argument('id'); $id = $this->argument('username');
if(ctype_digit($id) == true) {
$user = User::find($id); $user = User::whereUsername($id)->first();
} else {
$user = User::whereUsername($id)->first();
}
if(!$user) { if(!$user) {
$this->error('Could not find any user with that username or id.'); $this->error('Could not find any user with that username or id.');
exit; exit;
} }
$this->info('Found username: ' . $user->username); $this->info('Found username: ' . $user->username);
$state = $user->is_admin ? 'Remove admin privileges from this user?' : 'Add admin privileges to this user?'; $state = $user->is_admin ? 'Remove admin privileges from this user?' : 'Add admin privileges to this user?';
$confirmed = $this->confirm($state); $confirmed = $this->confirm($state);

View file

@ -33,6 +33,7 @@ class UserToggle2FA extends Command implements PromptsForMissingInput
'username' => 'Which username should we disable 2FA for?', 'username' => 'Which username should we disable 2FA for?',
]; ];
} }
/** /**
* Execute the console command. * Execute the console command.
*/ */
@ -40,6 +41,11 @@ class UserToggle2FA extends Command implements PromptsForMissingInput
{ {
$user = User::whereUsername($this->argument('username'))->first(); $user = User::whereUsername($this->argument('username'))->first();
if(!$user) {
$this->error('Could not find any user with that username');
exit;
}
if(!$user->{'2fa_enabled'}) { if(!$user->{'2fa_enabled'}) {
$this->info('User did not have 2FA enabled!'); $this->info('User did not have 2FA enabled!');
return; return;