Y
Y
Yuri2014-03-16 19:49:03
symfony
Yuri, 2014-03-16 19:49:03

How to authorize Symfony2 + FOSUserBundle?

Hello. I would like to know if it is possible to override the FOSUserBundle authorization, you need to authorize not by two fields (login / password) but by three (not email), for further application of ACL, I re-read a bunch of documentation and forums, I didn’t find anything suitable, maybe someone has already encountered such a task or will tell you in which direction to dig? Due to the complexity of the Symfony 2 core, it's hard to figure this out on your own.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
Y
Yuri, 2014-03-21
@error500

If anyone is interested, post it on stackoverflow

B
Boris Benkovsky, 2014-03-19
@benbor

Yes, you can, in short, then symfony.com/doc/current/cookbook/bundles/inheritan...
I solved this problem by inheriting my bundle from FOSUserBundle:

class MyUserBundle extends Symfony\Component\HttpKernel\Bundle\Bundle
{
    public function getParent()
    {
        return 'FOSUserBundle';
    }
}

then,
namespace MyUserBundle\Controller;

use FOS\UserBundle\Controller\SecurityController as ParentSecurityController;
use Symfony\Component\HttpFoundation\RedirectResponse;

class SecurityController extends ParentSecurityController
{

    public function loginAction()
    {
        if (!$this->container->get('security.context')->isGranted('IS_AUTHENTICATED_ANONYMOUSLY'))
        {
            return new RedirectResponse('/');
        }

        return parent::loginAction();
    }

the parent::loginAction() line transfers control to the FOSUserBundle, but you can choose not to do this, but implement your own version.

A
Alenija, 2016-09-21
@Alenija

Then already:

namespace MyUserBundle\Controller;

use FOS\UserBundle\Controller\SecurityController as ParentSecurityController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;

class SecurityController extends ParentSecurityController
{

    public function loginAction(Request $request)
    {
        if (!$this->container->get('security.context')->isGranted('IS_AUTHENTICATED_ANONYMOUSLY'))
        {
            return new RedirectResponse('/');
        }

        return parent::loginAction($request);
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question