|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
4
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
5
|
|
|
|
|
6
|
|
|
class ResetPasswordCommand extends SilverstripeCommand |
|
|
|
|
|
|
7
|
|
|
{ |
|
8
|
|
|
/** |
|
9
|
|
|
* @var string |
|
10
|
|
|
*/ |
|
11
|
|
|
protected $name = 'security:resetpassword'; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @var string |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $description = 'Send a reset password link to an email address'; |
|
17
|
|
|
|
|
18
|
|
|
public function fire() |
|
19
|
|
|
{ |
|
20
|
|
|
$member = $this->getMemberByEmailOrID(); |
|
21
|
|
|
|
|
22
|
|
|
if(!(bool)$member) { |
|
23
|
|
|
$this->error('Member not found'); |
|
24
|
|
|
} else { |
|
25
|
|
|
$link = $this->sendResetPasswordEmail($member); |
|
|
|
|
|
|
26
|
|
|
$this->info('Email sent to '.$member->getName().'<'.$member->Email.'>'); |
|
27
|
|
|
$this->line($link); |
|
28
|
|
|
} |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @return Member|null |
|
|
|
|
|
|
33
|
|
|
*/ |
|
34
|
|
|
protected function getMemberByEmailOrID() |
|
35
|
|
|
{ |
|
36
|
|
|
$emailorid = $this->argument('emailorid'); |
|
37
|
|
|
|
|
38
|
|
|
$member = null; |
|
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
if(Str::contains($emailorid, '@')) { |
|
|
|
|
|
|
41
|
|
|
$member = Member::get()->where("Email = '".Convert::raw2sql($emailorid)."'")->first(); |
|
42
|
|
|
}else{ |
|
43
|
|
|
$member = Member::get()->byID($emailorid); |
|
|
|
|
|
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
return $member; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Send the reset password email and return the generated link. |
|
51
|
|
|
* |
|
52
|
|
|
* @param Member $member |
|
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(array( |
|
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.