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