![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/mautic.corals.io/app/bundles/ApiBundle/EventListener/ |
<?php namespace Mautic\ApiBundle\EventListener; use Doctrine\ORM\EntityManager; use FOS\OAuthServerBundle\Event\PreAuthorizationEvent; use Mautic\CoreBundle\Security\Permissions\CorePermissions; use Symfony\Component\Security\Core\Exception\AccessDeniedException; use Symfony\Contracts\Translation\TranslatorInterface; class PreAuthorizationEventListener { public function __construct( private EntityManager $em, private CorePermissions $mauticSecurity, private TranslatorInterface $translator ) { } /** * @throws AccessDeniedException */ public function onPreAuthorizationProcess(PreAuthorizationEvent $event): void { if ($user = $this->getUser($event)) { // check to see if user has api access if (!$this->mauticSecurity->isGranted('api:access:full')) { throw new AccessDeniedException($this->translator->trans('mautic.core.error.accessdenied', [], 'flashes')); } $client = $event->getClient(); $event->setAuthorizedClient( $client->isAuthorizedClient($user, $this->em) ); } } public function onPostAuthorizationProcess(PreAuthorizationEvent $event): void { if ($event->isAuthorizedClient()) { if (null !== $client = $event->getClient()) { $user = $this->getUser($event); $client->addUser($user); $this->em->persist($client); $this->em->flush(); } } } /** * @return mixed */ protected function getUser(PreAuthorizationEvent $event) { return $this->em->getRepository(\Mautic\UserBundle\Entity\User::class)->findOneByUsername($event->getUser()->getUserIdentifier()); } }