【问题标题】:Laravel: Sentry Routes and Filter groupsLaravel:哨兵路线和过滤器组
【发布时间】:2015-04-23 17:26:58
【问题描述】:

我正在为 Laravel 尝试 Sentry,但遇到了这个问题。我有 2 组路线:

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');

});

我的过滤器是:

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')));
     }

});

问题是'Sentry|inGroup:Doctor' 的后一条路线正在触发。过滤器未获取管理员部分或组。

如何根据路由传递的参数过滤它们?或者,我怎样才能使它们动态化?

【问题讨论】:

  • 而不是检查他们是否在 X 组中,而是检查他们是否有权访问该路由,这样多个组可以查看该页面,如果有权限
  • 嗨,Gal,你能给我指出正确的方向吗?也许是一个简单而基本的实现。我刚开始使用 laravel 和哨兵。 tnx
  • Laravel Sentry snippet 你有inGroup 过滤器以及hasAccess。要阅读有关权限的更多信息,请转到cartalyst.com/manual/sentry/2.1#permissions
  • 我实际上正在尝试这种方法,但我很难理解哨兵的许可。

标签: laravel laravel-4 laravel-routing cartalyst-sentry


【解决方案1】:

您定义了两次相同的 "/" 路由,一次在第一个(管理员)组中,一次在第二个(医生)组中

Route::get('/', 'HomeController@index');

由于您没有在任一路由组上指定任何前缀,因此后面的(第二个)路由会覆盖第一个并且无法访问管理员路由。 您可以尝试使用前缀:

// 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');
});

【讨论】:

    猜你喜欢
    • 2014-01-21
    • 1970-01-01
    • 2014-06-11
    • 2012-04-03
    • 2019-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多