MAPG-115 make possible to secure controllers

This commit is contained in:
Bence Pőcze 2020-06-07 23:37:01 +02:00
parent ed9621d581
commit ecaf8ca9d4
4 changed files with 45 additions and 18 deletions

View File

@ -30,15 +30,31 @@ $match = Container::$routeCollection->match($method, explode('/', $url));
if ($match !== null) { if ($match !== null) {
list($route, $params) = $match; 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) { if ($response instanceof MapGuesser\Interfaces\Response\IContent) {
header('Content-Type: ' . $response->getContentType() . '; charset=UTF-8'); header('Content-Type: ' . $response->getContentType() . '; charset=UTF-8');
echo $response->render(); echo $response->render();
return;
} elseif ($response instanceof MapGuesser\Interfaces\Response\IRedirect) { } elseif ($response instanceof MapGuesser\Interfaces\Response\IRedirect) {
header('Location: ' . $host . '/' . $response->getUrl(), true, $response->getHttpCode()); 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';

View File

@ -1,6 +1,7 @@
<?php namespace MapGuesser\Controller; <?php namespace MapGuesser\Controller;
use MapGuesser\Database\Query\Select; use MapGuesser\Database\Query\Select;
use MapGuesser\Interfaces\Authorization\ISecured;
use MapGuesser\Interfaces\Database\IResultSet; use MapGuesser\Interfaces\Database\IResultSet;
use MapGuesser\Interfaces\Response\IContent; use MapGuesser\Interfaces\Response\IContent;
use MapGuesser\Repository\PlaceRepository; use MapGuesser\Repository\PlaceRepository;
@ -8,7 +9,7 @@ use MapGuesser\Response\HtmlContent;
use MapGuesser\Response\JsonContent; use MapGuesser\Response\JsonContent;
use MapGuesser\Util\Geo\Bounds; use MapGuesser\Util\Geo\Bounds;
class MapAdminController class MapAdminController implements ISecured
{ {
private PlaceRepository $placeRepository; private PlaceRepository $placeRepository;
@ -17,6 +18,13 @@ class MapAdminController
$this->placeRepository = new PlaceRepository(); $this->placeRepository = new PlaceRepository();
} }
public function authorize(): bool
{
//TODO
return false;
}
public function getMaps(): IContent public function getMaps(): IContent
{ {
//TODO //TODO

View File

@ -0,0 +1,6 @@
<?php namespace MapGuesser\Interfaces\Authorization;
interface ISecured
{
public function authorize(): bool;
}

View File

@ -20,6 +20,11 @@ class Route
return $this->id; return $this->id;
} }
public function getHandler(): array
{
return $this->handler;
}
public function generateLink(array $parameters = []): string public function generateLink(array $parameters = []): string
{ {
$link = []; $link = [];
@ -51,14 +56,6 @@ class Route
return implode('/', $link) . $query; 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 public function testAgainst(array $path): ?array
{ {
$parameters = []; $parameters = [];