Fix attempts issue

This commit is contained in:
Abhishek Kumar
2024-06-06 20:05:41 +05:30
parent de9adcb687
commit 96b57f39c3
6 changed files with 92 additions and 68 deletions

View File

@@ -38,22 +38,22 @@ class MaintenanceAccessCore extends ObjectModel
),
);
public function removeFailedAttempts($email, $ipAddress)
{
Db::getInstance()->execute(
'DELETE FROM `' . _DB_PREFIX_ .'maintenance_access` ma
WHERE (ma.`email` = "'.$email.'" OR ma.`ip_address` = "'.$ipAddress.'")'
);
}
public function getFailedAttemptsCount($email, $ipAddress)
{
return Db::getInstance()->getValue(
'SELECT COUNT(ma.`id_maintenance_access`)
FROM `'._DB_PREFIX_.'maintenance_access` ma
WHERE (ma.`email` = "'.$email.'" OR ma.`ip_address` = "'.$ipAddress.'")
AND ma.`date_add` > "'.date('Y-m-d H:i:s', strtotime('-'.MaintenanceAccess::LOGIN_ATTEMPTS_WINDOW.' minutes')).'"'
);
$sql = 'SELECT COUNT(ma.`id_maintenance_access`)
FROM `'._DB_PREFIX_.'maintenance_access` ma
WHERE (ma.`email` = "'.pSQL($email).'" OR ma.`ip_address` = "'.pSQL($ipAddress).'")
AND TIMESTAMPDIFF(
MINUTE,
ma.`date_add`,
(
SELECT MAX(ma.`date_add`)
FROM `'._DB_PREFIX_.'maintenance_access` ma
WHERE (ma.`email` = "'.pSQL($email).'" OR ma.`ip_address` = "'.pSQL($ipAddress).'")
)
) <= '.(int) MaintenanceAccess::LOGIN_ATTEMPTS_WINDOW;
return Db::getInstance()->getValue($sql);
}
public function getLastAttempt($email, $ipAddress)
@@ -61,7 +61,7 @@ class MaintenanceAccessCore extends ObjectModel
return Db::getInstance()->getRow(
'SELECT *
FROM `'._DB_PREFIX_.'maintenance_access` ma
WHERE (ma.`email` = "'.$email.'" OR ma.`ip_address` = "'.$ipAddress.'")
WHERE (ma.`email` = "'.pSQL($email).'" OR ma.`ip_address` = "'.pSQL($ipAddress).'")
ORDER BY ma.`date_add` DESC'
);
}

View File

