我的项目中有2个包:
每个人都有自己的数据库
实际上我的security.yml只有一个提供者:
providers:
korea:
entity: { class: Korea\AlmacenBundle\Entity\Usuario, property: username }
如您所见,两个捆绑包都通过同一个表进行身份验证:Usuario,in korea_motos
表:Usuario(korea_motos数据库)
- - - - - - - - 1 | ------------- ----------------管理员| ---- ------ AlmacenBundle ----------
- - - - - - - - 2 | ------------- ----------------管理员| ---- ------ ------- 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:false 迭代:1 加尔韦斯\ RepuestosBundle \实体\ Usuario: 算法: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
答案 0 :(得分:11)
老问题,但对于寻找解决方案的任何人来说,手册都解释了所有问题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 }
答案 1 :(得分:0)
这可能是您的类的命名空间的问题。检查类Galvez\RepuestosBundle\Entity\Usuario
是否在正确的命名空间中,如果配置正确 - 可能您不小心从另一个实体中留下了一些复制粘贴代码。
尝试保留这两个实体并获取它们(没有安全上下文) - 我想你会在那里找到你的问题。
答案 2 :(得分:0)
我对两个实体进行了测试:
$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');
当我尝试刷新时,Symfony会发出同样的错误:
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;
default_connection:韩国
为:
default_connection:galvez
Symfony说:
MappingException: The class 'Korea\AlmacenBundle\Entity\Usuario' was not found in the chain configured namespaces Galvez\RepuestosBundle\Entity
同样的错误,但反过来..
似乎monolog验证采用默认连接(在本例中为韩国)进行搜索和验证