kyawzinthet / role-permissions
Laravel role-permissions package
Installs: 129
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 0
Forks: 1
Open Issues: 0
pkg:composer/kyawzinthet/role-permissions
Requires
- php: ^8.2|^8.3|^8.4
README
Laravel package to manage roles and CRUD-style permissions per module.
This package provides simple Role and Permission models, migrations, and artisan commands to install default roles and permissions from a config file. Permissions are modelled with per-action booleans (create, read, update, delete) and can be attached to roles. User-level permission overrides are stored separately.
Current features
- Eloquent models:
Role,Permission(with soft deletes). - Permissions store per-action booleans:
create,read,update,delete. - Many-to-many relation between
rolesandpermissionsviarole_permissionspivot. The pivot stores the per-action booleans for a role-permission relationship. - A
user_permissionstable is present to store user-specific overrides for permissions. - Config-driven permission definitions in
config/permissions.phpand an artisan command to install or update permissions from that config. - A small command to create a default
super-adminrole. - Service provider that publishes the config, loads package migrations, and registers console commands.
Files of interest
config/permissions.php— default permission definitions (name, slug, create/read/update/delete booleans).src/Models/Permission.php— permission model withroles()relationship.src/Models/Role.php— role model withpermissions()relationship.src/Helpers/PermissionHelper.php— helper file (currently empty) intended for permission resolution logic.src/Providers/RolePermissionsServiceProvider.php— registers config, migrations and commands.src/Commands/InstallPermissionsCommand.php— readsconfig/permissions.phpandupdateOrCreatepermissions.src/Commands/InstallRolesCommand.php— createssuper-adminrole if absent.database/migrations/*— migrations forroles,permissions,role_permissions, anduser_permissions.
Database schema (summary)
-
roles:id,name,slug(unique), timestamps, soft deletes
-
permissions:id,name,slug(unique),create,read,update,delete(booleans), timestamps, soft deletes
-
role_permissions(pivot):id,role_id,permission_id,create,read,update,delete(booleans), timestamps
-
user_permissions:id,user_id,permission_id,create,read,update,delete(booleans), timestamps
Note: role_permissions currently does not set default boolean values or foreign key constraints in the migration — see "Gaps" below.
How to install
- Require the package via composer (if published) or add to your project as a local package.
composer require kyawzinthet/role-permissions
- Publish config:
php artisan vendor:publish --provider="KyawZinThet\RolePermissions\Providers\RolePermissionsServiceProvider" --tag=config
- Run migrations to create the tables:
php artisan migrate
- Seed default roles and permissions from config via command:
php artisan install:role php artisan install:permissions
Usage notes
- Permissions are defined in
config/permissions.phpas an array of permission objects. Each permission has aslugand per-action booleans. Runninginstall:permissionswill ensure thepermissionstable matches the config. - Roles have a many-to-many relationship with permissions. The pivot
role_permissionsstores the allowed actions for that role on the permission. - User-level overrides are stored in
user_permissions. The package does not currently include helpers to evaluate permissions or assign roles to users —src/Helpers/PermissionHelper.phpis intentionally present as the place to implement permission resolution logic.
Dynamic user model & trait usage
This package resolves your application's user model dynamically using Laravel's auth provider configuration. The migration and model helpers use:
Config::get('auth.providers.users.model', \App\Models\User::class);
What this means for you:
- Set your user model in
config/auth.phpunderproviders.users.modelif you use a custom user class or a different namespace. Example:
'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\Models\User::class, ], ],
-
The
user_permissionsmigration will instantiate your user model to determine the user table name, primary key name, and key type. If your user primary key is a string (for exampleuuidorulid), the migration will create astring('user_id')column; otherwise it will create anunsignedBigInteger('user_id')column and add a foreign key referencing your users table. -
Because the migration instantiates your user model at migration time, make sure your user model can be constructed without requiring application services that are not available during migrations. If you prefer, you can modify the migration to hard-code the table/key types for your project.
Example User model usage
Add the trait and the relations expected by the package to your application User model. The included PermissionHelper trait calls roles() and permissions() on the user, so those relationships should exist.
<?php namespace App\Models; use Illuminate\Foundation\Auth\User as Authenticatable; use KyawZinThet\RolePermissions\Traits\PermissionHelper; use KyawZinThet\RolePermissions\Models\Role; use KyawZinThet\RolePermissions\Models\Permission; class User extends Authenticatable { use PermissionHelper; public function role(): BelongsTo { return $this->belongsTo(Role::class); } public function permissions(): BelongsToMany { return $this->belongsToMany(Permission::class, 'user_permissions') ->withPivot('create', 'read', 'update', 'delete'); } }
Using the trait
hasRole(string $role)— returnstrueif the user has a role with the given slug.hasPermission(string $permission)— returnstrueif the user has the permission slug directly or via any of their roles.
Example:
if (auth()->user()->hasPermission('post')) { // allowed } if (auth()->user()->hasRole('admin')) { // admin-only logic }
Migration note
- Run
php artisan migrateafter you have setconfig/auth.phpto your intended user model so theuser_permissionsmigration detects the correct user table and key type. - If your user model uses dependency injection in its constructor or otherwise cannot be instantiated during migrations, consider editing the migration to use explicit table/key names or ensure the model can be constructed without side effects.
Example (planned) permission check API
- PermissionHelper::can($user, 'user', 'read') // returns boolean
- Middleware example:
->middleware('permission:user,read')
License
MIT