-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
67f69db
commit a5b2773
Showing
15 changed files
with
296 additions
and
46 deletions.
There are no files selected for viewing
Empty file.
58 changes: 58 additions & 0 deletions
58
Controllers/Abstracts/AbstractAdministrationController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace ArrayAccess\TrayDigita\App\Modules\Core\Controllers\Abstracts; | ||
|
||
use ArrayAccess\TrayDigita\Traits\Service\TranslatorTrait; | ||
use ArrayAccess\TrayDigita\Util\Filter\DataNormalizer; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use function var_dump; | ||
|
||
abstract class AbstractAdministrationController extends AbstractAuthenticationBasedController | ||
{ | ||
use TranslatorTrait; | ||
|
||
protected bool $doRedirect = true; | ||
|
||
/** | ||
* Doing check | ||
* | ||
* @param ServerRequestInterface $request | ||
* @param string $method | ||
* @param ...$arguments | ||
* @noinspection PhpMissingReturnTypeInspection | ||
* @noinspection PhpDocSignatureIsNotCompleteInspection | ||
*/ | ||
final public function doBeforeDispatch( | ||
ServerRequestInterface $request, | ||
string $method, | ||
...$arguments | ||
) { | ||
$redirect = $this->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 | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
Controllers/Abstracts/AbstractAuthenticationBasedController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace ArrayAccess\TrayDigita\App\Modules\Core\Controllers\Abstracts; | ||
|
||
use ArrayAccess\TrayDigita\App\Modules\Core\SubModules\Controllers\Attributes\Dashboard as DashboardAttribute; | ||
use ArrayAccess\TrayDigita\App\Modules\Core\SubModules\Controllers\Attributes\User as UserAttribute; | ||
use ArrayAccess\TrayDigita\App\Modules\Users\Entities\Admin; | ||
use ArrayAccess\TrayDigita\App\Modules\Users\Entities\User; | ||
use ArrayAccess\TrayDigita\App\Modules\Users\Users; | ||
use ArrayAccess\TrayDigita\Routing\AbstractController; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
|
||
abstract class AbstractAuthenticationBasedController extends AbstractController | ||
{ | ||
protected Users $users; | ||
|
||
protected ?User $user = null; | ||
|
||
protected ?Admin $admin = null; | ||
|
||
protected string $userAuthPath; | ||
|
||
protected string $dashboardAuthPath; | ||
|
||
protected ?string $authenticationMethod = null; | ||
|
||
const TYPE_USER = 'user'; | ||
|
||
const TYPE_ADMIN = 'admin'; | ||
|
||
protected function getAuthenticationMethod() : ?string | ||
{ | ||
return $this->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 | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace ArrayAccess\TrayDigita\App\Modules\Core\Controllers\Abstracts; | ||
|
||
use Psr\Http\Message\ServerRequestInterface; | ||
|
||
abstract class AbstractDashboardController extends AbstractAdministrationController | ||
{ | ||
protected ?string $authenticationMethod = self::TYPE_ADMIN; | ||
|
||
/** | ||
* @param ServerRequestInterface $request | ||
* @param string $method | ||
* @param ...$arguments | ||
*/ | ||
public function doAfterBeforeDispatch( | ||
ServerRequestInterface $request, | ||
string $method, | ||
...$arguments | ||
) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace ArrayAccess\TrayDigita\App\Modules\Core\Controllers\Abstracts; | ||
|
||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use function is_string; | ||
use function reset; | ||
use function str_ends_with; | ||
use function str_starts_with; | ||
|
||
abstract class AbstractUserController extends AbstractAdministrationController | ||
{ | ||
protected ?string $authenticationMethod = self::TYPE_USER; | ||
|
||
/** | ||
* @param ServerRequestInterface $request | ||
* @param string $method | ||
* @param ...$arguments | ||
* @return ResponseInterface|void|null | ||
*/ | ||
public function doAfterBeforeDispatch( | ||
ServerRequestInterface $request, | ||
string $method, | ||
...$arguments | ||
) { | ||
$reset = reset($arguments); | ||
if ($request->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 ? '/': '') | ||
) | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace ArrayAccess\TrayDigita\App\Modules\Core\Controllers\User; | ||
|
||
use ArrayAccess\TrayDigita\App\Modules\Core\Controllers\Abstracts\AbstractUserController; | ||
use ArrayAccess\TrayDigita\App\Modules\Core\SubModules\Controllers\Attributes\User; | ||
use ArrayAccess\TrayDigita\Routing\Attributes\Any; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use const PHP_INT_MIN; | ||
|
||
#[User('/auth')] | ||
class Auth extends AbstractUserController | ||
{ | ||
protected bool $doRedirect = false; | ||
|
||
protected bool $asJSON = false; | ||
|
||
#[Any( | ||
pattern: '/', | ||
priority: PHP_INT_MIN | ||
)] | ||
public function login( | ||
ServerRequestInterface $request, | ||
ResponseInterface $response, | ||
array $params | ||
): ResponseInterface { | ||
return $this->render( | ||
'user/login', | ||
[ | ||
'title' => $this->trans( | ||
'Login to member area', | ||
'core-module' | ||
), | ||
], | ||
$response | ||
); | ||
// return new \ArrayAccess\TrayDigita\Responder\FileResponder\FileResponder(__FILE__); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace ArrayAccess\TrayDigita\App\Modules\Core\Controllers\User; | ||
|
||
use ArrayAccess\TrayDigita\App\Modules\Core\Controllers\Abstracts\AbstractUserController; | ||
use ArrayAccess\TrayDigita\App\Modules\Core\SubModules\Controllers\Attributes\User; | ||
use ArrayAccess\TrayDigita\Routing\Attributes\Any; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use const PHP_INT_MAX; | ||
|
||
#[User] | ||
class Main extends AbstractUserController | ||
{ | ||
#[Any( | ||
pattern: '(/.*)?', | ||
priority: PHP_INT_MAX - 1000 | ||
)] | ||
public function dashboard( | ||
ServerRequestInterface $request, | ||
ResponseInterface $response | ||
) : ResponseInterface { | ||
return $this->render( | ||
'/user/dashboard', | ||
[], | ||
$response | ||
); | ||
} | ||
} |
Oops, something went wrong.