Bạn có thể tạo AuthenticationHandler
mà Symfony sẽ gọi khi người dùng đăng nhập thành công, bạn có thể lưu thời gian đăng nhập vào thuộc tính thực thể User
(giả sử bạn có kịch bản này).
Đầu tiên, tạo xử lý xác thực thành công:
namespace Acme\TestBundle\Handler;
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\DependencyInjection\ContainerAware;
class AuthenticationHandler extends ContainerAware implements AuthenticationSuccessHandlerInterface
{
function onAuthenticationSuccess(Request $request, TokenInterface $token)
{
$token->getUser()->setLoginTime(new \DateTime());
$this->container->get('doctrine')->getEntityManager()->flush();
return new RedirectResponse($this->container->get('router')->generate('login_success'));
}
}
Sau đó, bạn cần phải đăng ký xử lý xác thực như một dịch vụ trong một tập tin cấu hình, ví dụ, src/Acme/TestBundle/resources/Config/services.yml
services:
authentication_handler:
class: Acme\TestBundle\Handler\AuthenticationHandler
calls:
- [ setContainer, [ @service_container ] ]
Và cấu hình đăng nhập để sử dụng trình xử lý được tạo, hãy xem security.yml
form_login:
success_handler: authentication_handler
của bạn
Rõ ràng, để làm việc này, bạn cần có một thực thể User
với thuộc tính loginTime
và bộ đặt tương ứng. Và bạn cần định cấu hình đăng nhập để sử dụng kho lưu trữ đối tượng User
làm nhà cung cấp dịch vụ người dùng và DaoAuthenticationProvider
, như được giải thích tại đây: http://symfony.com/doc/current/book/security.html#loading-users-from-the-database.
Bạn có thể cho chúng tôi biết tại sao nó không phù hợp với nhu cầu của bạn không? Bạn cần làm gì khác? Dịch vụ chắc chắn sẽ là lựa chọn của tôi cho một cái gì đó như thế này ... –