Newer
Older
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
<?php
/**
* Copyright Maarch since 2008 under licence GPLv3.
* See LICENCE.txt file at the root folder for more details.
* This file is part of Maarch software.
*/
/**
* @brief Authentication Controller
*
* @author dev@maarch.org
*/
namespace SrcCore\controllers;
use SrcCore\models\AuthenticationModel;
use SrcCore\models\PasswordModel;
use SrcCore\models\ValidatorModel;
use User\models\UserModel;
class AuthenticationController
{
public static function handleFailedAuthentication(array $aArgs)
{
ValidatorModel::notEmpty($aArgs, ['userId']);
ValidatorModel::stringType($aArgs, ['userId']);
$passwordRules = PasswordModel::getEnabledRules();
if (!empty($passwordRules['lockAttempts'])) {
$user = UserModel::getByUserId(['select' => ['failed_authentication', 'locked_until'], 'userId' => $aArgs['userId']]);
if (!empty($user)) {
if (!empty($user['locked_until'])) {
$lockedDate = new \DateTime($user['locked_until']);
$currentDate = new \DateTime();
if ($currentDate > $lockedDate) {
AuthenticationModel::resetFailedAuthentication(['userId' => $aArgs['userId']]);
$user['failed_authentication'] = 0;
} else {
return "Nombre de tentatives de connexion dépassée. Compte vérouillé jusqu'au {$lockedDate->format('d/m/Y H:i')}";
}
}
AuthenticationModel::increaseFailedAuthentication(['userId' => $aArgs['userId'], 'tentatives' => $user['failed_authentication'] + 1]);
if (!empty($user['failed_authentication']) && ($user['failed_authentication'] + 1) >= $passwordRules['lockAttempts'] && !empty($passwordRules['lockTime'])) {
$lockedUntil = time() + 60 * $passwordRules['lockTime'];
AuthenticationModel::lockUser(['userId' => $aArgs['userId'], 'lockedUntil' => $lockedUntil]);
return "Nombre de tentatives de connexion dépassée. Votre compte est vérouillé pendant {$passwordRules['lockTime']} minutes.";
}
}
}
return _BAD_LOGIN_OR_PSW;
}
}