简体   繁体   中英

Symfony2 - Unrecognized option “entity” under “security.providers.db_users”

I'm implementing a user authentication for my project (an API) where the login will be done using oAuth third parties (Facebook and Google+).

My project is using MongoDb and Doctrine MongoDB Odm.

For reach this objective I've created a User Document:

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);
    }
}

With this YAML config file:

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

And this security.yml configuration:

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 }

And when I execute the code, I get the next error:

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

¿Am I missing something?

Finally I've found the answer in this link:

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

I hope this helps someone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM