0
votes

I have Symfony2 with a login system which is working correctly, it's using table VtigerPortalinfo to get user and password, then validate.

But I have roles in another table, this table returns 1 for admin and 0 for normal user, this is the query in the repository.

public function findRolTypeByCustomerId($customerId)
{
    $query = $this->getEntityManager()->createQuery("
            SELECT u.cf771
            FROM WbsGoclientsBundle:VtigerContactscf u
            WHERE u.contactid = :customerid
            ")->setParameter('customerid', $customerId);
    try {
        $rol = $query->getSingleResult();
        if($rol = 1)
            return 'ROLE_AGENT';
        else 
            return 'ROLE_USER';
    }
    catch (\Doctrine\ORM\NoResultException $e)
    {
        return null;
    }
}

And this is my Security.yml

security:
    encoders:
        WbsGo\clientsBundle\Entity\VtigerPortalinfo: plaintext
    role_hierarchy:
        ROLE_ADMIN: [ROLE_USER]

    providers:
        user_db:
          entity: { class: WbsGo\clientsBundle\Entity\VtigerPortalinfo, property: userName }
    firewalls:
        main:
            pattern: /.*
            provider: user_db
            form_login:
                always_use_default_target_path: true
                login_path: /login
                check_path: /login_check
                remember_me: true
            logout:
                path: /logout
                target: /
            remember_me:
                key: XXXXXXXX
                lifetime: 1800
                path: /.*
                domain: ~
            security: true
            anonymous: true
    access_control:
        - { path: /login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: /css, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: /js, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: /images, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: /user, roles: ROLE_USER }
        - { path: /admin, roles: ROLE_ADMIN }
        - { path: /.*, roles: ROLE_USER }

So my question is, how can I set roles from a different table than login info? As you can see, on the repo it returns the ROLE but, I don't know how to parse it to the security stuff...

1

1 Answers

0
votes

If I understand the question correctly then you just need to make your own user provider and query for the roles when myUserProvider.loadUserByUsername is called.

Pretty straight forward: http://symfony.com/doc/current/cookbook/security/custom_provider.html

Don't get confused by the web service stuff in the example. You just need to query for your user then add the roles to it.