]> BookStack Code Mirror - bookstack/commitdiff
Adds code to allow deletion of users via cmd line. 621/head
authorAbijeet <redacted>
Mon, 25 Dec 2017 20:52:41 +0000 (02:22 +0530)
committerAbijeet <redacted>
Mon, 25 Dec 2017 20:52:41 +0000 (02:22 +0530)
Fixes #579

Command:

php artisan bookstack:delete-users

Signed-off-by: Abijeet <redacted>
app/Console/Commands/DeleteUsers.php [new file with mode: 0644]
app/User.php

diff --git a/app/Console/Commands/DeleteUsers.php b/app/Console/Commands/DeleteUsers.php
new file mode 100644 (file)
index 0000000..c287dd5
--- /dev/null
@@ -0,0 +1,62 @@
+<?php
+
+namespace BookStack\Console\Commands;
+
+use BookStack\User;
+use BookStack\Repos\UserRepo;
+use Illuminate\Console\Command;
+
+class DeleteUsers extends Command{
+
+    /**
+     * 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;
+        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...');
+        }
+    }
+
+}
index 8033557e4cb9a0a048c1d7112c90f84dc4e4bf70..fd6879ba007dc1d551b7385c6a93c043aebc98db 100644 (file)
@@ -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);
     }
 
     /**
Morty Proxy This is a proxified and sanitized view of the page, visit original site.