This is a lightweight package that allows you assign roles and permissions to any Laravel model, or on a pivot table (many to many relationship).

Overview

Simple Laravel roles and permissions

Banner

Introduction

This package allows you to assign roles and permissions to any laravel model, or on a pivot table (many to many relationship).

Built and written by Ajimoti Ibukun

Documentation

You can read the proper documentation here

Quick Samples

On a Model

The example below explains how to use the package on a model after installation.

use App\Enums\Role;
use App\Enums\Permission;

// Assign a 'Super Admin' role to this user
$user->assign(Role::SuperAdmin);

// Check if the user has the role
$user->hasRole(Role::SuperAdmin);
// Or 
$user->isSuperAdmin(); // returns true

// Check if the user can perform a operation
$user->can(Permission::DeleteTransactions); 
// Or 
$user->canDeleteTransactions(); 

// Check if the user has multiple permissions
$user->holds(Permission::DeleteTransactions, Permission::BlockUsers);

Pivot table (many to many relationship)

This demonstrates how to use the package on a many to many relationship. In this example, we assume we have a merchant relationship in our User model. And this relationship returns an instance of Laravel's BelongsToMany class.

Import the App\Enums\Role and App\Enums\Permission class.

use App\Enums\Role;
use App\Enums\Permission;

// Sample merchant
$merchant = Merchant::where('name', 'wallmart')->first();

// Assign a 'Super Admin' role to this user on the selected merchant (wallmart)
$user->of($merchant)->assign(Role::SuperAdmin);

// Check if the user has a super admin role on the selected merchant (wallmart)
$user->of($merchant)->hasRole(Role::SuperAdmin);

// Check if the user can 'delete transactions' on the selected merchant (wallmart)
$user->of($merchant)->can(Permission::DeleteTransactions);

// Check if the user has multiple permissions on the selected merchant (wallmart)
$user->of($merchant)->holds(Permission::DeleteTransactions, Permission::BlockUsers);

We used the user model to make the example explanatory, similar to the examples above the package will work on any model class.

Requirements

  • PHP 8.0 or higher
  • Laravel 8.0 or higher
  • Upon installation, the package publishes a config/roles-and-permissions.php file, ensure you do not have a file with the same name in your config directory.

Pros

  • The package can be used on any model, i.e any model can be assigned roles, and permissions.
  • Roles can be given multiple permissions.
  • Models have permissions via roles.
  • Models can be assigned multiple roles.
  • A many to many relationship can be assigned roles. (i.e the package can be used on a pivot table).
  • Supports role hierarchy. (A higher level role can be configured to have the permissions of lower level roles).

Crons

  • Permissions cannot be assigned directly on a many to many relationship.

Installation

You can install the package via composer:

composer require ajimoti/roles-and-permissions

If you have existing pivot tables that you want to apply the package on, you can add the table names to the pivot.tables array in the config/roles-and-permissions.php config file. The command below will add a role column to every pivot table provided in the array.

Run the command below, then you are set to use the package.

php artisan roles:install

Documentation

Visit https://roles.ajimoti.com/docs/intro to better understand how to use the package.

Comments
Releases(1.1.2)
  • 1.1.2(Jan 24, 2022)

    ** Minor Changes**

    • [ ] Merged traits folder content into concerns folder
    • [ ] Fetched all roles and permissions outside loops.

    Full Changelog: https://github.com/ajimoti/roles-and-permissions/compare/1.1.1...1.1.2

    Source code(tar.gz)
    Source code(zip)
  • 1.1.1(Jan 22, 2022)

    What's Changed

    • Update readme file to reflect the new change by @ajimoti in https://github.com/ajimoti/roles-and-permissions/pull/2
    • Updated readme file to contain information about tests by @ajimoti in https://github.com/ajimoti/roles-and-permissions/pull/3
    • Introduce a new trait that acts as an interface for other traits that Interacts with models by @ajimoti in https://github.com/ajimoti/roles-and-permissions/pull/4

    Full Changelog: https://github.com/ajimoti/roles-and-permissions/compare/1.1.0...1.1.1

    Source code(tar.gz)
    Source code(zip)
  • 1.1.0(Jan 15, 2022)

    For example:

    After a model has been assigned a role, we shouldn't have to always use the ->hasRole() method to check if the user has a specific role. Or the holds() method to check if the model holds a permission. Developers should be able to prepend is with the role key and make a call on the model like so:

    // $model->is{role_key}()
    $model->isSuperAdmin(); // returns bool
    
    // Or on a many-to-many relationship
    $user->of($model)->isSuperAdmin();
    

    Similarly, perpend can with the permission to check if the model has permission:

    // $model->can{permission_key}()
    $model->canDeleteTransactions(); // returns bool
    
    // Or on a many-to-many relationship
    $user->of($model)->canDeleteTransactions();
    

    What's Changed

    • Include support for magic calls. by @ajimoti in https://github.com/ajimoti/roles-and-permissions/pull/1

    New Contributors

    • @ajimoti made their first contribution in https://github.com/ajimoti/roles-and-permissions/pull/1

    Full Changelog: https://github.com/ajimoti/roles-and-permissions/compare/1.0.0...1.1.0

    Source code(tar.gz)
    Source code(zip)
