-
Notifications
You must be signed in to change notification settings - Fork 104
/
LoginForm.php
156 lines (136 loc) · 4.05 KB
/
LoginForm.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
namespace amnah\yii2\user\models\forms;
use Yii;
use yii\base\Model;
/**
* LoginForm is the model behind the login form.
*/
class LoginForm extends Model
{
/**
* @var string Email and/or username
*/
public $email;
/**
* @var string Password
*/
public $password;
/**
* @var bool If true, users will be logged in for $loginDuration
*/
public $rememberMe = true;
/**
* @var \amnah\yii2\user\models\User
*/
protected $user = false;
/**
* @var \amnah\yii2\user\Module
*/
public $module;
/**
* @inheritdoc
*/
public function init()
{
if (!$this->module) {
$this->module = Yii::$app->getModule("user");
}
}
/**
* @return array the validation rules.
*/
public function rules()
{
return [
[["email", "password"], "required"],
["email", "validateUser"],
["password", "validatePassword"],
["rememberMe", "boolean"],
];
}
/**
* Validate user
*/
public function validateUser()
{
// check for valid user or if user registered using social auth
$user = $this->getUser();
if (!$user || !$user->password) {
if ($this->module->loginEmail && $this->module->loginUsername) {
$attribute = "Email / Username";
} else {
$attribute = $this->module->loginEmail ? "Email" : "Username";
}
$this->addError("email", Yii::t("user", "$attribute not found"));
// do we need to check $user->userAuths ???
}
// check if user is banned
if ($user && $user->banned_at) {
$this->addError("email", Yii::t("user", "User is banned - {banReason}", [
"banReason" => $user->banned_reason,
]));
}
// check status and resend email if inactive
if ($user && $user->status == $user::STATUS_INACTIVE) {
/** @var \amnah\yii2\user\models\UserToken $userToken */
$userToken = $this->module->model("UserToken");
$userToken = $userToken::generate($user->id, $userToken::TYPE_EMAIL_ACTIVATE);
$user->sendEmailConfirmation($userToken);
$this->addError("email", Yii::t("user", "Confirmation email resent"));
}
}
/**
* Validate password
*/
public function validatePassword()
{
// skip if there are already errors
if ($this->hasErrors()) {
return;
}
/** @var \amnah\yii2\user\models\User $user */
// check if password is correct
$user = $this->getUser();
if (!$user->validatePassword($this->password)) {
$this->addError("password", Yii::t("user", "Incorrect password"));
}
}
/**
* Get user based on email and/or username
* @return \amnah\yii2\user\models\User|null
*/
public function getUser()
{
// check if we need to get user
if ($this->user === false) {
// build query based on email and/or username login properties
$user = $this->module->model("User");
$user = $user::find();
if ($this->module->loginEmail) {
$user->orWhere(["email" => $this->email]);
}
if ($this->module->loginUsername) {
$user->orWhere(["username" => $this->email]);
}
$this->user = $user->one();
}
return $this->user;
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
// calculate attribute label for "email"
if ($this->module->loginEmail && $this->module->loginUsername) {
$attribute = "Email / Username";
} else {
$attribute = $this->module->loginEmail ? "Email" : "Username";
}
return [
"email" => Yii::t("user", $attribute),
"password" => Yii::t("user", "Password"),
"rememberMe" => Yii::t("user", "Remember Me"),
];
}
}