UserAdmin.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Contracts\Console\PromptsForMissingInput;
  5. use App\User;
  6. class UserAdmin extends Command implements PromptsForMissingInput
  7. {
  8. /**
  9. * The name and signature of the console command.
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'user:admin {username}';
  14. /**
  15. * The console command description.
  16. *
  17. * @var string
  18. */
  19. protected $description = 'Make a user an admin, or remove admin privileges.';
  20. /**
  21. * Prompt for missing input arguments using the returned questions.
  22. *
  23. * @return array
  24. */
  25. protected function promptForMissingArgumentsUsing()
  26. {
  27. return [
  28. 'username' => 'Which username should we toggle admin privileges for?',
  29. ];
  30. }
  31. /**
  32. * Execute the console command.
  33. *
  34. * @return mixed
  35. */
  36. public function handle()
  37. {
  38. $id = $this->argument('username');
  39. $user = User::whereUsername($id)->first();
  40. if(!$user) {
  41. $this->error('Could not find any user with that username or id.');
  42. exit;
  43. }
  44. $this->info('Found username: ' . $user->username);
  45. $state = $user->is_admin ? 'Remove admin privileges from this user?' : 'Add admin privileges to this user?';
  46. $confirmed = $this->confirm($state);
  47. if(!$confirmed) {
  48. exit;
  49. }
  50. $user->is_admin = !$user->is_admin;
  51. $user->save();
  52. $this->info('Successfully changed permissions!');
  53. }
  54. }