Symfony2 - “security.providers.db_users”

时间:2015-06-23 07:43:04

标签: php mongodb symfony doctrine-orm

我正在为我的项目(API)实施用户身份验证,其中登录将使用oAuth第三方(Facebook和Google +)完成。

我的项目使用的是MongoDb和Doctrine MongoDB Odm。

为达到这个目标,我创建了一个用户文档:

class User implements UserInterface, \Serializable
{
    protected $id;
    protected $provider;
    protected $providerId;
    protected $name;
    protected $email;
    protected $image;
    protected $roles = array('ROLE_USER');
    protected $isActive = true;
    protected $createdAt;
    protected $updatedAt;

    public function getId() {
        return $this->id;
    }

    /******* MORE GETTERS AND SETTERS. ********/

    public function eraseCredentials()
    {
    }

    /** @see \Serializable::serialize() */
    public function serialize()
    {
        return serialize(array(
            $this->id,
            $this->email,
            $this->getPassword(),
            $this->isActive
        ));
    }

    /** @see \Serializable::unserialize() */
    public function unserialize($serialized)
    {
        list (
            $this->id,
            $this->email,
            $this->password,
            $this->isActive
            ) = unserialize($serialized);
    }
}

使用此YAML配置文件:

AppBundle\Document\User:
    type: document
    db: db_name
    collection: user
    repositoryClass: AppBundle\Repository\UserRepository
    fields:
        id:
            type: id
            id:  true
            strategy: AUTO
        provider:
            type: string
        providerId:
            type: string
        name:
            type: string
        email:
            type: string
        image:
            type: string
        roles:
            type: collection
        isActive:
            type: boolean
        createdAt:
            type: timestamp
        updatedAt:
            type: timestamp

这个security.yml配置:

security:
    encoders:
        AppBundle\Document\User: { algorithm: sha512, iterations: 10 }

    role_hierarchy:
        ROLE_ADMIN:       ROLE_USER
        ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN]

    providers:
        db_users:
            entity: { class: AppBundle\Document\User, property: email }

    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt|error)|css|images|js)/
            security: false
        default:
            pattern:    ^/
            provider:   db_users

    access_control:
        - { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }

当我执行代码时,我收到下一个错误:

Unrecognized option "entity" under "security.providers.db_users"

¿我错过了什么吗?

1 个答案:

答案 0 :(得分:3)

最后我在这个链接中找到了答案:

https://test-sf-doc-es.readthedocs.org/en/latest/book/security/users.html#custom-user-provider

# app/config/security.yml
services:
    my.mongodb.provider:
        parent: doctrine_mongodb.odm.security.user.provider
        arguments: [Acme\MyBundle\Document\User, username]

security:
    providers:
        custom_provider:
            id: my.mongodb.provider

我希望这有助于某人。