|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Cjt Terabyte LLC [yii2-extension]. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Cjt Terabyte LLC [yii2-extension] <http://github.com/cjtterabytesoft>. |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE.md. |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
* |
|
10
|
|
|
* @link http://www.cjtterabyte.com. |
|
11
|
|
|
* @author Wilmer Arámbula <[email protected]>. |
|
12
|
|
|
* @copyright (c) 2015 Cjt Terabyte LLC. |
|
13
|
|
|
* @Extension: [yii2-adminlte-basic]. |
|
14
|
|
|
* @Models App [LoginForm]. |
|
15
|
|
|
* @since 1.0 |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
namespace cjtterabytesoft\adminlte\basic\models; |
|
19
|
|
|
|
|
20
|
|
|
use Yii; |
|
21
|
|
|
use yii\base\Model; |
|
22
|
|
|
|
|
23
|
|
|
class LoginForm extends Model |
|
24
|
|
|
{ |
|
25
|
|
|
public $username; |
|
26
|
|
|
public $password; |
|
27
|
|
|
public $rememberMe = true; |
|
28
|
|
|
|
|
29
|
|
|
private $_user = false; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @return array the validation rules. |
|
33
|
|
|
*/ |
|
34
|
|
|
public function rules() |
|
35
|
|
|
{ |
|
36
|
|
|
return [ |
|
37
|
|
|
// username and password are both required |
|
38
|
|
|
[['username', 'password'], 'required'], |
|
39
|
|
|
// rememberMe must be a boolean value |
|
40
|
|
|
['rememberMe', 'boolean'], |
|
41
|
|
|
// password is validated by validatePassword() |
|
42
|
|
|
['password', 'validatePassword'], |
|
43
|
|
|
]; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** @inheritdoc */ |
|
47
|
|
|
public function attributeLabels() |
|
48
|
|
|
{ |
|
49
|
|
|
return [ |
|
50
|
|
|
'username' => Yii::t('adminlte', 'Username'), |
|
51
|
|
|
'password' => Yii::t('adminlte', 'Password'), |
|
52
|
|
|
'rememberMe' => Yii::t('adminlte', 'Remember'), |
|
53
|
|
|
]; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Validates the password. |
|
58
|
|
|
* This method serves as the inline validation for password. |
|
59
|
|
|
* |
|
60
|
|
|
* @param string $attribute the attribute currently being validated |
|
61
|
|
|
* @param array $params the additional name-value pairs given in the rule |
|
62
|
|
|
*/ |
|
63
|
|
|
public function validatePassword($attribute, $params) |
|
|
|
|
|
|
64
|
|
|
{ |
|
65
|
|
|
if (!$this->hasErrors()) { |
|
66
|
|
|
$user = $this->getUser(); |
|
67
|
|
|
|
|
68
|
|
|
if (!$user || !$user->validatePassword($this->password)) { |
|
69
|
|
|
$this->addError($attribute, yii::t('adminlte', 'Incorrect username or password.')); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Logs in a user using the provided username and password. |
|
76
|
|
|
* @return boolean 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
|
|
|
return false; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Finds user by [[username]] |
|
88
|
|
|
* |
|
89
|
|
|
* @return User|null |
|
90
|
|
|
*/ |
|
91
|
|
|
public function getUser() |
|
92
|
|
|
{ |
|
93
|
|
|
if ($this->_user === false) { |
|
94
|
|
|
$this->_user = User::findByUsername($this->username); |
|
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return $this->_user; |
|
98
|
|
|
} |
|
99
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.