Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip: refactor authentication flow #1902

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion docker/web/development/php.ini
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ assert.exception = 1
zend.assertions = 1

[opcache]
opcache.enable = 1
opcache.enable = 0
opcache.memory_consumption = 256
opcache.interned_strings_buffer = 16
opcache.max_accelerated_files = 10000
Expand Down Expand Up @@ -67,6 +67,9 @@ session.gc_divisor = 100
session.gc_maxlifetime = 43200

[XDebug]
xdebug.remote_autostart=0
xdebug.remote_enable=0
xdebug.profiler_enable=0
xdebug.max_nesting_level = 256
xdebug.mode = develop,coverage,debug
xdebug.client_host = host.docker.internal
Expand Down
2 changes: 2 additions & 0 deletions module/Activity/config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Doctrine\ORM\Mapping\Driver\AttributeDriver;
use Laminas\Router\Http\Literal;
use Laminas\Router\Http\Segment;
use User\Listener\Authentication;

return [
'router' => [
Expand Down Expand Up @@ -449,6 +450,7 @@
'defaults' => [
'controller' => ApiController::class,
'action' => 'list',
'auth_type' => Authentication::AUTH_API,
],
],
'may_terminate' => false,
Expand Down
6 changes: 4 additions & 2 deletions module/Application/config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,14 @@
'template_map' => [
'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
'application/index/teapot' => __DIR__ . '/../view/error/418.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/401' => __DIR__ . '/../view/error/401.phtml',
'error/403' => __DIR__ . '/../view/error/403.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/418' => __DIR__ . '/../view/error/418.phtml',
'error/500' => __DIR__ . '/../view/error/500.phtml',
'error/debug/404' => __DIR__ . '/../view/error/debug/404.phtml',
'error/debug/401' => __DIR__ . '/../view/error/debug/401.phtml',
'error/debug/403' => __DIR__ . '/../view/error/debug/403.phtml',
'error/debug/404' => __DIR__ . '/../view/error/debug/404.phtml',
'error/debug/500' => __DIR__ . '/../view/error/debug/500.phtml',
'paginator/default' => __DIR__ . '/../view/partial/paginator.phtml',
],
Expand Down
23 changes: 23 additions & 0 deletions module/Application/src/Model/Enums/ApiResponseStatuses.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Application\Model\Enums;

/**
* Enum for the different statuses an API response can have.
*/
enum ApiResponseStatuses: string
{
// For 2xx codes
case Success = 'success';

// For 403 HTTP code
case Forbidden = 'forbidden';

// For 404 HTTP code
case NotFound = 'notfound';

// For 5xx HTTP codes
case Error = 'error';
}
2 changes: 1 addition & 1 deletion module/Application/src/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function onBootstrap(MvcEvent $e): void
$moduleRouteListener->attach($eventManager);

// Attach listener for locale determination through the `LanguageAwareTreeRouteStack`.
$eventManager->attach(MvcEvent::EVENT_ROUTE, [$this, 'onRoute']);
$eventManager->attach(MvcEvent::EVENT_ROUTE, [$this, 'onRoute'], 100);

$eventManager->attach(MvcEvent::EVENT_DISPATCH_ERROR, [$this, 'logError']);
$eventManager->attach(MvCEvent::EVENT_RENDER_ERROR, [$this, 'logError']);
Expand Down
22 changes: 22 additions & 0 deletions module/Application/view/error/401.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

use Application\View\HelperTrait;
use Laminas\View\Renderer\PhpRenderer;

/** @var PhpRenderer|HelperTrait $this */
?>
<section class="section">
<div class="container">
<h2><?= $this->translate('Unauthenticated') ?></h2>
<p><?= $this->translate('Log in to access this page.') ?></p>
<a href="<?= $this->url(
name: 'user/login',
options: ['query' => ['redirect_to' => base64_encode($this->serverUrl(true))]],
) ?>">
<span class="fas fa-user"></span>
<?= $this->translate('Login') ?>
</a>
</div>
</section>
22 changes: 3 additions & 19 deletions module/Application/view/error/403.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,10 @@ use Laminas\View\Renderer\PhpRenderer;
?>
<section class="section">
<div class="container">
<?php
if (!empty($this->exception->getMessage())): ?>
<?php if (!empty($this->exception->getMessage())): ?>
<h2><?= $this->exception->getMessage() ?></h2>
<?php
else: ?>
<?php else: ?>
<h2><?= $this->translate('You do not have the required privileges to view this page') ?></h2>
<?php
endif; ?>
<?php
if ($this->identity() === null): ?>
<p><?= $this->translate('You might be able to view this page by logging in') ?></p>
<a href="<?= $this->url(
name: 'user/login',
options: ['query' => ['redirect_to' => base64_encode($this->serverUrl(true))]],
) ?>">
<span class="fas fa-user"></span>
<?= $this->translate('Login') ?>
</a>

