2013-04-22 67 views
5

我有2捆在我的項目:Symfony2的安全性:多供應商

  • 的src /韓國/ AlmacenBundle
  • 的src /加爾韋斯/ RepuestosBundle

每一個都有自己的數據庫

  • korea_motos - > AlmacenBundle
  • galvez_motos - > Re puestosBundle

其實我security.yml只有一個供應商:

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

正如你可以看到,這兩個包被同桌認證:Usuario,在korea_motos

表:Usuario( korea_motos數據庫)

--ID-- | ---- ---- USERNAME | ---------管束---

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

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

現在我想驗證用戶,爲RepuestosBundle用galvez_motos中的表Usuario刪除上表中的「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 \實體\ Usuario: 算法:SHA1 encode_as_base64:假 迭代:1 加爾韋斯\ RepuestosBundle \實體\ 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 - 無法複製/粘貼所有:(

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

回答

0

這可能是與你的類的命名空間的問題。檢查類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');

當我嘗試沖洗,Sym fony launchs了同樣的錯誤:

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:嗯,我認爲這是不可能的,如果我改變了這個:

default_connection: korea

到:

default_connection: galvez

Symfony的說:

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

同樣的錯誤,但在反向..

這似乎是獨白驗證採取默認連接(在這種情況下,韓國)的搜索和驗證

10

老問題,但對於任何尋找解決方案的人來說,本手冊都將其全部解釋爲here。 基本上,你需要像這樣連鎖你的供應商:

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