2020-08-04 13:25:22 +02:00
|
|
|
<?php
|
|
|
|
|
2021-06-08 11:52:11 +02:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2021-08-23 13:05:16 +02:00
|
|
|
namespace Modules\Auth\Filters;
|
2020-07-31 18:05:10 +02:00
|
|
|
|
2023-04-14 13:11:53 +02:00
|
|
|
use App\Entities\Podcast;
|
2020-07-31 18:05:10 +02:00
|
|
|
use App\Models\PodcastModel;
|
2023-07-06 15:50:10 +02:00
|
|
|
use CodeIgniter\Filters\FilterInterface;
|
|
|
|
use CodeIgniter\HTTP\RequestInterface;
|
|
|
|
use CodeIgniter\HTTP\ResponseInterface;
|
2021-05-19 18:35:13 +02:00
|
|
|
use Config\Services;
|
2023-07-06 15:50:10 +02:00
|
|
|
use RuntimeException;
|
2020-07-31 18:05:10 +02:00
|
|
|
|
2023-07-03 12:57:03 +02:00
|
|
|
/**
|
|
|
|
* Permission Authorization Filter.
|
|
|
|
*/
|
2023-07-06 15:50:10 +02:00
|
|
|
class PermissionFilter implements FilterInterface
|
2020-07-31 18:05:10 +02:00
|
|
|
{
|
2023-07-06 15:50:10 +02:00
|
|
|
/**
|
|
|
|
* @param string[]|null $arguments
|
2023-12-21 16:21:45 +01:00
|
|
|
*
|
|
|
|
* @return RequestInterface|ResponseInterface|string|void
|
2023-07-06 15:50:10 +02:00
|
|
|
*/
|
|
|
|
public function before(RequestInterface $request, $arguments = null)
|
|
|
|
{
|
|
|
|
if ($arguments === null || $arguments === []) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! auth()->loggedIn()) {
|
|
|
|
return redirect()->route('login');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->isAuthorized($arguments)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new RuntimeException(lang('Auth.notEnoughPrivilege'), 403);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string[]|null $arguments
|
|
|
|
*/
|
|
|
|
public function after(RequestInterface $request, ResponseInterface $response, $arguments = null): void
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-07-31 18:05:10 +02:00
|
|
|
/**
|
2023-07-03 12:57:03 +02:00
|
|
|
* Ensures the user is logged in and has one or more
|
|
|
|
* of the permissions as specified in the filter.
|
2020-07-31 18:05:10 +02:00
|
|
|
*
|
2023-07-03 12:57:03 +02:00
|
|
|
* @param string[] $arguments
|
2020-07-31 18:05:10 +02:00
|
|
|
*/
|
2023-07-03 12:57:03 +02:00
|
|
|
protected function isAuthorized(array $arguments): bool
|
2020-07-31 18:05:10 +02:00
|
|
|
{
|
2022-10-15 13:22:08 +02:00
|
|
|
$result = true;
|
2020-07-31 18:05:10 +02:00
|
|
|
|
2023-07-03 12:57:03 +02:00
|
|
|
foreach ($arguments as $permission) {
|
|
|
|
// is permission specific to a podcast?
|
2022-10-15 13:22:08 +02:00
|
|
|
if (str_contains($permission, '#')) {
|
|
|
|
$router = Services::router();
|
|
|
|
$routerParams = $router->params();
|
|
|
|
|
|
|
|
// get podcast id
|
|
|
|
$podcastId = null;
|
|
|
|
if (is_numeric($routerParams[0])) {
|
|
|
|
$podcastId = (int) $routerParams[0];
|
|
|
|
} else {
|
|
|
|
$podcast = (new PodcastModel())->getPodcastByHandle($routerParams[0]);
|
2023-04-14 13:11:53 +02:00
|
|
|
if ($podcast instanceof Podcast) {
|
2022-10-15 13:22:08 +02:00
|
|
|
$podcastId = $podcast->id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($podcastId !== null) {
|
|
|
|
$permission = str_replace('#', '#' . $podcastId, $permission);
|
2020-07-31 18:05:10 +02:00
|
|
|
}
|
|
|
|
}
|
2022-10-15 13:22:08 +02:00
|
|
|
|
|
|
|
$result = $result && auth()
|
|
|
|
->user()
|
|
|
|
->can($permission);
|
2020-07-31 18:05:10 +02:00
|
|
|
}
|
|
|
|
|
2023-07-03 12:57:03 +02:00
|
|
|
return $result;
|
2020-07-31 18:05:10 +02:00
|
|
|
}
|
|
|
|
}
|