V-Beta-1.0.0
Vision is out of alpha !
This commit is contained in:
117
src/EventSubscriber/DatabaseActivitySubscriber.php
Normal file
117
src/EventSubscriber/DatabaseActivitySubscriber.php
Normal file
@@ -0,0 +1,117 @@
|
||||
<?php
|
||||
|
||||
namespace App\EventSubscriber;
|
||||
|
||||
use Twig\Environment;
|
||||
use App\Entity\Comment;
|
||||
use App\Entity\Notification;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Doctrine\Persistence\Event\LifecycleEventArgs;
|
||||
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
|
||||
|
||||
class DatabaseActivitySubscriber
|
||||
{
|
||||
protected TokenStorageInterface $tokenStorage;
|
||||
protected EntityManagerInterface $entityManager;
|
||||
protected Environment $twig;
|
||||
|
||||
public function __construct(
|
||||
TokenStorageInterface $tokenStorage,
|
||||
EntityManagerInterface $entityManager,
|
||||
Environment $twig
|
||||
) {
|
||||
$this->tokenStorage = $tokenStorage;
|
||||
$this->entityManager = $entityManager;
|
||||
$this->twig = $twig;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param LifecycleEventArgs $args
|
||||
*/
|
||||
public function postPersist(LifecycleEventArgs $args): void
|
||||
{
|
||||
$this->notify('postPersist', $args);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param LifecycleEventArgs $args
|
||||
*/
|
||||
public function postRemove(LifecycleEventArgs $args): void
|
||||
{
|
||||
$this->notify('postRemove', $args);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param LifecycleEventArgs $args
|
||||
*/
|
||||
public function postUpdate(LifecycleEventArgs $args): void
|
||||
{
|
||||
$this->notify('postUpdate', $args);
|
||||
}
|
||||
|
||||
private function notify(string $action, LifecycleEventArgs $args): void
|
||||
{
|
||||
$entity = $args->getObject();
|
||||
|
||||
if (
|
||||
is_subclass_of($entity, 'App\Entity\Document')
|
||||
&& method_exists($entity, 'getCreator')
|
||||
&& in_array($action, ['postUpdate', 'postRemove'])
|
||||
) {
|
||||
$sender = $this->tokenStorage->getToken()->getUser();
|
||||
$receiver = $entity->getCreator();
|
||||
if ($receiver != $sender) {
|
||||
$htmlContents = $this->twig->render('_notifications/document_' . $action . '.html.twig', [
|
||||
'sender' => $sender,
|
||||
'document' => $entity
|
||||
]);
|
||||
|
||||
|
||||
$notification = new Notification(
|
||||
$receiver,
|
||||
$htmlContents,
|
||||
$sender,
|
||||
'notification_icon_document_' . $action
|
||||
);
|
||||
|
||||
$this->entityManager->persist($notification);
|
||||
try {
|
||||
$this->entityManager->flush();
|
||||
} catch (\Throwable $th) {
|
||||
throw $th;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
$entity instanceof Comment
|
||||
&& method_exists($entity, 'getDocument')
|
||||
&& in_array($action, ['postUpdate', 'postRemove', 'postPersist'])
|
||||
) {
|
||||
$sender = $this->tokenStorage->getToken()->getUser();
|
||||
$receiver = $entity->getDocument()->getCreator();
|
||||
|
||||
if ($receiver != $sender) {
|
||||
$htmlContents = $this->twig->render('_notifications/comment_' . $action . '.html.twig', [
|
||||
'sender' => $sender,
|
||||
'document' => $entity->getDocument()
|
||||
]);
|
||||
|
||||
|
||||
$notification = new Notification(
|
||||
$receiver,
|
||||
$htmlContents,
|
||||
$sender,
|
||||
'notification_icon_comment_' . $action
|
||||
);
|
||||
|
||||
$this->entityManager->persist($notification);
|
||||
try {
|
||||
$this->entityManager->flush();
|
||||
} catch (\Throwable $th) {
|
||||
throw $th;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
54
src/EventSubscriber/LocaleSubscriber.php
Normal file
54
src/EventSubscriber/LocaleSubscriber.php
Normal file
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace App\EventSubscriber;
|
||||
|
||||
use Symfony\Component\HttpKernel\KernelEvents;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
use Symfony\Component\Security\Http\SecurityEvents;
|
||||
use Symfony\Component\HttpKernel\Event\RequestEvent;
|
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
|
||||
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
|
||||
|
||||
class LocaleSubscriber implements EventSubscriberInterface
|
||||
{
|
||||
private $defaultLocale;
|
||||
private $requestStack;
|
||||
|
||||
public function __construct(RequestStack $requestStack, ParameterBagInterface $params)
|
||||
{
|
||||
$this->defaultLocale = $params->get('kernel.default_locale');
|
||||
$this->requestStack = $requestStack;
|
||||
}
|
||||
|
||||
public function onKernelRequest(RequestEvent $event)
|
||||
{
|
||||
$request = $event->getRequest();
|
||||
if (!$request->hasPreviousSession()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$request->setLocale($request->getSession()->get('_locale', $this->defaultLocale));
|
||||
}
|
||||
|
||||
public function onInteractiveLogin(InteractiveLoginEvent $event)
|
||||
{
|
||||
/**
|
||||
* @var user $user
|
||||
*/
|
||||
$user = $event->getAuthenticationToken()->getUser();
|
||||
|
||||
if (null !== $user->getLocale()) {
|
||||
$this->requestStack->getSession()->set('_locale', $user->getLocale());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static function getSubscribedEvents()
|
||||
{
|
||||
return [
|
||||
KernelEvents::REQUEST => [['onKernelRequest', 20]],
|
||||
SecurityEvents::INTERACTIVE_LOGIN => 'onInteractiveLogin',
|
||||
];
|
||||
}
|
||||
}
|
||||
83
src/EventSubscriber/NotificationsSubscriber.php
Normal file
83
src/EventSubscriber/NotificationsSubscriber.php
Normal file
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
namespace App\EventSubscriber;
|
||||
|
||||
use App\Entity\User;
|
||||
use Twig\Environment;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use App\Repository\NotificationRepository;
|
||||
use Symfony\Component\HttpKernel\Event\ControllerEvent;
|
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
|
||||
|
||||
class NotificationsSubscriber implements EventSubscriberInterface
|
||||
{
|
||||
private Environment $twig;
|
||||
private TokenStorageInterface $tokenStorage;
|
||||
private EntityManagerInterface $entityManager;
|
||||
|
||||
public function __construct(
|
||||
Environment $twig,
|
||||
TokenStorageInterface $tokenStorage = null,
|
||||
EntityManagerInterface $entityManager
|
||||
) {
|
||||
$this->twig = $twig;
|
||||
$this->tokenStorage = $tokenStorage;
|
||||
$this->entityManager = $entityManager;
|
||||
}
|
||||
|
||||
public function onKernelController(ControllerEvent $event)
|
||||
{
|
||||
if (null === $this->tokenStorage) {
|
||||
return;
|
||||
}
|
||||
|
||||
$token = $this->tokenStorage->getToken();
|
||||
|
||||
if (null === $token) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @var NotificationRepository notificationRepo
|
||||
*/
|
||||
|
||||
if (null === ($notificationRepo = $this->entityManager->getRepository("App:Notification"))) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @var User $User
|
||||
*/
|
||||
$User = $token->getUser();
|
||||
|
||||
if (null === $User) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->twig->addGlobal(
|
||||
'user_notifications_list',
|
||||
$notificationRepo->listForUser($User)
|
||||
->readed(false)
|
||||
->limit(5)
|
||||
->order(['createdAt' => 'DESC'])
|
||||
->getResult()
|
||||
);
|
||||
|
||||
$this->twig->addGlobal(
|
||||
'user_notifications_count',
|
||||
$notificationRepo->listForUser($User)
|
||||
->readed(false)
|
||||
->limit(5)
|
||||
->order(['createdAt' => 'DESC'])
|
||||
->getCount()
|
||||
);
|
||||
}
|
||||
|
||||
public static function getSubscribedEvents()
|
||||
{
|
||||
return [
|
||||
'kernel.controller' => 'onKernelController',
|
||||
];
|
||||
}
|
||||
}
|
||||
64
src/EventSubscriber/VisionLoggerSubscriber.php
Normal file
64
src/EventSubscriber/VisionLoggerSubscriber.php
Normal file
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
namespace App\EventSubscriber;
|
||||
|
||||
use Gedmo\Loggable\LoggableListener;
|
||||
use Symfony\Component\HttpKernel\KernelEvents;
|
||||
use Symfony\Component\HttpKernel\Event\RequestEvent;
|
||||
use Symfony\Component\HttpKernel\HttpKernelInterface;
|
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
|
||||
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
|
||||
|
||||
class VisionLoggerSubscriber implements EventSubscriberInterface
|
||||
{
|
||||
private AuthorizationCheckerInterface $authorizationChecker;
|
||||
private TokenStorageInterface $tokenStorage;
|
||||
private LoggableListener $loggableListener;
|
||||
|
||||
public function __construct(
|
||||
LoggableListener $loggableListener,
|
||||
TokenStorageInterface $tokenStorage = null,
|
||||
AuthorizationCheckerInterface $authorizationChecker = null
|
||||
) {
|
||||
$this->loggableListener = $loggableListener;
|
||||
$this->tokenStorage = $tokenStorage;
|
||||
$this->authorizationChecker = $authorizationChecker;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
public function onKernelRequest(RequestEvent $event)
|
||||
{
|
||||
|
||||
|
||||
|
||||
if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (null === $this->tokenStorage || null === $this->authorizationChecker) {
|
||||
return;
|
||||
}
|
||||
|
||||
$token = $this->tokenStorage->getToken();
|
||||
|
||||
if (null !== $token && $this->authorizationChecker->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
|
||||
|
||||
/**
|
||||
* @var User $User
|
||||
*/
|
||||
$User = $token->getUser();
|
||||
|
||||
$this->loggableListener->setUsername($User->getFullname() . ', ' . $User->getMainGroup()->getName());
|
||||
}
|
||||
}
|
||||
|
||||
public static function getSubscribedEvents()
|
||||
{
|
||||
return array(
|
||||
KernelEvents::REQUEST => 'onKernelRequest',
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user