dynamik-dev/laravel-policy-engine
Composer 安装命令:
composer require dynamik-dev/laravel-policy-engine
包简介
A Laravel-native scoped permissions package. Composable, contract-driven authorization that integrates with Gates, Policies, middleware, and Blade.
README 文档
README
A letter of marque was a document issued by a government that turned pirates into privateers, granting them scoped permission to plunder in specific waters. This package does the same thing for Laravel (minus the plundering). A user can be an admin in one team and a viewer in another. Deny rules, permission boundaries, and JSON policy documents are built in. The whole thing plugs into Laravel's Gate.
composer require dynamik-dev/marque
Quick look
Marque::createRole('admin', 'Admin') ->grant(['members.*', 'posts.*']) ->assignTo($user, scope: $acmeTeam); Marque::createRole('viewer', 'Viewer') ->grant(['posts.read']) ->assignTo($user, scope: $widgetTeam); $user->can('members.remove', $acmeTeam); // true $user->can('members.remove', $widgetTeam); // false
Roles, boundaries, and deny rules can live in JSON files you import at deploy time:
{
"roles": [
{
"id": "editor",
"permissions": ["posts.*", "comments.create", "!posts.delete"]
}
],
"boundaries": [
{ "scope": "plan::free", "max_permissions": ["posts.read", "comments.read"] },
{ "scope": "plan::pro", "max_permissions": ["posts.*", "comments.*", "analytics.*"] }
]
}
php artisan marque:import policies/production.json
Features
Wired into the Gate
$user->can(), @can, $this->authorize(), and can: middleware all work without any extra wiring.
$user->assignRole('editor', $acmeOrg); $user->can('posts.create', $acmeOrg); // true Route::middleware('can:posts.create')->post('/posts', [PostController::class, 'store']);
@can('posts.create', $team) <button>New Post</button> @endcan
Deny rules
Prefix any permission with !. The denial overrides every other role that grants it.
Marque::createRole('editor', 'Editor') ->grant(['posts.*', 'comments.*']) ->deny(['posts.delete']); $editor->can('posts.create'); // true $editor->can('posts.delete'); // false -- deny wins
Permission boundaries
Boundaries set a ceiling on what any role can do inside a scope. A user with admin in a free-tier org still can't access pro-tier features. Pass a scope string or any Scopeable model.
Marque::boundary($freeOrg)->permits(['posts.read', 'comments.read']); Marque::boundary($proOrg)->permits(['posts.*', 'comments.*', 'analytics.*']); $user->assignRole('admin', $freeOrg); $user->can('analytics.view', $freeOrg); // false -- boundary blocks it $user->can('analytics.view', $proOrg); // true
Wildcards
'posts.*' // all post actions '*.read' // read anything '*.*' // superadmin 'posts.update.own' // fine-grained qualifiers
Resource policies
Attach authorization rules directly to a resource type. The when() closure receives the user and the resource instance.
Marque::resource(Post::class) ->allow('update') ->when(fn ($user, $post) => $post->author_id === $user->id); Marque::resource(Post::class) ->deny('delete');
Contract-driven
Every component implements a PHP interface. You can swap any implementation through the service container. See Swapping implementations.
Why not Spatie?
Spatie laravel-permission works well for flat RBAC. Marque adds scoped roles, deny rules, permission boundaries, and declarative policy documents. See the full comparison.
Requirements
| Dependency | Supported Versions |
|---|---|
| PHP | 8.4, 8.5 |
| Laravel | 12, 13 |
| PostgreSQL | 17+ |
| SQLite | 3.35+ |
| Valkey / Redis | 8+ |
SQLite works out of the box for development. PostgreSQL and Valkey are optional — the package tests against both in CI. MySQL is not officially supported but should work fine since Laravel's query builder abstracts the differences.
Documentation
Getting Started — How authorization works | Installation | Permission naming | Seeding permissions and roles
Authorization — Checking permissions | Roles | Scoped permissions | Deny rules | Boundaries | Resource policies
Integrations — Middleware | Blade | Model policies | Sanctum
Policy Documents — Document format | Import / Export
Extending — Swapping implementations | Events | Cache
Testing — Testing authorization
Reference — Configuration | Contracts | Facade | Events | Artisan commands | Comparison with Spatie
dynamik-dev/laravel-policy-engine 适用场景与选型建议
dynamik-dev/laravel-policy-engine 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4 次下载、GitHub Stars 达 11, 最近一次更新时间为 2026 年 04 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「authorization」 「laravel」 「Policy」 「roles」 「permissions」 「rbac」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 dynamik-dev/laravel-policy-engine 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 dynamik-dev/laravel-policy-engine 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 dynamik-dev/laravel-policy-engine 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Ory-Hydra OAuth 2.0 Client Provider for The PHP League OAuth2-Client
Simple and configurable SilverStripe module to notify users about cookie policies
A module for CSP headers in Silverstripe.
A lightweight and powerful OAuth 2.0 authorization and resource server library with support for all the core specification grants. This library will allow you to secure your API with OAuth and allow your applications users to approve apps that want to access their data from your API.
Laravel JWT auth service package
TYPO3 Access Control Component.
统计信息
- 总下载量: 4
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 11
- 点击次数: 38
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-04-09
