From 7da8804753b38529e6add829726de2145a0059d1 Mon Sep 17 00:00:00 2001 From: Abijeet Date: Tue, 26 Dec 2017 02:22:41 +0530 Subject: [PATCH] Adds code to allow deletion of users via cmd line. Fixes #579 Command: php artisan bookstack:delete-users Signed-off-by: Abijeet --- app/Console/Commands/DeleteUsers.php | 62 ++++++++++++++++++++++++++++ app/User.php | 2 +- 2 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 app/Console/Commands/DeleteUsers.php diff --git a/app/Console/Commands/DeleteUsers.php b/app/Console/Commands/DeleteUsers.php new file mode 100644 index 000000000..c287dd55e --- /dev/null +++ b/app/Console/Commands/DeleteUsers.php @@ -0,0 +1,62 @@ +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; + if (strtolower(trim($confirm)) === 'yes') + { + $totalUsers = User::count(); + $users = $this->user->where('system_name', '=', null)->with('roles')->get(); + foreach ($users as $user) + { + if ($user->hasRole('admin')) + { + // don't delete users with "admin" role + continue; + } + $this->userRepo->destroy($user); + ++$numDeleted; + } + $this->info("Deleted $numDeleted of $totalUsers total users."); + } + else + { + $this->info('Exiting...'); + } + } + +} diff --git a/app/User.php b/app/User.php index 8033557e4..fd6879ba0 100644 --- a/app/User.php +++ b/app/User.php @@ -81,7 +81,7 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon */ public function hasSystemRole($role) { - return $this->roles->pluck('system_name')->contains('admin'); + return $this->roles->pluck('system_name')->contains($role); } /**