数据库中的两个不同提供者

时间:2013-08-07 07:36:20

标签: symfony security

我的问题是我想使用2种不同的用户类型“Familia”和“Doctor”我用这种方式配置security.yml:

providers: 




    client_db:
        entity: 
          class: Goodday\PreditBundle\Entity\Familia
          property: email

    manager_db:
        entity: 
          class: Goodday\PreditBundle\Entity\Doctor
          property: email

问题是我可以使用Familia登录,但如果我尝试使用医生登录,则会返回“Bad Credentials”。两个实体都有

 class Doctor implements UserInterface
 {
 public function getRoles()
{
    return array('ROLE_DOC');
}

public function getSalt()

{
    return false;
}

public function getUsername()
{
    return $this->email;
}

public function eraseCredentials()
{

}

public function equals(UserInterface $user)
{
    return $user->getUsername() == $this->getUsername();
}

这是我的安全文件。

security:
encoders:
    Symfony\Component\Security\Core\User\User: sha512

    Goodday\PreditBundle\Entity\Familia: 
      algorithm: plaintext

    Goodday\PreditBundle\Entity\Doctor:
      algorithm: plaintext




providers: 
  chain_provider:
    chain:
      providers: [user_db, admin_db]



      user_db:
        entity: 
          class: Goodday\PreditBundle\Entity\Familia
          property: email

      admin_db:
        entity: 
          class: Goodday\PreditBundle\Entity\Doctor
          property: email

firewalls:

    secured_area:
        pattern:    ^/
        anonymous: ~
        form_login:
            check_path: /check
            login_path: /login
            always_use_default_target_path: false
            default_target_path:            /checking

            use_referer:                    false
        logout:
            path:   /logout
            target: /
        #anonymous: ~
        #http_basic:
        #    realm: "Secured Demo Area"

access_control:
    - { path: ^/familia, roles: ROLE_FAM }
    - { path: ^/doctor, roles: ROLE_DOC}

问题:

  • 安全代码部分写得好吗?还剩下什么吗?
  • 我是否必须在实体中添加内容?

1 个答案:

答案 0 :(得分:0)

您需要做的是将提供商链接如下:

# app/config/security.yml
security:
    providers:
        chain_provider:
            chain:
                providers: [client_db, manager_db]
        client_db:
            entity: 
                class: Goodday\PreditBundle\Entity\Familia
                property: email

        manager_db:
            entity: 
                class: Goodday\PreditBundle\Entity\Doctor
                property: email

这将首先尝试使用客户端提供程序进行登录,然后使用管理器进行尝试,如果两者都失败,它将告诉您某种“错误凭据”错误。