{"id":20737114,"url":"https://github.com/phpexpertsinc/laravel-rbac","last_synced_at":"2025-06-17T13:36:09.268Z","repository":{"id":57040347,"uuid":"161667035","full_name":"PHPExpertsInc/laravel-rbac","owner":"PHPExpertsInc","description":"A modern Role Based Authorization Control (RBAC) for Laravel 5.7+. Forked from thelfensdrfer/laravel-rbac","archived":false,"fork":false,"pushed_at":"2019-04-26T12:31:15.000Z","size":503,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-06-16T19:46:32.810Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/PHPExpertsInc.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-12-13T16:38:29.000Z","updated_at":"2019-04-26T12:31:17.000Z","dependencies_parsed_at":"2022-08-23T23:30:36.474Z","dependency_job_id":null,"html_url":"https://github.com/PHPExpertsInc/laravel-rbac","commit_stats":null,"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"purl":"pkg:github/PHPExpertsInc/laravel-rbac","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PHPExpertsInc%2Flaravel-rbac","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PHPExpertsInc%2Flaravel-rbac/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PHPExpertsInc%2Flaravel-rbac/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PHPExpertsInc%2Flaravel-rbac/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/PHPExpertsInc","download_url":"https://codeload.github.com/PHPExpertsInc/laravel-rbac/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PHPExpertsInc%2Flaravel-rbac/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260370355,"owners_count":22998813,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-17T06:13:34.432Z","updated_at":"2025-06-17T13:36:04.254Z","avatar_url":"https://github.com/PHPExpertsInc.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Laravel RBAC\nSuper simple RBAC/ACL implementation for Laravel 5. Laravel \u003e=5.4 compatible fork of https://github.com/thelfensdrfer/laravel-rbac.\n\n## Installation\nRequire this package with composer ([Packagist](https://packagist.org/packages/phpexperts/laravel-rbac)) using the following command\n\n```\ncomposer require phpexperts/laravel-rbac\n```\n\nor modify your `composer.json`\n\n```\n\"require\": {\n    ...\n    \"phpexperts/laravel-rbac\": \"^1.0\"\n}\n```\n\nthen run `composer update`.\n\nAfter installation register the ServiceProvider to the `providers` array in `config/app.php`\n\n```php\nPHPExperts\\LaravelRBAC\\RbacServiceProvider::class,\n```\n\nRun the migrations\n\n```\n$ php artisan migrate\n```\n\nAdd RBAC middleware to your `app/Http/Kernel.php`\n\n```php\nprotected $routeMiddleware = [\n    ...\n    'rbac' =\u003e '\\PHPExperts\\LaravelRBAC\\Middleware\\Rbac::class'\n];\n```\n\nAdd Rbac trait to your `User` model\n\n```php\nuse PHPExperts\\LaravelRBAC\\Traits\\Rbac;\n\t\nclass User extends Authenticatable\n{\n    use HasRoles;\n    ...\n\t    \n}\n```\n\n## Usage\n\n### Roles\n\n#### Create role\n\n```php\n$adminRole = new Role;\n$adminRole-\u003ename = 'Administrator';\n$adminRole-\u003eslug = 'administrator';\n$adminRole-\u003edescription = 'System Administrator';\n$adminRole-\u003esave();\n\n$editorRole = new Role;\n$editorRole-\u003ename = 'Editor';\n$editorRole-\u003eslug = 'editor';\n$editorRole-\u003edescription = 'Editor';\n$editorRole-\u003esave();\n```\n\n#### Assign role to user\n\t\n```php\n$user = User::find(1);\n$user-\u003eroles()-\u003eattach($adminRole-\u003eid);\n```\n\nyou can also assign multiple roles at once\n\n```php\n$user-\u003eroles()-\u003eattach([$adminRole-\u003eid, $editorRole-\u003eid]);\n```\n\n#### Revoke role from user\n\n```php\n$user-\u003eroles()-\u003edetach($adminRole-\u003eid);\n```\n\nyou can also revoke multiple roles at once\n\n```php\n$user-\u003eroles()-\u003edetach([$adminRole-\u003eid, $editorRole-\u003eid]);\n```\n\n#### Sync roles\n\n```php\n$user-\u003eroles()-\u003esync([$editorRole-\u003eid]);\n```\n\nAny role already assigned to user will be revoked if you don't pass its id to sync method.\n\n### Permissions\n\n#### Create permission\n\n```php\n$createUser = new Permission;\n$createUser-\u003ename = 'Create user';\n$createUser-\u003eslug = 'user.create';\n$createUser-\u003edescription = 'Permission to create user';\n$createUser-\u003esave();\n\n$updateUser = new Permission;\n$updateUser-\u003ename = 'Update user';\n$updateUser-\u003eslug = 'user.update';\n$updateUser-\u003edescription = 'Permission to update user';\n$updateUser-\u003esave();\n```\n\n#### Assign permission to role\n\n```php\n$adminRole = Role::find(1);\n$adminRole-\u003epermissions()-\u003eattach($createUser-\u003eid);\n```\n\nyou can also assign multiple permissions at once\n\n```php\n$adminRole-\u003epermissions()-\u003eattach([$createUser-\u003eid, $updateUser-\u003eid]);\n```\n\n#### Revoke permission from role\n\n```php\n$adminRole-\u003epermissions()-\u003edetach($createUser-\u003eid);\n```\n\nyou can also revoke multiple permissions at once\n\n```php\n$adminRole-\u003epermissions()-\u003edetach([$createUser-\u003eid, $updateUser-\u003eid]);\n```\n\n#### Sync permissions\n\n```php\n$adminRole-\u003epermissions()-\u003esync([$updateUser-\u003eid]);\n```\n\nAny permission already assigned to role will be revoked if you don't pass its id to sync method.\n\n### Check user roles/permissions\n\nRoles and permissions can be checked on `User` instance using `hasRole` and `canDo` methods.\n\n```php\n$isAdmin = Auth::user()-\u003ehasRole('administrator'); // pass role slug as parameter\n$isAdminOrEditor = Auth::user()-\u003ehasRole('administrator|editor'); // using OR operator\n$canUpdateUser = Auth::user()-\u003ecanDo('update.user'); // pass permission slug as parameter\n$canUpdateOrCreateUser = Auth::user()-\u003ecanDo('update.user|create.user'); // using OR operator\n```\n\n### Protect routes\n\nLaravel RBAC provides middleware to protect single route and route groups. Middleware expects 2 comma separated params: \n- **is** or **can** as first param - what to check (role/permission)\n- role/permission slug as second param\n\n```php\nRoute::get('/backend', [\n    'uses' =\u003e 'BackendController@index',\n    'middleware' =\u003e ['auth', 'rbac:is,administrator']\n]);\nRoute::get('/backend', [\n    'uses' =\u003e 'BackendController@index',\n    'middleware' =\u003e ['auth', 'rbac:is,administrator|editor']\n]);\nRoute::get('/dashboard', [\n    'uses' =\u003e 'DashboardController@index',\n    'middleware' =\u003e ['auth', 'rbac:can,view.dashboard']\n]);\nRoute::get('/dashboard', [\n    'uses' =\u003e 'DashboardController@index',\n    'middleware' =\u003e ['auth', 'rbac:can,view.dashboard|view.statistics']\n]);\n```\n\n### Blade directive\n\nLaravel RBAC provides two Blade directives to check if user has role/permission assigned.\n\nCheck for role\n\n```\n@ifUserIs('administrator')\n    // show admin content here\n@else\n    // sorry\n@endif\n\n@ifUserIs('administrator|editor')\n    // show editor content here\n@else\n    // sorry\n@endif\n```\n\nCheck for permission\n\n```\n@ifUserCan('delete.user')\n    // show delete button\n@endif\n\n@ifUserCan('delete.user|manage.user')\n    // show delete button\n@endif\n```\n\n## License\n\nLaravel RBAC is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphpexpertsinc%2Flaravel-rbac","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fphpexpertsinc%2Flaravel-rbac","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphpexpertsinc%2Flaravel-rbac/lists"}