Skip to content

Commit

Permalink
Internal: Refactoring event listeners - refs BT#21561
Browse files Browse the repository at this point in the history
  • Loading branch information
AngelFQC committed Apr 24, 2024
1 parent 70a28f4 commit 009cec6
Show file tree
Hide file tree
Showing 22 changed files with 63 additions and 237 deletions.
4 changes: 2 additions & 2 deletions config/packages/security.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ security:
ROLE_STUDENT_BOSS: [ROLE_STUDENT]
ROLE_INVITEE: [ROLE_STUDENT]

ROLE_CURRENT_COURSE_STUDENT: [ROLE_CURRENT_COURSE_STUDENT] # Set in the CourseListener
ROLE_CURRENT_COURSE_STUDENT: [ROLE_CURRENT_COURSE_STUDENT] # Set in the CidReqListener
ROLE_CURRENT_COURSE_TEACHER: [ROLE_CURRENT_COURSE_TEACHER, ROLE_CURRENT_COURSE_STUDENT] # Set in the course listener
ROLE_CURRENT_COURSE_GROUP_STUDENT: [ROLE_CURRENT_COURSE_GROUP_STUDENT] # Set in the CourseListener
ROLE_CURRENT_COURSE_GROUP_STUDENT: [ROLE_CURRENT_COURSE_GROUP_STUDENT] # Set in the CidReqListener
ROLE_CURRENT_COURSE_GROUP_TEACHER: [ROLE_CURRENT_COURSE_GROUP_TEACHER, ROLE_CURRENT_COURSE_GROUP_STUDENT]
ROLE_CURRENT_COURSE_SESSION_STUDENT: [ROLE_CURRENT_COURSE_SESSION_STUDENT]
ROLE_CURRENT_COURSE_SESSION_TEACHER: [ROLE_CURRENT_COURSE_SESSION_STUDENT, ROLE_CURRENT_COURSE_SESSION_TEACHER]
Expand Down
2 changes: 1 addition & 1 deletion public/main/admin/course_edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@
$course = $form->getSubmitValues();
$visibility = $course['visibility'];

// @todo should be check in the CourseListener
// @todo should be check in the CidReqListener
/*global $_configuration;
if (isset($_configuration[$urlId]) &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private function addWhere(QueryBuilder $queryBuilder, string $resourceClass): vo
$request = $this->requestStack->getCurrentRequest();

// Listing documents must contain the resource node parent (resourceNode.parent) and the course (cid)
// At least the cid so the CourseListener can be called.
// At least the cid so the CidReqListener can be called.
$resourceParentId = $request->query->get('resourceNode_parent');
$courseId = $request->query->getInt('cid');

Expand Down
4 changes: 2 additions & 2 deletions src/CoreBundle/Entity/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -910,15 +910,15 @@ public function addUserInCourse(int $status, User $user, Course $course): Sessio
}

/**
* currentCourse is set in CourseListener.
* currentCourse is set in CidReqListener.
*/
public function getCurrentCourse(): ?Course
{
return $this->currentCourse;
}

/**
* currentCourse is set in CourseListener.
* currentCourse is set in CidReqListener.
*/
public function setCurrentCourse(Course $course): self
{
Expand Down
13 changes: 2 additions & 11 deletions src/CoreBundle/EventListener/AssetListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@

use Chamilo\CoreBundle\Entity\Asset;
use Chamilo\CoreBundle\Repository\AssetRepository;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Vich\UploaderBundle\Event\Event;

class AssetListener implements EventSubscriberInterface
class AssetListener
{
protected AssetRepository $assetRepository;

Expand All @@ -20,7 +19,7 @@ public function __construct(AssetRepository $assetRepository)
$this->assetRepository = $assetRepository;
}

public function onVichUploaderPostRemove(Event $event): void
public function __invoke(Event $event): void
{
/** @var Asset $asset */
$asset = $event->getObject();
Expand All @@ -36,12 +35,4 @@ public function onVichUploaderPostRemove(Event $event): void
}*/
}
}

