如何使Acces级别控制最简单的方法? [英] How to have Acces level Control the easiest way?

查看:47
本文介绍了如何使Acces级别控制最简单的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设计CMS,并已根据角色设置了用户.如何根据用户的访问级别限制其权限?

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?

推荐答案

最简单的方法是按角色来吸引用户.在用户表中有一列称为角色或您命名的列.

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

您可以通过

在您的app\Providers\AuthServiceProvider中注册您的策略.示例:

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是我们在项目刀片中定义要使用的用户类型,控制器.您可以随意更改它.Role是您在表中创建的列,我们正在与这些表值进行比较用户键入userdealer.

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.

您可以使用laravel方法限制刀片中的值

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.

这篇关于如何使Acces级别控制最简单的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