0

出于用户管理用户界面的目的,我希望获得所有用户的权限。我使用 Cartalyst\Sentinel 的本机实现并尝试:

$users = Sentinel::getUserRepository()->with('roles')->get();
    $permissions = array();
    foreach ($users as $user) {
        $user_permissions = Sentinel::getResultingPermissionsFor($user);
        $permissions[$user['id']] = $user_permissions;
    }

但是函数“getResultingPermissionsFor()”在 V5 中似乎不再可用。

4

1 回答 1

0

我通过将角色对象传递给树枝模板解决了这个问题:

路由器.php

$app->get('/admin/users', function (Request $request, Response $response) {
    $loggedUser = Sentinel::check();
    $users = Sentinel::getUserRepository()->with('roles')->get();
    $roles = Sentinel::getRoleRepository()->get();

    if (!$loggedUser) {
        // do sth.
    }

    if (!$loggedUser->hasAccess('user.*')) {
        // do sth.
    }

    $view = Twig::fromRequest($request);
    $view->render($response, 'admin.users.html.twig', array(
        'loggedUser' => $loggedUser,
        'users' => $users,
        'roles' => $roles
    ));
    return $response;
});

树枝模板:

{% for user in users %}
   {% set rolePermissions = [] %}
      {% for role in user.roles %}
         {% set rolePermissions = rolePermissions|merge(role.permissions) %}
      {% endfor %}
   {% set resultingPermissions = rolePermissions %}
   {% set resultingPermissions = resultingPermissions|merge(user.permissions) %}
{% endfor %}

// followed by output html
于 2022-01-11T03:07:39.597 回答