Skip to content
Snippets Groups Projects
AuthenticationController.php 2.22 KiB
Newer Older
  • Learn to ignore specific revisions
  • Damien's avatar
    Damien committed
    <?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;
        }
    }