Чтобы реализовать функционал last visit datetime в Symfony, нужно создать обработчик успешного входа в систему. Для этого следует создать класс реализующий интерфейс
Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface
Создание класса
namespace Site\SecurityBundle\DependencyInjection;
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Doctrine\ORM\EntityManager;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\RouterInterface;
use DateTime;
class AuthenticationSuccessHandler implements AuthenticationSuccessHandlerInterface {
private $em;
private $router;
function __construct(EntityManager $em, RouterInterface $router) {
$this->em = $em;
$this->router = $router;
}
public function onAuthenticationSuccess(Request $request, TokenInterface $token) {
$user = $this->em->getRepository('SiteSecurityBundle:UserEntity')
->find($token->getUser()->getId());
$user->setLastVisit(new DateTime('now'));
$this->em->flush();
return new RedirectResponse($this->router->generate('profile'));
}
}
Регистрация службы
parameters:
security.authentication.success_handler.class:
Site\SecurityBundle\DependencyInjection\AuthenticationSuccessHandler
services:
security.authentication.success_handler:
class: %security.authentication.success_handler.class%
public: false
arguments: [ @doctrine.orm.entity_manager, @router ]
Настройка
В файле app/config.yml нужно импортировать файл services.yml:
imports:
- { resource: "@SiteSecurityBundle/Resources/config/services.yml" }
а в файле app/security.yml нужно указать созданный обработчик:
security:
#...
firewalls:
secured_area:
#...
form_login:
#...
success_handler: security.authentication.success_handler
Удачи!
Комментариев нет:
Отправить комментарий