1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
4
|
|
|
|
5
|
|
|
class ResetPasswordCommand extends SilverstripeCommand |
|
|
|
|
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @var string |
9
|
|
|
*/ |
10
|
|
|
protected $name = 'security:resetpassword'; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
protected $description = 'Send a reset password link to an email address'; |
16
|
|
|
|
17
|
|
|
public function fire() |
18
|
|
|
{ |
19
|
|
|
$member = $this->getMemberByEmailOrID(); |
20
|
|
|
|
21
|
|
|
if (!(bool) $member) { |
22
|
|
|
$this->error('Member not found'); |
23
|
|
|
} else { |
24
|
|
|
$link = $this->sendResetPasswordEmail($member); |
|
|
|
|
25
|
|
|
$this->info('Email sent to '.$member->getName().'<'.$member->Email.'>'); |
26
|
|
|
$this->line($link); |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @return Member|null |
|
|
|
|
32
|
|
|
*/ |
33
|
|
|
protected function getMemberByEmailOrID() |
34
|
|
|
{ |
35
|
|
|
$emailorid = $this->argument('emailorid'); |
36
|
|
|
|
37
|
|
|
$member = null; |
|
|
|
|
38
|
|
|
|
39
|
|
|
if (Str::contains($emailorid, '@')) { |
|
|
|
|
40
|
|
|
$member = Member::get()->where("Email = '".Convert::raw2sql($emailorid)."'")->first(); |
41
|
|
|
} else { |
42
|
|
|
$member = Member::get()->byID($emailorid); |
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return $member; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Send the reset password email and return the generated link. |
50
|
|
|
* |
51
|
|
|
* @param Member $member |
52
|
|
|
* |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
|
|
protected function sendResetPasswordEmail(Member $member) |
|
|
|
|
56
|
|
|
{ |
57
|
|
|
// hack ? |
58
|
|
|
global $_FILE_TO_URL_MAPPING; |
|
|
|
|
59
|
|
|
|
60
|
|
|
if ($_FILE_TO_URL_MAPPING[BASE_PATH]) { |
61
|
|
|
$_SERVER['REQUEST_URI'] = $_FILE_TO_URL_MAPPING[BASE_PATH]; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$token = $member->generateAutologinTokenAndStoreHash(); |
65
|
|
|
$link = Security::getPasswordResetLink($member, $token); |
66
|
|
|
|
67
|
|
|
/* @var Member_ForgotPasswordEmail $email */ |
68
|
|
|
$email = Member_ForgotPasswordEmail::create(); |
69
|
|
|
$email->populateTemplate($member); |
70
|
|
|
$email->populateTemplate([ |
71
|
|
|
'PasswordResetLink' => $link, |
72
|
|
|
]); |
73
|
|
|
$email->setTo($member->Email); |
74
|
|
|
$email->send(); |
75
|
|
|
|
76
|
|
|
return $link; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return array |
81
|
|
|
*/ |
82
|
|
|
protected function getArguments() |
83
|
|
|
{ |
84
|
|
|
return [ |
85
|
|
|
['emailorid', InputArgument::REQUIRED, 'The emailaddress or ID of the Member'], |
86
|
|
|
]; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.