|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace zacksleo\yii2\backend\models\forms; |
|
4
|
|
|
|
|
5
|
|
|
use yii; |
|
6
|
|
|
use yii\base\Model; |
|
7
|
|
|
use zacksleo\yii2\backend\models\Admin; |
|
8
|
|
|
use zacksleo\yii2\backend\Module; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* LoginForm is the model behind the login form. |
|
12
|
|
|
*/ |
|
13
|
|
|
class LoginForm extends Model |
|
14
|
|
|
{ |
|
15
|
|
|
public $username; |
|
16
|
|
|
public $password; |
|
17
|
|
|
public $rememberMe = true; |
|
18
|
|
|
|
|
19
|
|
|
private $_user = false; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @return array the validation rules. |
|
23
|
|
|
*/ |
|
24
|
1 |
|
public function rules() |
|
25
|
|
|
{ |
|
26
|
|
|
return [ |
|
27
|
|
|
// username and password are both required |
|
28
|
1 |
|
[['username', 'password'], 'required'], |
|
29
|
|
|
// rememberMe must be a boolean value |
|
30
|
|
|
['rememberMe', 'boolean'], |
|
31
|
|
|
// password is validated by validatePassword() |
|
32
|
|
|
['password', 'validatePassword'], |
|
33
|
|
|
]; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Validates the password. |
|
38
|
|
|
* This method serves as the inline validation for password. |
|
39
|
|
|
*/ |
|
40
|
1 |
|
public function validatePassword() |
|
41
|
|
|
{ |
|
42
|
1 |
|
if (!$this->hasErrors()) { |
|
43
|
1 |
|
$user = $this->getUser(); |
|
44
|
1 |
|
if (!$user || !$user->validatePassword($this->password)) { |
|
45
|
1 |
|
$this->addError('password', Module::t('backend', 'Incorrect username or password')); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
1 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @inheritdoc |
|
52
|
|
|
*/ |
|
53
|
1 |
|
public function attributeLabels() |
|
54
|
|
|
{ |
|
55
|
|
|
return [ |
|
56
|
1 |
|
'username' => Module::t('backend', 'username'), |
|
57
|
1 |
|
'password' => Module::t('backend', 'password'), |
|
58
|
|
|
]; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Logs in a user using the provided username and password. |
|
63
|
|
|
* @return boolean whether the user is logged in successfully |
|
64
|
|
|
*/ |
|
65
|
1 |
|
public function login() |
|
66
|
|
|
{ |
|
67
|
1 |
|
if ($this->validate()) { |
|
68
|
|
|
return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0); |
|
69
|
|
|
} else { |
|
70
|
1 |
|
return false; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Finds user by [[username]] |
|
76
|
|
|
* |
|
77
|
|
|
* @return Admin|null |
|
78
|
|
|
*/ |
|
79
|
1 |
|
public function getUser() |
|
80
|
|
|
{ |
|
81
|
1 |
|
if ($this->_user === false) { |
|
82
|
|
|
/* @var $class Admin */ |
|
83
|
1 |
|
$class = Yii::$app->user->identityClass; |
|
84
|
1 |
|
$this->_user = $class::find()->canLogin()->username($this->username)->one(); |
|
|
|
|
|
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
1 |
|
return $this->_user; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.