1
0

UserDelete.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\User;
  5. use App\Jobs\DeletePipeline\DeleteAccountPipeline;
  6. class UserDelete extends Command
  7. {
  8. /**
  9. * The name and signature of the console command.
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'user:delete {id} {--force}';
  14. /**
  15. * The console command description.
  16. *
  17. * @var string
  18. */
  19. protected $description = 'Delete account';
  20. /**
  21. * Create a new command instance.
  22. *
  23. * @return void
  24. */
  25. public function __construct()
  26. {
  27. parent::__construct();
  28. }
  29. /**
  30. * Execute the console command.
  31. *
  32. * @return mixed
  33. */
  34. public function handle()
  35. {
  36. $id = $this->argument('id');
  37. $force = $this->option('force');
  38. if(ctype_digit($id) == true) {
  39. $user = User::find($id);
  40. } else {
  41. $user = User::whereUsername($id)->first();
  42. }
  43. if(!$user) {
  44. $this->error('Could not find any user with that username or id.');
  45. exit;
  46. }
  47. if($user->status == 'deleted' && $force == false) {
  48. $this->error('Account has already been deleted.');
  49. return;
  50. }
  51. if($user->is_admin == true) {
  52. $this->error('Cannot delete an admin account from CLI.');
  53. exit;
  54. }
  55. if(!$this->confirm('Are you sure you want to delete this account?')) {
  56. exit;
  57. }
  58. $confirmation = $this->ask('Enter the username to confirm deletion');
  59. if($confirmation !== $user->username) {
  60. $this->error('Username does not match, exiting...');
  61. exit;
  62. }
  63. if($user->status !== 'deleted') {
  64. $profile = $user->profile;
  65. $profile->status = $user->status = 'deleted';
  66. $profile->save();
  67. $user->save();
  68. }
  69. DeleteAccountPipeline::dispatch($user)->onQueue('high');
  70. }
  71. }