[李景山php]每天laravel[038]-laravel 系统服务 --- 授权-1

来源:互联网 发布:c语言中 a表示什么 编辑:程序博客网 时间:2024/05/21 20:38

Authentication:认证
Authorization:授权
授权是对认证的一种补充。
认证:认证用户是否登录。
授权:授权用户是否有权限。

使用:Illnminate\Auth\Access\Gate 类: AuthServiceProvider :
这个就是中级验证里面的, 用户只能删除自己的 list 列表的功能

翻译:
定义能力:
我们可以在 Illuminate\Auth\Access\Gate 类中定义“能力”,让用户可以很容易的操作一个指定的方法。
这个 AuthServiceProvider 将会关联laravel 服务来方便的定义网页应用的能力。
举个例子:
让我定义“update-post”操作,简单点说就是当前用户执行一个Post模型的操作,需要定义当前用户的ID,跟产生这条数据的ID一样才可以。

代码如下:

public function boot(GateContract $gate){     $this->registerPolicies($gate);     $gate->define('update-post',function($user,$post){          return $user->id === $post->user_id;     });}

可以通过:class Based Abilites;

$gate->define('update-post','Class@method');

提前检查:

$gate->before(function($user,$ability){     if($user->isSuperAdmin()){          return true;          }});

之后检查

$gate->after(function($user,$ability,$result,$atguments){});

检测Abilites:
通过 Gate Facade 验证进行验证。
这样几个方式:check,allows,denies 方法。

public function update($id){     $post = Post::findOrFail($id);     if(Gate::denies('update-post',$post)){         abort(403);      }}

针对于特殊用户的检测:

if(Gate::forUser($user)->allows('update-poot',$post)){}
0 0
原创粉丝点击