1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Apps\Model\Front\User; |
4
|
|
|
|
5
|
|
|
use Apps\ActiveRecord\UserRecovery; |
6
|
|
|
use Ffcms\Core\App; |
7
|
|
|
use Ffcms\Core\Arch\Model; |
8
|
|
|
use Ffcms\Core\Exception\SyntaxException; |
9
|
|
|
use Ffcms\Core\Helper\Date; |
10
|
|
|
use Ffcms\Core\Helper\Type\Str; |
11
|
|
|
|
12
|
|
|
class FormRecovery extends Model |
13
|
|
|
{ |
14
|
|
|
const DELAY = 900; // delay between 2 recovery submits |
15
|
|
|
|
16
|
|
|
public $email; |
17
|
|
|
public $captcha; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Labels |
21
|
|
|
*/ |
22
|
|
|
public function labels() |
23
|
|
|
{ |
24
|
|
|
return [ |
25
|
|
|
'email' => __('Email'), |
26
|
|
|
'captcha' => __('Captcha') |
27
|
|
|
]; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Validation rules |
32
|
|
|
*/ |
33
|
|
|
public function rules() |
34
|
|
|
{ |
35
|
|
|
return [ |
36
|
|
|
[['email', 'captcha'], 'required'], |
37
|
|
|
['email', 'email'], |
38
|
|
|
['captcha', 'App::$Captcha::validate'], |
39
|
|
|
['email', 'App::$User::isMailExist'] |
40
|
|
|
]; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* After validation generate new pwd, recovery token and send email |
45
|
|
|
* @throws SyntaxException |
46
|
|
|
*/ |
47
|
|
|
public function make() |
48
|
|
|
{ |
49
|
|
|
$user = App::$User->getIdentityViaEmail($this->email); |
50
|
|
|
if ($user === null) { |
51
|
|
|
throw new SyntaxException('Email not found'); |
52
|
|
|
} |
53
|
|
|
if ($user->approve_token !== '0' && Str::length($user->approve_token) > 0) { |
|
|
|
|
54
|
|
|
throw new SyntaxException('You must approve your account'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$rows = UserRecovery::where('user_id', '=', $user->getId()) |
58
|
|
|
->orderBy('id', 'DESC') |
59
|
|
|
->first(); |
60
|
|
|
|
61
|
|
|
if ($rows !== null && $rows !== false) { |
62
|
|
|
// prevent spam of recovery messages |
63
|
|
|
if (Date::convertToTimestamp($rows->created_at) > time() - self::DELAY) { |
64
|
|
|
return; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
// generate pwd, token and pwdCrypt |
69
|
|
|
$newPwd = Str::randomLatinNumeric(mt_rand(8, 16)); |
70
|
|
|
$pwdCrypt = App::$Security->password_hash($newPwd); |
71
|
|
|
$token = Str::randomLatinNumeric(mt_rand(64, 128)); |
72
|
|
|
|
73
|
|
|
$rObject = new UserRecovery(); |
74
|
|
|
$rObject->user_id = $user->id; |
|
|
|
|
75
|
|
|
$rObject->password = $pwdCrypt; |
76
|
|
|
$rObject->token = $token; |
77
|
|
|
$rObject->save(); |
78
|
|
|
|
79
|
|
|
// generate mail template |
80
|
|
|
$mailTemplate = App::$View->render('user/_recoveryMail', [ |
81
|
|
|
'login' => $user->login, |
|
|
|
|
82
|
|
|
'email' => $this->email, |
83
|
|
|
'password' => $newPwd, |
84
|
|
|
'token' => $token, |
85
|
|
|
'id' => $rObject->id |
86
|
|
|
]); |
87
|
|
|
|
88
|
|
|
$sender = App::$Properties->get('adminEmail'); |
89
|
|
|
|
90
|
|
|
// format SWIFTMailer format |
91
|
|
|
$mailMessage = \Swift_Message::newInstance(App::$Translate->get('Profile', 'Account recovery on %site%', ['site' => App::$Request->getHost()])) |
92
|
|
|
->setFrom([$sender]) |
93
|
|
|
->setTo([$this->email]) |
94
|
|
|
->setBody($mailTemplate, 'text/html'); |
95
|
|
|
// send message |
96
|
|
|
App::$Mailer->send($mailMessage); |
97
|
|
|
} |
98
|
|
|
} |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: