|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace yii2mod\user\models; |
|
4
|
|
|
|
|
5
|
|
|
use Yii; |
|
6
|
|
|
use yii\base\Model; |
|
7
|
|
|
use yii2mod\user\models\enums\UserStatus; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Login Form |
|
11
|
|
|
* |
|
12
|
|
|
* @package yii2mod\user\models |
|
13
|
|
|
*/ |
|
14
|
|
|
class LoginForm extends Model |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var string email |
|
18
|
|
|
*/ |
|
19
|
|
|
public $email; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var string password |
|
23
|
|
|
*/ |
|
24
|
|
|
public $password; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var bool remember me |
|
28
|
|
|
*/ |
|
29
|
|
|
public $rememberMe = true; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var bool|UserModel |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $user = false; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @inheritdoc |
|
38
|
|
|
*/ |
|
39
|
|
|
public function rules() |
|
40
|
|
|
{ |
|
41
|
|
|
return [ |
|
42
|
|
|
[['email', 'password'], 'required'], |
|
43
|
|
|
['email', 'email'], |
|
44
|
|
|
['password', 'validatePassword'], |
|
45
|
|
|
['rememberMe', 'boolean'], |
|
46
|
|
|
]; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @inheritdoc |
|
51
|
|
|
*/ |
|
52
|
|
View Code Duplication |
public function attributeLabels() |
|
|
|
|
|
|
53
|
|
|
{ |
|
54
|
|
|
return [ |
|
55
|
|
|
'email' => Yii::t('yii2mod.user', 'Email'), |
|
56
|
|
|
'password' => Yii::t('yii2mod.user', 'Password'), |
|
57
|
|
|
'rememberMe' => Yii::t('yii2mod.user', 'Remember Me'), |
|
58
|
|
|
]; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Validates the password. |
|
63
|
|
|
* This method serves as the inline validation for password. |
|
64
|
|
|
* |
|
65
|
|
|
* @param $attribute |
|
66
|
|
|
* @param $params |
|
67
|
|
|
*/ |
|
68
|
|
|
public function validatePassword($attribute, $params) |
|
|
|
|
|
|
69
|
|
|
{ |
|
70
|
|
|
if (!$this->hasErrors()) { |
|
71
|
|
|
$user = $this->getUser(); |
|
72
|
|
|
if ($user && $user->status === UserStatus::DELETED) { |
|
73
|
|
|
$this->addError($attribute, Yii::t('yii2mod.user', 'Your account has been deactivated, please contact support for details.')); |
|
74
|
|
|
} elseif (!$user || !$user->validatePassword($this->password)) { |
|
75
|
|
|
$this->addError($attribute, Yii::t('yii2mod.user', 'Incorrect email or password.')); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Logs in a user using the provided username and password. |
|
82
|
|
|
* |
|
83
|
|
|
* @return bool whether the user is logged in successfully |
|
84
|
|
|
*/ |
|
85
|
|
|
public function login() |
|
86
|
|
|
{ |
|
87
|
|
|
if ($this->validate()) { |
|
88
|
|
|
return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0); |
|
89
|
|
|
} else { |
|
90
|
|
|
return false; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Finds user by [[email]] |
|
96
|
|
|
* |
|
97
|
|
|
* @return UserModel|null |
|
98
|
|
|
*/ |
|
99
|
|
|
public function getUser() |
|
100
|
|
|
{ |
|
101
|
|
|
if ($this->user === false) { |
|
102
|
|
|
$this->user = UserModel::findByEmail($this->email); |
|
|
|
|
|
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return $this->user; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.