]> BookStack Code Mirror - bookstack/blob - app/Console/Commands/DeleteUsers.php
Adds code to allow deletion of users via cmd line.
[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      * 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         {
42             $totalUsers = User::count();
43             $users = $this->user->where('system_name', '=', null)->with('roles')->get();
44             foreach ($users as $user)
45             {
46                 if ($user->hasRole('admin'))
47                 {
48                     // don't delete users with "admin" role
49                     continue;
50                 }
51                 $this->userRepo->destroy($user);
52                 ++$numDeleted;
53             }
54             $this->info("Deleted $numDeleted of $totalUsers total users.");
55         }
56         else
57         {
58             $this->info('Exiting...');
59         }
60     }
61
62 }