百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
N

ntrust

> 编程语言
开源

支持多用户的 Laravel 5.3 至 5.5 的基于角色的权限。

100 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

支持多用户的 Laravel 5.3 至 5.5 的基于角色的权限。

NTRUST Multi-User Roles & Permissions (Laravel 5.4)

Ntrust is a succinct and flexible way to add Role-based Permissions to Laravel 5.4.

Original Package: https://github.com/Zizaco/entrust

Contents

  • Installation
  • Configuration
    • User relation to roles
    • Models
      • Role
      • Permission
      • User
      • Soft Deleting
  • Usage
    • Concepts
      • Checking for Roles & Permissions
      • User ability
    • Blade templates
    • Middleware
    • Short syntax route filter
    • Route filter
  • Troubleshooting
  • License
  • Contribution guidelines
  • Additional information

Installation

In order to install Laravel 5 Ntrust, just add

"klaravel/ntrust": "1.1.*"

to your composer.json. Then run composer install or composer update.

or you can run the composer require command from your terminal:

composer require klaravel/ntrust

Then in your config/app.php add

php
Klaravel\Ntrust\NtrustServiceProvider::class,

in the providers array and

php
'Ntrust' => Klaravel\Ntrust\NtrustFacade::class,

to the aliases array.

If you are going to use Middleware (requires Laravel 5.1 or later) you also need to add

php
'role' => \Klaravel\Ntrust\Middleware\NtrustRole::class,
'permission' => \Klaravel\Ntrust\Middleware\NtrustPermission::class,
'ability' => \Klaravel\Ntrust\Middleware\NtrustAbility::class,

to routeMiddleware array in app/Http/Kernel.php.

Configuration

Just use php artisan vendor:publish and a ntrust.php file will be created in your app/config directory.

See that file to get more detail like how to add multiple users. Default we added two users example like normal user and admin.

User relation to roles

Now generate the Ntrust migration:

bash
php artisan ntrust:migration {name}

Profile {name} name of profile which you have in file config/ntrust.php section profiles.

It will generate the __ntrust_setup_tables.php migration. You may now run it with the artisan migrate command:

bash
php artisan migrate

After the migration, four new tables will be present like below or whatever you have entered table name in config/ntrust.php configuration file.

  • roles — stores role records
  • permissions — stores permission records
  • role_user — stores many-to-many relations between roles and users
  • permission_role — stores many-to-many relations between roles and permissions

Models

Role

Create a Role model inside app/Role.php using the following example:

php
delete(); // This will work no matter what

// Force Delete
$role->users()->sync([]); // Delete relationship data
$role->perms()->sync([]); // Delete relationship data

$role->forceDelete(); // Now force delete will work regardless of whether the pivot table has cascading delete

Usage

Concepts

Let's start by creating the following Roles and Permissions:

php
$owner = new Role();
$owner->name         = 'owner';
$owner->display_name = 'Project Owner'; // optional
$owner->description  = 'User is the owner of a given project'; // optional
$owner->save();

$admin = new Role();
$admin->name         = 'admin';
$admin->display_name = 'User Administrator'; // optional
$admin->description  = 'User is allowed to manage and edit other users'; // optional
$admin->save();

Next, with both roles created let's assign them to the users. Thanks to the HasRole trait this is as easy as:

php
$user = User::where('username', '=', 'michele')->first();

// role attach alias
$user->attachRole($admin); // parameter can be an Role object, array, or id

// or eloquent's original technique
$user->roles()->attach($admin->id); // id only

Now we just need to add permissions to those Roles:

…

Checking for Roles & Permissions

Now we can check for roles and permissions simply by doing:

php
$user->hasRole('owner');   // false
$user->hasRole('admin');   // true
$user->can('edit-user');   // false
$user->can('create-post'); // true

Both hasRole() and can() can receive an array of roles & permissions to check:

php
$user->hasRole(['owner', 'admin']);       // true
$user->can(['edit-user', 'create-post']); // true

By default, if any of the roles or permissions are present for a user then the method will return true. Passing true as a second parameter instructs the method to require all of the items:

php
$user->hasRole(['owner', 'admin']);             // true
$user->hasRole(['owner', 'admin'], true);       // false, user does not have admin role
$user->can(['edit-user', 'create-post']);       // true
$user->can(['edit-user', 'create-post'], true); // false, user does not have edit-user permission

You can have as many Roles as you want for each User and vice versa.

The Ntrust class has shortcuts to both can() and hasRole() for the currently logged in user:

php
Ntrust::hasRole('role-name');
Ntrust::can('permission-name');

// is identical to

