Merge branch 'master' into plugin-system-fresh

This commit is contained in:
objecttothis authored and GitHub committed 2026-08-31 16:18:59 +04:00
commit 459610cded
90 files changed
+4225 -2824

No files matched your search

+86 -31
View File
@@ -3,6 +3,7 @@
namespace App\Controllers;
use App\Models\Module;
use CodeIgniter\HTTP\Exceptions\RedirectException;
use CodeIgniter\HTTP\ResponseInterface;
use Config\Services;
@@ -79,8 +80,7 @@ class Employees extends Persons
$current_user = $this->employee->get_logged_in_employee_info();
if ($employee_id != NEW_ENTRY && !$this->employee->canModifyEmployee($person_info->person_id, $current_user->person_id)) {
header('Location: ' . base_url('no_access/employees/employees'));
exit();
throw new RedirectException('no_access/employees/employees');
}
foreach (get_object_vars($person_info) as $property => $value) {
@@ -114,13 +114,13 @@ class Employees extends Persons
* Inserts/updates an employee
* @return ResponseInterface
*/
public function postSave(int $employee_id = NEW_ENTRY): ResponseInterface
public function postSave(int $employeeId = NEW_ENTRY): ResponseInterface
{
$current_user = $this->employee->get_logged_in_employee_info();
$currentUser = $this->employee->get_logged_in_employee_info();
if ($employee_id != NEW_ENTRY) {
$target_employee = $this->employee->getInfo($employee_id);
if (!$this->employee->canModifyEmployee($target_employee->person_id, $current_user->person_id)) {
if ($employeeId != NEW_ENTRY) {
$targetEmployee = $this->employee->get_info($employeeId);
if (!$this->employee->canModifyEmployee($targetEmployee->person_id, $currentUser->person_id)) {
return $this->response->setJSON([
'success' => false,
'message' => lang('Employees.error_updating_admin'),
@@ -129,17 +129,17 @@ class Employees extends Persons
}
}
$first_name = $this->request->getPost('first_name', FILTER_SANITIZE_FULL_SPECIAL_CHARS); // TODO: duplicated code
$last_name = $this->request->getPost('last_name', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$firstName = $this->request->getPost('first_name', FILTER_SANITIZE_FULL_SPECIAL_CHARS); // TODO: duplicated code
$lastName = $this->request->getPost('last_name', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$email = strtolower($this->request->getPost('email', FILTER_SANITIZE_EMAIL));
// format first and last name properly
$first_name = $this->nameize($first_name);
$last_name = $this->nameize($last_name);
$firstName = $this->nameize($firstName);
$lastName = $this->nameize($lastName);
$person_data = [
'first_name' => $first_name,
'last_name' => $last_name,
$personData = [
'first_name' => $firstName,
'last_name' => $lastName,
'gender' => $this->request->getPost('gender', FILTER_SANITIZE_NUMBER_INT),
'email' => $email,
'phone_number' => $this->request->getPost('phone_number', FILTER_SANITIZE_FULL_SPECIAL_CHARS),
@@ -152,27 +152,55 @@ class Employees extends Persons
'comments' => $this->request->getPost('comments', FILTER_SANITIZE_FULL_SPECIAL_CHARS)
];
$grants_array = [];
$isAdmin = $this->employee->isAdmin($current_user->person_id);
$grantsArray = [];
$isAdmin = $this->employee->isAdmin($currentUser->person_id);
foreach ($this->module->get_all_permissions()->getResult() as $permission) {
$grants = [];
$grant = $this->request->getPost('grant_' . $permission->permission_id) != null ? $this->request->getPost('grant_' . $permission->permission_id, FILTER_SANITIZE_FULL_SPECIAL_CHARS) : '';
if ($grant == $permission->permission_id) {
if (!$isAdmin && !$this->employee->has_grant($permission->permission_id, $current_user->person_id)) {
if (!$isAdmin && !$this->employee->has_grant($permission->permission_id, $currentUser->person_id)) {
continue;
}
$grants['permission_id'] = $permission->permission_id;
$grants['menu_group'] = $this->request->getPost('menu_group_' . $permission->permission_id) != null ? $this->request->getPost('menu_group_' . $permission->permission_id, FILTER_SANITIZE_FULL_SPECIAL_CHARS) : '--';
$grants_array[] = $grants;
$grantsArray[] = $grants;
}
}
$minimumGrants = ['employees', 'home', 'office'];
$missingMinimumGrant = array_diff($minimumGrants, array_column($grantsArray, 'permission_id'));
if ($isAdmin && $employeeId == $currentUser->person_id && !empty($missingMinimumGrant)) {
return $this->response->setJSON([
'success' => false,
'message' => lang('Employees.error_cannot_remove_own_minimum_grant'),
'id' => $employeeId
]);
}
if (filter_var(getenv('DISALLOW_GRANT_CHANGE'), FILTER_VALIDATE_BOOLEAN)
&& $this->hasGrantsChanged($employeeId, $isAdmin, $currentUser, $grantsArray)) {
return $this->response->setJSON([
'success' => false,
'message' => lang('Employees.error_grant_change_disallowed'),
'id' => $employeeId
]);
}
if (!empty($this->request->getPost('password')) && ENVIRONMENT != 'testing' && filter_var(getenv('DISALLOW_PASSWORD_CHANGE'), FILTER_VALIDATE_BOOLEAN)) {
return $this->response->setJSON([
'success' => false,
'message' => lang('Employees.error_password_change_disallowed'),
'id' => $employeeId
]);
}
// Password has been changed OR first time password set
if (!empty($this->request->getPost('password')) && ENVIRONMENT != 'testing') {
$exploded = explode(":", $this->request->getPost('language', FILTER_SANITIZE_FULL_SPECIAL_CHARS));
$employee_data = [
$employeeData = [
'username' => $this->request->getPost('username', FILTER_SANITIZE_FULL_SPECIAL_CHARS),
'password' => password_hash($this->request->getPost('password'), PASSWORD_DEFAULT),
'hash_version' => 2,
@@ -181,42 +209,69 @@ class Employees extends Persons
];
} else { // Password not changed
$exploded = explode(":", $this->request->getPost('language', FILTER_SANITIZE_FULL_SPECIAL_CHARS));
$employee_data = [
$employeeData = [
'username' => $this->request->getPost('username', FILTER_SANITIZE_FULL_SPECIAL_CHARS),
'language_code' => $exploded[0],
'language' => $exploded[1]
];
}
if ($this->employee->save_employee($person_data, $employee_data, $grants_array, $employee_id)) {
if ($this->employee->save_employee($personData, $employeeData, $grantsArray, $employeeId)) {
// New employee
if ($employee_id == NEW_ENTRY) {
if ($employeeId == NEW_ENTRY) {
return $this->response->setJSON([
'success' => true,
'message' => lang('Employees.successful_adding') . ' ' . $first_name . ' ' . $last_name,
'id' => $employee_data['person_id']
'message' => lang('Employees.successful_adding') . ' ' . $firstName . ' ' . $lastName,
'id' => $employeeData['person_id']
]);
} else { // Existing employee
$logged_in_employee_id = session()->get('person_id');
if ($employee_id == $logged_in_employee_id) {
session()->set('language_code', $employee_data['language_code']);
session()->set('language', $employee_data['language']);
$loggedInEmployeeId = session()->get('person_id');
if ($employeeId == $loggedInEmployeeId) {
session()->set('language_code', $employeeData['language_code']);
session()->set('language', $employeeData['language']);
}
return $this->response->setJSON([
'success' => true,
'message' => lang('Employees.successful_updating') . ' ' . $first_name . ' ' . $last_name,
'id' => $employee_id
'message' => lang('Employees.successful_updating') . ' ' . $firstName . ' ' . $lastName,
'id' => $employeeId
]);
}
} else { // Failure
return $this->response->setJSON([
'success' => false,
'message' => lang('Employees.error_adding_updating') . ' ' . $first_name . ' ' . $last_name,
'message' => lang('Employees.error_adding_updating') . ' ' . $firstName . ' ' . $lastName,
'id' => NEW_ENTRY
]);
}
}
/**
* Determines whether the submitted grants differ from the employee's current grants,
* limited to the permissions the current user has authority over when not an admin.
*/
private function hasGrantsChanged(int $employeeId, bool $isAdmin, object $currentUser, array $grantsArray): bool
{
$currentGrantIds = [];
if ($employeeId != NEW_ENTRY) {
$currentGrantIds = array_column($this->employee->get_employee_grants($employeeId), 'permission_id');
if (!$isAdmin) {
$currentGrantIds = array_values(array_filter(
$currentGrantIds,
fn ($permissionId) => $this->employee->has_grant($permissionId, $currentUser->person_id)
));
}
}
$submittedGrantIds = array_column($grantsArray, 'permission_id');
sort($currentGrantIds);
sort($submittedGrantIds);
return $currentGrantIds !== $submittedGrantIds;
}
/**
* This deletes employees from the employees table
* @return ResponseInterface