3 namespace BookStack\Console\Commands;
5 use BookStack\Users\Models\User;
6 use BookStack\Users\UserRepo;
7 use Illuminate\Console\Command;
9 class DeleteUsers extends Command
12 * The name and signature of the console command.
16 protected $signature = 'bookstack:delete-users';
21 * The console command description.
25 protected $description = 'Delete users that are not "admin" or system users';
27 public function __construct(UserRepo $userRepo)
29 $this->userRepo = $userRepo;
30 parent::__construct();
33 public function handle()
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)');
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
45 $this->userRepo->destroy($user);
48 $this->info("Deleted $numDeleted of $totalUsers total users.");
50 $this->info('Exiting...');