2019-03-27 02:25:27 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
|
|
|
|
use Illuminate\Console\Command;
|
2023-10-11 02:20:18 +00:00
|
|
|
use Illuminate\Contracts\Console\PromptsForMissingInput;
|
2019-03-27 02:25:27 +00:00
|
|
|
use App\User;
|
|
|
|
|
2023-10-11 02:20:18 +00:00
|
|
|
class UserAdmin extends Command implements PromptsForMissingInput
|
2019-03-27 02:25:27 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* The name and signature of the console command.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2023-10-11 02:20:18 +00:00
|
|
|
protected $signature = 'user:admin {username}';
|
2019-03-27 02:25:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The console command description.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $description = 'Make a user an admin, or remove admin privileges.';
|
|
|
|
|
|
|
|
/**
|
2023-10-11 02:20:18 +00:00
|
|
|
* Prompt for missing input arguments using the returned questions.
|
2019-03-27 02:25:27 +00:00
|
|
|
*
|
2023-10-11 02:20:18 +00:00
|
|
|
* @return array
|
2019-03-27 02:25:27 +00:00
|
|
|
*/
|
2023-10-11 02:20:18 +00:00
|
|
|
protected function promptForMissingArgumentsUsing()
|
2019-03-27 02:25:27 +00:00
|
|
|
{
|
2023-10-11 02:20:18 +00:00
|
|
|
return [
|
|
|
|
'username' => 'Which username should we toggle admin privileges for?',
|
|
|
|
];
|
2019-03-27 02:25:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the console command.
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
2023-10-11 02:20:18 +00:00
|
|
|
$id = $this->argument('username');
|
|
|
|
|
|
|
|
$user = User::whereUsername($id)->first();
|
|
|
|
|
2019-03-27 02:25:27 +00:00
|
|
|
if(!$user) {
|
|
|
|
$this->error('Could not find any user with that username or id.');
|
|
|
|
exit;
|
|
|
|
}
|
2023-10-11 02:20:18 +00:00
|
|
|
|
2019-03-27 02:25:27 +00:00
|
|
|
$this->info('Found username: ' . $user->username);
|
|
|
|
$state = $user->is_admin ? 'Remove admin privileges from this user?' : 'Add admin privileges to this user?';
|
|
|
|
$confirmed = $this->confirm($state);
|
|
|
|
if(!$confirmed) {
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
$user->is_admin = !$user->is_admin;
|
|
|
|
$user->save();
|
|
|
|
$this->info('Successfully changed permissions!');
|
|
|
|
}
|
|
|
|
}
|