|
1
|
|
|
<?php |
|
2
|
|
|
namespace App\Http\Form; |
|
3
|
|
|
|
|
4
|
|
|
use App\Form\BaseForm; |
|
5
|
|
|
use App\Model\User; |
|
6
|
|
|
use Yii; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Login form |
|
10
|
|
|
*/ |
|
11
|
|
|
class LoginForm extends BaseForm |
|
12
|
|
|
{ |
|
13
|
|
|
public $username; |
|
14
|
|
|
public $password; |
|
15
|
|
|
public $rememberMe = true; |
|
16
|
|
|
|
|
17
|
|
|
private $user; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* {@inheritdoc} |
|
21
|
|
|
*/ |
|
22
|
3 |
|
public function rules() |
|
23
|
|
|
{ |
|
24
|
|
|
return [ |
|
25
|
|
|
// username and password are both required |
|
26
|
3 |
|
[['username', 'password'], 'required'], |
|
27
|
|
|
// rememberMe must be a boolean value |
|
28
|
|
|
['rememberMe', 'boolean'], |
|
29
|
|
|
// password is validated by validatePassword() |
|
30
|
|
|
['password', 'validatePassword'], |
|
31
|
|
|
]; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function attributeLabels() |
|
35
|
|
|
{ |
|
36
|
|
|
return [ |
|
37
|
|
|
'username' => Yii::t('app', 'Username'), |
|
38
|
|
|
'password' => Yii::t('app', 'Password'), |
|
39
|
|
|
'rememberMe' => Yii::t('app', 'Remember Me'), |
|
40
|
|
|
]; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Validates the password. |
|
45
|
|
|
* This method serves as the inline validation for password. |
|
46
|
|
|
* |
|
47
|
|
|
* @param string $attribute the attribute currently being validated |
|
48
|
|
|
*/ |
|
49
|
3 |
|
public function validatePassword($attribute) |
|
50
|
|
|
{ |
|
51
|
3 |
|
if (!$this->hasErrors()) { |
|
52
|
3 |
|
$user = $this->getUser(); |
|
53
|
3 |
|
if (!$user || !$user->validatePassword($this->password)) { |
|
54
|
2 |
|
$this->addError($attribute, Yii::t('app', 'Incorrect username or password')); |
|
55
|
2 |
|
return; |
|
56
|
|
|
} |
|
57
|
1 |
|
if (!$user->isActive()) { |
|
58
|
|
|
$this->addError($attribute, Yii::t('app', 'Your account is inactive')); |
|
59
|
|
|
return; |
|
60
|
|
|
} |
|
61
|
1 |
|
if ($user->isDeleted()) { |
|
62
|
|
|
$this->addError($attribute, Yii::t('app', 'Your account was deleted')); |
|
63
|
|
|
return; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
1 |
|
} |
|
67
|
|
|
|
|
68
|
|
|
protected function handleInternal() |
|
69
|
|
|
{ |
|
70
|
|
|
return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0); |
|
|
|
|
|
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Logs in a user using the provided username and password. |
|
75
|
|
|
* |
|
76
|
|
|
* @return bool whether the user 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
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return false; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Finds user by [[username]] |
|
89
|
|
|
* |
|
90
|
|
|
* @return User|null |
|
91
|
|
|
*/ |
|
92
|
3 |
|
protected function getUser() |
|
93
|
|
|
{ |
|
94
|
3 |
|
if ($this->user === null) { |
|
95
|
3 |
|
$this->user = User::findByUsername($this->username); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
3 |
|
return $this->user; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|