Pharaonic
Loading...

# Roles

Most of medium/big projects needs to users roles so we provide to you the easy way.

# Creation

You can create multilingual role, you can use the way that you prefer.

1use Pharaonic\Laravel\Users\Models\Roles\Role;
2 
3// Code (string)
4// Title (string or array)
5// Permissions (array of permissions codes)
6// Locale (string) (only with string title)
7 
8// Way 1
9Role::set('admin', 'Administrator');
10 
11// Way 2
12Role::set('admin', 'Administrator', [], 'en');
13 
14// Way 3
15Role::set('admin', [
16 'ar' => ['title' => 'إداري'],
17 'en' => ['title' => 'Administrator']
18], ['post.create', 'post.update', 'post.delete']);

# Permissions

Attach permission to the role.

1use Pharaonic\Laravel\Users\Models\Roles\Role;
2 
3$role = Role::findByCode('admin');
4$role->permit('post.create', 'post.edit', 'post.delete');

# Inclusion

This is how you can include the role into your user model.

1namespace App\Models;
2 
3use Pharaonic\Laravel\Users\Traits\HasRoles;
4 
5class User extends Authenticatable
6{
7 use HasRoles;
8}

# entrust

Giving the user one or many roles.

1$user->entrust('DevOps', 'Developer');
2// Returns Boolean

# entrusted

Check if the user has all these roles.

1$user->entrusted('DevOps');
2// Returns Boolean

# entrustedAny

Check if the user has one of these roles.

1$user->entrustedAny(['Developer', 'Musician']);
2// Returns Boolean

# distrust

Distrusting the user from having these roles.

1$user->distrust('Musician');
2// Returns Boolean

# distrusted

Check if the user has not all these roles.

1$user->distrusted(['CEO', 'CFO']);
2// Returns Boolean

# distrustedAny

Check if the user has not one of these roles.

1$user->distrustedAny(['CEO', 'CFO', 'Musician']);
2// Returns Boolean

# syncRoles

Clear all user roles and give him/her these list.

1$user->syncRoles('CEO', 'Developer', 'Musician');
2// Returns Boolean

# Middlewares

You can use middlewares to strict your routes.

1Route::middleware('entrusted:CFO')->group(...);
2Route::middleware('entrustedAny:CFO,CEO')->group(...);

# Attributes

  • rolesList

# Blade Directives

  • entrusted
  • entrustedAny
  • distrusted
  • distrustedAny