【问题标题】:Multiple Roles Filters in Laravel Controller ConstructorLaravel 控制器构造函数中的多个角色过滤器
【发布时间】:2014-11-14 16:25:22
【问题描述】:

我有一个关于为多个用户角色过滤控制器及其操作的问题。假设我有一个名为 MyController 的控制器:

public class MyController extends \BaseController
{
   public static function index()
   {
   }

   public static function show()
   {
   }

   public static function create()
   {
   }

   public static function store()
   {
   }

   public static function other()
   {
   }
}

我为每个角色设置了 2 个过滤器,分别命名为 admin 和 staff:

Route::filter('admin', function()
{
    // Lines of code to get role
    if($role != 'admin') return View::make('errors.401');
});

Route::filter('staff', function()
{
    // Lines of code to get role
    if($role != 'staff') return View::make('errors.401');
});

然后,我尝试在 MyController 的构造函数上使用 beforeFilter :

public function __construct()
{
    $this->beforeFilter('admin', ['only' => ['index', 'show', 'create', 'store']]);
    $this->beforeFilter('staff', ['only' => ['index', 'show']]);
}

当我添加第一个 beforeFilter 时,它按预期工作(当我以员工身份登录到我的应用程序时,我无法访问 index、show、create 和 store 方法)。但是当我添加第二个过滤器并再次以员工身份登录时,我无法访问索引和显示操作,我希望员工可以访问。

我的问题是,是否可以在控制器的构造函数中为多个角色定义过滤器? (在这种情况下,我想让管理员和员工可以访问操作索引和显示,但创建和存储只能由管理员访问)如果可能,我该如何实现?

谢谢。

【问题讨论】:

    标签: laravel laravel-4 filter acl before-filter


    【解决方案1】:

    管理员总是员工吗?如果是这样 - 你可以这样做:

    Route::filter('staff', function()
    {
        // Lines of code to get role
        if(($role != 'staff') && ($role != 'admin'))return View::make('errors.401');
    });
    

    【讨论】:

    • 不,管理员和员工是 2 个不同的角色,彼此无关。
    【解决方案2】:

    我假设您有管理员可以访问所有功能,并且工作人员可以访问除“显示”之外的所有内容

    这是控制器

    class MyController extends \BaseController
    {
       public function __construct(){
           $this->beforeFilter('admin', ['only' => ['show']]);
       }    
       public function index()
       {
        echo "index";
       }
    
       public function show()
       {
        echo "show";
       }
    }
    

    在你的上一篇文章中看到,你正在使用公共类,我相信在 PHP 中你只需要类,在函数中最好不要使用静态。

    这是filters.php

    Route::filter('admin', function()
    {
        // Lines of code to get role
        if($role != 'admin') return "This is only for admin";
    });
    

    在 routes.php 中

    Route::get("/my", "MyController@index");
    Route::get("/show", "MyController@show");
    

    然后尝试以管理员身份登录,您将可以访问“索引”和“显示” 然后尝试以员工身份登录,您将可以访问“索引”但无法访问“显示”

    【讨论】:

      【解决方案3】:

      首先你应该制作一个控制器来处理访问控制......如下所示

      Acl 控制器

       class ACLController extends \BaseController {
      
              /**
               * admin access control list
               * @return array
               */
              private function adminACL() {
                  return array(
                      'users' => array(
                          'users',
                          'users.show',
         //similar access list for admin users
      
                      ),
      
      
      
            );
              }
      
      
      
      /**
           * staff access control list
           * @return array
           */
          private function staffACL() {
              return array(
      
                  'staff' => array(
                      'staff',
                      'staff.index',
      //similar access list for staff user
      
                  ),
      
              );
          }
      
      //Method that check access of related user
      
          /**
           * check access level
           * @param string $value
           * @return boolean
           */
          public function hasAccessLevel($value) {
              $user = //get user role here
              if ($user->roles == 'staff') {
                  return TRUE;
      
                  } elseif ($user->roles == 'admin') {
                      $newAcl = array();
                      foreach ($this->adminACL() as $aclBreak) {
                          foreach ($aclBreak as $acl) {
                              $newAcl[] = $acl;
                          }
                      }
                      if (!in_array($value, $newAcl)) {
                          return FALSE;
                      } else {
                          return TRUE;
                      }
                  } else {
                      $newAcl = array();
                      foreach ($this->staffACL() as $aclBreak) {
                          foreach ($aclBreak as $acl) {
                              $newAcl[] = $acl;
                          }
                      }
                      if (!in_array($value, $newAcl)) {
                          return FALSE;
                      } else {
                          return TRUE;
                      }
                  }
              }
              }
      

      过滤访问路径...

      Route::filter('hasAccess',function($route,$request,$value){
          try{
              $Routeacl = new App\Controllers\ACLController();        
              if(!$acl->hasAccessLevel($value))
              {            
                  return Redirect::to('admin/dashboard')->withErrors(array(Lang::get('en.user_noaccess')));
              }
          }catch(\Exception $e){
              echo $e->getMessage();
          }
      });
      

      然后在您的路线中检查它是否可以访问

          Route::get('/', array('as' => 'index', 'before' => 'hasAccess:index', 'uses' => 'MyController@Index'));
      

      编码愉快:)

      【讨论】:

        猜你喜欢
        • 2013-06-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-06
        • 1970-01-01
        • 1970-01-01
        • 2023-04-09
        • 2016-04-17
        相关资源
        最近更新 更多