Symfony2 custom UserProvider


For my reference (and just in case someone else is searching for this). I spen almost the entire day trying to figure out how to do this… lol. Anyway. This example below is made just to understand a little how this works, because with the already provided Doctrine Entity Provider you already have the most used type of provider ( the one where you check in the database if the user exits, etc… )

A custom Provider is usefull if you do more complicated stuff like getting the data from another server or something like that.

(!) I’m using Symfony2 BETA1 making the assumption that you read the book ( im on the security chapter )

To create a custom user provider you first need to declare your user provider class as a service on your app/config/config.yml, so add this or create this (if you dont have a “services” tag already declared on your config):

services:
 my_provider:
 class: YourApp\YourBundle\YourUserProvider

Now on your main security.yml, on the providers declaration add:

providers:
 default:
 id: my_provider

Now, when you try to log in, Symfony will use YourUserProvider to get an instance of a valid UserInterface based on the credentials ( username ) This is an example on how the YourUserProvider might look:

[preserved_text 5c7a41adfb21f9300423a013b5d43ee4 /]

For more information read the sourcecodes located in:

vendor/symfony/src/symfony/component/security/core/user/

…of your Symfony proyect folder. There you will see a bunch of classes which contains usefull information on how this thing works.

Keep reading more: http://symfony.com/doc/current/book/security/users.html

Advertisements