Skip to content

Commit dea19c5

Browse files
Apply permission checks to the appointment and unavailability search (#1753)
1 parent e8490cc commit dea19c5

3 files changed

Lines changed: 55 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ developers to maintain and readjust their custom modifications on the main proje
1111
- Fix the "Load More" JS error in secretaries page (#1677)
1212
- Fix the PHP compatibility error of the appointments index API endpoint (#1678)
1313
- Catch individual email delivery exceptions (#1670)
14+
- Apply permission checks to the appointment and unavailability search (#1753)
1415

1516

1617

application/controllers/Appointments.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,33 @@ public function search(): void
8888

8989
$appointments = $this->appointments_model->search($keyword, $limit, $offset, $order_by);
9090

91+
$user_id = session('user_id');
92+
$role_slug = session('role_slug');
93+
94+
// If the current user is a provider he must only see his own appointments.
95+
if ($role_slug === DB_SLUG_PROVIDER) {
96+
foreach ($appointments as $index => $appointment) {
97+
if ((int) $appointment['id_users_provider'] !== (int) $user_id) {
98+
unset($appointments[$index]);
99+
}
100+
}
101+
102+
$appointments = array_values($appointments);
103+
}
104+
105+
// If the current user is a secretary he must only see the appointments of his providers.
106+
if ($role_slug === DB_SLUG_SECRETARY) {
107+
$provider_ids = $this->secretaries_model->find($user_id)['providers'];
108+
109+
foreach ($appointments as $index => $appointment) {
110+
if (!in_array((int) $appointment['id_users_provider'], $provider_ids)) {
111+
unset($appointments[$index]);
112+
}
113+
}
114+
115+
$appointments = array_values($appointments);
116+
}
117+
91118
json_response($appointments);
92119
} catch (Throwable $e) {
93120
json_exception($e);

application/controllers/Unavailabilities.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,33 @@ public function search(): void
6969

7070
$unavailabilities = $this->unavailabilities_model->search($keyword, $limit, $offset, $order_by);
7171

72+
$user_id = session('user_id');
73+
$role_slug = session('role_slug');
74+
75+
// If the current user is a provider he must only see his own appointments.
76+
if ($role_slug === DB_SLUG_PROVIDER) {
77+
foreach ($unavailabilities as $index => $unavailability) {
78+
if ((int) $unavailability['id_users_provider'] !== (int) $user_id) {
79+
unset($unavailabilities[$index]);
80+
}
81+
}
82+
83+
$unavailabilities = array_values($unavailabilities);
84+
}
85+
86+
// If the current user is a secretary he must only see the unavailabilities of his providers.
87+
if ($role_slug === DB_SLUG_SECRETARY) {
88+
$provider_ids = $this->secretaries_model->find($user_id)['providers'];
89+
90+
foreach ($unavailabilities as $index => $unavailability) {
91+
if (!in_array((int) $unavailability['id_users_provider'], $provider_ids)) {
92+
unset($unavailabilities[$index]);
93+
}
94+
}
95+
96+
$unavailabilities = array_values($unavailabilities);
97+
}
98+
7299
json_response($unavailabilities);
73100
} catch (Throwable $e) {
74101
json_exception($e);

0 commit comments

Comments
 (0)