|
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
|
|
|
use Apps\ActiveRecord\UserLog; |
|
12
|
|
|
|
|
13
|
|
|
class FormRecovery extends Model |
|
14
|
|
|
{ |
|
15
|
|
|
const DELAY = 900; // delay between 2 recovery submits |
|
16
|
|
|
|
|
17
|
|
|
public $email; |
|
18
|
|
|
public $captcha; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Labels |
|
22
|
|
|
*/ |
|
23
|
|
|
public function labels() |
|
24
|
|
|
{ |
|
25
|
|
|
return [ |
|
26
|
|
|
'email' => __('Email'), |
|
27
|
|
|
'captcha' => __('Captcha') |
|
28
|
|
|
]; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Validation rules |
|
33
|
|
|
*/ |
|
34
|
|
|
public function rules() |
|
35
|
|
|
{ |
|
36
|
|
|
return [ |
|
37
|
|
|
[['email', 'captcha'], 'required'], |
|
38
|
|
|
['email', 'email'], |
|
39
|
|
|
['captcha', 'App::$Captcha::validate'], |
|
40
|
|
|
['email', 'App::$User::isMailExist'] |
|
41
|
|
|
]; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* After validation generate new pwd, recovery token and send email |
|
46
|
|
|
* @throws SyntaxException |
|
47
|
|
|
*/ |
|
48
|
|
|
public function make() |
|
49
|
|
|
{ |
|
50
|
|
|
$user = App::$User->getIdentityViaEmail($this->email); |
|
51
|
|
|
if ($user === null) { |
|
52
|
|
|
throw new SyntaxException('Email not found'); |
|
53
|
|
|
} |
|
54
|
|
|
if ($user->approve_token !== '0' && Str::length($user->approve_token) > 0) { |
|
|
|
|
|
|
55
|
|
|
throw new SyntaxException('You must approve your account'); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$rows = UserRecovery::where('user_id', '=', $user->getId()) |
|
59
|
|
|
->orderBy('id', 'DESC') |
|
60
|
|
|
->first(); |
|
61
|
|
|
|
|
62
|
|
|
if ($rows !== null && $rows !== false) { |
|
63
|
|
|
// prevent spam of recovery messages |
|
64
|
|
|
if (Date::convertToTimestamp($rows->created_at) > time() - self::DELAY) { |
|
65
|
|
|
return; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
// generate pwd, token and pwdCrypt |
|
70
|
|
|
$newPwd = Str::randomLatinNumeric(mt_rand(8, 16)); |
|
71
|
|
|
$pwdCrypt = App::$Security->password_hash($newPwd); |
|
72
|
|
|
$token = Str::randomLatinNumeric(mt_rand(64, 128)); |
|
73
|
|
|
|
|
74
|
|
|
// write new data to recovery table |
|
75
|
|
|
$rObject = new UserRecovery(); |
|
76
|
|
|
$rObject->user_id = $user->id; |
|
|
|
|
|
|
77
|
|
|
$rObject->password = $pwdCrypt; |
|
78
|
|
|
$rObject->token = $token; |
|
79
|
|
|
$rObject->save(); |
|
80
|
|
|
|
|
81
|
|
|
// write logs data |
|
82
|
|
|
$log = new UserLog(); |
|
83
|
|
|
$log->user_id = $user->id; |
|
|
|
|
|
|
84
|
|
|
$log->type = 'RECOVERY'; |
|
|
|
|
|
|
85
|
|
|
$log->message = __('Password recovery is initialized from: %ip%', ['ip' => App::$Request->getClientIp()]); |
|
|
|
|
|
|
86
|
|
|
$log->save(); |
|
87
|
|
|
|
|
88
|
|
|
// generate mail template |
|
89
|
|
|
$mailTemplate = App::$View->render('user/mail/recovery', [ |
|
90
|
|
|
'login' => $user->login, |
|
|
|
|
|
|
91
|
|
|
'email' => $this->email, |
|
92
|
|
|
'password' => $newPwd, |
|
93
|
|
|
'token' => $token, |
|
94
|
|
|
'id' => $rObject->id |
|
95
|
|
|
]); |
|
96
|
|
|
|
|
97
|
|
|
$sender = App::$Properties->get('adminEmail'); |
|
98
|
|
|
|
|
99
|
|
|
// format SWIFTMailer format |
|
100
|
|
|
$mailMessage = \Swift_Message::newInstance(App::$Translate->get('Profile', 'Account recovery on %site%', ['site' => App::$Request->getHost()])) |
|
101
|
|
|
->setFrom([$sender]) |
|
102
|
|
|
->setTo([$this->email]) |
|
103
|
|
|
->setBody($mailTemplate, 'text/html'); |
|
104
|
|
|
// send message |
|
105
|
|
|
App::$Mailer->send($mailMessage); |
|
|
|
|
|
|
106
|
|
|
} |
|
107
|
|
|
} |
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: