Refactor authentication system (#2602)
Make auth configuration available via UI, remove FOSUserBundle and SAML-Bundle dependency
This commit is contained in:
39
src/EventSubscriber/EmailSubscriber.php
Normal file
39
src/EventSubscriber/EmailSubscriber.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Kimai time-tracking app.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\EventSubscriber;
|
||||
|
||||
use App\Event\EmailEvent;
|
||||
use App\Mail\KimaiMailer;
|
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||
|
||||
/**
|
||||
* Event subscriber to handle emails.
|
||||
*/
|
||||
final class EmailSubscriber implements EventSubscriberInterface
|
||||
{
|
||||
private $mailer;
|
||||
|
||||
public function __construct(KimaiMailer $mailer)
|
||||
{
|
||||
$this->mailer = $mailer;
|
||||
}
|
||||
|
||||
public static function getSubscribedEvents(): array
|
||||
{
|
||||
return [
|
||||
EmailEvent::class => ['onMailEvent', 100],
|
||||
];
|
||||
}
|
||||
|
||||
public function onMailEvent(EmailEvent $event)
|
||||
{
|
||||
$this->mailer->send($event->getEmail());
|
||||
}
|
||||
}
|
||||
56
src/EventSubscriber/LastLoginSubscriber.php
Normal file
56
src/EventSubscriber/LastLoginSubscriber.php
Normal file
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Kimai time-tracking app.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\EventSubscriber;
|
||||
|
||||
use App\Entity\User;
|
||||
use App\Event\UserInteractiveLoginEvent;
|
||||
use App\Repository\UserRepository;
|
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
|
||||
use Symfony\Component\Security\Http\SecurityEvents;
|
||||
|
||||
class LastLoginSubscriber implements EventSubscriberInterface
|
||||
{
|
||||
private $repository;
|
||||
|
||||
public function __construct(UserRepository $repository)
|
||||
{
|
||||
$this->repository = $repository;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public static function getSubscribedEvents()
|
||||
{
|
||||
return [
|
||||
UserInteractiveLoginEvent::class => 'onImplicitLogin',
|
||||
SecurityEvents::INTERACTIVE_LOGIN => 'onSecurityInteractiveLogin',
|
||||
];
|
||||
}
|
||||
|
||||
public function onImplicitLogin(UserInteractiveLoginEvent $event)
|
||||
{
|
||||
$user = $event->getUser();
|
||||
|
||||
$user->setLastLogin(new \DateTime());
|
||||
$this->repository->saveUser($user);
|
||||
}
|
||||
|
||||
public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
|
||||
{
|
||||
$user = $event->getAuthenticationToken()->getUser();
|
||||
|
||||
if ($user instanceof User) {
|
||||
$user->setLastLogin(new \DateTime());
|
||||
$this->repository->saveUser($user);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,78 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Kimai time-tracking app.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\EventSubscriber;
|
||||
|
||||
use App\Entity\User;
|
||||
use FOS\UserBundle\Event\FormEvent;
|
||||
use FOS\UserBundle\FOSUserEvents;
|
||||
use FOS\UserBundle\Model\UserManagerInterface;
|
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||
use Symfony\Component\HttpFoundation\RedirectResponse;
|
||||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
||||
|
||||
/**
|
||||
* This class intercepts the registration to make sure:
|
||||
*
|
||||
* - the first-ever registered user will get the SUPER_ADMIN role
|
||||
* - the user uses the current request locale as initial language setting
|
||||
*/
|
||||
final class RegistrationSubscriber implements EventSubscriberInterface
|
||||
{
|
||||
/**
|
||||
* @var UserManagerInterface
|
||||
*/
|
||||
private $userManager;
|
||||
/**
|
||||
* @var UrlGeneratorInterface
|
||||
*/
|
||||
private $router;
|
||||
|
||||
public function __construct(UserManagerInterface $userManager, UrlGeneratorInterface $router)
|
||||
{
|
||||
$this->userManager = $userManager;
|
||||
$this->router = $router;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public static function getSubscribedEvents(): array
|
||||
{
|
||||
return [
|
||||
FOSUserEvents::REGISTRATION_SUCCESS => ['onRegistrationSuccess', 200],
|
||||
FOSUserEvents::RESETTING_RESET_SUCCESS => ['onResettingSuccess', 200],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param FormEvent $event
|
||||
*/
|
||||
public function onRegistrationSuccess(FormEvent $event)
|
||||
{
|
||||
/** @var User $user */
|
||||
$user = $event->getForm()->getData();
|
||||
$roles = [User::ROLE_USER];
|
||||
|
||||
if (empty($this->userManager->findUsers())) {
|
||||
$roles = [User::ROLE_SUPER_ADMIN];
|
||||
}
|
||||
|
||||
$user->setLanguage($event->getRequest()->getLocale());
|
||||
$user->setRoles($roles);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param FormEvent $event
|
||||
*/
|
||||
public function onResettingSuccess(FormEvent $event)
|
||||
{
|
||||
$event->setResponse(new RedirectResponse($this->router->generate('my_profile')));
|
||||
}
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Kimai time-tracking app.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\EventSubscriber;
|
||||
|
||||
use App\Entity\User;
|
||||
use FOS\UserBundle\Event\GetResponseNullableUserEvent;
|
||||
use FOS\UserBundle\FOSUserEvents;
|
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
|
||||
|
||||
/**
|
||||
* Makes sure that only internally registered users can reset their password.
|
||||
*/
|
||||
class ResetPasswordSubscriber implements EventSubscriberInterface
|
||||
{
|
||||
public static function getSubscribedEvents(): array
|
||||
{
|
||||
return [
|
||||
FOSUserEvents::RESETTING_SEND_EMAIL_INITIALIZE => ['onInitializeResetPassword', 200]
|
||||
];
|
||||
}
|
||||
|
||||
public function onInitializeResetPassword(GetResponseNullableUserEvent $event)
|
||||
{
|
||||
$user = $event->getUser();
|
||||
if (!($user instanceof User)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// that is not nice :-D
|
||||
if (!$user->isInternalUser()) {
|
||||
throw new AccessDeniedHttpException(
|
||||
sprintf('The user "%s" tried to reset the password, but it is registered as "%s" auth-type.', $user->getUsername(), $user->getAuth())
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user