如何在Laravel 4中对路由组应用多个过滤器?

时间:2014-12-01 19:49:29

标签: laravel laravel-4 routes laravel-routing

  

目标:我想使用Route::groupRoute::filter

在Laravel 4中进行路由过滤

  

描述

我有两种类型的用户:

  1. 内部
  2. 经销商
  3. 对于,Internal,我有两组:

    • 管理员
    • 常规

    对于Distributor,我有4个小组:

    • gold
    • 青铜
    • OEM
      

    符合条件的路线

    OEM经销商仅有5条路线。

    Route::get('distributors/{id}', array('before' =>'profile', 'uses'=>'DistributorController@show'));
    Route::get('distributors/{id}/edit', 'DistributorController@edit');
    Route::put('distributors/{id}/update', array('as'=>'distributors.update', 'uses'=>'DistributorController@update'));
    Route::get('catalog_downloads','CatalogDownloadController@index');
    Route::get('catalog_downloads/{id}/download','CatalogDownloadController@file_download');
    

    定期分销商有资格获得8条路线。

    Route::get('distributors/{id}', array('before' =>'profile', 'uses'=>'DistributorController@show'));
    Route::get('distributors/{id}/edit', 'DistributorController@edit');
    Route::put('distributors/{id}/update', array('as'=>'distributors.update', 'uses'=>'DistributorController@update'));
    Route::get('catalog_downloads','CatalogDownloadController@index');
    Route::get('catalog_downloads/{id}/download','CatalogDownloadController@file_download');
    Route::get('marketing_materials','MarketingMaterialController@index');
    Route::get('marketing_materials/{id}/download/thumb_path','MarketingMaterialController@thumb_download');
    Route::get('marketing_materials/{id}/download/media_path','MarketingMaterialController@media_download');
    
      

    代码

      

    问题

    • 有人可以帮助我或至少指导我正确的方向

2 个答案:

答案 0 :(得分:1)

根据你的情况......

我建议:

  1. routes.php
  2. 中查看您的Auth::user()->type权限
  3. 请勿在检查用户类型条件之前忘记检查Auth :: check()
  4. 为OEM做,重复为非OEM提供相同的逻辑。
  5. 以下是代码 - 请修改以满足您的确切需求。

    <?
    
    // OEM Routes
    if(Auth::check()){
        if ( (Auth::user()->type == "Distributor") AND (Auth::user()->distributor()->first()->type == 'OEM') ){
    
            Route::group(array('before'=>'auth'),function() {
                Route::group(array('before'=>'csrf'),   function(){ 
    
                // Other important routes like sign-out, dashboard, or change password should also listed here
                Route::get('/account/sign-out',array('as'=>'account-sign-out','uses'=>'AccountController@getSignOut' ));
                Route::get('/dashboard', array('as' =>'dashboard','uses'=>'HomeController@dashboard'));
    
                // Allow routes
                Route::get('distributors/{id}', array('uses'=>'DistributorController@show'));
                Route::get('distributors/{id}/edit', 'DistributorController@edit');
                Route::put('distributors/{id}/update', array('as'=>'distributors.update', 'uses'=>'DistributorController@update'));
                Route::get('catalog_downloads','CatalogDownloadController@index');
                Route::get('catalog_downloads/{id}/download','CatalogDownloadController@file_download');
    
    
            }); 
        }
    }else{
        return Redirect::route('home'); // I assume you have this declare somewhere
    }
    
    
    
    // Not OEM Routes  
    if(Auth::check()){
        if ( (Auth::user()->type == "Distributor") AND (Auth::user()->distributor()->first()->type !== 'OEM') ){
    
            Route::group(array('before'=>'auth'),function() {
                Route::group(array('before'=>'csrf'),   function(){ 
    
                // Other important routes like sign-out, dashboard, or change password should also listed here
                Route::get('/account/sign-out',array('as'=>'account-sign-out','uses'=>'AccountController@getSignOut' ));
                Route::get('/dashboard', array('as' =>'dashboard','uses'=>'HomeController@dashboard'));
    
    
                // Allow routes 
                Route::get('marketing_materials','MarketingMaterialController@index');
                Route::get('marketing_materials/{id}/download/thumb_path','MarketingMaterialController@thumb_download');
                Route::get('marketing_materials/{id}/download/media_path','MarketingMaterialController@media_download');
    
                Route::get('distributors/{id}', array('uses'=>'DistributorController@show'));
                Route::get('distributors/{id}/edit', 'DistributorController@edit');
                Route::put('distributors/{id}/update', array('as'=>'distributors.update', 'uses'=>'DistributorController@update'));
                Route::get('catalog_downloads','CatalogDownloadController@index');
                Route::get('catalog_downloads/{id}/download','CatalogDownloadController@file_download');
    
    
            }); 
        }
    }else{
        return Redirect::route('home'); // I assume you have this declare somewhere
    }
    

答案 1 :(得分:0)

如你所知,我告诉你另一个问题的方法。我认为它更灵活,更易于维护。

我的最终目标是获得那种路线:

Route::group(['before' => 'acl.permitted'], function() {

    Route::get('/something', ['as' => 'some_route_alias', 'uses' => 'SomeController@someMethod']);
    Route::get('/something_else', ['as' => 'another_route_alias', 'uses' => 'AnotherController@anotherMethod']);
    ...
});

这样您acl.permitted过滤检查是否允许用户访问某条路线。我们来看看过滤器。

class AclPermissionFilter {
    public function filter ($route, $request) {
        $user = Auth::user();
        if ($user){

            // Maybe add a superuser break 

            $user->load('groups', 'groups.permissions');
            $permission = AclPermissions::where('route_alias', $route->getName())->get()->first();
            foreach ($user->groups as $group) {
                $pass = $group->permissions->contains($permission);
                if ($pass) break;
            }
        } else {
            // Maybe check for public scopes
        }
        return $pass; 
    }
}

现在我们可以声明过滤器了。例如,在过滤器文件中:

Route::filter('acl.permitted', 'AclPermittedFilter');

然后我们需要User-&gt; Group-&gt; Permission层次结构。

Class User {
    ...
    public function groups() {
        return $this-->belongstoMany('AclGroup','acl_user_groups','user_id','group_id');
    }
}

Class AclGroups {
    ...
    // Add here the attributes that you want. i.e: name, description,...  
    ...

    public function users() {
        return $this->belongsToMany('User', 'acl_user_groups');
    }
    public function permissions() {
        return $this->belongsToMany('AclPermission', 'acl_group_permissions');
    } 
}

Class AclPermissions {
    ...
    // Add here the attributes that you want. i.e: route_alias, description,... 
    ...

    public function groups() {
        return $this->belongsToMany('AclGroup', 'acl_group_permissions');
    }
    public function getKey() {
        return $this->attributes['route_alias'];
    } 
}

然后,您可以使用所有路由别名为权限表设定种子。

class AclTableSeeder extends Seeder {
    public function run(){
        foreach (Route::getRoutes() as $route){
            $action = $route->getAction();
            if (isset($action['as'])){
                AclPermissions::create(['route_alias' => $action['as']]);
            }
        }
    }
}

最后,您需要创建自己的群组(&#39;内部&#39;和#39;经销商&#39;在您的情况下),并将他们转移到适当的权限。如果您不想创建用户组权限维护,则可以在种子文件中执行此操作。

此解决方案的主要优点是您不必对许可的逻辑进行编码,而且它是一个更全球化的解决方案。另一方面,您可能需要在将来的某个时间创建组权限维护管理页面。

我希望它可以帮到你。