merge function login

This commit is contained in:
Fred Tempez 2020-07-17 17:27:34 +02:00
parent 0d7b674e0f
commit 83d9980486
1 changed files with 76 additions and 69 deletions

View File

@ -363,31 +363,55 @@ class user extends common {
if($this->isPost()) { if($this->isPost()) {
$userId = $this->getInput('userLoginId', helper::FILTER_ID, true); $userId = $this->getInput('userLoginId', helper::FILTER_ID, true);
// le userId n'existe pas, créer ou mettre à jour une entrée dans la liste noire /**
* Aucun compte existant
*/
if ( !$this->getData(['user', $userId])) { if ( !$this->getData(['user', $userId])) {
//Stockage de l'IP //Stockage de l'IP
$this->setData([ $this->setData([
'blacklist', 'blacklist',
$userId, $userId,
[ [
'connectFail' => $this->getData(['blacklist',$userId,'connectFail']) ? $this->getData(['blacklist',$userId,'connectFail']) + 1 : 1, 'connectFail' => $this->getData(['blacklist',$userId,'connectFail']) + 1,
'lastFail' => time(), 'lastFail' => time(),
'ip' => $_SERVER['REMOTE_ADDR'] 'ip' => helper::getIp()
] ]
]); ]);
$notification = 'Identifiant ou mot de passe incorrect'; // Verrouillage des IP
$ipBlackList = helper::arrayCollumn($this->getData(['blacklist']), 'ip');
if ( $this->getData(['blacklist',$userId,'connectFail']) >= $this->getData(['config', 'connect', 'attempt'])
AND in_array($this->getData(['blacklist',$userId,'ip']),$ipBlackList) ) {
// Valeurs en sortie
$this->addOutput([
'notification' => 'Trop de tentatives, compte verrouillé',
'redirect' => helper::baseUrl(),
'state' => false
]);
} else {
// Valeurs en sortie
$this->addOutput([
'notification' => 'Identifiant ou mot de passe incorrect'
]);
} }
/** /**
* Compte valide : * Le compte existe
* Mot de passe
* Groupe
*/ */
} else {
// Cas 4 : le délai de blocage est dépassé et le compte est au max - Réinitialiser
if ($this->getData(['user',$userId,'connectTimeout']) + $this->getData(['config', 'connect', 'timeout']) < time() if ($this->getData(['user',$userId,'connectTimeout']) + $this->getData(['config', 'connect', 'timeout']) < time()
AND $this->getData(['user',$userId,'connectFail']) === $this->getData(['config', 'connect', 'attempt']) ) {
$this->setData(['user',$userId,'connectFail',0 ]);
$this->setData(['user',$userId,'connectTimeout',0 ]);
}
// Check la présence des variables et contrôle du blocage du compte si valeurs dépassées
// Vérification du mot de passe et du groupe
if (
( $this->getData(['user',$userId,'connectTimeout']) + $this->getData(['config', 'connect', 'timeout']) ) < time()
AND $this->getData(['user',$userId,'connectFail']) < $this->getData(['config', 'connect', 'attempt']) AND $this->getData(['user',$userId,'connectFail']) < $this->getData(['config', 'connect', 'attempt'])
AND password_verify($this->getInput('userLoginPassword', helper::FILTER_STRING_SHORT, true), $this->getData(['user', $userId, 'password'])) AND password_verify($this->getInput('userLoginPassword', helper::FILTER_STRING_SHORT, true), $this->getData(['user', $userId, 'password']))
AND $this->getData(['user', $userId, 'group']) >= self::GROUP_MEMBER AND $this->getData(['user', $userId, 'group']) >= self::GROUP_MEMBER
) { ) {
// Expiration
$expire = $this->getInput('userLoginLongTime') ? strtotime("+1 year") : 0; $expire = $this->getInput('userLoginLongTime') ? strtotime("+1 year") : 0;
setcookie('ZWII_USER_ID', $userId, $expire, helper::baseUrl(false, false), '', helper::isHttps(), true); setcookie('ZWII_USER_ID', $userId, $expire, helper::baseUrl(false, false), '', helper::isHttps(), true);
setcookie('ZWII_USER_PASSWORD', $this->getData(['user', $userId, 'password']), $expire, helper::baseUrl(false, false), '', helper::isHttps(), true); setcookie('ZWII_USER_PASSWORD', $this->getData(['user', $userId, 'password']), $expire, helper::baseUrl(false, false), '', helper::isHttps(), true);
@ -398,21 +422,12 @@ class user extends common {
$this->getData(['config', 'maintenance']) $this->getData(['config', 'maintenance'])
AND $this->getData(['user', $userId, 'group']) < self::GROUP_ADMIN AND $this->getData(['user', $userId, 'group']) < self::GROUP_ADMIN
) { ) {
// Verrouillage des IP
$ipBlackList = helper::arrayCollumn($this->getData(['blacklist']), 'ip');
if ( $this->getData(['blacklist',$userId,'connectFail']) >= $this->getData(['config', 'connect', 'attempt'])
AND in_array($this->getData(['blacklist',$userId,'ip']),$ipBlackList) ) {
// Valeurs en sortie
$this->addOutput([ $this->addOutput([
'notification' => 'Seul un administrateur peut se connecter lors d\'une maintenance', 'notification' => 'Seul un administrateur peut se connecter lors d\'une maintenance',
'redirect' => helper::baseUrl(), 'redirect' => helper::baseUrl(),
'state' => false 'state' => false
]); ]);
} } else {
else {
// RAZ compteur échec connexion
$this->setData(['user',$userId,'connectFail',0 ]);
$this->setData(['user',$userId,'connectTimeout',0 ]);
// Valeurs en sortie // Valeurs en sortie
$this->addOutput([ $this->addOutput([
'notification' => 'Connexion réussie', 'notification' => 'Connexion réussie',
@ -420,33 +435,24 @@ class user extends common {
'state' => true 'state' => true
]); ]);
} }
}
// Sinon notification d'échec // Sinon notification d'échec
else {
// L'utilisateur existe : incrémenter le compteur d'échec de connexion
if ( is_array($this->getdata(['user',$userId]))
) {
$this->setData(['user',$userId,'connectFail',$this->getdata(['user',$userId,'connectFail']) + 1 ]);
// Mettre à jour le timer et notifier
if ( $this->getdata(['user',$userId,'connectFail']) >= $this->getData(['config', 'connect', 'attempt'])
) {
$notification = 'Trop de tentatives, accès bloqué durant ' . ($this->getData(['config', 'connect', 'timeout']) / 60) . ' minutes.';
// Incrémenter le timer
if ($this->getData(['user',$userId,'connectTimeout']) + $this->getData(['config', 'connect', 'timeout']) < time() ) {
$this->setData(['user',$userId,'connectTimeout', time()]);
}
} else { } else {
$notification = 'Identifiant ou mot de passe incorrect'; $notification = 'Identifiant ou mot de passe incorrect';
// Cas 1 le nombre de connexions est inférieur aux tentatives autorisées : incrément compteur d'échec
if ($this->getData(['user',$userId,'connectFail']) < $this->getData(['config', 'connect', 'attempt'])) {
$this->setData(['user',$userId,'connectFail',$this->getdata(['user',$userId,'connectFail']) + 1 ]);
} }
// L'utilisateur n'existe pas // Cas 2 la limite du nombre de connexion est atteinte : placer le timer
// Bloquer l'IP après les tentatives autorisées avec ce compte, if ( $this->getdata(['user',$userId,'connectFail']) == $this->getData(['config', 'connect', 'attempt']) ) {
} elseif ( $this->setData(['user',$userId,'connectTimeout', time()]);
$this->getData(['blacklist',$userId,'connectFail']) >= $this->getData(['config', 'connect', 'attempt']) }
) { // Cas 3 le délai de bloquage court
$notification = 'Trop de tentatives, compte verrouillé'; if ($this->getData(['user',$userId,'connectTimeout']) + $this->getData(['config', 'connect', 'timeout']) > time() ) {
$notification = 'Trop de tentatives, accès bloqué durant ' . ($this->getData(['config', 'connect', 'timeout']) / 60) . ' minutes.';
} }
// Journalisation // Journalisation
$dataLog = strftime('%d/%m/%y',time()) . ';' . strftime('%R',time()) . ';' ; $dataLog = strftime('%d/%m/%y',time()) . ';' . strftime('%R',time()) . ';' ;
$dataLog .= helper::getIp() . ';';
$dataLog .= $userId . ';' ; $dataLog .= $userId . ';' ;
$dataLog .= $this->getUrl() .';' ; $dataLog .= $this->getUrl() .';' ;
$dataLog .= 'échec de connexion' ; $dataLog .= 'échec de connexion' ;
@ -460,6 +466,7 @@ class user extends common {
]); ]);
} }
} }
}
// Valeurs en sortie // Valeurs en sortie
$this->addOutput([ $this->addOutput([
'display' => self::DISPLAY_LAYOUT_LIGHT, 'display' => self::DISPLAY_LAYOUT_LIGHT,