This repository has been archived on 2023-01-06. You can view files and clone it, but cannot push or open issues or pull requests.
web/app/lib/App/Core/AccessControl.php

106 lines
2.5 KiB
PHP
Raw Normal View History

2022-01-30 21:11:38 +00:00
<?php
2022-03-02 06:15:12 +00:00
namespace App\Core;
use \Exception;
/**
2022-03-13 19:54:34 +00:00
* Decides what is allowed and what not
2022-03-02 06:15:12 +00:00
* TODO: ...
*/
2022-01-30 21:11:38 +00:00
class AccessControl
{
2022-03-13 19:54:34 +00:00
public App $app;
2022-01-30 21:11:38 +00:00
2022-03-13 19:54:34 +00:00
private array $acl;
private string $currentPage;
public function __construct(App $app)
2022-01-30 21:11:38 +00:00
{
2022-03-13 19:54:34 +00:00
$this->app = $app;
2022-02-07 09:36:00 +00:00
2022-03-13 19:54:34 +00:00
$this->acl = [
// routes that need power level 1 and up
[
"routes" => [
"teamtable/edit/"
],
"catcher" => [
"name" => "page",
"args" => 1,
],
],
// routes that dont need any auth
[
"routes" => [
""
],
"catcher" => [
"name" => "nothing",
],
]
2022-01-30 21:11:38 +00:00
];
2022-02-07 06:14:33 +00:00
2022-03-13 19:54:34 +00:00
$this->currentPage = substr(
$_SERVER["PHP_SELF"],
strlen($this->app->config["root_url"])
);
2022-02-07 09:36:00 +00:00
2022-03-13 19:54:34 +00:00
foreach ($this->acl as $key => $value)
2022-02-07 09:36:00 +00:00
{
2022-03-13 19:54:34 +00:00
$routes = $value["routes"];
$catcher = $value["catcher"];
2022-02-07 09:36:00 +00:00
2022-03-13 19:54:34 +00:00
foreach ($routes as $key => $value)
2022-02-07 09:36:00 +00:00
{
2022-03-13 19:54:34 +00:00
// check if string starts with
if (strncmp($this->currentPage, $value, strlen($value)) !== 0)
{
continue;
}
#if ($value !== $this->currentPage)
#{
# continue;
#}
if (isset($catcher["args"]))
{
call_user_func([$this, $catcher["name"]], $catcher["args"]);
} else {
call_user_func([$this, $catcher["name"]]);
}
2022-02-07 09:36:00 +00:00
2022-03-13 19:54:34 +00:00
return;
}
2022-02-07 09:36:00 +00:00
}
2022-03-13 19:54:34 +00:00
throw new Exception("Could not find current page in access control list, did you add it?");
2022-01-30 21:11:38 +00:00
}
2022-03-13 19:54:34 +00:00
private function page(int $powerLevel): void
2022-01-30 21:11:38 +00:00
{
2022-03-14 06:33:38 +00:00
if ($this->app->user->loggedIn && $this->app->user->powerLevel >= $powerLevel)
2022-03-13 19:54:34 +00:00
{
2022-02-07 09:36:00 +00:00
echo "Authorized!";
} else {
2022-03-13 19:54:34 +00:00
http_response_code(401);
$this->app->view("template/header", ["title" => "Ingen tilgang!"]);
$this->app->view("Core/AccessControl/unauthorized");
$this->app->view("template/footer");
die();
2022-02-07 09:36:00 +00:00
}
2022-01-30 21:11:38 +00:00
}
2022-03-13 19:54:34 +00:00
/**
* Does... nothing! For when the page does not need any access control.
*/
private function nothing(): void
{
return;
}
private function api($powerLevel): void
{
// ...
}
2022-01-30 21:11:38 +00:00
}