I am trying Sentry for Laravel and I came accross with this problem. I have 2 group of routes:
Route::group(array( 'before' => 'Sentry|inGroup:Administrator'), function(){
Route::get('/', 'HomeController@index');
Route::resource('user', 'UserController');
});
Route::group(array( 'before' => 'Sentry|inGroup:Doctor'), function(){
Route::get('/', 'HomeController@index');
//Route::resource('user', 'UserController');
});
And my filters are:
Route::filter('inGroup', function($route, $request, $value)
{
try{
$user = Sentry::getUser();
$group = Sentry::findGroupByName($value);
//dd($user->getGroups());
//dd($user->inGroup($group));
if( ! $user->inGroup($group)){
return Redirect::to('/login')->withErrors(array(Lang::get('user.noaccess')));
}
}catch (Cartalyst\Sentry\Users\UserNotFoundException $e){
return Redirect::to('/login')->withErrors(array(Lang::get('user.notfound')));
}catch (Cartalyst\Sentry\Groups\GroupNotFoundException $e){
return Redirect::to('/login')->withErrors(array(Lang::get('group.notfound')));
}
});
Route::filter('hasAccess', function($route, $request, $value)
{
try{
$user = Sentry::getUser();
//dd($user); $user->hasAccess($value)
if( Sentry::check() ) {
//return Redirect::to('/')->withErrors(array(Lang::get('user.noaccess')));
return Redirect::to('/');
}
}catch (Cartalyst\Sentry\Users\UserNotFoundException $e){
return Redirect::to('/login')->withErrors(array(Lang::get('user.notfound')));
}
});
The problem is the latter route with the 'Sentry|inGroup:Doctor'
is firing. The filter is not getting the Administrator part or group.
How can I achieve to filter them based on the parameter that is passed on by the routes? Or, how can I make them dynamic?
You have defined twice the same "/" route, once in the first (Administrator's) group, and once in the second (Doctor's) group
Route::get('/', 'HomeController@index');
Since you haven't specified any prefix on either route group, the later (second) route overrides the first and the Administrator route cannot be reached. You can try using prefixes:
// http://localhost/admin
Route::group(array('prefix' => 'admin', 'before' => 'Sentry|inGroup:Admins'), function()
{
Route::get('/', 'HomeController@index');
});
// http://localhost/doctors
Route::group(array('prefix' => 'doctors', 'before' => 'Sentry|inGroup:Doctor'), function()
{
Route::get('/', 'HomeController@index');
});