From c0da90bd3e356a4c6cb21b5d25bc04d432ee730b Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Thu, 23 Jul 2020 23:33:00 +0000 Subject: [PATCH] [COMMON][SECURITY][WRAPPER] Added security service static wrapper and Common::getUser --- src/Core/GNUsocial.php | 9 ++++++-- src/Core/Security.php | 48 ++++++++++++++++++++++++++++++++++++++++++ src/Util/Common.php | 7 ++++++ 3 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 src/Core/Security.php diff --git a/src/Core/GNUsocial.php b/src/Core/GNUsocial.php index 5b3ce0544d..a71213d963 100644 --- a/src/Core/GNUsocial.php +++ b/src/Core/GNUsocial.php @@ -58,6 +58,7 @@ use Symfony\Component\HttpKernel\Event\RequestEvent; use Symfony\Component\HttpKernel\KernelEvents; use Symfony\Component\Messenger\MessageBusInterface; use Symfony\Component\Routing\RouterInterface; +use Symfony\Component\Security\Core\Security as SSecurity; use Symfony\Component\Security\Http\Util\TargetPathTrait; use Symfony\Contracts\Translation\TranslatorInterface; @@ -73,6 +74,7 @@ class GNUsocial implements EventSubscriberInterface protected MessageBusInterface $message_bus; protected EventDispatcherInterface $event_dispatcher; protected SessionInterface $session; + protected SSecurity $security; /** * Symfony dependency injection gives us access to these services @@ -84,7 +86,8 @@ class GNUsocial implements EventSubscriberInterface FormFactoryInterface $ff, MessageBusInterface $mb, EventDispatcherInterface $ed, - SessionInterface $s) + SessionInterface $sess, + SSecurity $sec) { $this->logger = $logger; $this->translator = $trans; @@ -93,7 +96,8 @@ class GNUsocial implements EventSubscriberInterface $this->form_factory = $ff; $this->message_bus = $mb; $this->event_dispatcher = $ed; - $this->session = $s; + $this->session = $sess; + $this->security = $sec; $this->register(); } @@ -112,6 +116,7 @@ class GNUsocial implements EventSubscriberInterface Router::setRouter($this->router); Form::setFactory($this->form_factory); Queue::setMessageBus($this->message_bus); + Security::setHelper($this->security); DefaultSettings::setDefaults(); Cache::setupCache(); diff --git a/src/Core/Security.php b/src/Core/Security.php new file mode 100644 index 0000000000..b5530778f9 --- /dev/null +++ b/src/Core/Security.php @@ -0,0 +1,48 @@ +. +// }}} + +/** + * Wrapper around Symfony's Security service, for static access + * + * @package GNUsocial + * @category Security + * + * @author Hugo Sales + * @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); + } +} diff --git a/src/Util/Common.php b/src/Util/Common.php index f4dac1c4a9..94474e98b8 100644 --- a/src/Util/Common.php +++ b/src/Util/Common.php @@ -32,6 +32,8 @@ namespace App\Util; use App\Core\DB\DB; use App\Core\Router; +use App\Core\Security; +use Symfony\Component\Security\Core\User\UserInterface; abstract class Common { @@ -59,6 +61,11 @@ abstract class Common DB::flush(); } + public static function user(): UserInterface + { + return Security::getUser(); + } + /** * Is the given string identical to a system path or route? * This could probably be put in some other class, but at