/**
* @return array<string, mixed>
*/
public static function getSubscribedEvents(): array
{
return ['vich_uploader.post_remove' => 'onVichUploaderPostRemove'];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,10 @@
use Chamilo\CourseBundle\Entity\CGroup;
use ChamiloSession;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
Expand All @@ -33,17 +30,16 @@
use Twig\Environment;

/**
* Class CourseListener.
* Sets the course and session objects in the controller that implements the CourseControllerInterface.
*/
class CourseListener implements EventSubscriberInterface
class CidReqListener
{
public function __construct(
private readonly Environment $twig,
private readonly AuthorizationCheckerInterface $authorizationChecker,
private readonly TranslatorInterface $translator,
private readonly EntityManagerInterface $entityManager,
private TokenStorageInterface $tokenStorage,
private readonly TokenStorageInterface $tokenStorage,
) {}

/**
Expand Down Expand Up @@ -197,8 +193,6 @@ public function onKernelRequest(RequestEvent $event): void
}
}

public function onKernelResponse(ResponseEvent $event): void {}

/**
* Once the onKernelRequest was fired, we check if the course/session object were set and we inject them in the controller.
*/
Expand Down Expand Up @@ -314,16 +308,4 @@ private function generateCourseUrl(?Course $course, int $sessionId, int $groupId

return '';
}

/**
* @return array<string, mixed>
*/
public static function getSubscribedEvents(): array
{
return [
KernelEvents::REQUEST => ['onKernelRequest', 6],
KernelEvents::RESPONSE => 'onKernelResponse',
KernelEvents::CONTROLLER => 'onKernelController',
];
}
}
28 changes: 7 additions & 21 deletions src/CoreBundle/EventListener/CourseAccessListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,25 @@
use Chamilo\CoreBundle\Entity\TrackECourseAccess;
use Chamilo\CourseBundle\Event\CourseAccess;
use Doctrine\ORM\EntityManager;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;

/**
* In and outs of a course
* This listeners is always called when user enters the course home.
* This listener is always called when user enters the course home.
*/
class CourseAccessListener implements EventSubscriberInterface
class CourseAccessListener
{
protected EntityManager $em;

protected ?Request $request = null;

public function __construct(EntityManager $em)
{
$this->em = $em;
}

public function setRequest(RequestStack $requestStack): void
{
public function __construct(
private readonly EntityManager $em,
RequestStack $requestStack
) {
$this->request = $requestStack->getCurrentRequest();
}

public function onCourseAccessEvent(CourseAccess $event): void
public function __invoke(CourseAccess $event): void
{
// CourseAccess
$user = $event->getUser();
Expand All @@ -51,12 +45,4 @@ public function onCourseAccessEvent(CourseAccess $event): void
$this->em->persist($access);
$this->em->flush();
}

/**
* @return array<string, mixed>
*/
public static function getSubscribedEvents(): array
{
return ['chamilo_course.course.access' => 'onCourseAccessEvent'];
}
}
14 changes: 2 additions & 12 deletions src/CoreBundle/EventListener/ExceptionListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,15 @@
namespace Chamilo\CoreBundle\EventListener;

use Chamilo\CoreBundle\Exception\NotAllowedException;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Twig\Environment;

class ExceptionListener implements EventSubscriberInterface
class ExceptionListener
{
protected Environment $twig;
protected TokenStorageInterface $tokenStorage;
Expand All @@ -30,7 +28,7 @@ public function __construct(Environment $twig, TokenStorageInterface $tokenStora
$this->router = $router;
}

public function onKernelException(ExceptionEvent $event): void
public function __invoke(ExceptionEvent $event): void
{
// You get the exception object from the received event
$exception = $event->getThrowable();
Expand Down Expand Up @@ -80,12 +78,4 @@ public function onKernelException(ExceptionEvent $event): void
// sends the modified response object to the event
$event->setResponse($response);
}

/**
* @return array<string, mixed>
*/
public static function getSubscribedEvents(): array
{
return [KernelEvents::EXCEPTION => 'onKernelException'];
}
}
14 changes: 2 additions & 12 deletions src/CoreBundle/EventListener/HTTPExceptionListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@

namespace Chamilo\CoreBundle\EventListener;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\KernelEvents;

final class HTTPExceptionListener implements EventSubscriberInterface
final class HTTPExceptionListener
{
public function onKernelException(ExceptionEvent $event): void
public function __invoke(ExceptionEvent $event): void
{
$exception = $event->getThrowable();
if (!($exception instanceof HttpException)
Expand All @@ -30,12 +28,4 @@ public function onKernelException(ExceptionEvent $event): void

$event->setResponse($response);
}

/**
* @return array<string, mixed>
*/
public static function getSubscribedEvents(): array
{
return [KernelEvents::EXCEPTION => 'onKernelException'];
}
}
27 changes: 3 additions & 24 deletions src/CoreBundle/EventListener/LegacyListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,9 @@
use Chamilo\CoreBundle\Repository\Node\AccessUrlRepository;
use Chamilo\CoreBundle\Settings\SettingsManager;
use Exception;
use Symfony\Bundle\FrameworkBundle\Routing\Router;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
Expand All @@ -29,7 +24,7 @@
* Works as old global.inc.php
* Setting old php requirements so pages inside main/* could work correctly.
*/
class LegacyListener implements EventSubscriberInterface
class LegacyListener
{
public function __construct(
private readonly Environment $twig,
Expand All @@ -41,7 +36,7 @@ public function __construct(
private readonly ContainerInterface $container,

Check failure on line 36 in src/CoreBundle/EventListener/LegacyListener.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 Test on ubuntu-latest

ContainerDependency

src/CoreBundle/EventListener/LegacyListener.php:36:9: ContainerDependency: Container must not inject into services as dependency! Use dependency-injection.
) {}

public function onKernelRequest(RequestEvent $event): void
public function __invoke(RequestEvent $event): void
{
if (!$event->isMainRequest()) {
return;
Expand Down Expand Up @@ -142,7 +137,7 @@ public function onKernelRequest(RequestEvent $event): void
$twig->addGlobal('header_extra_content', $extraHeader);

// We set cid_reset = true if we enter inside a main/admin url
// CourseListener check this variable and deletes the course session
// CidReqListener check this variable and deletes the course session
if (str_contains((string) $request->get('name'), 'admin/')) {
$session->set('cid_reset', true);
} else {
Expand All @@ -161,20 +156,4 @@ public function onKernelRequest(RequestEvent $event): void

$session->set('access_url_id', $urlId);
}

public function onKernelResponse(ResponseEvent $event): void {}

public function onKernelController(ControllerEvent $event): void {}

/**
* @return array<string, mixed>
*/
public static function getSubscribedEvents(): array
{
return [
KernelEvents::REQUEST => 'onKernelRequest',
KernelEvents::RESPONSE => 'onKernelResponse',
KernelEvents::CONTROLLER => 'onKernelController',
];
}
}
42 changes: 9 additions & 33 deletions src/CoreBundle/EventListener/LoginSuccessHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,40 +17,24 @@
use Chamilo\CoreBundle\Settings\SettingsManager;
use DateTime;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use UserManager;

// class LoginSuccessHandler implements AuthenticationSuccessHandlerInterface
class LoginSuccessHandler implements EventSubscriberInterface
class LoginSuccessHandler
{
protected UrlGeneratorInterface $router;
protected AuthorizationCheckerInterface $checker;
protected SettingsManager $settingsManager;
protected EntityManagerInterface $entityManager;
private LoginAttemptLogger $loginAttemptLogger;

public function __construct(
UrlGeneratorInterface $urlGenerator,
AuthorizationCheckerInterface $checker,
SettingsManager $settingsManager,
EntityManagerInterface $entityManager,
LoginAttemptLogger $loginAttemptLogger
) {
$this->router = $urlGenerator;
$this->checker = $checker;
$this->settingsManager = $settingsManager;
$this->entityManager = $entityManager;
$this->loginAttemptLogger = $loginAttemptLogger;
}

/**
* @return null|RedirectResponse
*/
public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
private readonly UrlGeneratorInterface $router,
private readonly AuthorizationCheckerInterface $checker,
private readonly SettingsManager $settingsManager,
private readonly EntityManagerInterface $entityManager,
private readonly LoginAttemptLogger $loginAttemptLogger
) {}

public function __invoke(InteractiveLoginEvent $event): ?RedirectResponse
{
$request = $event->getRequest();
$session = $request->getSession();
Expand Down Expand Up @@ -179,12 +163,4 @@ public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)

return $response;
}

/**
* @return array<string, mixed>
*/
public static function getSubscribedEvents(): array
{
return ['security.interactive_login' => 'onSecurityInteractiveLogin'];
}
}
Loading

0 comments on commit 009cec6

Please sign in to comment.