2018-02-01 12:49:40 +01:00
|
|
|
<?php
|
2020-10-15 00:19:31 +02:00
|
|
|
|
2018-08-05 00:05:14 +02:00
|
|
|
namespace App\Middleware;
|
2018-02-01 12:49:40 +01:00
|
|
|
|
2021-01-11 12:08:06 +01:00
|
|
|
use App\Acl;
|
2018-02-01 12:49:40 +01:00
|
|
|
use App\Auth;
|
2018-08-05 00:05:14 +02:00
|
|
|
use App\Customization;
|
2020-07-10 06:15:27 +02:00
|
|
|
use App\Entity;
|
2019-08-09 17:00:21 +02:00
|
|
|
use App\Http\ServerRequest;
|
2020-12-10 23:46:03 +01:00
|
|
|
use DI\FactoryInterface;
|
2019-08-07 06:33:55 +02:00
|
|
|
use Psr\Http\Message\ResponseInterface;
|
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
|
|
use Psr\Http\Server\MiddlewareInterface;
|
|
|
|
use Psr\Http\Server\RequestHandlerInterface;
|
2018-02-01 12:49:40 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the current user entity object and assign it into the request if it exists.
|
|
|
|
*/
|
2019-08-07 06:33:55 +02:00
|
|
|
class GetCurrentUser implements MiddlewareInterface
|
2018-02-01 12:49:40 +01:00
|
|
|
{
|
2020-12-10 23:46:03 +01:00
|
|
|
protected FactoryInterface $factory;
|
2018-02-01 12:49:40 +01:00
|
|
|
|
2020-12-10 23:46:03 +01:00
|
|
|
public function __construct(FactoryInterface $factory)
|
|
|
|
{
|
|
|
|
$this->factory = $factory;
|
2018-02-01 12:49:40 +01:00
|
|
|
}
|
|
|
|
|
2019-08-07 06:33:55 +02:00
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
|
2018-02-01 12:49:40 +01:00
|
|
|
{
|
2020-07-10 06:15:27 +02:00
|
|
|
// Initialize the Auth for this request.
|
2020-12-10 23:46:03 +01:00
|
|
|
$auth = $this->factory->make(
|
|
|
|
Auth::class,
|
|
|
|
[
|
|
|
|
'session' => $request->getAttribute(ServerRequest::ATTR_SESSION),
|
|
|
|
]
|
2020-12-03 05:18:06 +01:00
|
|
|
);
|
2020-07-10 06:15:27 +02:00
|
|
|
$user = ($auth->isLoggedIn()) ? $auth->getLoggedInUser() : null;
|
|
|
|
|
|
|
|
$request = $request
|
|
|
|
->withAttribute(ServerRequest::ATTR_AUTH, $auth)
|
|
|
|
->withAttribute(ServerRequest::ATTR_USER, $user)
|
|
|
|
->withAttribute('is_logged_in', (null !== $user));
|
2019-09-24 04:22:08 +02:00
|
|
|
|
2020-07-10 06:15:27 +02:00
|
|
|
// Initialize Customization (timezones, locales, etc) based on the current logged in user.
|
2021-02-28 03:50:45 +01:00
|
|
|
|
|
|
|
/** @var Customization $customization */
|
2020-12-10 23:46:03 +01:00
|
|
|
$customization = $this->factory->make(
|
|
|
|
Customization::class,
|
|
|
|
[
|
|
|
|
'request' => $request,
|
|
|
|
]
|
|
|
|
);
|
2018-02-01 12:49:40 +01:00
|
|
|
|
2021-01-11 12:08:06 +01:00
|
|
|
// Initialize ACL (can only be initialized after Customization as it contains localizations).
|
2021-02-28 03:50:45 +01:00
|
|
|
|
|
|
|
/** @var Acl $acl */
|
2021-01-11 12:08:06 +01:00
|
|
|
$acl = $this->factory->make(
|
|
|
|
Acl::class,
|
|
|
|
[
|
|
|
|
'user' => $user,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2020-07-10 06:15:27 +02:00
|
|
|
$request = $request
|
2021-02-28 03:50:45 +01:00
|
|
|
->withAttribute(ServerRequest::ATTR_LOCALE, $customization->getLocale())
|
2021-01-11 12:08:06 +01:00
|
|
|
->withAttribute(ServerRequest::ATTR_CUSTOMIZATION, $customization)
|
|
|
|
->withAttribute(ServerRequest::ATTR_ACL, $acl);
|
2018-02-01 12:49:40 +01:00
|
|
|
|
2019-08-15 01:50:53 +02:00
|
|
|
// Set the Audit Log user.
|
2020-07-10 06:15:27 +02:00
|
|
|
Entity\AuditLog::setCurrentUser($user);
|
2019-08-15 01:50:53 +02:00
|
|
|
|
2020-07-10 06:15:27 +02:00
|
|
|
$response = $handler->handle($request);
|
|
|
|
|
|
|
|
Entity\AuditLog::setCurrentUser(null);
|
2018-02-01 12:49:40 +01:00
|
|
|
|
2020-07-10 06:15:27 +02:00
|
|
|
return $response;
|
2018-02-01 12:49:40 +01:00
|
|
|
}
|
2018-07-04 00:51:05 +02:00
|
|
|
}
|