0

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?

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

In case someone bumps into this again, I had this problem when I forgot to install doctrine composer require doctrine

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.