|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace modules\users\models; |
|
4
|
|
|
|
|
5
|
|
|
use Yii; |
|
6
|
|
|
use yii\base\Model; |
|
7
|
|
|
use modules\users\Module; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class LoginForm |
|
11
|
|
|
* @package modules\users\models |
|
12
|
|
|
* |
|
13
|
|
|
* @property string $email Email |
|
14
|
|
|
* @property string $password Password |
|
15
|
|
|
* @property-read null|array|User $user |
|
16
|
|
|
* @property bool $rememberMe Remember Me |
|
17
|
|
|
*/ |
|
18
|
|
|
class LoginForm extends Model |
|
19
|
|
|
{ |
|
20
|
|
|
public $email; |
|
21
|
|
|
public $password; |
|
22
|
|
|
public $rememberMe = false; |
|
23
|
|
|
|
|
24
|
|
|
private $user; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @inheritdoc |
|
28
|
|
|
* @return array |
|
29
|
|
|
*/ |
|
30
|
|
|
public function rules() |
|
31
|
|
|
{ |
|
32
|
|
|
return [ |
|
33
|
|
|
// username and password are both required |
|
34
|
|
|
[['email', 'password'], 'required'], |
|
35
|
|
|
[['email'], 'email'], |
|
36
|
|
|
// rememberMe must be a boolean value |
|
37
|
|
|
['rememberMe', 'boolean'], |
|
38
|
|
|
// password is validated by validatePassword() |
|
39
|
|
|
['password', 'validatePassword'], |
|
40
|
|
|
]; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @inheritdoc |
|
45
|
|
|
* @return array |
|
46
|
|
|
*/ |
|
47
|
|
|
public function attributeLabels() |
|
48
|
|
|
{ |
|
49
|
|
|
return [ |
|
50
|
|
|
'username' => Module::t('module', 'Username'), |
|
51
|
|
|
'email' => Module::t('module', 'Email'), |
|
52
|
|
|
'password' => Module::t('module', 'Password'), |
|
53
|
|
|
'rememberMe' => Module::t('module', 'Remember Me'), |
|
54
|
|
|
]; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Validates the password. |
|
59
|
|
|
* This method serves as the inline validation for password. |
|
60
|
|
|
* |
|
61
|
|
|
* @param string $attribute the attribute currently being validated |
|
62
|
|
|
*/ |
|
63
|
|
|
public function validatePassword($attribute) |
|
64
|
|
|
{ |
|
65
|
|
|
if (!$this->hasErrors()) { |
|
66
|
|
|
$user = $this->getUser(); |
|
67
|
|
|
if (!$user || !$user->validatePassword($this->password)) { |
|
68
|
|
|
$this->addError($attribute, Module::t('module', 'Invalid email or password.')); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Logs in a users using the provided username and password. |
|
75
|
|
|
* |
|
76
|
|
|
* @return bool whether the users is logged in successfully |
|
77
|
|
|
*/ |
|
78
|
|
|
public function login() |
|
79
|
|
|
{ |
|
80
|
|
|
if ($this->validate()) { |
|
81
|
|
|
return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0); |
|
|
|
|
|
|
82
|
|
|
} else { |
|
83
|
|
|
return false; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Logout users |
|
89
|
|
|
* @return bool |
|
90
|
|
|
*/ |
|
91
|
|
|
public function logout() |
|
92
|
|
|
{ |
|
93
|
|
|
return Yii::$app->user->logout(); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Finds users by [[username]] |
|
98
|
|
|
* |
|
99
|
|
|
* @return array|null|User |
|
100
|
|
|
*/ |
|
101
|
|
|
protected function getUser() |
|
102
|
|
|
{ |
|
103
|
|
|
if ($this->user === null) { |
|
104
|
|
|
$this->user = User::findByUsernameEmail($this->email); |
|
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
return $this->user; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.