项目作者: echods

项目描述 :
Laravel Roles
高级语言: PHP
项目地址: git://github.com/echods/Roles.git
创建时间: 2018-03-27T16:12:37Z
项目社区:https://github.com/echods/Roles

开源协议:MIT License

下载


Echods Roles

Laravel based roles for Laravel 6+.

Installing the package

  1. composer require echods/roles

For Laravel < 5.8

For Laravel version less than 5.8 please use tag branch 0.9.8

Upgrading from 1.0.4 to 1.1.0

Making the shift to use handle instead of name. A name attribute was added for backwards compatibility hopefully to help. To upgrade don’t forget to run the following:

  1. $ php artisan migrate

Publish config file

  1. $ php artisan vendor:publish --provider="Echods\Roles\RoleServiceProvider" --tag=config

Modify config file to set roles

Add the roles you need for your application and descriptions in the config file. Also change if you would like big integer migrations or note.

  1. 'admin' => [
  2. 'name' => 'admin',
  3. 'description' => 'Admin role for all stuff'
  4. ],
  5. 'editor' => [
  6. 'name' => 'editor',
  7. 'description' => 'Editor role for all stuff'
  8. ]
  9. ],
  10. 'migrations' => [
  11. 'useBigInteger' => true
  12. ]

Run migrations

Run migrations for roles table and role_user to be created.

  1. $ php artisan migrate

Run setup for roles

This will fill the database with roles from the config file.

  1. $ php artisan roles:generate

HasRole Trait

  1. use Echods\Roles\Traits\HasRole;
  2. class User extends Authenticatable
  3. {
  4. use HasRole;
  5. //...
  6. }

Usage

Checking Roles

You can check roles of a user from the list of roles you have in the config. For example if you have roles admin, editor, employee then you can do the following. Returns boolean.

  1. $user = User::find(1);
  2. $user->isAdmin();
  3. $user->isEditor();
  4. $user->isEmployee();

Alternatively you can do the following:

  1. $user->hasRole('editor');

Checking for multiple roles

Checking if a user has multiple roles

  1. $user->hasRoles(['admin', 'editor']);
  2. // or
  3. $user->hasRoles(['editor', 'admin']);

Attaching a role

  1. $user->attachRole('superAdmin');