summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Exceptions/Handlers/Whoops.php11
-rw-r--r--src/Helpers/Authenticator.php56
-rw-r--r--src/Helpers/AuthenticatorServiceProvider.php17
-rw-r--r--src/Middleware/LegacyMiddleware.php11
-rw-r--r--src/Models/BaseModel.php2
-rw-r--r--src/Renderer/Twig/Extensions/Authentication.php26
-rw-r--r--src/Renderer/Twig/Extensions/Globals.php16
7 files changed, 128 insertions, 11 deletions
diff --git a/src/Exceptions/Handlers/Whoops.php b/src/Exceptions/Handlers/Whoops.php
index 630aca1d..72e97776 100644
--- a/src/Exceptions/Handlers/Whoops.php
+++ b/src/Exceptions/Handlers/Whoops.php
@@ -4,6 +4,7 @@ namespace Engelsystem\Exceptions\Handlers;
use Engelsystem\Application;
use Engelsystem\Container\Container;
+use Engelsystem\Helpers\Authenticator;
use Engelsystem\Http\Request;
use Throwable;
use Whoops\Handler\JsonResponseHandler;
@@ -81,9 +82,15 @@ class Whoops extends Legacy implements HandlerInterface
*/
protected function getData()
{
- global $user;
-
$data = [];
+ $user = null;
+
+ if ($this->app->has('authenticator')) {
+ /** @var Authenticator $authenticator */
+ $authenticator = $this->app->get('authenticator');
+ $user = $authenticator->user();
+ }
+
$data['user'] = $user;
$data['Booted'] = $this->app->isBooted();
diff --git a/src/Helpers/Authenticator.php b/src/Helpers/Authenticator.php
new file mode 100644
index 00000000..eee7b965
--- /dev/null
+++ b/src/Helpers/Authenticator.php
@@ -0,0 +1,56 @@
+<?php
+
+namespace Engelsystem\Helpers;
+
+use Engelsystem\Models\BaseModel;
+use Engelsystem\Models\User\User;
+use Engelsystem\Models\User\User as UserRepository;
+use Symfony\Component\HttpFoundation\Session\Session;
+
+class Authenticator
+{
+ /** @var UserRepository */
+ protected $user = null;
+
+ /** @var Session */
+ protected $session;
+
+ /** @var BaseModel */
+ protected $userRepository;
+
+ /**
+ * @param Session $session
+ * @param UserRepository $userRepository
+ */
+ public function __construct(Session $session, UserRepository $userRepository)
+ {
+ $this->session = $session;
+ $this->userRepository = $userRepository;
+ }
+
+ /**
+ * @return User|null
+ */
+ public function user()
+ {
+ if ($this->user) {
+ return $this->user;
+ }
+
+ $userId = $this->session->get('uid');
+ if (!$userId) {
+ return null;
+ }
+
+ $user = $this
+ ->userRepository
+ ->find($userId);
+ if (!$user) {
+ return null;
+ }
+
+ $this->user = $user;
+
+ return $user;
+ }
+}
diff --git a/src/Helpers/AuthenticatorServiceProvider.php b/src/Helpers/AuthenticatorServiceProvider.php
new file mode 100644
index 00000000..b7508b01
--- /dev/null
+++ b/src/Helpers/AuthenticatorServiceProvider.php
@@ -0,0 +1,17 @@
+<?php
+
+namespace Engelsystem\Helpers;
+
+use Engelsystem\Container\ServiceProvider;
+
+class AuthenticatorServiceProvider extends ServiceProvider
+{
+ public function register()
+ {
+ /** @var Authenticator $authenticator */
+ $authenticator = $this->app->make(Authenticator::class);
+
+ $this->app->instance(Authenticator::class, $authenticator);
+ $this->app->instance('authenticator', $authenticator);
+ }
+}
diff --git a/src/Middleware/LegacyMiddleware.php b/src/Middleware/LegacyMiddleware.php
index 4f61ad32..ce1eadef 100644
--- a/src/Middleware/LegacyMiddleware.php
+++ b/src/Middleware/LegacyMiddleware.php
@@ -2,6 +2,7 @@
namespace Engelsystem\Middleware;
+use Engelsystem\Helpers\Authenticator;
use Engelsystem\Helpers\Translator;
use Engelsystem\Http\Request;
use Engelsystem\Http\Response;
@@ -35,12 +36,17 @@ class LegacyMiddleware implements MiddlewareInterface
/** @var ContainerInterface */
protected $container;
+ /** @var Authenticator */
+ protected $auth;
+
/**
* @param ContainerInterface $container
+ * @param Authenticator $auth
*/
- public function __construct(ContainerInterface $container)
+ public function __construct(ContainerInterface $container, Authenticator $auth)
{
$this->container = $container;
+ $this->auth = $auth;
}
/**
@@ -56,7 +62,6 @@ class LegacyMiddleware implements MiddlewareInterface
ServerRequestInterface $request,
RequestHandlerInterface $handler
): ResponseInterface {
- global $user;
global $privileges;
global $page;
@@ -68,7 +73,7 @@ class LegacyMiddleware implements MiddlewareInterface
$page = str_replace('-', '_', $page);
}
if ($page == '/') {
- $page = isset($user) ? 'news' : 'login';
+ $page = $this->auth->user() ? 'news' : 'login';
}
$title = $content = '';
diff --git a/src/Models/BaseModel.php b/src/Models/BaseModel.php
index 2367f5b6..49255905 100644
--- a/src/Models/BaseModel.php
+++ b/src/Models/BaseModel.php
@@ -30,7 +30,7 @@ abstract class BaseModel extends Model
*
* @param mixed $id
* @param array $columns
- * @return Builder|Builder[]|Collection|Model|null
+ * @return Builder|Builder[]|Collection|static|null
*/
public static function find($id, $columns = ['*'])
{
diff --git a/src/Renderer/Twig/Extensions/Authentication.php b/src/Renderer/Twig/Extensions/Authentication.php
index 6a72d825..20ede828 100644
--- a/src/Renderer/Twig/Extensions/Authentication.php
+++ b/src/Renderer/Twig/Extensions/Authentication.php
@@ -2,11 +2,23 @@
namespace Engelsystem\Renderer\Twig\Extensions;
+use Engelsystem\Helpers\Authenticator;
use Twig_Extension as TwigExtension;
use Twig_Function as TwigFunction;
class Authentication extends TwigExtension
{
+ /** @var Authenticator */
+ protected $auth;
+
+ /**
+ * @param Authenticator $auth
+ */
+ public function __construct(Authenticator $auth)
+ {
+ $this->auth = $auth;
+ }
+
/**
* @return TwigFunction[]
*/
@@ -19,18 +31,26 @@ class Authentication extends TwigExtension
];
}
+ /**
+ * @return bool
+ */
public function isAuthenticated()
{
- global $user;
-
- return !empty($user);
+ return (bool)$this->auth->user();
}
+ /**
+ * @return bool
+ */
public function isGuest()
{
return !$this->isAuthenticated();
}
+ /**
+ * @param $privilege
+ * @return bool
+ */
public function checkAuth($privilege)
{
global $privileges;
diff --git a/src/Renderer/Twig/Extensions/Globals.php b/src/Renderer/Twig/Extensions/Globals.php
index f9bffbc8..ef29a819 100644
--- a/src/Renderer/Twig/Extensions/Globals.php
+++ b/src/Renderer/Twig/Extensions/Globals.php
@@ -2,11 +2,23 @@
namespace Engelsystem\Renderer\Twig\Extensions;
+use Engelsystem\Helpers\Authenticator;
use Twig_Extension as TwigExtension;
use Twig_Extension_GlobalsInterface as GlobalsInterface;
class Globals extends TwigExtension implements GlobalsInterface
{
+ /** @var Authenticator */
+ protected $auth;
+
+ /**
+ * @param Authenticator $auth
+ */
+ public function __construct(Authenticator $auth)
+ {
+ $this->auth = $auth;
+ }
+
/**
* Returns a list of global variables to add to the existing list.
*
@@ -14,10 +26,10 @@ class Globals extends TwigExtension implements GlobalsInterface
*/
public function getGlobals()
{
- global $user;
+ $user = $this->auth->user();
return [
- 'user' => isset($user) ? $user : [],
+ 'user' => $user ? $user : [],
];
}
}