]> BookStack Code Mirror - bookstack/blobdiff - app/Console/Commands/DeleteUsers.php
Aligned command class code
[bookstack] / app / Console / Commands / DeleteUsers.php
index c16a5d9a694f30a3f276b871377c87b7de8cc9c4..f3e7c68525e28fa5db2221ddf9eeacc31c4f3ccf 100644 (file)
@@ -15,8 +15,6 @@ class DeleteUsers extends Command
      */
     protected $signature = 'bookstack:delete-users';
 
-    protected $userRepo;
-
     /**
      * The console command description.
      *
@@ -24,30 +22,32 @@ class DeleteUsers extends Command
      */
     protected $description = 'Delete users that are not "admin" or system users';
 
-    public function __construct(UserRepo $userRepo)
+    /**
+     * Execute the console command.
+     */
+    public function handle(UserRepo $userRepo): int
     {
-        $this->userRepo = $userRepo;
-        parent::__construct();
-    }
+        $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?');
 
-    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)');
+        if (!$confirm) {
+            return 0;
+        }
+
+        $totalUsers = User::query()->count();
         $numDeleted = 0;
-        if (strtolower(trim($confirm)) === 'yes') {
-            $totalUsers = User::query()->count();
-            $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;
-                }
-                $this->userRepo->destroy($user);
-                $numDeleted++;
+        $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;
             }
-            $this->info("Deleted $numDeleted of $totalUsers total users.");
-        } else {
-            $this->info('Exiting...');
+            $userRepo->destroy($user);
+            $numDeleted++;
         }
+
+        $this->info("Deleted $numDeleted of $totalUsers total users.");
+        return 0;
     }
 }