goldoni/laravel-model-permissions
Composer 安装命令:
composer require goldoni/laravel-model-permissions
包简介
Model-scoped and global permission sync, base policies, role provisioning, and Super Admin override (Spatie-compatible).
关键字:
README 文档
README
Model-scoped and global permissions with a clean naming convention, optional Spatie integration, a reusable base policy, Super Admin override, and artisan sync tooling — built with SOLID principles.
✨ Features
- 🔑 Permission keys:
modelNameAbility(camelCase), e.g.userViewAny,orderUpdate,blogPostDeleteAny - 🧱 Policies:
BaseModelPolicy+ChecksModelPermissionstrait - 🦸 Super Admin: configurable role bypass via
Gate::before - 👥 Roles: Super Admin, Manager, User (configurable)
- 🧩 Global permissions (not tied to any model), e.g.
impersonate - ⚙️ Artisan sync: generate permissions, create roles, assign permissions
- 🧠 SOLID design: services, interfaces, swappable repositories (Spatie or null)
📦 Installation
composer require goldoni/laravel-model-permissions
Spatie is optional but recommended for storing roles and permissions.
composer require spatie/laravel-permission
php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider"
php artisan migrate
Publish this package config:
php artisan vendor:publish --tag="model-permissions-config"
⚙️ Configuration
config/model-permissions.php (excerpt)
return [ 'guard_name' => 'web', 'super_admin_role' => 'Super Admin', 'roles' => [ 'super_admin' => 'Super Admin', 'manager' => 'Manager', 'user' => 'User', ], 'models' => [ App\Models\User::class, App\Models\Event::class, App\Models\Order::class, App\Models\Customer::class, ], 'abilities' => [ 'viewAny','view','create','update','delete','deleteAny', 'restore','restoreAny','forceDelete','forceDeleteAny', 'replicate','reorder','attach','attachAny','detach','detachAny', ], 'global_permissions' => [ 'impersonate','accessAdmin','viewReports', ], 'role_ability_map' => [ 'Super Admin' => ['*'], 'Manager' => ['viewAny','view','create','update','delete','deleteAny','restore','replicate','reorder','attach','attachAny','detach','detachAny'], 'User' => ['viewAny','view','create','update'], ], 'role_model_ability_map' => [ 'Manager' => [ App\Models\User::class => ['viewAny','view','update','delete','deleteAny'], App\Models\Event::class => ['*'], App\Models\Order::class => ['*'], App\Models\Customer::class => ['viewAny','view','create','update'], ], 'User' => [ App\Models\Ticket::class => ['viewAny','view','create'], App\Models\Order::class => ['viewAny','view'], App\Models\Event::class => ['viewAny','view'], ], ], 'role_global_permissions' => [ 'Super Admin' => ['*'], 'Manager' => ['viewReports','accessAdmin'], 'User' => [], ], ];
🚀 Quick Start
Generate permissions for your configured models and abilities:
php artisan model-permissions:sync
Preview without writing anything:
php artisan model-permissions:sync --dry
Create roles and assign permissions from the config maps:
php artisan model-permissions:sync --with-roles
Replace existing role assignments instead of adding:
php artisan model-permissions:sync --with-roles --reset
🧭 Naming Convention
Permission key = lcfirst(class_basename(Model)) . ucfirst(ability)
Examples:
App\Models\User+viewAny→userViewAnyApp\Models\Order+update→orderUpdateApp\Models\BlogPost+deleteAny→blogPostDeleteAny
Global permissions are plain strings, e.g. impersonate.
🧑⚖️ Using Policies
Extend the base policy and set the model class:
namespace App\Policies; use App\Models\Post; use Goldoni\ModelPermissions\Policies\BaseModelPolicy; class PostPolicy extends BaseModelPolicy { protected string $modelClass = Post::class; }
Register in your AuthServiceProvider as usual.
Then authorize as you normally do:
$user->can('postUpdate'); $user->can('postDeleteAny');
🦸 Super Admin Override
If the authenticated user has the configured super_admin_role on the configured guard_name, all checks are allowed via Gate::before. Change both in the config to fit your app.
🌍 Global Permissions
Add any app-wide abilities under global_permissions, for example:
'global_permissions' => ['impersonate','accessAdmin']
Assign them per role via:
'role_global_permissions' => [ 'Super Admin' => ['*'], 'Manager' => ['accessAdmin'], ]
Check them with:
$user->can('impersonate');
🧠 Architecture (SOLID)
-
Services:
SyncPermissionsService(build + sync permission names)RoleAssignmentService(resolve + assign role permissions)
-
Interfaces:
AuthorizationRepositoryInterface(storage abstraction)PermissionNamerInterface(naming strategy)
-
Repositories:
SpatieAuthorizationRepositoryNullAuthorizationRepository(no-op when Spatie is absent)
This keeps the command thin and your domain logic testable and replaceable.
🧰 Troubleshooting
-
❗
BindingResolutionExceptionfor services Runcomposer dump-autoload -oand ensure the service provider is auto-discovered (it is viaextra.laravel.providers). -
❗ Permissions created but roles not updated Use
--with-rolesand optionally--reset. Ensure Spatie tables are migrated. -
❗ Super Admin does not bypass Verify the role name and guard in
config/model-permissions.php, and that the user actually has the role on that guard. -
❗ Keys not matching your expectations Confirm models and abilities in config. Naming uses the class basename lowercased first letter +
ucfirst(ability).
🤝 Contributing
Issues and PRs are welcome. Keep code PSR-12, no inline comments, English identifiers.
📄 License
MIT © Goldoni Bogning Fouotsa
goldoni/laravel-model-permissions 适用场景与选型建议
goldoni/laravel-model-permissions 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 09 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「authorization」 「nova」 「acl」 「laravel」 「Policy」 「roles」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 goldoni/laravel-model-permissions 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 goldoni/laravel-model-permissions 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 goldoni/laravel-model-permissions 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A Laravel Nova card that shows you your system information.
A Laravel Nova card.
Laravel Multiauth package
Ory-Hydra OAuth 2.0 Client Provider for The PHP League OAuth2-Client
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.
A Laravel Nova package for publishable fields
统计信息
- 总下载量: 1
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 24
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-09-13