-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathChangeEmail.php
More file actions
48 lines (33 loc) · 1.27 KB
/
ChangeEmail.php
File metadata and controls
48 lines (33 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
namespace App\Console\Commands\User;
use App\Jobs\UserVerificationCreateTokenAndSendJob;
use App\User;
use Illuminate\Console\Command;
class ChangeEmail extends Command {
protected $signature = 'wbs-user:change-email {--from=} {--to=}';
protected $description = 'Change e-mail address for user';
public function handle(): int {
$emailOld = $this->option('from');
$emailNew = $this->option('to');
$user = User::whereEmail($emailOld)->first();
if (!$user) {
$this->error("Error: Could not find a user for '$emailOld'.");
return 1;
}
if ($emailNew == $emailOld) {
$this->error('Error: New email matches current email.');
return 2;
}
$user->email = $emailNew;
$user->verified = false;
if ($user->save()) {
UserVerificationCreateTokenAndSendJob::newForReverification($user)->handle();
$this->info("Successfully changed user email from '$emailOld' to '$emailNew'");
$this->line("Note: a verification mail was sent to the new address ('$emailNew').");
return 0;
} else {
$this->error('Error: Failed to save changes to the database.');
return 3;
}
}
}