]> BookStack Code Mirror - bookstack/blob - app/Console/Commands/DeleteUsers.php
Updated minimum php version from 7.3 to 7.4
[bookstack] / app / Console / Commands / DeleteUsers.php
1 <?php
2
3 namespace BookStack\Console\Commands;
4
5 use BookStack\Auth\User;
6 use BookStack\Auth\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 $user;
19
20     protected $userRepo;
21
22     /**
23      * The console command description.
24      *
25      * @var string
26      */
27     protected $description = 'Delete users that are not "admin" or system users';
28
29     public function __construct(User $user, UserRepo $userRepo)
30     {
31         $this->user = $user;
32         $this->userRepo = $userRepo;
33         parent::__construct();
34     }
35
36     public function handle()
37     {
38         $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)');
39         $numDeleted = 0;
40         if (strtolower(trim($confirm)) === 'yes') {
41             $totalUsers = $this->user->count();
42             $users = $this->user->where('system_name', '=', null)->with('roles')->get();
43             foreach ($users as $user) {
44                 if ($user->hasSystemRole('admin')) {
45                     // don't delete users with "admin" role
46                     continue;
47                 }
48                 $this->userRepo->destroy($user);
49                 $numDeleted++;
50             }
51             $this->info("Deleted $numDeleted of $totalUsers total users.");
52         } else {
53             $this->info('Exiting...');
54         }
55     }
56 }