alifcoder/permissions

A simple role and permission management package for Laravel.

v1.0.11 2025-05-20 05:41 UTC

This package is auto-updated.

Last update: 2025-05-20 11:08:16 UTC


README

A simple, flexible role and permission management system for Laravel applications โ€” designed to support Gate::before, SUPER ADMIN logic, modular apps (nwidart/laravel-modules), and dynamic user model resolution.

โœจ Features

  • Role and permission management with pivot tables
  • SUPER ADMIN bypass support using Gate::before()
  • HasRoles trait for easy user integration
  • Dynamically configurable user model
  • Language file localization (EN, customizable)
  • Clean service provider with publishable config and migrations
  • Works with modular Laravel apps (like nwidart/laravel-modules)

๐Ÿ“ฆ Requirements

  • PHP >=8.2
  • Laravel ^11.0 || ^12.0

๐Ÿš€ Installation

composer require alifcoder/permissions

Then publish the config, translations, and migrations:

php artisan vendor:publish --tag=permissions
php artisan migrate

This will publish:

  • lang/vendor/permissions
  • config/permission.php
  • database/migrations/xxxx_xx_xx_xxxxxx_create_permissions_table.php

โš™๏ธ Configuration

Inside config/permissions.php:

return [
    'models'    => [
        'role'       => \Alif\Permissions\Models\Role::class,
        'permission' => \Alif\Permissions\Models\Permission::class,
    ],
    
    'cacheable' => true,
    
    'is_model_uuid' => true,
        
];

You can override the default user model or super-admin slug here.

๐Ÿงฌ Traits

In your User model, add the trait:

use Alif\Permissions\Traits\HasRoles;

class User extends Authenticatable
{
    use HasRoles;
}

๐Ÿ” Super Admin Access

Add this in your app (e.g., AuthServiceProvider) โ€” or it's auto-registered by the package:

Gate::before(function ($user, $ability) {
    return $user->isSuperAdmin() ? true : null;
});

This lets super-admin users bypass all policy/gate checks.

๐Ÿง  Usage

Assign Roles & Permissions

$admin = Role::create(['name' => 'Admin', 's_code' => 'admin']);
$edit = Permission::create(['name' => 'products.update']);

$admin->permissions()->attach($edit->id);
$user->syncRoles($admin->id);

Check Permissions

$user->hasAllRoles('admin'); // true
$user->hasAnyRoles('admin'); // true
$user->hasPermission('products.update'); // true
$user->isSuperAdmin(); // true or false

๐ŸŒ Localization

The package includes English (en) translations. To override or translate:

Then add resources/lang/vendor/permissions/{locale}/permissions.php.

๐Ÿง‘โ€๐Ÿ’ป Usage macro

Also you can use Route macro to check permissions and roles:

Route::put('/products/{product}', function () {
    // Your logic here
})->permission('products.update');

Route::put('/admin', function () {
    // Your logic here
})->role('admin');

๐Ÿงฉ Folder Structure

src/
โ”œโ”€โ”€ Models/
โ”‚   โ”œโ”€โ”€ Role.php
โ”‚   โ””โ”€โ”€ Permission.php
โ”œโ”€โ”€ Traits/
โ”‚   โ””โ”€โ”€ HasRoles.php
โ”œโ”€โ”€ Middleware/
โ”œโ”€โ”€ Console/
โ”œโ”€โ”€ PermissionServiceProvider.php
config/
โ””โ”€โ”€ permissions.php
resources/
โ””โ”€โ”€ lang/en/permissions.php
database/
โ””โ”€โ”€ migrations/

๐Ÿงน Clear permission caches

Run this command to clear the permission cache:

php artisan permission:cache:clear

๐Ÿงน Uninstall (Clean Up)

Run this command before removing the package:

php artisan permission:uninstall

๐Ÿ“œ License

MIT ยฉ Shukhratjon Yuldashev

๐Ÿ™Œ Contributing

Pull requests and suggestions are welcome!