Symfony2 custom user checker based on accepted eula

前端 未结 3 1899
余生分开走
余生分开走 2020-12-20 15:59

I want to create custom user checker to validate login action against last accepted eula. \' Idea is quite simple, there will be many versions of eula and user can\'

相关标签:
3条回答
  • 2020-12-20 16:06

    The answer is actually quite obvious. In your custom bundle:

    config.yml

    parameters:
        security.user_checker.class: Acme\Bundle\UserBundle\Security\UserChecker
    

    Userchecker:

    class UserChecker extends BaseUserChecker
    {
        /**
         * {@inheritdoc}
         */
        public function checkPreAuth(UserInterface $user)
        {
            //do your custom preauth stuff here
            parent::checkPreAuth($user);
        }
    }
    
    0 讨论(0)
  • 2020-12-20 16:06

    Why not attach event listener to the kernel.request event and watch if the current logged user has accepted the latest EULA?

    To get the current user you can use something like this:

    $securityContext = $this->container->get('security.context');
    if (!$securityContext) {
        return;
    }
    
    $user = $securityContext->getToken()->getUser();
    
    0 讨论(0)
  • 2020-12-20 16:28

    You can redefine Symfony's user checker service (security.user_checker) in your bundle:

    # services.yml
    security.user_checker:
        class: MyBundle\Checker\UserChecker
        arguments: [ "@some.service", "%some.param%" ]
    

    Then extend Symfony's UserChecker:

    # UserChecker.php
    
    use Symfony\Component\Security\Core\User\UserChecker as BaseUserChecker;
    
    class UserChecker extends BaseUserChecker
    {
        public function checkPreAuth(UserInterface $user)
        {
            parent::checkPreAuth($user);
    
            // your stuff
        }
    
        public function checkPostAuth(UserInterface $user)
        {
            parent::checkPostAuth($user);
    
            // your stuff
        }
    }
    

    The security.user_checker service gets called in \Symfony\Component\Security\Core\Authentication\Provider\UserAuthenticationProvider::authenticate() during the authentication process

    0 讨论(0)
提交回复
热议问题