Hi!
I'd like to share a small code tweak to control when specific employees can log in to your back office based on the day and time. This can be especially useful if you wish to limit access for particular profiles during non-working hours.
Code Overview:
- Extending Employee Class: We extend the core Employee.php class without modifying the core file.
- Password Check Alteration: We override the checkPassword function to incorporate our logic. If an employee with a specific profile (e.g., id_profile = 5) attempts to log in outside the allowed hours, they're denied access, and the attempt is logged.
- Timezone Handling: Before checking login timings, we set the timezone to the shop's timezone.
- Defining Working Hours: We've defined working hours for each day. By default, weekdays (Mon-Fri) are from 10 am to 7 pm, Saturdays from 12 pm to 6 pm, and Sundays are restricted completely.
<?php if (!defined('_PS_VERSION_')) { exit; } class Employee extends EmployeeCore { public static function checkPassword($id_employee, $passwd, $id_profile = null) { $restrictedProfiles = [5, 13]; // Adjust the profile IDs as needed if (in_array($id_profile, $restrictedProfiles) && !$this->canEmployeeLogin()) { // Log the failed login attempt for administrative purposes PrestaShopLogger::addLog("Employee with ID {$id_employee} attempted to log in outside the working hours.", 1, null, 'Employee', $id_employee); return false; } return parent::checkPassword($id_employee, $passwd, $id_profile); } private function canEmployeeLogin() { $originalTimezone = date_default_timezone_get(); $shopTimezone = Configuration::get('PS_TIMEZONE'); if (!in_array($shopTimezone, timezone_identifiers_list())) { PrestaShopLogger::addLog("Invalid timezone set in PrestaShop configuration: {$shopTimezone}.", 3, null, 'Configuration', null); return false; } date_default_timezone_set($shopTimezone); $currentHour = date('G'); $currentDay = date('N'); $canLogin = $this->isLoginTime($currentDay, $currentHour); date_default_timezone_set($originalTimezone); return $canLogin; } private function isLoginTime($currentDay, $currentHour) { $defaultWeekdayHours = ['start' => 9, 'end' => 21]; $loginHours = [ 1 => $defaultWeekdayHours, // Monday 2 => $defaultWeekdayHours, // Tuesday 3 => $defaultWeekdayHours, // Wednesday 4 => $defaultWeekdayHours, // Thursday 5 => $defaultWeekdayHours, // Friday 6 => ['start' => 10, 'end' => 20], // Saturday 7 => ['start' => 0, 'end' => 0], // Sunday ]; return $currentHour >= $loginHours[$currentDay]['start'] && $currentHour < $loginHours[$currentDay]['end']; } }
Adapting for Your Needs:
- Employee Profile ID: Change the $id_profile === 5 condition if you want to apply restrictions for other profiles.
- Modify Working Hours: Adjust the $loginHours array to define your working hours per day.
$loginHours = [ 1 => ['start' => 10, 'end' => 19], // Monday // ... add or modify days as needed ];
Note: Ensure that the timezone set in your Prestashop configuration is valid. Invalid timezones will be logged and restrict access.
Feedback and suggestions are always welcome! 🚀