How to use the credentials expired property in Symfony AdvancedUserInterface?

巧了我就是萌 提交于 2019-12-04 06:00:38

Actually, it's the CredentialsExpiredException you want to catch. If you're using the Symfony Security component, then the simplest way to handle this is to check for the exception in the loginAction of your SecurityController:

use Symfony\Component\Security\Core\Exception\CredentialsExpiredException;
use Symfony\Component\Security\Core\SecurityContextInterface;

...

$error = $this->get('session')->get(SecurityContextInterface::AUTHENTICATION_ERROR);

// check if credentials have expired
if ($error instanceof CredentialsExpiredException) {
    // display the change password form
    return new Response($this->renderView('AcmeDemoBundle:Security:changePassword.html.twig'));
}

You'll obviously need to create a route for changing passwords, which you can set as the form action of your changePassword template. Password change requests can then be handled accordingly in your SecurityController.

The core of your business logic can/should exist within a UserManager (or whatever you wish to call it) service class, which you can instantiate and invoke as needed from your SecurityController.

Hope that helps.

NOTE: For posterity, the expired user object is stored within the CredentialsExpiredException exception, so you can easily retrieve it if you need to act upon it for handling expired passwords:

$error->getUser();

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!