doutu1939 2019-02-13 06:29
浏览 29
已采纳

如何让访问级别控制最简单?

I am designing a CMS and i have setup users based on the role.How do i limit the users of their permissions based on their access level?

  • 写回答

1条回答 默认 最新

  • douxingmou4533 2019-02-13 06:47
    关注

    The easiest way is to get users by their role. Have a column for your users table called role or whatever you name it.

    You can do Access Level Control easily with Gates

    In your app\Providers\AuthServiceProvider register your policy. Example:

    use Illuminate\Support\Facades\Gate;
    use Illuminate\Contracts\Auth\Access\Gate as GateContract;
    
    public function boot(GateContract $gate)
        {
    
            $this->registerPolicies($gate);
    
            $gate->define('isUser', function($user){
              return $user->role == 'user';
            });
    
            $gate->define('isDealer', function($user){
              return $user->role == 'dealer';
            });
        }
    

    isUser , isDealer are the user Types we are defining to Use in the project blade,controllers.You can change it as you like.Role is the column that you created in the table and we are comparing with the table values which are the user types user and dealer.

    you can limit values in blade with laravel method

    @can('isUser')
    <only visible to users based on role user>
    @endcan
    

    It will be still accessible via routes so you can limit via controller functions or routes.

    //controller
    public function create()
    {
       if(!Gate::allows('isUser')){  // || for multiple parameters  can('isAdmin' || 'isUser)
                abort(404,"Abort");
       }
       return view('yourView');
    }
    
    

    This way the controller function will be not accessible for the roles defined. Check the official documentation for in detail methods and information.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 Ubuntu20.04无法连接GitHub
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题
  • ¥15 multisim电路设计
  • ¥20 用keil,写代码解决两个问题,用库函数
  • ¥50 ID中开关量采样信号通道、以及程序流程的设计
  • ¥15 U-Mamba/nnunetv2固定随机数种子
  • ¥30 C++行情软件的tick数据如何高效的合成K线