gopex/laratrust-permission-creator
Composer 安装命令:
composer require gopex/laratrust-permission-creator
包简介
README 文档
README
Manage your Laratrust roles and permissions using a Code-First (Configuration-Driven) approach. Define your ACL structure in clean PHP files, keep them version-controlled via Git, and seamlessly synchronize them with your database in both directions.
Why Use This Package?
In large-scale applications, managing roles and permissions through database seeders or UI dashboards can become messy and hard to track across different environments (local, staging, production).
Laratrust Permission Creator solves this by allowing you to:
- 🛠️ Declare Everything in Code: Define roles, permissions, and their assignments in a simple PHP array.
- 🌿 Git Version Control: Track every change, addition, or deletion in your permission schema via Git.
- 🧩 Modular Files: Split your permissions into separate files (e.g., by module or feature) to avoid massive, unreadable configuration files.
- 🔄 Two-Way Sync: Push configuration changes to the database OR export an existing database schema back into code.
Installation
You can install the package via Composer:
composer require gopex/laratrust-permission-creator
After installing the package, run the publish command to automatically generate the initial laratrust directory and the template configuration file in your project's root:
php artisan vendor:publish --tag="laratrust-permissions-file"
This will create the following file in your project:
/laratrust/RoleAndPermissions.php
Configuration Structure
The generated laratrust/RoleAndPermissions.php file returns an array with 4 main keys. Let's break down each section with clear examples:
1. Splitting Files (fromFile)
If your application is large, don't put everything in one file. You can split your configurations into multiple separate files and link them here. The paths must be relative to your project's root directory.
"fromFile" => [ "laratrust/BasicRolePermissions.php", "laratrust/TraderRequestPermissions.php", ],
2. Defining Permissions (permissions)
You can define permissions in two ways:
- Simple (String): Just the permission name.
display_nameanddescriptionwill be empy. - Advanced (Array): Provide a custom
display_nameanddescription.
"permissions" => [ // 1. Simple Format (No extra details needed) "edit_posts", // 2. Advanced Format (With custom display name and description) "delete_users" => [ "display_name" => "Delete Registered Users", // Optional "description" => "Allows a user to permanently delete accounts" // Optional ] ],
3. Defining Roles (roles)
Just like permissions, roles can be defined as a simple string or a detailed array:
"roles" => [ // 1. Simple Format "moderator", // 2. Advanced Format "super_admin" => [ "display_name" => "Super Administrator", // Optional "description" => "Has full control over the entire system" // Optional ] ],
4. Binding Roles to Permissions (rolesPermissions)
This is where you connect your roles to their respective permissions.
"rolesPermissions" => [ // Bind existing permissions to an existing role "super_admin" => [ "edit_posts", "delete_users", ], // Smart Behavior Example: "support_agent" => [ "edit_posts", "view_logs" // This permission will be auto-created even if missing in the 'permissions' array! ] ]
💡 Key Rules & Smart Behaviors (How it works under the hood)
To avoid breaking your application, the package follows these strict yet flexible rules during synchronization:
- Auto-Creation: If you add a role or permission inside the
rolesPermissionsrelation array, but forgot to declare it under the main"roles"or"permissions"tags, the package will safely create it anyway. - Updates: If you change the
display_nameordescriptionof a role/permission in the file, runningphp artisan laratrust:from-configwill instantly update those values in the database. - No Redundancy Required (Independent Definition): You do not have to link every single permission or role to each other.
- If you define a permission under the
"permissions"key but leave it out of"rolesPermissions", it will still be created/updated in the database. - Similarly for roles: If you define a role under the
"roles"key but do not assign any permissions to it inside"rolesPermissions", it will still be successfully created/updated in your database for future use or manual assignment.
- If you define a permission under the
Usage & Artisan Commands
This package provides two simple yet powerful commands to handle two-way synchronization.
1. Synchronize Config to Database
When you add, modify, or remove roles and permissions inside your PHP files, run the following command to detect and apply changes directly to your database:
php artisan laratrust:from-config
Automatically handles insertions, updates, and schema integrity.
2. Export Database to Config (Reverse Sync)
If you are introducing this package into an existing project that already has roles and permissions populated in the database, you can update or generate your configuration file based on your current database state by running:
php artisan laratrust:from-db
Perfect for legacy projects or migrating to a code-first approach without losing existing data.
Contributing
Contributions are welcome! Please feel free to submit Pull Requests or open Issues for bugs and feature requests.
License
The MIT License (MIT). Please see License File for more information.
gopex/laratrust-permission-creator 适用场景与选型建议
gopex/laratrust-permission-creator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 01 月 29 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 gopex/laratrust-permission-creator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 gopex/laratrust-permission-creator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 8
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 12
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: GPL3
- 更新时间: 2024-01-29