MAPG-115 make possible to secure controllers
This commit is contained in:
parent
ed9621d581
commit
ecaf8ca9d4
@ -30,15 +30,31 @@ $match = Container::$routeCollection->match($method, explode('/', $url));
|
||||
if ($match !== null) {
|
||||
list($route, $params) = $match;
|
||||
|
||||
$response = $route->callController($params);
|
||||
$handler = $route->getHandler();
|
||||
|
||||
$controller = new $handler[0];
|
||||
|
||||
if ($controller instanceof MapGuesser\Interfaces\Authorization\ISecured) {
|
||||
$authorized = $controller->authorize();
|
||||
} else {
|
||||
$authorized = true;
|
||||
}
|
||||
|
||||
if ($authorized) {
|
||||
$response = call_user_func([$controller, $handler[1]], $params);
|
||||
|
||||
if ($response instanceof MapGuesser\Interfaces\Response\IContent) {
|
||||
header('Content-Type: ' . $response->getContentType() . '; charset=UTF-8');
|
||||
echo $response->render();
|
||||
|
||||
return;
|
||||
} elseif ($response instanceof MapGuesser\Interfaces\Response\IRedirect) {
|
||||
header('Location: ' . $host . '/' . $response->getUrl(), true, $response->getHttpCode());
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
header('Content-Type: text/html; charset=UTF-8', true, 404);
|
||||
require ROOT . '/views/error/404.php';
|
||||
}
|
||||
|
||||
header('Content-Type: text/html; charset=UTF-8', true, 404);
|
||||
require ROOT . '/views/error/404.php';
|
||||
|
@ -1,6 +1,7 @@
|
||||
<?php namespace MapGuesser\Controller;
|
||||
|
||||
use MapGuesser\Database\Query\Select;
|
||||
use MapGuesser\Interfaces\Authorization\ISecured;
|
||||
use MapGuesser\Interfaces\Database\IResultSet;
|
||||
use MapGuesser\Interfaces\Response\IContent;
|
||||
use MapGuesser\Repository\PlaceRepository;
|
||||
@ -8,7 +9,7 @@ use MapGuesser\Response\HtmlContent;
|
||||
use MapGuesser\Response\JsonContent;
|
||||
use MapGuesser\Util\Geo\Bounds;
|
||||
|
||||
class MapAdminController
|
||||
class MapAdminController implements ISecured
|
||||
{
|
||||
private PlaceRepository $placeRepository;
|
||||
|
||||
@ -17,6 +18,13 @@ class MapAdminController
|
||||
$this->placeRepository = new PlaceRepository();
|
||||
}
|
||||
|
||||
public function authorize(): bool
|
||||
{
|
||||
//TODO
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function getMaps(): IContent
|
||||
{
|
||||
//TODO
|
||||
|
6
src/Interfaces/Authorization/ISecured.php
Normal file
6
src/Interfaces/Authorization/ISecured.php
Normal file
@ -0,0 +1,6 @@
|
||||
<?php namespace MapGuesser\Interfaces\Authorization;
|
||||
|
||||
interface ISecured
|
||||
{
|
||||
public function authorize(): bool;
|
||||
}
|
@ -20,6 +20,11 @@ class Route
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function getHandler(): array
|
||||
{
|
||||
return $this->handler;
|
||||
}
|
||||
|
||||
public function generateLink(array $parameters = []): string
|
||||
{
|
||||
$link = [];
|
||||
@ -51,14 +56,6 @@ class Route
|
||||
return implode('/', $link) . $query;
|
||||
}
|
||||
|
||||
public function callController(array $parameters)
|
||||
{
|
||||
$controllerName = $this->handler[0];
|
||||
$controller = new $controllerName();
|
||||
|
||||
return call_user_func([$controller, $this->handler[1]], $parameters);
|
||||
}
|
||||
|
||||
public function testAgainst(array $path): ?array
|
||||
{
|
||||
$parameters = [];
|
||||
|
Loading…
Reference in New Issue
Block a user