-
Notifications
You must be signed in to change notification settings - Fork 691
Expand file tree
/
Copy pathUser.php
More file actions
177 lines (165 loc) Β· 7.28 KB
/
User.php
File metadata and controls
177 lines (165 loc) Β· 7.28 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\helpers;
use Craft;
use craft\elements\User as UserElement;
/**
* Class User
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 3.1.7
*/
class User
{
/**
* @param UserElement $user
* @return string|null
*/
public static function getAuthStatus(UserElement $user): ?string
{
switch ($user->getStatus()) {
case UserElement::STATUS_INACTIVE:
case UserElement::STATUS_ARCHIVED:
return UserElement::AUTH_INVALID_CREDENTIALS;
case UserElement::STATUS_PENDING:
return UserElement::AUTH_PENDING_VERIFICATION;
case UserElement::STATUS_SUSPENDED:
return UserElement::AUTH_ACCOUNT_SUSPENDED;
case UserElement::STATUS_ACTIVE:
if ($user->locked) {
// Let them know how much time they have to wait (if any) before their account is unlocked.
if (Craft::$app->getConfig()->getGeneral()->cooldownDuration) {
return UserElement::AUTH_ACCOUNT_COOLDOWN;
}
return UserElement::AUTH_ACCOUNT_LOCKED;
}
// Is a password reset required?
if ($user->passwordResetRequired) {
return UserElement::AUTH_PASSWORD_RESET_REQUIRED;
}
$request = Craft::$app->getRequest();
if (!$request->getIsConsoleRequest()) {
if ($request->getIsCpRequest()) {
if (!$user->can('accessCp')) {
return UserElement::AUTH_NO_CP_ACCESS;
}
if (
Craft::$app->getIsLive() === false &&
$user->can('accessCpWhenSystemIsOff') === false
) {
return UserElement::AUTH_NO_CP_OFFLINE_ACCESS;
}
} elseif (
Craft::$app->getIsLive() === false &&
$user->can('accessSiteWhenSystemIsOff') === false
) {
return UserElement::AUTH_NO_SITE_OFFLINE_ACCESS;
}
}
}
return null;
}
/**
* @param UserElement|null $user
* @return string|null
*/
public static function getAuthFailureMessage(?UserElement $user): ?string
{
switch ($user->authError ?? "") {
case UserElement::AUTH_PENDING_VERIFICATION:
return Craft::t('app', 'Account has not been activated.');
case UserElement::AUTH_ACCOUNT_LOCKED:
return Craft::t('app', 'Account locked.');
case UserElement::AUTH_ACCOUNT_COOLDOWN:
$timeRemaining = $user?->getRemainingCooldownTime();
if ($timeRemaining) {
return Craft::t('app', 'Account locked. Try again in {time}.', ['time' => DateTimeHelper::humanDuration($timeRemaining)]);
}
return Craft::t('app', 'Account locked.');
case UserElement::AUTH_PASSWORD_RESET_REQUIRED:
return Craft::t('app', 'You need to reset your password.');
case UserElement::AUTH_ACCOUNT_SUSPENDED:
return Craft::t('app', 'Account suspended.');
case UserElement::AUTH_NO_CP_ACCESS:
return Craft::t('app', 'You cannot access the control panel with that account.');
case UserElement::AUTH_NO_CP_OFFLINE_ACCESS:
return Craft::t('app', 'You cannot access the control panel while the system is offline with that account.');
case UserElement::AUTH_NO_SITE_OFFLINE_ACCESS:
return Craft::t('app', 'You cannot access the site while the system is offline with that account.');
}
return null;
}
/**
* @param string|null $authError
* @param UserElement|null $user
* @return array{0:string,1:string}
* @since 5.8.10
*/
public static function getLoginFailureInfo(?string $authError, ?UserElement $user): array
{
// if preventUserEnumeration is true and the account is locked
// set the $authError to a value that will trigger the generic, default message
if (
Craft::$app->getConfig()->getGeneral()->preventUserEnumeration &&
in_array($authError, [UserElement::AUTH_ACCOUNT_LOCKED, UserElement::AUTH_ACCOUNT_COOLDOWN])
) {
$authError = UserElement::AUTH_INVALID_CREDENTIALS;
}
return [$authError, static::getLoginFailureMessage($authError, $user)];
}
/**
* @param string|null $authError
* @param UserElement|null $user
* @return string
* @deprecated in 5.8.10
*/
public static function getLoginFailureMessage(?string $authError, ?UserElement $user): string
{
switch ($authError) {
case UserElement::AUTH_PENDING_VERIFICATION:
$message = Craft::t('app', 'Account has not been activated.');
break;
case UserElement::AUTH_ACCOUNT_LOCKED:
$message = Craft::t('app', 'Account locked.');
break;
case UserElement::AUTH_ACCOUNT_COOLDOWN:
$timeRemaining = $user?->getRemainingCooldownTime();
if ($timeRemaining) {
$message = Craft::t('app', 'Account locked. Try again in {time}.', ['time' => DateTimeHelper::humanDuration($timeRemaining)]);
} else {
$message = Craft::t('app', 'Account locked.');
}
break;
case UserElement::AUTH_PASSWORD_RESET_REQUIRED:
if (Craft::$app->getUsers()->sendPasswordResetEmail($user)) {
$message = Craft::t('app', 'You need to reset your password. Check your email for instructions.');
} else {
$message = Craft::t('app', 'You need to reset your password, but an error was encountered when sending the password reset email.');
}
break;
case UserElement::AUTH_ACCOUNT_SUSPENDED:
$message = Craft::t('app', 'Account suspended.');
break;
case UserElement::AUTH_NO_CP_ACCESS:
$message = Craft::t('app', 'You cannot access the control panel with that account.');
break;
case UserElement::AUTH_NO_CP_OFFLINE_ACCESS:
$message = Craft::t('app', 'You cannot access the control panel while the system is offline with that account.');
break;
case UserElement::AUTH_NO_SITE_OFFLINE_ACCESS:
$message = Craft::t('app', 'You cannot access the site while the system is offline with that account.');
break;
default:
if (Craft::$app->getConfig()->getGeneral()->useEmailAsUsername) {
$message = Craft::t('app', 'Invalid email or password.');
} else {
$message = Craft::t('app', 'Invalid username or password.');
}
}
return $message;
}
}