-
Notifications
You must be signed in to change notification settings - Fork 452
/
Copy pathValidation.php
587 lines (510 loc) · 18.8 KB
/
Validation.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
<?php
/**
* @file classes/security/Validation.php
*
* Copyright (c) 2014-2021 Simon Fraser University
* Copyright (c) 2003-2021 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class Validation
*
* @ingroup security
*
* @brief Class providing user validation/authentication operations.
*/
namespace PKP\security;
use APP\core\Application;
use APP\facades\Repo;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;
use PKP\config\Config;
use PKP\core\Core;
use PKP\db\DAORegistry;
use PKP\site\Site;
use PKP\site\SiteDAO;
use PKP\user\User;
use PKP\userGroup\UserGroup;
use PKP\security\Role;
use PKP\core\PKPApplication;
class Validation
{
public const ADMINISTRATION_PROHIBITED = 0;
public const ADMINISTRATION_PARTIAL = 1;
public const ADMINISTRATION_FULL = 2;
/**
* Authenticate user credentials and mark the user as logged in in the current session.
*
* @param string $username
* @param string $password unencrypted password
* @param string $reason reference to string to receive the reason an account was disabled; null otherwise
* @param bool $remember remember a user's session past the current browser session
*
* @return ?User the User associated with the login credentials, or false if the credentials are invalid
*/
public static function login($username, $password, &$reason, $remember = false)
{
$reason = null;
return Auth::attempt(['username' => $username, 'password' => $password], $remember)
? static::registerUserSession(Auth::user(), $reason)
: false;
}
/**
* Verify if the input password is correct
*
* @param string $username the string username
* @param string $password the plaintext password
* @param string $hash the password hash from the database
* @param string &$rehash if password needs rehash, this variable is used
*
* @return bool
*/
public static function verifyPassword($username, $password, $hash, &$rehash)
{
if (password_needs_rehash($hash, PASSWORD_BCRYPT)) {
// update to new hashing algorithm
$oldHash = self::encryptCredentials($username, $password, false, true);
if ($oldHash === $hash) {
// update hash
$rehash = self::encryptCredentials($username, $password);
return true;
}
}
return password_verify($password, $hash);
}
/**
* Mark the user as logged in in the current session.
*
* @param User $user user to register in the session
* @param string $reason reference to string to receive the reason an account
* was disabled; null otherwise
*
* @return mixed User or boolean the User associated with the login credentials,
* or false if the credentials are invalid
*/
public static function registerUserSession($user, &$reason)
{
if (!$user instanceof User) {
return false;
}
if ($user->getDisabled()) { // The user has been disabled.
$reason = $user->getDisabledReason();
if ($reason === null) {
$reason = '';
}
return false;
}
$request = Application::get()->getRequest();
$request->getSessionGuard()->setUserDataToSession($user)->updateSession($user->getId());
$user->setDateLastLogin(Core::getCurrentDate());
Repo::user()->edit($user);
return $user;
}
/**
* Mark the user as logged out in the current session.
*
* @return bool
*/
public static function logout()
{
$request = Application::get()->getRequest();
$session = $request->getSession();
$user = Auth::user(); /** @var \PKP\user\User $user */
Auth::logout();
$session->invalidate();
$session->regenerateToken();
$session->put('username', $user->getUsername());
$session->put('email', $user->getEmail());
$request->getSessionGuard()->updateSession(null);
return true;
}
/**
* Redirect to the login page, appending the current URL as the source.
*
* @param string $message Optional name of locale key to add to login page
*/
public static function redirectLogin($message = null)
{
$args = [];
if (isset($_SERVER['REQUEST_URI'])) {
$args['source'] = $_SERVER['REQUEST_URI'];
}
if ($message !== null) {
$args['loginMessage'] = $message;
}
$request = Application::get()->getRequest();
$request->redirect(null, 'login', null, null, $args);
}
/**
* Check if a user's credentials are valid.
*
* @param string $username username
* @param string $password unencrypted password
*
* @return bool
*/
public static function checkCredentials($username, $password)
{
$user = Repo::user()->getByUsername($username, false);
if (!$user) {
return false;
}
// Validate against user database
$rehash = null;
if (!self::verifyPassword($username, $password, $user->getPassword(), $rehash)) {
return false;
}
if (!empty($rehash)) {
// update to new hashing algorithm
$user->setPassword($rehash);
// save new password hash to database
Repo::user()->edit($user);
}
return true;
}
/**
* Check if a user is authorized to access the specified role in the specified context.
*
* @param int $roleId
* @param int $contextId optional (e.g., for global site admin role), the ID of the context
*
* @return bool
*/
public static function isAuthorized($roleId, ?int $contextId = Application::SITE_CONTEXT_ID)
{
if (!self::isLoggedIn()) {
return false;
}
$user = Auth::user(); /** @var \PKP\user\User $user */
$roleDao = DAORegistry::getDAO('RoleDAO'); /** @var RoleDAO $roleDao */
return $roleDao->userHasRole($contextId, $user->getId(), $roleId);
}
/**
* Encrypt user passwords for database storage.
* The username is used as a unique salt to make dictionary
* attacks against a compromised database more difficult.
*
* @param string $username username (kept for backwards compatibility)
* @param string $password unencrypted password
* @param string $encryption optional encryption algorithm to use, defaulting to the value from the site configuration
* @param bool $legacy if true, use legacy hashing technique for backwards compatibility
*
* @return string encrypted password
*/
public static function encryptCredentials($username, $password, $encryption = false, $legacy = false)
{
if ($legacy) {
$valueToEncrypt = $username . $password;
if ($encryption == false) {
$encryption = Config::getVar('security', 'encryption');
}
switch ($encryption) {
case 'sha1':
if (function_exists('sha1')) {
return sha1($valueToEncrypt);
}
// no break
case 'md5':
default:
return md5($valueToEncrypt);
}
} else {
return password_hash($password, PASSWORD_BCRYPT);
}
}
/**
* Generate a random password.
* Assumes the random number generator has already been seeded.
*
* @param int $length the length of the password to generate (default is site minimum)
*
* @return string
*/
public static function generatePassword($length = null)
{
if (!$length) {
$siteDao = DAORegistry::getDAO('SiteDAO'); /** @var SiteDAO $siteDao */
$site = $siteDao->getSite(); /** @var Site $site */
$length = $site->getMinPasswordLength();
}
$letters = 'abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ';
$numbers = '23456789';
$password = '';
for ($i = 0; $i < $length; $i++) {
$password .= random_int(1, 4) == 4 ? $numbers[random_int(0, strlen($numbers) - 1)] : $letters[random_int(0, strlen($letters) - 1)];
}
return $password;
}
/**
* Generate a hash value to use for confirmation to reset a password.
*
* @param int $userId
* @param int $expiry timestamp when hash expires, defaults to CURRENT_TIME + RESET_SECONDS
*
* @return string (boolean false if user is invalid)
*/
public static function generatePasswordResetHash($userId, $expiry = null)
{
if (($user = Repo::user()->get($userId)) == null) {
// No such user
return false;
}
// create hash payload
$salt = Config::getVar('security', 'salt');
if (empty($expiry)) {
$expires = (int) Config::getVar('security', 'reset_seconds', 7200);
$expiry = time() + $expires;
}
// use last login time to ensure the hash changes when they log in
$data = $user->getUsername() . $user->getPassword() . $user->getDateLastLogin() . $expiry;
// generate hash and append expiry timestamp
$algos = hash_algos();
foreach (['sha256', 'sha1', 'md5'] as $algo) {
if (in_array($algo, $algos)) {
return hash_hmac($algo, $data, $salt) . ':' . $expiry;
}
}
// fallback to MD5
return md5($data . $salt) . ':' . $expiry;
}
/**
* Check if provided password reset hash is valid.
*
* @param int $userId
* @param string $hash
*
* @return bool
*/
public static function verifyPasswordResetHash($userId, $hash)
{
// append ":" to ensure the explode results in at least 2 elements
[, $expiry] = explode(':', $hash . ':');
if (empty($expiry) || ((int) $expiry < time())) {
// expired
return false;
}
return ($hash === self::generatePasswordResetHash($userId, $expiry));
}
/**
* Suggest a username given the first and last names.
*
* @param string $givenName
* @param string $familyName
*
* @return string
*/
public static function suggestUsername($givenName, $familyName = null)
{
$name = $givenName;
if (!empty($familyName)) {
$initial = Str::substr($givenName, 0, 1);
$name = $initial . $familyName;
}
$suggestion = Str::of($name)->ascii()->lower()->replaceMatches('/[^a-zA-Z0-9_-]/', '');
for ($i = ''; Repo::user()->getByUsername($suggestion . $i, true); $i++);
return $suggestion . $i;
}
/**
* Check if the user is logged in.
*/
public static function isLoggedIn(): bool
{
return (bool) Application::get()->getRequest()->getSessionGuard()->getUserId();
}
/**
* Check if the user is logged in as a different user. Returns the original user ID or null
*/
public static function loggedInAs(): ?int
{
return Application::get()->getRequest()->getSession()->get('signedInAs') ?: null;
}
/**
* Check if the user is logged in as a different user.
*
*
* @deprecated 3.4
*/
public static function isLoggedInAs(): bool
{
return (bool) static::loggedInAs();
}
/**
* Shortcut for checking authorization as site admin.
*
* @return bool
*/
public static function isSiteAdmin()
{
return self::isAuthorized(Role::ROLE_ID_SITE_ADMIN);
}
/**
* Check whether a user is allowed to administer another user.
*
* @param int $administeredUserId User ID of user to potentially administer
* @param int $administratorUserId User ID of user who wants to do the administrating
*
* @return bool True IFF the administration operation is permitted
*
* @deprecated 3.4 Use the method getAdministrationLevel and checked against the ADMINISTRATION_* constants
*/
public static function canAdminister($administeredUserId, $administratorUserId)
{
// You can administer yourself
if ($administeredUserId == $administratorUserId) {
return true;
}
$siteContextId = \PKP\core\PKPApplication::SITE_CONTEXT_ID;
// check if administered user is site admin
$isAdministeredUserSiteAdmin = UserGroup::query()
->withContextIds($siteContextId)
->withRoleIds(Role::ROLE_ID_SITE_ADMIN)
->whereHas('userUserGroups', function ($query) use ($administeredUserId) {
$query->withUserId($administeredUserId)
->withActive();
})
->exists();
if ($isAdministeredUserSiteAdmin) {
return false;
}
// check if administrator user is site admin
$isAdministratorUserSiteAdmin = UserGroup::query()
->withContextIds($siteContextId)
->withRoleIds(Role::ROLE_ID_SITE_ADMIN)
->whereHas('userUserGroups', function ($query) use ($administratorUserId) {
$query->withUserId($administratorUserId)
->withActive();
})
->exists();
if ($isAdministratorUserSiteAdmin) {
return true;
}
// Get contexts where administered user has roles
$administeredUserContexts = UserGroup::query()
->whereHas('userUserGroups', function ($query) use ($administeredUserId) {
$query->withUserId($administeredUserId)
->withActive();
})
->get()
->map(fn ($userGroup) => $userGroup->contextId)
->unique()
->values()
->toArray();
// get contexts where administrator user has manager role
$administratorManagerContexts = UserGroup::query()
->withRoleIds(Role::ROLE_ID_MANAGER)
->whereHas('userUserGroups', function ($query) use ($administratorUserId) {
$query->withUserId($administratorUserId)
->withActive();
})
->get()
->map(fn ($userGroup) => $userGroup->contextId)
->unique()
->values()
->toArray();
// check for conflicting contexts
$conflictingContexts = array_diff($administeredUserContexts, $administratorManagerContexts);
if (!empty($conflictingContexts)) {
// found conflicting contexts: disqualified
return false;
}
// Make sure the administering user has a manager role somewhere
if (empty($administratorManagerContexts)) {
return false;
}
// There were no conflicting roles. Permit administration.
return true;
}
/**
* Get the user's administration level
*
* @param int $administeredUserId User ID of user to potentially administer
* @param int $administratorUserId User ID of user who wants to do the administrating
* @param int $contextId The journal/context Id
*
* @return int The authorized administration level
*/
public static function getAdministrationLevel(
int $administeredUserId,
int $administratorUserId,
?int $contextId = null
): int {
if ($administeredUserId === $administratorUserId) {
return self::ADMINISTRATION_FULL;
}
$siteContextId = PKPApplication::SITE_CONTEXT_ID;
// single query to fetch user groups assigned to either user
$allUserGroups = UserGroup::query()
->whereHas('userUserGroups', fn($q) =>
$q->withActive()->withUserIds([$administratorUserId, $administeredUserId])
)
->with(['userUserGroups' => fn($q) =>
$q->withActive()->withUserIds([$administratorUserId, $administeredUserId])
])
->get();
$administratorMap = [];
$administeredMap = [];
foreach ($allUserGroups as $userGroup) {
$roleId = $userGroup->roleId;
$userGroupContextId = $userGroup->contextId ?? PKPApplication::SITE_CONTEXT_ID;
// then each user assignment row
foreach ($userGroup->userUserGroups as $uug) {
if ($uug->userId === $administratorUserId) {
$administratorMap[$userGroupContextId][] = $roleId;
} elseif ($uug->userId === $administeredUserId) {
$administeredMap[$userGroupContextId][] = $roleId;
}
}
}
if (
isset($administeredMap[$siteContextId]) &&
in_array(Role::ROLE_ID_SITE_ADMIN, $administeredMap[$siteContextId], true)
) {
return self::ADMINISTRATION_PROHIBITED;
}
// if administrator user is site admin => FULL
if (
isset($administratorMap[$siteContextId]) &&
in_array(Role::ROLE_ID_SITE_ADMIN, $administratorMap[$siteContextId], true)
) {
return self::ADMINISTRATION_FULL;
}
// gather manager contexts for the administrator
$administratorManagerContexts = [];
foreach ($administratorMap as $ctx => $roles) {
if (in_array(Role::ROLE_ID_MANAGER, $roles, true)) {
$administratorManagerContexts[] = $ctx;
}
}
if (empty($administratorManagerContexts)) {
return self::ADMINISTRATION_PROHIBITED;
}
$administeredUserContexts = array_keys($administeredMap);
$conflictingContexts = array_diff($administeredUserContexts, $administratorManagerContexts);
if (!empty($conflictingContexts)) {
if ($contextId !== null && in_array($contextId, $administratorManagerContexts, true)) {
return self::ADMINISTRATION_PARTIAL;
}
return self::ADMINISTRATION_PROHIBITED;
}
return self::ADMINISTRATION_FULL;
}
/**
* Determine if the current user can "Log In As" the target user.
*
* By default, we do a cross-journal check (contextId = null)
* to enforce "Log In As" only in a single journal context, pass $contextId.
*/
public static function canUserLoginAs(
int $targetUserId,
int $currentUserId,
?int $contextId = null
): bool {
// prevent self-login
if ($targetUserId === $currentUserId) {
return false;
}
return self::getAdministrationLevel($targetUserId, $currentUserId, $contextId) === self::ADMINISTRATION_FULL;
}
}
if (!PKP_STRICT_MODE) {
class_alias('\PKP\security\Validation', '\Validation');
}