diff --git a/classes/MaintenanceAccess.php b/classes/MaintenanceAccess.php index 7db5cee1c..8760ac16a 100644 --- a/classes/MaintenanceAccess.php +++ b/classes/MaintenanceAccess.php @@ -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' ); } diff --git a/classes/controller/FrontController.php b/classes/controller/FrontController.php index 4ad395c2f..47b0eb0b9 100644 --- a/classes/controller/FrontController.php +++ b/classes/controller/FrontController.php @@ -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)); } } diff --git a/controllers/admin/AdminMaintenanceController.php b/controllers/admin/AdminMaintenanceController.php index c06165c30..9e33f84f6 100644 --- a/controllers/admin/AdminMaintenanceController.php +++ b/controllers/admin/AdminMaintenanceController.php @@ -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'); } } diff --git a/js/maintenance.js b/js/admin/maintenance.js similarity index 69% rename from js/maintenance.js rename to js/admin/maintenance.js index 43611d9d3..dadb576fd 100644 --- a/js/maintenance.js +++ b/js/admin/maintenance.js @@ -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'); - } - }); }); diff --git a/themes/hotel-reservation-theme/js/maintenance.js b/themes/hotel-reservation-theme/js/maintenance.js new file mode 100644 index 000000000..ed1690c98 --- /dev/null +++ b/themes/hotel-reservation-theme/js/maintenance.js @@ -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 +* @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'); + } + }); +}); diff --git a/themes/hotel-reservation-theme/maintenance.tpl b/themes/hotel-reservation-theme/maintenance.tpl index 3fd9d2a5f..b2293fc88 100644 --- a/themes/hotel-reservation-theme/maintenance.tpl +++ b/themes/hotel-reservation-theme/maintenance.tpl @@ -39,7 +39,7 @@ - +