A Powerful package for handling roles and permissions in Laravel with GUI.

Laravel Roles A Powerful package for handling roles and permissions in Laravel. Supports Laravel 5.3, 5.4, 5.5, 5.6, 5.7, 5.8, 6.0, 7.0, and 8.0+. Tab

Jeremy Kenedy 792 Sep 28, 2022
Laravel Auth is a Complete Build of Laravel 8 with Email Registration Verification, Social Authentication, User Roles and Permissions, User Profiles, and Admin restricted user management system.

Laravel Auth is a Complete Build of Laravel 8 with Email Registration Verification, Social Authentication, User Roles and Permissions, User Profiles, and Admin restricted user management system. Built on Bootstrap 4.

Jeremy Kenedy 2.8k Sep 23, 2022
Tech-Admin is Laravel + Bootstrap Admin Panel With User Management And Access Control based on Roles and Permissions.

Tech-Admin | Laravel 8 + Bootstrap 4 Tech-Admin is Admin Panel With Preset of Roles, Permissions, ACL, User Management, Profile Management. Features M

TechTool India 31 Sep 22, 2022
Handle roles and permissions in your Laravel application

Laratrust (Laravel Package) Version Compatibility Laravel Laratrust 8.x 6.x 7.x 6.x 6.x 6.x 5.6.x - 5.8.x 5.2 5.3.x - 5.5.x 5.1 5.0.x - 5.2.x 4.0. Ins

Santiago García 1.9k Sep 20, 2022
Laravel Roles and Permissions

Introduction to Laravel Roles and Permission App Starter Kit Roles and sanctions are a paramount part of many web applications. In project, we have op

Brian Kiprono Koech 1 Nov 1, 2021
Roles & Permissions for Laravel 8 / 7 / 6 / 5

Defender Defender is an Access Control List (ACL) Solution for Laravel 5 / 6 / 7 (single auth). (Not compatible with multi-auth) With security and usa

Artesãos 434 Sep 12, 2022
Laravel Users (Roles & Permissions, Devices, Password Hashing, Password History).

LARAVEL USERS Roles & Permissions Devices Password Hashing Password History Documentation You can find the detailed documentation here in Laravel User

Pharaonic 7 Sep 12, 2022
Associate users with roles and permissions

Associate users with permissions and roles Sponsor If you want to quickly add authentication and authorization to Laravel projects, feel free to check

Spatie 10.7k Sep 26, 2022
PermissionsMakr is a Laravel package that will help any developer to easily manage the system's users permissions

PermissionsMakr is a Laravel package that will help any developer to easily manage the system's users permissions

Alvarium Digital 3 Nov 30, 2021
This package helps you to associate users with permissions and permission groups with laravel framework

Laravel ACL This package allows you to manage user permissions and groups in a database, and is compatible with Laravel v5.8 or higher. Please check t

Mateus Junges 531 Sep 16, 2022
Eloquent roles and abilities.

Bouncer Bouncer is an elegant, framework-agnostic approach to managing roles and abilities for any app using Eloquent models. Table of Contents Click

Joseph Silber 3.1k Sep 27, 2022
Proyecto para aprender a utilizar privilegios (roles y permisos) con CRUDBooster

About Laravel Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experie

Informática DP 3 May 9, 2022
Role-based Permissions for Laravel 5

ENTRUST (Laravel 5 Package) Entrust is a succinct and flexible way to add Role-based Permissions to Laravel 5. If you are looking for the Laravel 4 ve

Zizaco 6.1k Sep 16, 2022
Light-weight role-based permissions system for Laravel 6+ built in Auth system.

Kodeine/Laravel-ACL Laravel ACL adds role based permissions to built in Auth System of Laravel 8.0+. ACL middleware protects routes and even crud cont

Kodeine 776 Sep 14, 2022
Manage authorization with granular role-based permissions in your Laravel Apps.

Governor For Laravel Manage authorization with granular role-based permissions in your Laravel apps. Goal Provide a simple method of managing ACL in a

GeneaLabs, LLC 147 Jul 25, 2022
Laravel mongodb permissions

Laravel mongodb permissions

null 4 May 10, 2022
A minimal package that helps you login with any password on local environments

Laravel Anypass Built with ❤️ for every "lazy" laravel developer ;) It is always painful to remember and type in the correct password in the login for

Iman 206 Jul 16, 2022
PHPoAuthLib provides oAuth support in PHP 7.2+ and is very easy to integrate with any project which requires an oAuth client.

PHPoAuthLib NOTE: I'm looking for someone who could help to maintain this package alongside me, just because I don't have a ton of time to devote to i

David Desberg 1.1k Sep 21, 2022