|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace inblank\activeuser\models\forms; |
|
4
|
|
|
|
|
5
|
|
|
use inblank\activeuser\traits\CommonTrait; |
|
6
|
|
|
use yii; |
|
7
|
|
|
use yii\base\Model; |
|
8
|
|
|
use yii\base\Security; |
|
9
|
|
|
|
|
10
|
|
|
class LoginForm extends Model |
|
11
|
|
|
{ |
|
12
|
|
|
use CommonTrait; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var string user email for login |
|
16
|
|
|
*/ |
|
17
|
|
|
public $email; |
|
18
|
|
|
/** |
|
19
|
|
|
* @var string user password for login in scenario SCENARIO_LOGIN |
|
20
|
|
|
*/ |
|
21
|
|
|
public $password; |
|
22
|
|
|
/** |
|
23
|
|
|
* @var bool remember me check in scenario SCENARIO_LOGIN |
|
24
|
|
|
*/ |
|
25
|
|
|
public $remember; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Founded user |
|
29
|
|
|
* @var \inblank\activeuser\models\User |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $user = false; |
|
32
|
|
|
|
|
33
|
|
|
/** @inheritdoc */ |
|
34
|
1 |
|
public function attributeLabels() |
|
35
|
|
|
{ |
|
36
|
|
|
return [ |
|
37
|
1 |
|
'email' => Yii::t('activeuser_general', 'Email'), |
|
38
|
1 |
|
'password' => Yii::t('activeuser_general', 'Password'), |
|
39
|
1 |
|
'remember' => Yii::t('activeuser_general', 'Remember me next time'), |
|
40
|
1 |
|
'hash' => Yii::t('activeuser_general', 'Special hash'), |
|
41
|
|
|
]; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** @inheritdoc */ |
|
45
|
1 |
|
public function rules() |
|
46
|
|
|
{ |
|
47
|
|
|
return [ |
|
48
|
1 |
|
[['email', 'password'], 'required'], |
|
49
|
|
|
['password', function () { |
|
50
|
1 |
|
if ($this->getUser() === null || !(new Security())->validatePassword($this->password, $this->getUser()->pass_hash)) { |
|
51
|
1 |
|
$this->addError('password', Yii::t('activeuser_general', 'Invalid email or password')); |
|
52
|
|
|
} |
|
53
|
1 |
|
}], |
|
54
|
1 |
|
['email', function () { |
|
55
|
1 |
|
if ($this->getUser() !== null) { |
|
56
|
1 |
|
if (!$this->getUser()->isConfirmed()) { |
|
57
|
1 |
|
$this->addError('password', Yii::t('activeuser_general', 'You need to confirm your email address')); |
|
58
|
1 |
|
} elseif ($this->getUser()->isBlocked()) { |
|
59
|
1 |
|
$this->addError('password', Yii::t('activeuser_general', 'Your account has been blocked')); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
1 |
|
}], |
|
63
|
|
|
]; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Validates form and logs the user in. |
|
68
|
|
|
* @return bool whether the user is logged in successfully |
|
69
|
|
|
*/ |
|
70
|
1 |
|
public function login() |
|
71
|
|
|
{ |
|
72
|
1 |
|
if ($this->validate()) { |
|
73
|
1 |
|
return Yii::$app->getUser()->login($this->getUser(), $this->remember ? $this->module->rememberTime : 0); |
|
|
|
|
|
|
74
|
|
|
} |
|
75
|
1 |
|
return false; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
1 |
|
protected function getUser() |
|
79
|
|
|
{ |
|
80
|
1 |
|
if ($this->user === false) { |
|
81
|
1 |
|
$userClass = self::di('User'); |
|
82
|
1 |
|
$this->user = empty($this->email) ? null : $userClass::findOne(['email' => $this->email]); |
|
83
|
|
|
} |
|
84
|
1 |
|
return $this->user; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: