Laravel RBAC package
, (*1)
1 Introduction
LaRbac - Package for the Laravel framework which provides management with the next data:
- Roles
- Permissions
- Assign roles for users, (*2)
, (*3)
2 Dependencies
- laravel 8+ | 9+ | 10+ | 11+ | 12+
- Bootstrap 4 for styling
- JQuery
- php >= 7.3.0
- composer
3 Installation
Note!, (*4)
Version 3.x is for laravel 8+, 9+, 10+, 11+, 12+., (*5)
Version 2.x is for laravel 6 or 7. You can use branch laravel67-rbac
with 2.x versions., (*6)
3.1 General installation from remote repository
Run the composer command:, (*7)
composer require itstructure/laravel-rbac "^3.0.18"
, (*8)
3.2 Next internal installation steps
Notes:, (*9)
-
Make sure that a table for the users is already existing in your project., (*10)
-
Make sure that a model for the users table is already existing in your project., (*11)
Recommendation:, (*12)
If you don't have any layout yet, it is useful to install for example AdminLTE
or you can make your special any layout template.
Cause in this package there is no a layout specially. But in config it is necessary to set it (see the next point 2 about a configure)., (*13)
Let's go:, (*14)
-
Publish files., (*15)
Note: rbac.php
config file and seeders LaRbacDatabaseSeeder
, PermissionSeeder
, RoleSeeder
must be published surely!, (*16)
-
To publish config run command:, (*17)
php artisan rbac:publish --only=config
, (*18)
It stores config file to config
folder., (*19)
-
To publish seeders run command:, (*20)
php artisan rbac:publish --only=seeders
, (*21)
It stores seeder files to database/seeders
folder., (*22)
-
To publish migrations run command:, (*23)
php artisan rbac:publish --only=migrations
, (*24)
It stores migration files to database/migrations
folder., (*25)
-
To publish views run command:, (*26)
php artisan rbac:publish --only=views
, (*27)
It stores view files to resources/views/vendor/rbac
folder., (*28)
-
To publish translations run command:, (*29)
php artisan rbac:publish --only=lang
, (*30)
It stores translation files to resources/lang/vendor/rbac
folder., (*31)
-
To publish all parts run command without only
argument:, (*32)
php artisan rbac:publish
, (*33)
Else you can use --force
argument to rewrite already published files., (*34)
-
Configure published config/rbac.php
file:, (*35)
-
set layout
. Example: 'layout' => 'adminlte::page'
, (*36)
-
change userModelClass
if it is needed to change, (*37)
-
set adminUserId
which you wanted to be with the role of administrator. At least at the beginning stage., (*38)
It is necessary for the next time system to let you go into the Rbac control panel, after you assigned an administrator role for you (Later see point 4)., (*39)
-
Most likely you have to change memberNameAttributeKey
., (*40)
It is to display the user name in control panel by getMemberNameAttribute()
method of Administrable
trait. It can be string or a callback:, (*41)
'memberNameAttributeKey' => function ($row) {
return $row->first_name . ' ' . $row->last_name;
}
-
Tuning your User
model (as shown in 4.1 point):, (*42)
-
Implement User
model from RbacUserInterface
., (*43)
-
Add roles attribute to $fillable
., (*44)
-
Apply Administrable
trait., (*45)
-
Run command to run migrations and seeders:, (*46)
php artisan rbac:database
, (*47)
Or optional:, (*48)
To run just migrations php artisan rbac:database --only=migrate
, (*49)
To run just seeds php artisan rbac:database --only=seed
, (*50)
-
Alternative variant for seeders., (*51)
You can set published rbac LaRbacDatabaseSeeder
seeder class in to a special DatabaseSeeder
:, (*52)
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
public function run()
{
$this->call(LaRbacDatabaseSeeder::class);
}
}
and run command: php artisan db:seed
., (*53)
-
Run command to set Admin role for user with identifier adminUserId
, defined in 2 point:, (*54)
php artisan rbac:admin
, (*55)
4 Usage
Notes:, (*56)
-
Make sure you use a Bootstrap 4 for styling and JQuery in your application., (*57)
-
Make sure that a laravel initial factory authorization is already working in your application., (*58)
4.1 Model part
According with the Itstructure\LaRbac\Interfaces\RbacUserInterface
use functions from Itstructure\LaRbac\Traits\Administrable
trait as in example:, (*59)
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Itstructure\LaRbac\Interfaces\RbacUserInterface;
use Itstructure\LaRbac\Traits\Administrable;
class User extends Authenticatable implements RbacUserInterface
{
use Notifiable, Administrable;
protected $fillable = [
'name', 'email', 'password', 'roles'
];
protected $hidden = [
'password', 'remember_token',
];
}
4.2 Routes part
There are already integrated base RBAC routes to manage users, roles and permissions. See in routes.php
package file., (*60)
They are guarded by the next:, (*61)
- middleware
auth
(editable by config).
- permission
can:administrate
(editable by config).
This routes allow you to go to the next routes:, (*62)
-
Users section, (*63)
For get request method, (*64)
http://example-domain.com/rbac/users
http://example-domain.com/rbac/users/show/{id}
http://example-domain.com/rbac/users/edit/{id}
For post request method, (*65)
http://example-domain.com/rbac/users/update/{id}
http://example-domain.com/rbac/users/delete
-
Roles section, (*66)
For get request method, (*67)
http://example-domain.com/rbac/roles
http://example-domain.com/rbac/roles/show/{id}
http://example-domain.com/rbac/roles/create
http://example-domain.com/rbac/roles/edit/{role}
For post request method, (*68)
http://example-domain.com/rbac/roles/store
http://example-domain.com/rbac/roles/update/{role}
http://example-domain.com/rbac/roles/delete
-
Permissions section, (*69)
For get request method, (*70)
http://example-domain.com/rbac/permissions
http://example-domain.com/rbac/permissions/show/{id}
http://example-domain.com/rbac/permissions/create
http://example-domain.com/rbac/permissions/edit/{permission}
For post request method, (*71)
http://example-domain.com/rbac/permissions/store
http://example-domain.com/rbac/permissions/update/{permission}
http://example-domain.com/rbac/permissions/delete
4.3 Gates part
There are already integrated base RBAC gates to access control in your application to some of the resources. See provider file RbacAuthServiceProvider.php
., (*72)
It provides the next gate definitions:, (*73)
administrate
assign-role
delete-member
view-record
create-record
update-record
delete-record
publish-record
Read more in Laravel gates, (*74)
5 View examples
Users, (*75)
, (*76)
Roles, (*77)
, (*78)
Permissions, (*79)
, (*80)
License
Copyright © 2018-2025 Andrey Girnik girnikandrey@gmail.com., (*81)
Licensed under the MIT license. See LICENSE.txt for details., (*82)