1

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?

Gustavo Straube
  • 3,744
  • 6
  • 39
  • 62
ken-master
  • 15
  • 2
  • Instead of checking if they are in group X check if they have the permission to access the route this way multiple groups could view the page if the have permission to – Gal Sisso Feb 22 '15 at 08:46
  • Hi Gal, can you point me to the right direction? a simple and basic implementation perhaps. i just started using laravel and sentry. tnx – ken-master Feb 22 '15 at 09:02
  • [Laravel Sentry snippet](http://laravelsnippets.com/snippets/sentry-route-filters) You have there `inGroup` filter as well as `hasAccess`.To read more about permission go to https://cartalyst.com/manual/sentry/2.1#permissions – Gal Sisso Feb 22 '15 at 09:23
  • I'm actually trying that approach but i'm having difficulty understanding sentry's permission. – ken-master Feb 22 '15 at 10:22

1 Answers1

0

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');
});
dede
  • 2,523
  • 2
  • 28
  • 32