PHPackages                             vmorozov/yii2-entrust - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. vmorozov/yii2-entrust

ActiveYii2-extension[Authentication &amp; Authorization](/categories/authentication)

vmorozov/yii2-entrust
=====================

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

v1.1(9y ago)3561BSD-3-ClausePHPPHP &gt;=5.4

Since Feb 23Pushed 9y ago2 watchersCompare

[ Source](https://github.com/freezer278/yii2-entrust)[ Packagist](https://packagist.org/packages/vmorozov/yii2-entrust)[ RSS](/packages/vmorozov-yii2-entrust/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (3)DependenciesVersions (4)Used By (0)

yii2-entrust
============

[](#yii2-entrust)

Roles and permissions system like zizaco/entrust in Laravel for Yii2.

\##Description This plugin helps you to manage users roles and permissions very fast and easily.
\####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()](#check-if-user-has-permission)\####For very simple roles systems You can use only roles to check if user has rights for the action with [hasRole()](#check-if-user-has-role) method

\##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`
2. **Run migrations to create rbac tables**
    `php yii migrate/up --migrationPath=@vendor/vmorozov/yii2-entrust/migrations`
3. **Put given code to User model:**

```
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.

```
$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
```

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity13

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

3

Last Release

3364d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/f1487aab1174b98688909d82fa4014f59f66c80242d61fef733ae1045ab17f4d?d=identicon)[vmorozov](/maintainers/vmorozov)

---

Top Contributors

[![freezer278](https://avatars.githubusercontent.com/u/17979033?v=4)](https://github.com/freezer278 "freezer278 (30 commits)")

---

Tags

rbacyii2-entrust-pluginyii2-extensionyii2-role-systemrolespermissionsrbacyii2extensionmoduleUsers

### Embed Badge

![Health badge](/badges/vmorozov-yii2-entrust/health.svg)

```
[![Health](https://phpackages.com/badges/vmorozov-yii2-entrust/health.svg)](https://phpackages.com/packages/vmorozov-yii2-entrust)
```

###  Alternatives

[budyaga/yii2-users

Module for manage users and their rights with the support of registration through social services and assigned to each user more than one social service.

409.1k](/packages/budyaga-yii2-users)[hasinhayder/tyro

Tyro - The ultimate Authentication, Authorization, and Role &amp; Privilege Management solution for Laravel 12 &amp; 13

6712.1k2](/packages/hasinhayder-tyro)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
