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