So I'm attempting to authenticate my user's using Laravel's custom filters. I have my LDAP PHP script working and I have essentially plugged it in to my custom filter. However, I need to pass this script the username and password that the user enters on the log in screen; in other words, I need to pass my custom filter this username and password from the log in form.
Here is my code to help explain my problem:
routes.php
Route::group(array('before' => 'ldapTest'), function() {
Route::controller('apps', 'AppController', array(
//named routes here
));
});
filters.php
Route::filter('ldapTest', function()
{
$username = //how do I get this?
$password = //how do I get this?
//LDAP logic goes here; assume $ldapConn and $userDN are properly initialized
$userBind = @ldap_bind($ldapConn, $userDN, $password);
if($userBind)
{
Auth::login(//what goes here? I want to access $username later on in applications);
return Redirect::to('apps/home');
}
else
{
echo 'Incorrect password';
}
});
From reading the documentation I understand you can pass parameters as strings to filters like so: Route::filter('ldapTest:400', function()
, but I don't understand how I could use this to pass my username and password using what I assume would be Input::get().
How can I do this?
Actually you can pass parameters into your custom filter and in this case your filter should look like this:
Route::filter('ldapTest', function($route, $request, $param){
//...
});
In your Closure
the third argument will receive the parameter you passed in and it's $param
, so you are able to pass it like this in your before filter:
array('before' => 'ldapTest:someString')
So, in the filter, the $param
will contain someString
but in your case that would be a bit different I think, because you want to receive the user inputs submitted through a form so to get those inputs you may use something like this in your filter's handler (Closure):
$username = $request->get('username'); // Assumed that username is a form field
$password = $request->get('password');
Also you may use Input::get('username')
instead of $request
if you want but it'll be working with $request
instance variable and I would prefer that to use.
I had a similar needing in my project, solved with this (not so much elegant but working) workaround:
Route::filter('multiParamFilter', function($route, $request, $params){
list($p1, $p2) = explode(':', $params);
//Now you have $p1 and $p2 initialized with parameters
....
}
In routes.php you can call:
Route::get('path', array('before' => 'multiParamFilter:p1:p2' ......
Note: It requires that you dont use ':' (or at least another symbol) in your parameter values