Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Login using a Third Parameter

Tags:

php

symfony

I am working on a relatively simple login system for Symfony2.

I have the basics down and working just fine.

What makes this a bit special is I need a way to provide a third value (an ecosystem value). The user names in my database are not unique on their own, but make unique pairs with an ecosystem value.

The ecosystem value is provided by the form that they log in from.

How can I use this ecosystem value be taken into consideration when performing a login?

like image 243
samanime Avatar asked Mar 11 '12 03:03

samanime


1 Answers

Take a look at the Cookbook tutorial on How to load Security Users from the Database (the Entity Provider) - specifically the part about Authenticating Someone with a Custom Entity Provider.

This part explains how to alter/create a UserProviderInterface to allow logging in via an e-mail address or username. Obviously, this isn't what you want, but - it should be relatively trivial to modify the code to also query/validate against your 3rd login parameter.

Ideally, you will probabaly want to create your own UserProvider and luckily, there is a cookbook tutorial just for that: How to create a custom User Provider.


Update: To have the 3rd login option handled and passed to your custom providers (created above), you will also need to create a custom Authentication Provider. Take a look at (yet another) cookbook article on How to create a custom Authentication Provider. This article is tailored for handling extra request header parameters for authentication, but using this example you should be able to swap out request headers for request POST values.

Use the cookbook article to get an understanding of how authentication with Symfony2 works, but use (and extend) the username/password functionality of Symfony2 and just include your 3rd parameter. Some classes to look at:

  • UsernamePasswordFormAuthenticationListener
  • UsernamePasswordToken
  • UserAuthenticationProvider
like image 174
leek Avatar answered Oct 22 '22 01:10

leek