]> BookStack Code Mirror - bookstack/blob - app/Console/Commands/DeleteUsers.php
Played around with a new app structure
[bookstack] / app / Console / Commands / DeleteUsers.php
1 <?php
2
3 namespace BookStack\Console\Commands;
4
5 use BookStack\Users\Models\User;
6 use BookStack\Users\UserRepo;
7 use Illuminate\Console\Command;
8
9 class DeleteUsers extends Command
10 {
11     /**
12      * The name and signature of the console command.
13      *
14      * @var string
15      */
16     protected $signature = 'bookstack:delete-users';
17
18     protected $userRepo;
19
20     /**
21      * The console command description.
22      *
23      * @var string
24      */
25     protected $description = 'Delete users that are not "admin" or system users';
26
27     public function __construct(UserRepo $userRepo)
28     {
29         $this->userRepo = $userRepo;
30         parent::__construct();
31     }
32
33     public function handle()
34     {
35         $confirm = $this->ask('This will delete all users from the system that are not "admin" or system users. Are you sure you want to continue? (Type "yes" to continue)');
36         $numDeleted = 0;
37         if (strtolower(trim($confirm)) === 'yes') {
38             $totalUsers = User::query()->count();
39             $users = User::query()->whereNull('system_name')->with('roles')->get();
40             foreach ($users as $user) {
41                 if ($user->hasSystemRole('admin')) {
42                     // don't delete users with "admin" role
43                     continue;
44                 }
45                 $this->userRepo->destroy($user);
46                 $numDeleted++;
47             }
48             $this->info("Deleted $numDeleted of $totalUsers total users.");
49         } else {
50             $this->info('Exiting...');
51         }
52     }
53 }