julienmonnerie/kirby/config/api/authentication.php

28 lines
673 B
PHP
Raw Normal View History

2022-06-17 17:51:59 +02:00
<?php
use Kirby\Exception\PermissionException;
return function () {
2022-08-31 15:02:43 +02:00
$auth = $this->kirby()->auth();
$allowImpersonation = $this->kirby()->option('api.allowImpersonation') ?? false;
2022-06-17 17:51:59 +02:00
2022-08-31 15:02:43 +02:00
// csrf token check
if (
$auth->type($allowImpersonation) === 'session' &&
$auth->csrf() === false
) {
throw new PermissionException('Unauthenticated');
}
2022-06-17 17:51:59 +02:00
2022-08-31 15:02:43 +02:00
// get user from session or basic auth
if ($user = $auth->user(null, $allowImpersonation)) {
if ($user->role()->permissions()->for('access', 'panel') === false) {
throw new PermissionException(['key' => 'access.panel']);
}
2022-06-17 17:51:59 +02:00
2022-08-31 15:02:43 +02:00
return $user;
}
2022-06-17 17:51:59 +02:00
2022-08-31 15:02:43 +02:00
throw new PermissionException('Unauthenticated');
2022-06-17 17:51:59 +02:00
};