]> BookStack Code Mirror - bookstack/blob - app/Console/Commands/DeleteUsersCommand.php
Opensearch: Fixed XML declaration when php short tags enabled
[bookstack] / app / Console / Commands / DeleteUsersCommand.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 DeleteUsersCommand 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     /**
19      * The console command description.
20      *
21      * @var string
22      */
23     protected $description = 'Delete users that are not "admin" or system users';
24
25     /**
26      * Execute the console command.
27      */
28     public function handle(UserRepo $userRepo): int
29     {
30         $this->warn('This will delete all users from the system that are not "admin" or system users.');
31         $confirm = $this->confirm('Are you sure you want to continue?');
32
33         if (!$confirm) {
34             return 0;
35         }
36
37         $totalUsers = User::query()->count();
38         $numDeleted = 0;
39         $users = User::query()->whereNull('system_name')->with('roles')->get();
40
41         foreach ($users as $user) {
42             if ($user->hasSystemRole('admin')) {
43                 // don't delete users with "admin" role
44                 continue;
45             }
46             $userRepo->destroy($user);
47             $numDeleted++;
48         }
49
50         $this->info("Deleted $numDeleted of $totalUsers total users.");
51         return 0;
52     }
53 }