laravel authorize(授权)
1.方法一 直接在AuthServiceProvider 中定义闭包,比较灵活
namespace App\Providers;
...
class AuthServiceProvider extends ServiceProvider
{
... public function boot(GateContract $gate)
{
parent::registerPolicies($gate);
//使用闭包定义授权能力
$gate->define('update-post', function($user, $post){
return $user->id == $post->user_id;
});
//使用类函数定义
$gate->define('update-post', 'PostPolicy@update');
}
}
namespace App\Http\Controllers;
...
//在控制器中检验授权
class PostController extends Controller
{
public function show($id)
{
//auth()->logout();
auth()->loginUsingId(3);
$post = Post::findOrFail($id);
// if(Gate::denies('show-post', $post)){
// abort(403, 'sorry, not sorry!');
// }
//$this->authorize('update-post', $post);
return view('posts.show', compact('post'));
}
}
//在view中检验授权,如果通过才显示update链接
<h2>{{$post->title}}</h2>
@can('update-post', $post)
<a href=#>update</a>
@endcan
2.方法二 ,注册策略类来实现
建立policy
php artisan make:policy PostPolicy
注册 模型 =>策略
namespace App\Providers;
...
class AuthServiceProvider extends ServiceProvider
{ protected $policies = [
// 'App\Model' => 'App\Policies\ModelPolicy',
\App\Post::class => \App\Policies\PostPolicy::class,
];
定义类策略函数
namespace App\Policies; use App\User;
use App\Post; class PostPolicy
{ //可以建立多个检验方法对应不同场景
public function update(User $user, Post $post)
{
return $user->id == $post->user_id;
}
}
使用方法同上。
laravel authorize(授权)的更多相关文章
- Laravel Gate 授权方式的使用指南
参考链接:An Introduction to Laravel Authorization Gates 本文使用 Laravel 的 Gate 授权方式 实现一个基于用户角色的博客发布系统. 在系统包 ...
- laravel 创建授权策略
用户只能编辑自己的资料 在完成对未登录用户的限制之后,接下来我们要限制的是已登录用户的操作,当 id 为 1 的用户去尝试更新 id 为 2 的用户信息时,我们应该返回一个 403 禁止访问的异常.在 ...
- MVC源码分析 - Authorize授权过滤器
从 上一篇 其实能看到, 程序执行的过滤器, 有四种 : 过滤器类型 接口 描述 Authorization IAuthorizationFilter 此类型(或过滤器)用于限制进入控制器或控制器的某 ...
- mvc - Authorize授权
from : http://www.cnblogs.com/asks/p/4372783.html http://www.cnblogs.com/myindex/p/5479428.html
- SimpleSSO:使用Microsoft.Owin.Security.OAuth搭建OAuth2.0授权服务端
目录 前言 OAuth2.0简介 授权模式 (SimpleSSO示例) 使用Microsoft.Owin.Security.SimpleSSO模拟OpenID认证 通过authorization co ...
- [一步一步MVC]第二回:还是ActionFilter,实现对业务逻辑的统一Authorize处理 OnActionExecuting内如何获取参数
如何获取参数:http://www.cnblogs.com/anytao/archive/2009/04/23/anytao-mvc-02-actionauthorize.html 由问题引出 在AS ...
- Spring Cloud2.0之Oauth2环境搭建(授权码模式和密码授权模式)
oauth2 server 微服务授权中心, github源码 https://github.com/spring-cloud/spring-cloud-security 对微服务接口做一些权 ...
- 微信授权就是这个原理,Spring Cloud OAuth2 授权码模式
上一篇文章Spring Cloud OAuth2 实现单点登录介绍了使用 password 模式进行身份认证和单点登录.本篇介绍 Spring Cloud OAuth2 的另外一种授权模式-授权码模式 ...
- Spring Authorization Server授权服务器入门
11月8日Spring官方已经强烈建议使用Spring Authorization Server替换已经过时的Spring Security OAuth2.0,距离Spring Security OA ...
随机推荐
- 亿条数据在PHP中实现Mysql数据库分表100张
当数据量猛增的时候,大家都会选择库表散列等等方式去优化数据读写速度.笔者做了一个简单的尝试,1亿条数据,分100张表.具体实现过程如下: 首先创建100张表: $i=0; while($i<=9 ...
- 让html元素随浏览器的大小自适应垂直居中
转自:http://www.cnblogs.com/linjiqin/archive/2011/06/15/2081362.html 表格可以实现td中的元素垂直居中显示,但是前提条件必须定义td的高 ...
- MySQL出现无法删除行记录
今天mysql在删除一张InnoDB类型的表时,出现错误Error No. 1451 MYSQL: Cannot delete or update a parent row: a foreign ke ...
- 编译fdk-aac for ios
Build all: build-fdk-aac.sh Build for some architectures: build-fdk-aac.sh armv7s x86_64 Build unive ...
- http://jingyan.baidu.com/article/d169e186aa8728436611d8f3.html
http://jingyan.baidu.com/article/d169e186aa8728436611d8f3.html
- 在python包管理中使用easy_install软件的步骤
本文主要介绍的是让python包管理变得更加容易的实际应用方法,就是运用easy_install这一软件,下面是文章的具体介绍. easy_install让python包管理变得 如果你想对Pytho ...
- Google Code Jam 2010 Round 1B Problem A. File Fix-it
https://code.google.com/codejam/contest/635101/dashboard#s=p0 Problem On Unix computers, data is s ...
- git 创建branch分支
开发者user1 负责用getopt 进行命令解析的功能,因为这个功能用到getopt 函数,于是将这个分支命名为user1/getopt.(1)确保是在开发者user1的工作区中cd /home/j ...
- eWebeditor编辑器上传图片路径错误解决方法[疑难杂症]【转,作者:unvs】
做了一个多版本的网站,后台用的编辑器是eWebeditor,NET版,后面发现上传图片或者文件之后,路径错误无法显示,必须手工修改才行.. 为了更清楚的说明问题,我下面会说的比较详细,首先是网站文件框 ...
- linux系统tomcat启动正常访问不到主页面
环境: linux系统 tomcat 6.0.24 jdk 1.6 防火墙的问题: 查看防火墙命令:chkconfig --list | grep -i iptables 关闭防火墙命令:/sbin/ ...