2017-12-25 15:52:41 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Console\Commands;
|
|
|
|
|
2023-05-17 12:56:55 -04:00
|
|
|
use BookStack\Users\Models\User;
|
|
|
|
use BookStack\Users\UserRepo;
|
2017-12-25 15:52:41 -05:00
|
|
|
use Illuminate\Console\Command;
|
|
|
|
|
2023-05-24 08:21:46 -04:00
|
|
|
class DeleteUsersCommand extends Command
|
2018-01-28 11:58:52 -05:00
|
|
|
{
|
2017-12-25 15:52:41 -05:00
|
|
|
/**
|
|
|
|
* The name and signature of the console command.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $signature = 'bookstack:delete-users';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The console command description.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2020-04-09 11:58:40 -04:00
|
|
|
protected $description = 'Delete users that are not "admin" or system users';
|
2017-12-25 15:52:41 -05:00
|
|
|
|
2023-05-24 07:59:50 -04:00
|
|
|
/**
|
|
|
|
* Execute the console command.
|
|
|
|
*/
|
|
|
|
public function handle(UserRepo $userRepo): int
|
2017-12-25 15:52:41 -05:00
|
|
|
{
|
2023-05-24 07:59:50 -04:00
|
|
|
$this->warn('This will delete all users from the system that are not "admin" or system users.');
|
|
|
|
$confirm = $this->confirm('Are you sure you want to continue?');
|
2017-12-25 15:52:41 -05:00
|
|
|
|
2023-05-24 07:59:50 -04:00
|
|
|
if (!$confirm) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
$totalUsers = User::query()->count();
|
2017-12-25 15:52:41 -05:00
|
|
|
$numDeleted = 0;
|
2023-05-24 07:59:50 -04:00
|
|
|
$users = User::query()->whereNull('system_name')->with('roles')->get();
|
|
|
|
|
|
|
|
foreach ($users as $user) {
|
|
|
|
if ($user->hasSystemRole('admin')) {
|
|
|
|
// don't delete users with "admin" role
|
|
|
|
continue;
|
2017-12-25 15:52:41 -05:00
|
|
|
}
|
2023-05-24 07:59:50 -04:00
|
|
|
$userRepo->destroy($user);
|
|
|
|
$numDeleted++;
|
2017-12-25 15:52:41 -05:00
|
|
|
}
|
2023-05-24 07:59:50 -04:00
|
|
|
|
|
|
|
$this->info("Deleted $numDeleted of $totalUsers total users.");
|
|
|
|
return 0;
|
2017-12-25 15:52:41 -05:00
|
|
|
}
|
|
|
|
}
|