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 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题