Julien Malige
Julien Malige

Reputation: 3475

Using Entity for SecurityUserProvider on Silex

On silex, my authentication works when I use a simple UserProvider with Doctrine ORM (just DBAL) :

$app['security.firewalls']=array(
 'app_secure' => array(
    'pattern' => '^/app/',
    'form' => array(
        'login_path' => '/login',
        'check_path' => '/app/login_check',
        'always_use_default_target_path' => true,
        'default_target_path' => '/app/'
        ),

    'logout' => array(
        'logout_path' => '/app/logout',
        'target_url' => "/login",
        'invalidate_session'=> "true"
        ),

    'users' => $app->share(function() use ($app) {
        return new App\Service\UserProvider($app['db']);
    })
  ),
);

On the Silex documentation, I have read :

If you are using the Doctrine ORM, the Symfony bridge for Doctrine provides a user provider class that is able to load users from your entities.

So I try to work with Entities (with the dflydev Doctrine ORM Service Provider) but I can't find how to configure my firewall :

'users' => $app->share(function() use ($app) {
            $em = $app['orm.em'];
            return $em->getRepository('MyProject\Entity\User');
        })  

This solution return me an error : attempts a UserProvider.

One of my sources : https://groups.google.com/forum/#!topic/silex-php/k-0X-BdG6Zw

What I'm doing wrong ?

Thanks

Upvotes: 1

Views: 275

Answers (1)

mTorres
mTorres

Reputation: 3590

As stated in the docs:

The users setting can be defined as a service that returns an instance of UserProviderInterface

You have to make sure that the user repository is implementing the UserProviderInterface.

In order to use the Doctrine one (take this with caution, I've never used it!), you first need to install the DoctrineBridge (composer require symfony/doctrine-bridge 2.7) and then just configure the Doctrine user provider:

<?php

$app['security.firewall'] = array(
  //...
  'users' => function() use ($app) {
    return new Symfony\Bridge\Doctrine\Security\User\EntityUserProvider($app['registry_manager'], 'MyProject\Entity\User');
  }

Be aware, you'll need a ManagerRegistry instance, you can take a look at this registry manager provider or create one on your own.

Upvotes: 2

Related Questions