Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 security: Multiple providers

I have 2 bundles in my project:

  • src/Korea/AlmacenBundle
  • src/Galvez/RepuestosBundle

Each with their own database

  • korea_motos -> AlmacenBundle
  • galvez_motos -> RepuestosBundle

Actually my security.yml has only one provider:

providers:
    korea:
        entity: { class: Korea\AlmacenBundle\Entity\Usuario, property: username }

As you can see, both bundles are authenticated by the same table: Usuario, in korea_motos

TABLE: Usuario (korea_motos database)

--ID--|----USERNAME----|---------BUNDLE---

-----1-----|-------------admin----------------|----------AlmacenBundle----------

-----2-----|-------------admin----------------|----------RepuestosBundle-------

Now i want to validate the users, for RepuestosBundle with a table Usuario in galvez_motos, removing the column "bundle" in the previous table.

The problem is in the security.yml file. If i make this:

providers:
    korea:
        entity: { class: Korea\AlmacenBundle\Entity\Usuario, property: username }
    galvez:
        entity: { class: Galvez\RepuestosBundle\Entity\Usuario, property: username }

Symfony launch a exception:

The class 'Galvez\RepuestosBundle\Entity\Usuario' was not found in the chain configured namespaces Korea\AlmacenBundle\Entity

Im trying to use 2 providers, one table per each bundle.. is this possible?

Files: security.yml

jms_security_extra:
secure_all_services: false
expressions: true

security: encoders: Korea\AlmacenBundle\Entity\Usuario: algorithm: sha1 encode_as_base64: false iterations: 1 Galvez\RepuestosBundle\Entity\Usuario: algorithm: sha1 encode_as_base64: false iterations: 1

role_hierarchy:
    ROLE_ADMIN:       ROLE_USER
    ROLE_SUPER_ADMIN: [ ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH ]

providers:
    korea:
        entity: { class: Korea\AlmacenBundle\Entity\Usuario, property: username }
    galvez:
        entity: { class: Galvez\RepuestosBundle\Entity\Usuario, property: username }

firewalls:
    dev:
        pattern:  ^/(_(profiler|wdt)|css|images|js)/
        security: false

    login:
        pattern:  ^/demo/secured/login$
        security: false

    secured_area:
        pattern:    ^/
        anonymous: ~
        access_denied_handler: accessdenied_handler
        form_login:
            login_path:  /login
            check_path:  /login_check
            default_target_path: /redirect
            always_use_default_target_path: true
        logout:
            path:   /logout
            target: /login
        #anonymous: ~
        #http_basic:
        #    realm: "Secured Demo Area"

access_control:
    - { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/redirect, roles: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/galvez, roles: ROLE_ADMIN_GALVEZ }
    - { path: ^/, roles: ROLE_ADMIN_KOREA }

config.yml -- can't copy/paste all :(

doctrine:
dbal:
    default_connection:   default
    connections:
        default:
            driver:   "%database_driver%"
            dbname:   "%database_name%"
            user:     "%database_user%"
            password: "%database_password%"
            host:     "%database_host%"
            port:     "%database_port%"
            charset:  UTF8
        galvez:
            driver:   %database_driver%
            dbname:   %database_name2%
            user:     %database_user2%
            password: %database_password2%
            host:     %database_host%
            port:     %database_port%
            charset:  UTF8
orm:
    default_entity_manager:   default
    entity_managers:
        default:
            connection:       default
            mappings:
                AlmacenBundle: ~
        galvez:
            connection:       galvez
            mappings:
                RepuestosBundle: ~

parameters.yml

parameters:
database_driver: pdo_mysql
database_host: localhost
database_port: null
database_name: korea_motos
database_user: root
database_password:
mailer_transport: smtp
mailer_host: localhost
mailer_user: null
mailer_password: null
locale: en
secret: 5f7ac4e7c2b38d6dbe55a1f05bee2b02
database_path: null

database_name2: galvez_motos
database_user2: root
database_password2:

PD: Sry for my english :S

like image 281
Andy.Diaz Avatar asked Apr 22 '13 18:04

Andy.Diaz


1 Answers

Old question but for anyone looking for a solution, the manual explains it all here. Basically, you need to chain your providers like this:

# app/config/security.yml
security:
    providers:
        chain_provider:
            chain:
                providers: [korea, galvez]
        korea:
            entity: { class: Korea\AlmacenBundle\Entity\Usuario, property: username }
        galvez:
            entity: { class: Galvez\RepuestosBundle\Entity\Usuario, property: username }
like image 150
Callistino Avatar answered Nov 07 '22 05:11

Callistino