thepinecode / policy
Requires
- php: ^7.2.5 | ^8.0
- laravel/framework: ^6.0 || ^7.0 || ^8.22.1
Requires (Dev)
- fzaninotto/faker: ^1.9.1
- laravel/laravel: ^8.0
- mockery/mockery: ^1.3.1
- phpunit/phpunit: ^9.0
README
在客户端使用Laravel的授权。
适用于SPA和前端重型应用的优秀工具。
如果您想了解包的内部实现,建议阅读以下文章:在客户端实现Laravel的授权。
目录
入门
您可以使用composer安装该包,运行composer require thepinecode/policy
命令。
由于该包支持自动发现,Laravel将自动在幕后注册服务提供者。
在某些情况下,您可能需要禁用此包的自动发现。您可以将提供者类添加到dont-discover
数组中禁用它。然后您需要手动再次注册它。
发布和设置JavaScript库
默认情况下,该包提供了一个Gate.js
文件,该文件将处理策略。使用php artisan vendor:publish
命令并选择Pine\Policy\PolicyServiceProvider
提供者。发布后,如果您使用Laravel 5.7+,可以在resources/js/policies
文件夹中找到您的最新副本。如果您的应用程序低于5.7,JS将在resources/assets/js/policies
中发布。
设置Gate.js
然后您可以将Gate
类导入并分配给window
对象。
import Gate from './policies/Gate'; window.Gate = Gate;
初始化Gate实例
从这一点开始,您可以从应用程序的任何地方初始化翻译服务。
let gate = new Gate;
将用户传递给Gate实例
Gate
对象需要传递的用户才能正常工作。这可以是一个string
或一个object
。默认情况下,它查找window['user']
对象,但您也可以自定义键或对象本身。
let gate = new Gate; // window['user'] let gate = new Gate('admin'); // window['admin'] let gate = new Gate({ ... }); // uses the custom object
注意,您可以传递任何对象作为用户。如果您传递团队或组对象,它也可以正常工作。由于您定义了
Gate
背后的逻辑,您可以传递任何您希望的对象。
将其作为Vue服务使用
如果您想直接从Vue模板中使用它,您可以轻松扩展Vue。
Vue.prototype.$Gate = new Gate;
<template> <div v-if="$Gate.allow('view', model)">...</div> </template>
computed: { hasPermission: { return this.$Gate.allow('view', this.model); } }
@currentUser blade指令
为了使其更快,该软件包附带了一个@currentUser
blade指令。这并不会做更多的事情,只是将当前认证用户以JSON
的形式打印出来,并将其分配给window
对象。
@currentUser <!-- Result --> <script>window['user'] = { ... };</script>
您可以覆盖用户默认的键。您可以通过传递一个字符串给blade指令来实现这一点。
@currentUser ('admin') <!-- Result --> <script>window['admin'] = { ... };</script>
如果没有认证用户,值将是
null
。
使用策略和Gate.js
可用方法
allow()
allow()
接受两个参数。第一个是执行的操作,第二个是模型对象或模型名称,例如在Laravel中。
注意:模型名称应该是Laravel中实际模型名称的小写版本:例如
Comment
变为comment
。
gate.allow('view', model); gate.allow('create', 'comment');
deny()
deny()
具有与allow()
相同的签名,但它将否定其返回值。
gate.deny('view', model); gate.deny('create', 'comment');
before()
类似于Laravel,在before()
方法中,您可以提供自定义逻辑来检查特殊条件。如果条件通过,则allow()
或deny()
中的其余策略规则将完全不会运行。然而,如果条件失败,策略规则将会执行。要使用before()
方法,您可能需要扩展gate对象并定义您的自定义逻辑。
Gate.prototype.before = function () { return this.user.is_admin; }
请注意,为了正确使用
this
对象,请使用传统的函数签名而不是箭头函数(() => {})。
将UsesModelName
特性添加到模型中
由于策略使用真实的JSON形状的eloquent模型,因此模型必须使用Pine\Policy\UsesModelName
特性,该特性生成正确的模型名称。这个模型名称属性被Gate.js
用于通过模型匹配适当的策略。
use Pine\Policy\UsesModelName; use Illuminate\Database\Eloquent\Model; class Comment extends Model { use UsesModelName; protected $appends = ['model_name']; }
请注意,为了能够在前端使用此属性,该属性必须附加到JSON表单中。您可以在文档中了解更多关于向JSON附加值的信息。
使用Artisan生成策略
该软件包默认附带了一个Artisan命令,该命令可以帮助您轻松生成JavaScript策略。要创建策略,运行php artisan make:js-policy Model
命令,其中Model
是模型的名称。
php artisan make:js-policy Comment
此命令将在resources/js/policies
目录中紧邻Gate.js
创建CommentPolicy.js
文件。如果您使用的是低于Laravel 5.7的版本,策略将在resources/assets/js/policies
目录中生成。
注意,该命令将自动在文件名中附加
Policy
。这意味着您在运行命令时可以仅传递模型名称。
在您生成了策略文件后,使用npm
编译所有JavaScript,包括策略。
重要!
策略会自动注册。这意味着不需要手动导入。门实例将 自动 填充策略。每个策略将在与模型 model_name
属性匹配的位置使用。
基于 Laravel 的默认 app.js,当调用 npm run dev
、npm run prod
等命令时,门实例 会自动注册策略。
编写策略规则
策略(如 Laravel 中)默认有以下方法:viewAny
、view
、create
、update
、restore
、delete
和 forceDelete
。当然,您也可以使用自定义方法,策略可以完全自定义。
... view(user, model) { return user.id == model.user_id; } create(user) { return user.is_admin; } approve(user, model) { return user.is_editor && user.id == model.user_id; } ...
示例
// app.js Vue.prototype.$Gate = new Gate; Vue.component('posts', { mounted() { axios.get('/api/posts') .then(response => this.posts = response.data); }, data() { return { posts: [], }; }, template: ` <ul><li v-for="post in posts" v-if="$Gate.allow('update', post)"></li></ul> <button v-if="$Gate.allow('create', 'post')">Create post</button> ` }); let app = new Vue({ // })
<body> <posts></posts> @currentUser <script src="{{ asset('js/app.js') }}"></script> </body>
贡献
如果您发现了错误或者有关于该包的想法,请随意打开一个 issue。