Auth::user()->hasRole('role-name');
Auth::user()->can('permission-name);

You can also use placeholders (wildcards) to check any matching permission by doing:

php
// match any admin permission
$user->can("admin.*"); // true

// match any permission about users
$user->can("*_users"); // true

User ability

More advanced checking can be done using the awesome ability function. It takes in three parameters (roles, permissions, options):

  • roles is a set of roles to check.
  • permissions is a set of permissions to check.

Either of the roles or permissions variable can be a comma separated string or array:

php
$user->ability(array('admin', 'owner'), array('create-post', 'edit-user'));

// or

$user->ability('admin,owner', 'create-post,edit-user');

This will check whether the user has any of the provided roles and permissions. In this case it will return true since the user is an admin and has the create-post permission.

The third parameter is an options array:

php
$options = array(
    'validate_all' => true | false (Default: false),
    'return_type'  => boolean | array | both (Default: boolean)
);
  • validate_all is a boolean flag to set whether to check all the values for true, or to return true if at least one role or permission is matched.
  • return_type specifies whether to return a boolean, array of checked values, or both in an array.

Here is an example output:

php
$options = array(
    'validate_all' => true,
    'return_type' => 'both'
);

list($validate, $allValidations) = $user->ability(
    array('admin', 'owner'),
    array('create-post', 'edit-user'),
    $options
);

var_dump($validate);
// bool(false)

var_dump($allValidations);
// array(4) {
//     ['role'] => bool(true)
//     ['role_2'] => bool(false)
//     ['create-post'] => bool(true)
//     ['edit-user'] => bool(false)
// }

The Ntrust class has a shortcut to ability() for the currently logged in user:

php
Ntrust::ability('admin,owner', 'create-post,edit-user');

// is identical to

Auth::user()->ability('admin,owner', 'create-post,edit-user');

Blade templates

Three directives are available for use within your Blade templates. What you give as the directive arguments will be directly passed to the corresponding Ntrust function.

…

Middleware

You can use a middleware to filter routes and route groups by permission or role

php
Route::group(['prefix' => 'admin', 'middleware' => ['role:admin']], function() {
    Route::get('/', 'AdminController@welcome');
    Route::get('/manage', ['middleware' => ['permission:manage-admins'], 'uses' => 'AdminController@manageAdmins']);
});

It is possible to use pipe symbol as OR operator:

php
'middleware' => ['role:admin|root']

To emulate AND functionality just use multiple instances of middleware

php
'middleware' => ['permission:owner', 'permission:writer']

For more complex situations use ability middleware which accepts 3 parameters: roles, permissions, validate_all

php
'middleware' => ['ability:admin|owner,create-post|edit-user,true']

Short syntax route filter

To filter a route by permission or role you can call the following in your app/Http/routes.php:

php
// only users with roles that have the 'manage_posts' permission will be able to access any route within admin/post
Ntrust::routeNeedsPermission('admin/post*', 'create-post');

// only owners will have access to routes within admin/advanced
Ntrust::routeNeedsRole('admin/advanced*', 'owner');

// optionally the second parameter can be an array of permissions or roles
// user would need to match all roles or permissions for that route
Ntrust::routeNeedsPermission('admin/post*', array('create-post', 'edit-comment'));
Ntrust::routeNeedsRole('admin/advanced*', array('owner','writer'));

Both of these methods accept a third parameter. If the third parameter is null then the return of a prohibited access will be App::abort(403), otherwise the third parameter will be returned. So you can use it like:

php
Ntrust::routeNeedsRole('admin/advanced*', 'owner', Redirect::to('/home'));

Furthermore both of these methods accept a fourth parameter. It defaults to true and checks all roles/permissions given. If you set it to false, the function will only fail if all roles/permissions fail for that user. Useful for admin applications where you want to allow access for multiple groups.

…

Route filter

Ntrust roles/permissions can be used in filters by simply using the can and hasRole methods from within the Facade:

php
Route::filter('manage_posts', function()
{
    // check the current user
    if (!Ntrust::can('create-post')) {
        return Redirect::to('admin');
    }
});

// only users with roles that have the 'manage_posts' permission will be able to access any admin/post route
Route::when('admin/post*', 'manage_posts');

Using a filter to check for a role:

php
Route::filter('owner_role', function()
{
    // check the current user
    if (!Ntrust::hasRole('Owner')) {
        App::abort(403);
    }
});

// only owners will have access to routes within admin/advanced
Route::when('admin/advanced*', 'owner_role');

As you can see Ntrust::hasRole() and Ntrust::can() checks if the user is logged in, and then if he or she has the role or permission. If the user is not logged the return will also be false.

Troubleshooting

If you encounter an error when doing the migration that looks like:

SQLSTATE[HY000]: General error: 1005 Can't create table 'laravelbootstrapstarter.#sql-42c_f8' (errno: 150)
    (SQL: alter table `role_user` add constraint role_user_user_id_foreign foreign key (`user_id`)
    references `users` (`id`)) (Bindings: array ())

Then it's likely that the id column in your user table does not match the user_id column in role_user. Make sure both are INT(10).

When trying to use the NtrustUserTrait methods, you encounter the error which looks like

Class name must be a valid object or a string

then probably you don't have published Ntrust assets or something went wrong when you did it. First of all check that you have the ntrust.php file in your app/config directory. If you don't, then try php artisan vendor:publish and, if it does not appear, manually copy the /vendor/klaravel/ntrust/src/config/config.php file in your config directory and rename it ntrust.php.

License

Ntrust is free software di

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

PHPpermission-managerrole-managementrole-manager

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言