Symfony2安全性:多个提供程序 [英] Symfony2 security: Multiple providers
问题描述
我的项目中有2个捆绑包:
I have 2 bundles in my project:
- src/韩国/AlmacenBundle
- src/Galvez/RepuestosBundle
每个都有自己的数据库
- korea_motos-> AlmacenBundle
- galvez_motos-> RepuestosBundle
实际上我的security.yml只有一个提供程序:
Actually my security.yml has only one provider:
providers:
korea:
entity: { class: Korea\AlmacenBundle\Entity\Usuario, property: username }
如您所见,两个捆绑包均通过同一张表进行身份验证:Usuario,位于korea_motos中
As you can see, both bundles are authenticated by the same table: Usuario, in korea_motos
表:Usuario(korea_motos数据库)
TABLE: Usuario (korea_motos database)
----- 1 ----- | ------------- admin ---------------- | ---- ------ AlmacenBundle ----------
-----1-----|-------------admin----------------|----------AlmacenBundle----------
----- 2 ----- || ------------- admin ---------------- | ---- ------ RepuestosBundle -------
-----2-----|-------------admin----------------|----------RepuestosBundle-------
现在,我要验证用户,对于galvez_motos中具有表Usuario的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.
问题出在security.yml文件中.如果我这样做:
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启动异常:
Symfony launch a exception:
The class 'Galvez\RepuestosBundle\Entity\Usuario' was not found in the chain configured namespaces Korea\AlmacenBundle\Entity
我正在尝试使用2个提供程序,每个捆绑包一个表..这可能吗?
Im trying to use 2 providers, one table per each bundle.. is this possible?
文件: security.yml
Files: security.yml
jms_security_extra:
secure_all_services: false
expressions: true
安全性: 编码器: 韩国\ AlmacenBundle \ Entity \ Usuario: 算法:sha1 encode_as_base64:否 迭代次数:1 Galvez \ RepuestosBundle \ Entity \ Usuario: 算法:sha1 encode_as_base64:否 迭代次数:1
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-无法复制/粘贴所有:(
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.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:抱歉,我的英语是:S
PD: Sry for my english :S
推荐答案
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 }
这篇关于Symfony2安全性:多个提供程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!