All Projects → huang-yi → laravel-rbac

huang-yi / laravel-rbac

Licence: MIT license
A RBAC package for Laravel.

Programming Languages

PHP
23972 projects - #3 most used programming language
HTML
75241 projects

Projects that are alternatives of or similar to laravel-rbac

Php Casbin
An authorization library that supports access control models like ACL, RBAC, ABAC in PHP .
Stars: ✭ 865 (+2603.13%)
Mutual labels:  permission, rbac
laravel-casbin
This repository has moved to https://github.com/php-casbin/laravel-authz
Stars: ✭ 42 (+31.25%)
Mutual labels:  permission, rbac
Casbin
An authorization library that supports access control models like ACL, RBAC, ABAC in Golang
Stars: ✭ 10,872 (+33875%)
Mutual labels:  permission, rbac
objection-authorize
isomorphic, "magical" authorization integration with Objection.js 🎉
Stars: ✭ 71 (+121.88%)
Mutual labels:  permission, rbac
lua-casbin
An authorization library that supports access control models like ACL, RBAC, ABAC in Lua (OpenResty)
Stars: ✭ 43 (+34.38%)
Mutual labels:  permission, rbac
Casbin.net
An authorization library that supports access control models like ACL, RBAC, ABAC in .NET (C#)
Stars: ✭ 535 (+1571.88%)
Mutual labels:  permission, rbac
Casbin Cpp
An authorization library that supports access control models like ACL, RBAC, ABAC in C/C++
Stars: ✭ 113 (+253.13%)
Mutual labels:  permission, rbac
Jcasbin
An authorization library that supports access control models like ACL, RBAC, ABAC in Java
Stars: ✭ 1,335 (+4071.88%)
Mutual labels:  permission, rbac
sqlx-adapter
Asynchronous casbin adapter for mysql, postgres, sqlite based on sqlx-rs
Stars: ✭ 27 (-15.62%)
Mutual labels:  permission, rbac
Think Casbin
专为ThinkPHP定制的Casbin的扩展包,Casbin是一个功能强大,高效的开源访问控制库。
Stars: ✭ 138 (+331.25%)
Mutual labels:  permission, rbac
Casbin Rs
An authorization library that supports access control models like ACL, RBAC, ABAC in Rust.
Stars: ✭ 375 (+1071.88%)
Mutual labels:  permission, rbac
access-control
Simple, flexible and reliable access control for NodeJS and Typescript. Supports both RBAC and ABAC.
Stars: ✭ 29 (-9.37%)
Mutual labels:  permission, rbac
rbac-react-redux-aspnetcore
A starter template for creating JWT token from ASP.NET Core API project and applying that JWT token authentication on React application
Stars: ✭ 54 (+68.75%)
Mutual labels:  permission, rbac
Pycasbin
An authorization library that supports access control models like ACL, RBAC, ABAC in Python
Stars: ✭ 625 (+1853.13%)
Mutual labels:  permission, rbac
Node Casbin
An authorization library that supports access control models like ACL, RBAC, ABAC in Node.js and Browser
Stars: ✭ 1,757 (+5390.63%)
Mutual labels:  permission, rbac
actix-casbin-auth
Casbin Actix-web access control middleware
Stars: ✭ 40 (+25%)
Mutual labels:  permission, rbac
casbin-ex
An authorization library that supports access control models like ACL, RBAC, ABAC in Elixir
Stars: ✭ 37 (+15.63%)
Mutual labels:  permission, rbac
PermissionDirector
a iOS permission manager writtern by Swift
Stars: ✭ 29 (-9.37%)
Mutual labels:  permission
zf3-circlical-user
Turnkey Authentication, Identity, and RBAC for Laminas and Zend Framework 3. Supports Doctrine and Middleware.
Stars: ✭ 35 (+9.38%)
Mutual labels:  rbac
silly-android
Android plugins for Java, making core Android APIs easy to use
Stars: ✭ 40 (+25%)
Mutual labels:  permission

English | 中文

Laravel RBAC

This package helps you to manage permissions and roles.

Installation

You may install this package via Composer:

composer require huang-yi/laravel-rbac

Next, you should publish configuration and migration files using the vendor:publish Artisan command:

php artisan vendor:publish --provider="HuangYi\Rbac\RbacServiceProvider"

Finally, you should run your database migrations:

php artisan migrate

Configuration

  • user: The user model class you are using.
  • database:
    • connection: The database connection for RBAC tables.
    • prefix: The common prefix for RBAC tables.
  • cache: The cache switch.

Usage

Your User model must be configured to rbac.user option. It should implement the HuangYi\Rbac\Contracts\Authorizable interface and use the HuangYi\Rbac\Concerns\Authorizable trait.

namespace App;

use HuangYi\Rbac\Concerns\Authorizable;
use HuangYi\Rbac\Contracts\Authorizable as AuthorizableContract;

class User extends Authenticatable implement AuthorizableContract
{
    use Authorizable, Notifiable;
}

Store a permission to database:

use HuangYi\Rbac\Permission;

Permission::make('edit post');

Store a role to database:

use HuangYi\Rbac\Role;

Permission::make('personnel manager');

Attach or detach permissions to role:

$role->attachPermissions($permissions);

$role->detachPermissions($permissions);

$role->syncPermissions($permissions);

Attach or detach roles to user:

$user->attachRoles($roles);

$user->detachRoles($roles);

$user->syncRoles($roles);

Attach or detach permissions to user:

$user->attachPermissions($permissions);

$user->detachPermissions($permissions);

$user->syncPermissions($permissions);

Determine if the user has roles:

$user->hasRole('author');

$user->hasRoles(['author', 'personnel manager']);

$user->hasAnyRoles(['author', 'personnel manager']);

Determine if the user has permissions:

$user->hasPermission('create post');

$user->hasPermissions(['create post', 'edit post']);

$user->hasAnyPermissions(['create post', 'edit post']);

// this is similar to hasAnyPermissions
$user->can('edit post|edit post');

Super Admin

You may register a callback for determining if the user is a super admin by using Rbac::checkSuperAdminUsing() method:

namespace App\Providers;

use HuangYi\Rbac\Rbac;
use Illuminate\Support\ServiceProvider;

class AuthServiceProvider extends ServiceProvider
{
    public function boot()
    {
        Rbac::checkSuperAdminUsing(function ($user) {
            return in_array($user->email, ['[email protected]']);
        });
    }
}

Middleware

// role middleware
Route::get('admin/staffs', [StaffController::class, 'index'])->middleware('role:personnel manager|vice president');

// permission middleware
Route::post('post/{post}', [PostController::class, 'update'])->middleware('permission:create post|edit post');

// this is similar to 'permission' middleware
Route::post('post/{post}', [PostController::class, 'update'])->middleware('can:create post|edit post');

Blade Directives

Role directives:

  • @role, @elserole, @endrolehasRole
  • @roles, @elseroles, @endroleshasRoles
  • @anyroles, @elseanyroles, @endanyroleshasAnyRoles

Permission directives:

  • @permission, @elsepermission, @endpermissionhasPermission
  • @permissions, @elsepermissions, @endpermissionshasPermissions
  • @anypermissions, @elseanypermissions, @endanypermissionshasAnyPermissions

Tests

composer test

License

This package is open-sourced software licensed under the MIT license.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].