<?php
endif; ?>
<?php endif; ?>
</div>
</section>
15 changes: 15 additions & 0 deletions module/Application/view/error/debug/401.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

use Application\View\HelperTrait;
use Laminas\View\Renderer\PhpRenderer;

/** @var PhpRenderer|HelperTrait $this */
?>
<section class="section">
<div class="container">
<h1><?= $this->translate('403 Unauthenticated') ?></h1>
<h3><?= $this->exception->getMessage() ?></h3>
</div>
</section>
6 changes: 2 additions & 4 deletions module/Application/view/layout/layout.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,9 @@ $viewModel = current($this->viewModel()->getCurrent()->getChildren());

if (str_contains($viewModel->getTemplate(), 'admin')): ?>
<?= $this->partial('partial/admin.phtml', ['content' => $this->content]) ?>
<?php
elseif (str_contains($viewModel->getTemplate(), 'company-account')): ?>
<?php elseif (str_contains($viewModel->getTemplate(), 'company-account')): ?>
<?= $this->partial('partial/company.phtml', ['content' => $this->content]) ?>
<?php
else: ?>
<?php else: ?>
<div class="content-container">
<?= $this->content ?>
</div>
Expand Down
2 changes: 2 additions & 0 deletions module/Company/config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Doctrine\ORM\Mapping\Driver\AttributeDriver;
use Laminas\Router\Http\Literal;
use Laminas\Router\Http\Segment;
use User\Listener\Authentication;

return [
'router' => [
Expand Down Expand Up @@ -124,6 +125,7 @@
'route' => '/company',
'defaults' => [
'controller' => CompanyAccountController::class,
'auth_type' => Authentication::AUTH_COMPANY_USER,
],
],
'may_terminate' => false,
Expand Down
4 changes: 4 additions & 0 deletions module/Photo/config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Photo\Controller\PhotoAdminController;
use Photo\Controller\PhotoController;
use Photo\Controller\TagController;
use User\Listener\Authentication;

return [
'router' => [
Expand All @@ -31,6 +32,7 @@
'defaults' => [
'controller' => PhotoController::class,
'action' => 'index',
'auth_type' => Authentication::AUTH_USER,
],
],
'may_terminate' => true,
Expand Down Expand Up @@ -172,6 +174,7 @@
'defaults' => [
'controller' => AlbumAdminController::class,
'action' => 'index',
'auth_type' => Authentication::AUTH_USER,
],
],
'may_terminate' => true,
Expand Down Expand Up @@ -329,6 +332,7 @@
'defaults' => [
'controller' => ApiController::class,
'action' => 'index',
'auth_type' => Authentication::AUTH_API,
],
],
'may_terminate' => true,
Expand Down
5 changes: 5 additions & 0 deletions module/User/config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use User\Controller\Factory\UserControllerFactory;
use User\Controller\UserAdminController;
use User\Controller\UserController;
use User\Listener\Authentication;

return [
'router' => [
Expand Down Expand Up @@ -120,6 +121,9 @@
'type' => Literal::class,
'options' => [
'route' => '/admin/user',
'defaults' => [
'auth_type' => Authentication::AUTH_USER,
],
],
'may_terminate' => false,
'child_routes' => [
Expand Down Expand Up @@ -177,6 +181,7 @@
'defaults' => [
'controller' => ApiAuthenticationController::class,
'action' => 'token',
'auth_type' => Authentication::AUTH_USER,
],
],
'priority' => 100,
Expand Down
Loading
Loading