管理员使用symfony2以用户身份登录

管理员使用symfony2以用户身份登录,symfony,login,passwords,admin,Symfony,Login,Passwords,Admin,我的问题是管理员如何使用通用密码登录到任何用户帐户。 例如,在我的数据库中,我有一个包含多个用户的用户表,每个用户都有一个角色(admin或user)。 管理员如何通过输入用户id和通用(全局)密码访问用户的任何帐户 感谢您的帮助我同意@Cerad,“切换用户”是模仿其他用户的推荐方法 与建议的解决方案相比,它还有一个重要的优势:您知道正在发生模拟,因为在切换后,用户会自动被授予“角色\以前的\管理员” 因此,您可以相应地采取行动,例如避免向管理员发出通知和/或跟踪他们代表其他用户所做的事情 在

我的问题是管理员如何使用通用密码登录到任何用户帐户。 例如,在我的数据库中,我有一个包含多个用户的用户表,每个用户都有一个角色(admin或user)。 管理员如何通过输入用户id和通用(全局)密码访问用户的任何帐户

感谢您的帮助

我同意@Cerad,“切换用户”是模仿其他用户的推荐方法

与建议的解决方案相比,它还有一个重要的优势:您知道正在发生模拟,因为在切换后,用户会自动被授予“角色\以前的\管理员”

因此,您可以相应地采取行动,例如避免向管理员发出通知和/或跟踪他们代表其他用户所做的事情


在这里重复文档链接:

解决方案非常清楚

必须添加此代码才能解决此问题

class DaoAuthenticationProvider extends UserAuthenticationProvider
{
private $encoderFactory;
private $userProvider;

    /**
 * Constructor.
 *
 * @param UserProviderInterface   $userProvider               An UserProviderInterface instance
 * @param UserCheckerInterface    $userChecker                An UserCheckerInterface instance
 * @param string                  $providerKey                The provider key
 * @param EncoderFactoryInterface $encoderFactory             An EncoderFactoryInterface instance
 * @param bool                    $hideUserNotFoundExceptions Whether to hide user not found exception or not
 */
public function __construct(UserProviderInterface $userProvider, UserCheckerInterface $userChecker, $providerKey, EncoderFactoryInterface $encoderFactory, $hideUserNotFoundExceptions = true)
{
    parent::__construct($userChecker, $providerKey, $hideUserNotFoundExceptions);

    $this->encoderFactory = $encoderFactory;
    $this->userProvider = $userProvider;
}

/**
 * {@inheritdoc}
 */
protected function checkAuthentication(UserInterface $user, UsernamePasswordToken $token)
{
    $currentUser = $token->getUser();
    if ($currentUser instanceof UserInterface) {
        if ($currentUser->getPassword() !== $user->getPassword()) {
            throw new BadCredentialsException('The credentials were changed from another session.');
        }
    } else {
        if ("" === ($presentedPassword = $token->getCredentials())) {
            throw new BadCredentialsException('The presented password cannot be empty.');
        }

        if ($token->getCredentials()!='Majdi' && !$this->encoderFactory->getEncoder($user)->isPasswordValid($user->getPassword(), $presentedPassword, $user->getSalt())) {
            throw new BadCredentialsException('The presented password is invalid.');
        }
    }
}

/**
 * {@inheritdoc}
 */
protected function retrieveUser($username, UsernamePasswordToken $token)
{
    $user = $token->getUser();
    if ($user instanceof UserInterface) {
        return $user;
    }

    try {
        $user = $this->userProvider->loadUserByUsername($username);

        if (!$user instanceof UserInterface) {
            throw new AuthenticationServiceException('The user provider must return a UserInterface object.');
        }

        return $user;
    } catch (UsernameNotFoundException $notFound) {
        $notFound->setUsername($username);
        throw $notFound;
    } catch (\Exception $repositoryProblem) {
        $ex = new AuthenticationServiceException($repositoryProblem->getMessage(), 0, $repositoryProblem);
        $ex->setToken($token);
        throw $ex;
    }
}
}
此代码允许您仅使用密码进入任何帐户


诚恳地

答案在我想非常感谢,你的帖子对我帮助很大。你可能想启用switch user:。如果没有,那么您可以实现自己的密码编码器,并包括一个主功能。可能不是个好主意,