Skip to content

Commit 6e1c680

Browse files
lukasbestleafbora
andcommitted
Check permissions in API and Fiber
Co-authored-by: Ahmet Bora <[email protected]>
1 parent 2561fb1 commit 6e1c680

File tree

4 files changed

+67
-11
lines changed

4 files changed

+67
-11
lines changed

config/api/routes/languages.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,35 @@
88
'pattern' => 'languages',
99
'method' => 'GET',
1010
'action' => function () {
11-
return $this->kirby()->languages();
11+
return $this->languages();
1212
}
1313
],
1414
[
1515
'pattern' => 'languages',
1616
'method' => 'POST',
1717
'action' => function () {
18-
return $this->kirby()->languages()->create($this->requestBody());
18+
return $this->languages()->create($this->requestBody());
1919
}
2020
],
2121
[
2222
'pattern' => 'languages/(:any)',
2323
'method' => 'GET',
2424
'action' => function (string $code) {
25-
return $this->kirby()->languages()->find($code);
25+
return $this->languages()->find($code);
2626
}
2727
],
2828
[
2929
'pattern' => 'languages/(:any)',
3030
'method' => 'PATCH',
3131
'action' => function (string $code) {
32-
return $this->kirby()->languages()->find($code)?->update($this->requestBody());
32+
return $this->languages()->find($code)?->update($this->requestBody());
3333
}
3434
],
3535
[
3636
'pattern' => 'languages/(:any)',
3737
'method' => 'DELETE',
3838
'action' => function (string $code) {
39-
return $this->kirby()->languages()->find($code)?->delete();
39+
return $this->languages()->find($code)?->delete();
4040
}
4141
]
4242
];

src/Cms/Api.php

+17-3
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,14 @@ public function language(): string|null
138138
$this->requestHeaders('x-language');
139139
}
140140

141+
/**
142+
* Returns the languages collection
143+
*/
144+
public function languages(): Languages
145+
{
146+
return $this->kirby()->languages()->filter('isAccessible', true);
147+
}
148+
141149
/**
142150
* Returns the page object for the given id
143151
*
@@ -225,9 +233,15 @@ public function session(array $options = []): Session
225233
/**
226234
* Returns the site object
227235
*/
228-
public function site(): Site
236+
public function site(): Site|null
229237
{
230-
return $this->kirby->site();
238+
$site = $this->kirby->site();
239+
240+
if ($site->isAccessible() === true) {
241+
return $site;
242+
}
243+
244+
return null;
231245
}
232246

233247
/**
@@ -255,6 +269,6 @@ public function user(string|null $id = null): User|null
255269
*/
256270
public function users(): Users
257271
{
258-
return $this->kirby->users();
272+
return $this->kirby->users()->filter('isAccessible', true);
259273
}
260274
}

src/Cms/Find.php

+15-3
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ public static function file(
5252
*/
5353
public static function language(string $code): Language|null
5454
{
55-
if ($language = App::instance()->language($code)) {
55+
$language = App::instance()->language($code);
56+
57+
if ($language?->isAccessible() === true) {
5658
return $language;
5759
}
5860

@@ -158,13 +160,23 @@ public static function user(string|null $id = null): User|null
158160
$kirby->option('api.allowImpersonation', false)
159161
);
160162

161-
return $user ?? throw new NotFoundException(
163+
if ($user?->isAccessible() === true) {
164+
return $user;
165+
}
166+
167+
throw new NotFoundException(
162168
key: 'user.undefined'
163169
);
164170
}
165171

166172
// get a specific user by id
167-
return $kirby->user($id) ?? throw new NotFoundException(
173+
$user = $kirby->user($id);
174+
175+
if ($user?->isAccessible() === true) {
176+
return $user;
177+
}
178+
179+
throw new NotFoundException(
168180
key: 'user.notFound',
169181
data: ['name' => $id]
170182
);

tests/Cms/Api/ApiTest.php

+30
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,36 @@ public function testUsers()
436436
$this->assertSame($this->app->users(), $this->api->users());
437437
}
438438

439+
public function testUsersWithoutPermissions()
440+
{
441+
$app = $this->app->clone([
442+
'users' => [
443+
['email' => '[email protected]']
444+
]
445+
]);
446+
$app->impersonate('[email protected]');
447+
448+
$this->assertNotSame($app->users(), $app->api()->users());
449+
}
450+
451+
public function testUsersWithoutPermissionsDebugEnabled()
452+
{
453+
$app = $this->app->clone([
454+
'options' => [
455+
'debug' => true
456+
],
457+
'users' => [
458+
['email' => '[email protected]']
459+
]
460+
]);
461+
$app->impersonate('[email protected]');
462+
463+
$this->expectException(AuthException::class);
464+
$this->expectExceptionMessage('You are not allowed to access the users');
465+
466+
$app->api()->users();
467+
}
468+
439469
public function testFileGetRoute()
440470
{
441471
// regular

0 commit comments

Comments
 (0)