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