简体   繁体   English

在 Laravel 5 / Entrust 中检索具有特定权限的所有用户

[英]Retrieving all users of a specific permission in Laravel 5 / Entrust

How can I retrieve the users of a specific permission in Entrust for Laravel 5.0.如何在 Entrust for Laravel 5.0 中检索具有特定权限的用户。

use App\Role;
use App\Permission;    

$permission = Permission::find(1);
$roles = $permission->roles;
$users = $roles->users;

I know the code doesn't work... Just an example.我知道代码不起作用......只是一个例子。

尝试这个

$users = User::permission('edit articles')->get(); // Returns only users with the permission 'edit articles'

I've never used Entrust before, but I looked through the code on GitHub.我以前从未使用过 Entrust,但我查看了 GitHub 上的代码。

Permission has a belongsToMany for roles. Permission有一个belongsToMany角色的belongsToMany

Role has a belongsToMany for users. Role有一个belongsToMany用户的belongsToMany

Therefore you should be able to chain the commands like so:因此,您应该能够像这样链接命令:

$permission = Permission::find(1);
$users = $permission->roles()->users;

If you are planning on chaining relationships, you should add braces onto the relationship name.如果您计划链接关系,则应在关系名称上添加大括号。

Thanks to ntzm, I was able to figure out how to find all the users who have access to the Permission.感谢 ntzm,我能够弄清楚如何找到所有有权访问权限的用户。 Not sure if this will help anyone.不确定这是否会帮助任何人。

There are two different types of employees who has access to a permission.有两种不同类型的员工有权访问权限。 One from the roles and second is where the permission is given direction to them.一个来自角色,第二个是向他们授予权限的地方。 I haven't used the direct one yet, but I do have roles that have access to permissions.我还没有使用直接的,但我确实有可以访问权限的角色。 Here's how I was able to get the users with a certain permission:以下是我如何获得具有特定权限的用户:

    $permission = Permission::findByName('edit posts');

    $employees = [];

    //Loop through each role.
    foreach($permission->roles as $r){
        $emps = $r->users;

        //Cycle through the users.
        foreach($emps as $e){
            $employees[] = $e;
        }
    }

    return $employees;

Hopefully this helped someone who also needed this.希望这能帮助那些也需要这个的人。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM