2017-12-26 04:52:41 +08:00
< ? php
namespace BookStack\Console\Commands ;
2018-09-25 19:30:50 +08:00
use BookStack\Auth\User ;
use BookStack\Auth\UserRepo ;
2017-12-26 04:52:41 +08:00
use Illuminate\Console\Command ;
2018-01-29 00:58:52 +08:00
class DeleteUsers extends Command
{
2017-12-26 04:52:41 +08:00
/**
* The name and signature of the console command .
*
* @ var string
*/
protected $signature = 'bookstack:delete-users' ;
protected $userRepo ;
/**
* The console command description .
*
* @ var string
*/
2020-04-09 23:58:40 +08:00
protected $description = 'Delete users that are not "admin" or system users' ;
2017-12-26 04:52:41 +08:00
2022-02-03 23:12:50 +08:00
public function __construct ( UserRepo $userRepo )
2017-12-26 04:52:41 +08:00
{
$this -> userRepo = $userRepo ;
parent :: __construct ();
}
public function handle ()
{
$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)' );
$numDeleted = 0 ;
2018-01-29 00:58:52 +08:00
if ( strtolower ( trim ( $confirm )) === 'yes' ) {
2022-02-03 23:12:50 +08:00
$totalUsers = User :: query () -> count ();
$users = User :: query () -> whereNull ( 'system_name' ) -> with ( 'roles' ) -> get ();
2018-01-29 00:58:52 +08:00
foreach ( $users as $user ) {
if ( $user -> hasSystemRole ( 'admin' )) {
2017-12-26 04:52:41 +08:00
// don't delete users with "admin" role
continue ;
}
$this -> userRepo -> destroy ( $user );
2021-06-26 23:23:15 +08:00
$numDeleted ++ ;
2017-12-26 04:52:41 +08:00
}
$this -> info ( " Deleted $numDeleted of $totalUsers total users. " );
2018-01-29 00:58:52 +08:00
} else {
2017-12-26 04:52:41 +08:00
$this -> info ( 'Exiting...' );
}
}
}