Skip to content

Commit 89e06f4

Browse files
authored
adds wbs-user:disable command (#992)
* adds wbs-user:disable command * linting * cleanup * add unit test * linting fix
1 parent 216b16d commit 89e06f4

2 files changed

Lines changed: 117 additions & 0 deletions

File tree

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

0 commit comments

Comments
 (0)