[COMMON][SECURITY][WRAPPER] Added security service static wrapper and Common::getUser

This commit is contained in:
Hugo Sales 2020-07-23 23:33:00 +00:00 committed by Hugo Sales
parent f812d9142f
commit 0af82054ff
3 changed files with 62 additions and 2 deletions

View File

@ -58,6 +58,7 @@ use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents; use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Messenger\MessageBusInterface; use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Routing\RouterInterface; use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Security as SSecurity;
use Symfony\Component\Security\Http\Util\TargetPathTrait; use Symfony\Component\Security\Http\Util\TargetPathTrait;
use Symfony\Contracts\Translation\TranslatorInterface; use Symfony\Contracts\Translation\TranslatorInterface;
@ -73,6 +74,7 @@ class GNUsocial implements EventSubscriberInterface
protected MessageBusInterface $message_bus; protected MessageBusInterface $message_bus;
protected EventDispatcherInterface $event_dispatcher; protected EventDispatcherInterface $event_dispatcher;
protected SessionInterface $session; protected SessionInterface $session;
protected SSecurity $security;
/** /**
* Symfony dependency injection gives us access to these services * Symfony dependency injection gives us access to these services
@ -84,7 +86,8 @@ class GNUsocial implements EventSubscriberInterface
FormFactoryInterface $ff, FormFactoryInterface $ff,
MessageBusInterface $mb, MessageBusInterface $mb,
EventDispatcherInterface $ed, EventDispatcherInterface $ed,
SessionInterface $s) SessionInterface $sess,
SSecurity $sec)
{ {
$this->logger = $logger; $this->logger = $logger;
$this->translator = $trans; $this->translator = $trans;
@ -93,7 +96,8 @@ class GNUsocial implements EventSubscriberInterface
$this->form_factory = $ff; $this->form_factory = $ff;
$this->message_bus = $mb; $this->message_bus = $mb;
$this->event_dispatcher = $ed; $this->event_dispatcher = $ed;
$this->session = $s; $this->session = $sess;
$this->security = $sec;
$this->register(); $this->register();
} }
@ -112,6 +116,7 @@ class GNUsocial implements EventSubscriberInterface
Router::setRouter($this->router); Router::setRouter($this->router);
Form::setFactory($this->form_factory); Form::setFactory($this->form_factory);
Queue::setMessageBus($this->message_bus); Queue::setMessageBus($this->message_bus);
Security::setHelper($this->security);
DefaultSettings::setDefaults(); DefaultSettings::setDefaults();
Cache::setupCache(); Cache::setupCache();

48
src/Core/Security.php Normal file
View File

@ -0,0 +1,48 @@
<?php
// {{{ License
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU social is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
// }}}
/**
* Wrapper around Symfony's Security service, for static access
*
* @package GNUsocial
* @category Security
*
* @author Hugo Sales <hugo@fc.up.pt>
* @copyright 2020 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
namespace App\Core;
use Symfony\Component\Security\Core\Security as SSecurity;
abstract class Security
{
private static ?SSecurity $security;
public static function setHelper($s): void
{
self::$security = $s;
}
public static function __callStatic(string $name, array $args)
{
return self::$security->{$name}(...$args);
}
}

View File

@ -32,6 +32,8 @@ namespace App\Util;
use App\Core\DB\DB; use App\Core\DB\DB;
use App\Core\Router; use App\Core\Router;
use App\Core\Security;
use Symfony\Component\Security\Core\User\UserInterface;
abstract class Common abstract class Common
{ {
@ -59,6 +61,11 @@ abstract class Common
DB::flush(); DB::flush();
} }
public static function user(): UserInterface
{
return Security::getUser();
}
/** /**
* Is the given string identical to a system path or route? * Is the given string identical to a system path or route?
* This could probably be put in some other class, but at * This could probably be put in some other class, but at