diff --git a/Controllers/.gitkeep b/Controllers/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/Controllers/Abstracts/AbstractAdministrationController.php b/Controllers/Abstracts/AbstractAdministrationController.php new file mode 100644 index 0000000..933f5a6 --- /dev/null +++ b/Controllers/Abstracts/AbstractAdministrationController.php @@ -0,0 +1,58 @@ +doRedirect ? match ($this->getAuthenticationMethod()) { + self::TYPE_ADMIN => $this->admin ? null : $this->dashboardAuthPath, + self::TYPE_USER => $this->admin ? null : $this->userAuthPath, + default => null, + } : null; + return $redirect + ? $this->redirect( + $this + ->getView() + ->getBaseURI($redirect) + ->withQuery( + 'redirect=' + . DataNormalizer::normalizeUnixDirectorySeparator($request->getUri()->getPath()) + ) + ) : $this->doAfterBeforeDispatch($request, $method, $arguments); + } + + /** + * @param ServerRequestInterface $request + * @param string $method + * @param ...$arguments + */ + abstract public function doAfterBeforeDispatch( + ServerRequestInterface $request, + string $method, + ...$arguments + ); +} diff --git a/Controllers/Api/Abstracts/AbstractApiController.php b/Controllers/Abstracts/AbstractApiController.php similarity index 58% rename from Controllers/Api/Abstracts/AbstractApiController.php rename to Controllers/Abstracts/AbstractApiController.php index 72ae341..93d7c0c 100644 --- a/Controllers/Api/Abstracts/AbstractApiController.php +++ b/Controllers/Abstracts/AbstractApiController.php @@ -1,36 +1,22 @@ authenticationMethod; - } - - final public function beforeDispatch( + final public function doBeforeDispatch( ServerRequestInterface $request, string $method, ...$arguments - ) { - + ): ?ResponseInterface { $this->statusCode = 404; // set result as json if return is not string $this->asJSON = true; @@ -44,27 +30,23 @@ final public function beforeDispatch( static fn ($flags) => JSON_PRETTY_PRINT|$flags ); } - $response = $this->doBeforeDispatch($request, $method, ...$arguments); - if ($response instanceof ResponseInterface) { - return $response; - } $method = $this->getAuthenticationMethod(); if ($method === null) { return null; } $jsonResponder = $this->getJsonResponder(); - $auth = Decorator::module(Users::class); $match = match ($method) { - self::TYPE_USER => $auth->isUserLoggedIn() + self::TYPE_USER => $this->user ? null : $jsonResponder->serve(Code::UNAUTHORIZED), - self::TYPE_ADMIN => $auth->isAdminLoggedIn() + self::TYPE_ADMIN => $this->admin ? null : $jsonResponder->serve(Code::UNAUTHORIZED), - default => $auth->isAdminLoggedIn() || $auth->isUserLoggedIn() + default => $this->admin || $this->user ? null : $jsonResponder->serve(Code::UNAUTHORIZED), }; + return $match ?? $this->doAfterBeforeDispatch( $request, $method, @@ -76,17 +58,7 @@ final public function beforeDispatch( * @param ServerRequestInterface $request * @param string $method * @param ...$arguments - * @return mixed - * @noinspection PhpReturnDocTypeMismatchInspection - * @noinspection PhpInconsistentReturnPointsInspection */ - public function doBeforeDispatch( - ServerRequestInterface $request, - string $method, - ...$arguments - ) { - } - public function doAfterBeforeDispatch( ServerRequestInterface $request, string $method, diff --git a/Controllers/Abstracts/AbstractAuthenticationBasedController.php b/Controllers/Abstracts/AbstractAuthenticationBasedController.php new file mode 100644 index 0000000..f1706b8 --- /dev/null +++ b/Controllers/Abstracts/AbstractAuthenticationBasedController.php @@ -0,0 +1,59 @@ +authenticationMethod; + } + + final public function beforeDispatch(ServerRequestInterface $request, string $method, ...$arguments) + { + $this->userAuthPath = UserAttribute::path('/auth'); + $this->dashboardAuthPath = DashboardAttribute::path('/auth'); + $this->users = $this->getModule(Users::class); + $this->user = $this->users->getAdminAccount(); + $this->admin = $this->users->getUserAccount(); + $this->getView()->setParameter('user', $this->user); + $this->getView()->setParameter('admin', $this->admin); + return $this->doBeforeDispatch($request, $method, ...$arguments); + } + + /** + * @param ServerRequestInterface $request + * @param string $method + * @param ...$arguments + */ + abstract public function doBeforeDispatch( + ServerRequestInterface $request, + string $method, + ...$arguments + ); +} diff --git a/Controllers/Abstracts/AbstractDashboardController.php b/Controllers/Abstracts/AbstractDashboardController.php new file mode 100644 index 0000000..8ce58da --- /dev/null +++ b/Controllers/Abstracts/AbstractDashboardController.php @@ -0,0 +1,23 @@ +getMethod() !== 'GET' + || !($path = (reset($reset)?:[])[0]??null) + || !is_string($path) + ) { + return null; + } + if (($end = str_ends_with($path, '//')) || str_starts_with($path, '//')) { + return $this->redirect( + $this->getView()->getBaseURI( + '/'. + trim($path, '/') + . ($end ? '/': '') + ) + ); + } + } +} diff --git a/Controllers/Api/Admin/Upload/MediaUpload.php b/Controllers/Api/Admin/Upload/MediaUpload.php index 94b5b12..9e5242b 100644 --- a/Controllers/Api/Admin/Upload/MediaUpload.php +++ b/Controllers/Api/Admin/Upload/MediaUpload.php @@ -3,7 +3,7 @@ namespace ArrayAccess\TrayDigita\App\Modules\Core\Controllers\Api\Admin\Upload; -use ArrayAccess\TrayDigita\App\Modules\Core\Controllers\Api\Abstracts\AbstractApiController; +use ArrayAccess\TrayDigita\App\Modules\Core\Controllers\Abstracts\AbstractApiController; use ArrayAccess\TrayDigita\App\Modules\Core\SubModules\Api\Attributes\DashboardAPI; use ArrayAccess\TrayDigita\App\Modules\Media\Media; use ArrayAccess\TrayDigita\App\Modules\Users\Users; diff --git a/Controllers/Api/Admin/User.php b/Controllers/Api/Admin/User.php index 44422a4..a8146ad 100644 --- a/Controllers/Api/Admin/User.php +++ b/Controllers/Api/Admin/User.php @@ -4,7 +4,7 @@ namespace ArrayAccess\TrayDigita\App\Modules\Core\Controllers\Api\Admin; -use ArrayAccess\TrayDigita\App\Modules\Core\Controllers\Api\Abstracts\AbstractApiController; +use ArrayAccess\TrayDigita\App\Modules\Core\Controllers\Abstracts\AbstractApiController; use ArrayAccess\TrayDigita\App\Modules\Core\SubModules\Api\Attributes\DashboardAPI; use ArrayAccess\TrayDigita\App\Modules\Users\Users; use ArrayAccess\TrayDigita\Kernel\Decorator; diff --git a/Controllers/Api/GlobalApi.php b/Controllers/Api/GlobalApi.php index 984de29..d662138 100644 --- a/Controllers/Api/GlobalApi.php +++ b/Controllers/Api/GlobalApi.php @@ -3,7 +3,7 @@ namespace ArrayAccess\TrayDigita\App\Modules\Core\Controllers\Api; -use ArrayAccess\TrayDigita\App\Modules\Core\Controllers\Api\Abstracts\AbstractApiController; +use ArrayAccess\TrayDigita\App\Modules\Core\Controllers\Abstracts\AbstractApiController; use ArrayAccess\TrayDigita\App\Modules\Core\SubModules\Api\Attributes\RouteAPI; use ArrayAccess\TrayDigita\Routing\Attributes\Any; use Psr\Http\Message\ResponseInterface; diff --git a/Controllers/Api/User/Upload/MediaUpload.php b/Controllers/Api/User/Upload/MediaUpload.php index 1670b66..19b72d5 100644 --- a/Controllers/Api/User/Upload/MediaUpload.php +++ b/Controllers/Api/User/Upload/MediaUpload.php @@ -3,7 +3,7 @@ namespace ArrayAccess\TrayDigita\App\Modules\Core\Controllers\Api\User\Upload; -use ArrayAccess\TrayDigita\App\Modules\Core\Controllers\Api\Abstracts\AbstractApiController; +use ArrayAccess\TrayDigita\App\Modules\Core\Controllers\Abstracts\AbstractApiController; use ArrayAccess\TrayDigita\App\Modules\Core\SubModules\Api\Attributes\UserAPI; use ArrayAccess\TrayDigita\App\Modules\Media\Media; use ArrayAccess\TrayDigita\App\Modules\Users\Users; diff --git a/Controllers/Api/User/User.php b/Controllers/Api/User/User.php index 6dc4ea8..ec9a9d2 100644 --- a/Controllers/Api/User/User.php +++ b/Controllers/Api/User/User.php @@ -4,7 +4,7 @@ namespace ArrayAccess\TrayDigita\App\Modules\Core\Controllers\Api\User; -use ArrayAccess\TrayDigita\App\Modules\Core\Controllers\Api\Abstracts\AbstractApiController; +use ArrayAccess\TrayDigita\App\Modules\Core\Controllers\Abstracts\AbstractApiController; use ArrayAccess\TrayDigita\App\Modules\Core\SubModules\Api\Attributes\UserAPI; use ArrayAccess\TrayDigita\App\Modules\Users\Users; use ArrayAccess\TrayDigita\Routing\Attributes\Abstracts\HttpMethodAttributeAbstract; diff --git a/Controllers/User/Auth.php b/Controllers/User/Auth.php new file mode 100644 index 0000000..988e74a --- /dev/null +++ b/Controllers/User/Auth.php @@ -0,0 +1,41 @@ +render( + 'user/login', + [ + 'title' => $this->trans( + 'Login to member area', + 'core-module' + ), + ], + $response + ); + // return new \ArrayAccess\TrayDigita\Responder\FileResponder\FileResponder(__FILE__); + } +} diff --git a/Controllers/User/Main.php b/Controllers/User/Main.php new file mode 100644 index 0000000..9e0ab20 --- /dev/null +++ b/Controllers/User/Main.php @@ -0,0 +1,30 @@ +render( + '/user/dashboard', + [], + $response + ); + } +} diff --git a/SubModules/Api/Attributes/RouteAPI.php b/SubModules/Api/Attributes/RouteAPI.php index 1e21b74..3c291b4 100644 --- a/SubModules/Api/Attributes/RouteAPI.php +++ b/SubModules/Api/Attributes/RouteAPI.php @@ -10,6 +10,7 @@ use Psr\Http\Message\UriInterface; use function in_array; use function sprintf; +use function str_starts_with; use function substr; #[Attribute(Attribute::TARGET_CLASS)] @@ -30,8 +31,9 @@ public function __construct(string $pattern = '') $prefixRoute = static::prefix(); // if contains delimiter if (in_array($prefix, Router::REGEX_DELIMITER)) { - $prefixRoute = "$prefix$prefixRoute"; + $prefixRoute = "$prefix^$prefixRoute"; $pattern = substr($pattern, 1); + $pattern = "(?:$pattern)"; } $pattern = $prefixRoute . $pattern; parent::__construct($pattern); diff --git a/SubModules/Controllers/Attributes/Dashboard.php b/SubModules/Controllers/Attributes/Dashboard.php index f693d61..a73d4a7 100644 --- a/SubModules/Controllers/Attributes/Dashboard.php +++ b/SubModules/Controllers/Attributes/Dashboard.php @@ -8,7 +8,9 @@ use ArrayAccess\TrayDigita\Util\Filter\DataNormalizer; use Attribute; use function in_array; +use function str_starts_with; use function substr; +use function var_dump; #[Attribute(Attribute::TARGET_CLASS)] class Dashboard extends Group @@ -19,19 +21,37 @@ public function __construct(string $pattern = '') { $prefix = substr($pattern, 0, 1); // use static prefix - $prefixRoute = static::getPrefix(); + $prefixRoute = static::prefix(); + if (!str_starts_with($prefixRoute, '/')) { + $prefixRoute = "/$prefixRoute"; + } + // if contains delimiter if (in_array($prefix, Router::REGEX_DELIMITER)) { - $prefixRoute = "$prefix$prefixRoute"; + $prefixRoute = "$prefix^$prefixRoute"; $pattern = substr($pattern, 1); + $pattern = "(?:$pattern)"; } $pattern = $prefixRoute . $pattern; parent::__construct($pattern); } - public static function getPrefix(): string + public static function prefix(): string { - return static::$prefix; + $prefix = static::$prefix; + return str_starts_with($prefix, '/') + ? $prefix + : "/$prefix"; + } + + public static function path(string $path = ''): string + { + $prefix = static::prefix(); + if ($path && !str_starts_with($path, '/')) { + $path = "/$path"; + } + + return $prefix . $path; } public static function setPrefix(string $prefix): void