@@ -814,19 +814,23 @@ class FrontControllerCore extends Controller
protected function processLogin($email, $passwd)
{
$remoteIpAddress = Tools::getRemoteAddr();
$objMaintenanceAccess = new MaintenanceAccess();
$attemptsCount = $objMaintenanceAccess->getFailedAttemptsCount($email, $remoteIpAddress); // does not include the current one
$maxAttempts = Configuration::get('PS_ALLOW_EMP_MAX_ATTEMPTS');
if ($attemptsCount >= $maxAttempts) {
if ($lastAttempt = $objMaintenanceAccess->getLastAttempt($email, $remoteIpAddress)) {
$minutesElapsed = (int) ((time() - strtotime($lastAttempt['date_add'])) / 60);
$minutesLeft = MaintenanceAccess::LOGIN_ATTEMPTS_WINDOW - $minutesElapsed;
if ($maxAttempts) {
$objMaintenanceAccess = new MaintenanceAccess();
$attemptsCount = $objMaintenanceAccess->getFailedAttemptsCount($email, $remoteIpAddress); // does not include the current one
if ($attemptsCount >= $maxAttempts) {
if ($lastAttempt = $objMaintenanceAccess->getLastAttempt($email, $remoteIpAddress)) {
$minutesElapsed = (int) ((time() - strtotime($lastAttempt['date_add'])) / 60);
if ($minutesLeft > 1) {
$this->errors[] = Tools::displayError(sprintf('You have reached the limit of login attempts, please try after %d minutes.', $minutesLeft));
} else {
$this->errors[] = Tools::displayError(sprintf('You have reached the limit of login attempts, please try after 1 minute.', $minutesLeft));
if ($minutesElapsed <= MaintenanceAccess::LOGIN_ATTEMPTS_WINDOW) {
$minutesLeft = MaintenanceAccess::LOGIN_ATTEMPTS_WINDOW - $minutesElapsed;
if ($minutesLeft > 1) {
$this->errors[] = Tools::displayError(sprintf('You have reached the limit of login attempts, please try after %d minutes.', $minutesLeft));
} elseif ($minutesLeft == 1) {
$this->errors[] = Tools::displayError(sprintf('You have reached the limit of login attempts, please try after 1 minute.', $minutesLeft));
}
}
}
}
}
@@ -863,28 +867,28 @@ class FrontControllerCore extends Controller
$cookie->enable_maintenance_view = $this->context->employee->id;
$cookie->remote_addr = ip2long($remoteIpAddress);
$cookie->write();
// Reset attempts count on successful login
$objMaintenanceAccess->removeFailedAttempts($email, $remoteIpAddress);
}
if (count($this->errors)) {
$objMaintenanceAccess = new MaintenanceAccess();
$objMaintenanceAccess->email = $email;
$objMaintenanceAccess->ip_address = $remoteIpAddress;
$objMaintenanceAccess->save();
// Save only if attempts limit is set
if ($maxAttempts) {
$objMaintenanceAccess = new MaintenanceAccess();
$objMaintenanceAccess->email = $email;
$objMaintenanceAccess->ip_address = $remoteIpAddress;
$objMaintenanceAccess->save();
if ($attemptsCount < $maxAttempts) {
$attemptsLeft = $maxAttempts - $attemptsCount - 1;
if ($attemptsCount < $maxAttempts) {
$attemptsLeft = $maxAttempts - $attemptsCount - 1;
if ($attemptsLeft > 0) {
if ($attemptsLeft > 1) {
$this->errors[] = Tools::displayError(sprintf('%d attempts left.', ($maxAttempts - $attemptsCount - 1)));
if ($attemptsLeft > 0) {
if ($attemptsLeft > 1) {
$this->errors[] = Tools::displayError(sprintf('%d attempts left.', ($maxAttempts - $attemptsCount - 1)));
} else {
$this->errors[] = Tools::displayError(sprintf('%d attempt left.', ($maxAttempts - $attemptsCount - 1)));
}
} else {
$this->errors[] = Tools::displayError(sprintf('%d attempt left.', ($maxAttempts - $attemptsCount - 1)));
$this->errors[] = Tools::displayError(sprintf('You have reached the limit of login attempts, please try after %d minutes.', MaintenanceAccess::LOGIN_ATTEMPTS_WINDOW));
}
} else {
$this->errors[] = Tools::displayError(sprintf('You have reached the limit of login attempts, please try after %d minutes.', MaintenanceAccess::LOGIN_ATTEMPTS_WINDOW));
}
}

View File

@@ -62,7 +62,7 @@ class AdminMaintenanceControllerCore extends AdminController
'cast' => 'intval',
'type' => 'text',
'class' => 'fixed-width-xl',
'desc' => sprintf($this->l('Set the number of maximum login attempts allowed in %d minutes.'), MaintenanceAccess::LOGIN_ATTEMPTS_WINDOW),
'desc' => sprintf($this->l('Set the number of maximum login attempts allowed in %d minutes. Set to 0 to disable this feature.'), MaintenanceAccess::LOGIN_ATTEMPTS_WINDOW),
'form_group_class' => ((Tools::getValue('PS_SHOP_ENABLE', Configuration::get('PS_SHOP_ENABLE'))) || !(Tools::getValue('PS_ALLOW_EMP', Configuration::get('PS_ALLOW_EMP')))) ? ' collapse' : '',
),
'PS_MAINTENANCE_IP' => array(
@@ -88,10 +88,10 @@ class AdminMaintenanceControllerCore extends AdminController
// validations
if (!$shopEnable && $allowEmp) {
if ($allowEmpMaxAttempts === '') {
if ($allowEmpMaxAttempts == '') {
$this->errors[] = $this->l('Maximum Login Attempts is a required field.');
} elseif ($allowEmpMaxAttempts === '0' || !Validate::isUnsignedInt($allowEmpMaxAttempts)) {
$this->errors[] = $this->l('Maximum Login Attempts is invalid.');
} elseif (!Validate::isUnsignedInt($allowEmpMaxAttempts)) {
$this->errors[] = $this->l('Maximum Login Attempts is invalid. Please enter a value greater than or equal to 0.');
}
}
@@ -116,6 +116,6 @@ class AdminMaintenanceControllerCore extends AdminController
public function setMedia()
{
parent::setMedia();
$this->addJS(_PS_JS_DIR_.'maintenance.js');
$this->addJS(_PS_JS_DIR_.'/admin/maintenance.js');
}
}

View File

@@ -40,26 +40,4 @@ $(document).ready(function() {
$(document).on('change', '[name="PS_ALLOW_EMP"]', function () {
manageFieldMaximumAttempts(parseInt($('[name="PS_ALLOW_EMP"]:checked').val()));
});
$('.clicker.blue').on('click', function() {
$('.login-form-wrap').toggle(200);
});
$('#cancelLogin').on('click', function() {
$('.login-form-wrap').hide(200);
});
// manage dropdowns
$(document).on('click', function (e) {
const closestDropdown = $(e.target).closest('.dropdown');
if (closestDropdown.length) {
if ($(e.target).closest('.dropdown-toggle').length) {
$('.dropdown').not(closestDropdown).removeClass('open');
closestDropdown.toggleClass('open');
}
} else {
$('.dropdown').removeClass('open');
}
});
});

View File

@@ -0,0 +1,42 @@
/**
* Since 2010 Webkul.
*
* NOTICE OF LICENSE
*
* All right is reserved,
* Please go through this link for complete license : https://store.webkul.com/license.html
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this module to newer
* versions in the future. If you wish to customize this module for your
* needs please refer to https://store.webkul.com/customisation-guidelines/ for more information.
*
* @author Webkul IN <support@webkul.com>
* @copyright Since 2010 Webkul IN
* @license https://store.webkul.com/license.html
*/
$(document).ready(function() {
$('.clicker.blue').on('click', function() {
$('.login-form-wrap').toggle(200);
});
$('#cancelLogin').on('click', function() {
$('.login-form-wrap').hide(200);
});
// manage dropdowns
$(document).on('click', function (e) {
const closestDropdown = $(e.target).closest('.dropdown');
if (closestDropdown.length) {
if ($(e.target).closest('.dropdown-toggle').length) {
$('.dropdown').not(closestDropdown).removeClass('open');
closestDropdown.toggleClass('open');
}
} else {
$('.dropdown').removeClass('open');
}
});
});

View File

@@ -39,7 +39,7 @@
<link rel="shortcut icon" href="{$favicon_url}">
<link href="{$css_dir}maintenance.css" rel="stylesheet">
<script src="{$base_dir}js/jquery/jquery-1.11.0.min.js"></script>
<script src="{$base_dir}js/maintenance.js"></script>
<script src="{$js_dir}maintenance.js"></script>
<link href='//fonts.googleapis.com/css?family=Open+Sans:600' rel='stylesheet'>
</head>