Symfony2 Security: Multiple Providers

I have 2 packages in my project:

  • src / Korea / AlmacenBundle
  • SRC / Galves / RepuestosBundle

Each with its own database

  • korea_motos → AlmacenBundle
  • galvez_motos → RepuestosBundle

In fact, my security.yml has only one provider:

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

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

TABLE: Usuario (korea_motos database)

- ID-- | ---- USERNAME ---- | --------- PACKAGE ---

----- 1 ----- | ------------- Administrator ---------------- | ---- ------ AlmacenBundle ----------

----- 2 ----- | ------------- Administrator ---------------- | ---- ------ RepuestosBundle -------

, RepuestosBundle, Usuario galvez_motos, "bundle" .

security.yml. :

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

Symfony :

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

2 , .. ?

: security.yml

jms_security_extra:
secure_all_services: false
expressions: true

:   :       \AlmacenBundle\Entity\:           : sha1           encode_as_base64: false           : 1       \RepuestosBundle\Entity\:           : sha1           encode_as_base64: false           : 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 - / : (

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 : S

+5
3

, , , . , :

# 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 }
+10

, , . , Galvez\RepuestosBundle\Entity\Usuario , - , .

( ). , .

0

:

abcController.php

$em= $this->get('doctrine')->getManager('galvez');

$usuario_g = $this->get('doctrine')->getRepository('RepuestosBundle:Usuario', 'galvez')->find(1);
$usuario_g->setUsername('asdasd');
$em->persist($usuario_g);
$em->flush();

,

$em = $this- > getDoctrine() → getEntityManager();

$ em = $ this-> get ('doctrine') → getManager ('galvez');

When I try to flash, Symfony fires the same error:

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

Usuario.php (AlmacenBundle)

<?php

namespace Korea\AlmacenBundle\Entity;

use Symfony\Component\Security\Core\User\UserInterface;
use Doctrine\ORM\Mapping as ORM;

/**
 * Usuario
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="Korea\AlmacenBundle\Entity\UsuarioRepository")
 */
class Usuario implements UserInterface
{

/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var string
 *
 * @ORM\Column(name="username", type="string", length=255)
 */
private $username;

/**
 * @var string
 *
 * @ORM\Column(name="password", type="string", length=255)
 */
private $password;

/**
 * @var string
 *
 * @ORM\Column(name="salt", type="string", length=255)
 */
private $salt;

Usuario.php (RepuestosBundle)

<?php

namespace Galvez\RepuestosBundle\Entity;

use Symfony\Component\Security\Core\User\UserInterface;
use Doctrine\ORM\Mapping as ORM;

/**
* Usuario
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="Galvez\RepuestosBundle\Entity\UsuarioRepository")
*/
class Usuario implements UserInterface
{

/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var string
 *
 * @ORM\Column(name="username", type="string", length=255)
 */
private $username;

/**
 * @var string
 *
 * @ORM\Column(name="password", type="string", length=255)
 */
private $password;

/**
 * @var string
 *
 * @ORM\Column(name="salt", type="string", length=255)
 */
private $salt;

PD: Well, I think it is not possible if I change this:

default_connection: korea

at

default_connection: galvez

Symfony says:

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

same error, but vice versa.

Monologic verification seems to accept the default connection (in this case, Korea) for search and validation

0
source

All Articles