2017-12-25 20:52:41 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Console\Commands;
|
|
|
|
|
2023-05-17 16:56:55 +00:00
|
|
|
use BookStack\Users\Models\User;
|
|
|
|
use BookStack\Users\UserRepo;
|
2017-12-25 20:52:41 +00:00
|
|
|
use Illuminate\Console\Command;
|
|
|
|
|
2023-05-24 12:21:46 +00:00
|
|
|
class DeleteUsersCommand extends Command
|
2018-01-28 16:58:52 +00:00
|
|
|
{
|
2017-12-25 20:52:41 +00: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 15:58:40 +00:00
|
|
|
protected $description = 'Delete users that are not "admin" or system users';
|
2017-12-25 20:52:41 +00:00
|
|
|
|
2023-05-24 11:59:50 +00:00
|
|
|
/**
|
|
|
|
* Execute the console command.
|
|
|
|
*/
|
|
|
|
public function handle(UserRepo $userRepo): int
|
2017-12-25 20:52:41 +00:00
|
|
|
{
|
2023-05-24 11:59:50 +00: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 20:52:41 +00:00
|
|
|
|
2023-05-24 11:59:50 +00:00
|
|
|
if (!$confirm) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
$totalUsers = User::query()->count();
|
2017-12-25 20:52:41 +00:00
|
|
|
$numDeleted = 0;
|
2023-05-24 11:59:50 +00: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 20:52:41 +00:00
|
|
|
}
|
2023-05-24 11:59:50 +00:00
|
|
|
$userRepo->destroy($user);
|
|
|
|
$numDeleted++;
|
2017-12-25 20:52:41 +00:00
|
|
|
}
|
2023-05-24 11:59:50 +00:00
|
|
|
|
|
|
|
$this->info("Deleted $numDeleted of $totalUsers total users.");
|
|
|
|
return 0;
|
2017-12-25 20:52:41 +00:00
|
|
|
}
|
|
|
|
}
|