2017 © Pedro Peláez
 

yii2-extension yii2-entrust

Roles and permissions system like zizaco/entrust in laravel for yii2.

image

vmorozov/yii2-entrust

Roles and permissions system like zizaco/entrust in laravel for yii2.

  • Friday, February 24, 2017
  • by vmorozov
  • Repository
  • 2 Watchers
  • 3 Stars
  • 10 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

yii2-entrust

Roles and permissions system like zizaco/entrust in Laravel for Yii2., (*1)

Description

This plugin helps you to manage users roles and permissions very fast and easily., (*2)

For complex roles systems

Roles are presets of permissions. So it means that you have independent control of each user permissions.
So after assigning role to some user will assign all permissions assigned to role to this user.
You can check permission of user with hasPermission(), (*3)

For very simple roles systems

You can use only roles to check if user has rights for the action with hasRole() method, (*4)

Installation

IMPORTANT!
Before installing this plugin you should have fully configured User model to work with database.
And you should have table with users with any name attached to User model in your database.
In the other case you will get the error while running plugin migration.
  1. Install with Composer:
    composer require vmorozov/tii2-entrust dev-master, (*5)

  2. Run migrations to create rbac tables
    php yii migrate/up --migrationPath=@vendor/vmorozov/yii2-entrust/migrations, (*6)

  3. Put given code to User model:, (*7)

use vmorozov\entrust\traits\EntrustUserTrait;  

class User extends ActiveRecord implements IdentityInterface
{
    use EntrustUserTrait; // insert this line
    ....

}

Usage

Create role and Permission

// create role
$adminRole = new Role();
$adminRole->attributes = [
    'name' => 'admin', // short name for use in code
    'display_name' => 'Admin', // More beautiful name to display it to users if it is needed
    'description' => 'Administrator (has all permissions)', // Description of role
];
$adminRole->save();

// create permission
$create_post_permission = new Permission();
$create_post_permission->attributes = [
    'name' => 'create_post', // short name for use in code
    'display_name' => 'Create Post', // More beautiful name to display it to users if it is needed
    'description' => 'Permission to create new post', // Description of permission
];
$create_post_permission->save();

Assign Permissions to Role

// add permission to role with permission name string
$adminRole->attachPermission('create_post');
// add permission to role with permission object
$adminRole->attachPermission($create_post_permission);


// remove permission from role with permission name string
$adminRole->detachPermission('create_post');
// remove permission from role with permission object
$adminRole->detachPermission($create_post_permission);

Assign Role To User

$user->assignRole($adminRole); // $adminRole must be a Role object

Assign Permissions Directly To User

// add permission to user with permission name string
$user->attachPermission('create_post');
// add permission to user with permission object
$user->attachPermission($create_post_permission);


// remove permission from user with permission name string
$user->detachPermission('create_post');
// remove permission from user with permission object
$user->detachPermission($create_post_permission);

Check if user has permission

$user->hasPermission('create-post');
// returns true if user has permission with given name

Check if user has role

Commonly this method is used in simple roles systems where there are no permissions, only roles., (*8)

$user->hasRole('admin');
// returns true if user has role with given name

Get Permissions Of User

// get permissions relation of user. You can manipulate with it as you want.
$user->permissions()

// get all permissions of user
$user->permissions()->all()

// configure getting permissions with sql query methods
$user->permissions()->where(['like', 'name', 'test%'])->all()

Get user role

$role = $user->role(); 
// returns vmorozov\entrust\models\Role Object

The Versions

24/02 2017

dev-master

9999999-dev

Roles and permissions system like zizaco/entrust in laravel for yii2.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4

 

extension roles permissions yii2 module rbac users

24/02 2017

v1.1

1.1.0.0

Roles and permissions system like zizaco/entrust in laravel for yii2.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4

 

extension roles permissions yii2 module rbac users

23/02 2017

v1.0.1

1.0.1.0

Roles and permissions system like zizaco/entrust in laravel for yii2.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4

 

extension roles permissions yii2 module rbac users

23/02 2017

v1.0

1.0.0.0

Roles and permissions system like zizaco/entrust in laravel for yii2.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4

 

extension roles permissions yii2 module rbac users