Skip to content

Commit a74edc8

Browse files
committed
adds wbs-user:disable command
1 parent 1562a97 commit a74edc8

1 file changed

Lines changed: 70 additions & 0 deletions

File tree

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace App\Console\Commands\User;
4+
5+
/**
6+
* Disables a user account, deletes information about their email address and their password hash.
7+
* Requires the user to manage zero wikis.
8+
*/
9+
10+
use App\User;
11+
use App\WikiManager;
12+
use App\Wiki;
13+
use Illuminate\Console\Command;
14+
15+
class Disable extends Command {
16+
protected $signature = 'wbs-user:disable {--email=}';
17+
18+
protected $description = 'Disable user account';
19+
20+
public function handle(): int {
21+
$email = $this->option('email');
22+
23+
$user = User::whereEmail($email)->first();
24+
25+
if (empty($email)) {
26+
$this->error("Error: no email address provided. usage: wbs-user:disable --email='mail@address.com'");
27+
28+
return 1;
29+
}
30+
31+
if (!$user) {
32+
$this->error("Error: Could not find a user for '$email'.");
33+
34+
return 2;
35+
}
36+
37+
$userWikiManagers = WikiManager::whereUserId($user->id)->with('wiki')->get();
38+
$undeletedWikis = [];
39+
40+
foreach($userWikiManagers as $userWikiManager) {
41+
$userWiki = $userWikiManager->wiki;
42+
43+
if ($userWiki !== null) {
44+
$undeletedWikis[] = $userWiki->domain;
45+
}
46+
}
47+
48+
if (! empty($undeletedWikis)) {
49+
$this->error('Error: User still has wikis: '.print_r($undeletedWikis, true));
50+
51+
return 3;
52+
}
53+
54+
$userId = $user->id;
55+
$user->email = '';
56+
$user->password = random_bytes(10);
57+
$user->verified = false;
58+
59+
if ($user->save()) {
60+
$this->info("Successfully disabled user account with email '$email' (id: '$userId')");
61+
$this->info("Information about email and password hash was deleted.");
62+
63+
return 0;
64+
} else {
65+
$this->error('Error: Failed to save changes to the database.');
66+
67+
return 4;
68+
}
69+
}
70+
}

0 commit comments

Comments
 (0)