Permissions.php 2.25 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Definition of Views\user\Plugin\views\field\Permissions.
6 7
 */

8 9 10 11 12
namespace Views\user\Plugin\views\field;

use Drupal\Core\Annotation\Plugin;
use Drupal\views\Plugin\views\field\PrerenderList;

13 14 15 16
/**
 * Field handler to provide a list of permissions.
 *
 * @ingroup views_field_handlers
17
 *
18
 * @Plugin(
19 20
 *   id = "user_permissions",
 *   module = "user"
21 22 23
 * )
 */
class Permissions extends PrerenderList {
24

25
  public function construct() {
26 27 28 29
    parent::construct();
    $this->additional_fields['uid'] = array('table' => 'users', 'field' => 'uid');
  }

30
  public function query() {
31 32 33 34 35 36 37 38 39
    $this->add_additional_fields();
    $this->field_alias = $this->aliases['uid'];
  }

  function pre_render(&$values) {
    $uids = array();
    $this->items = array();

    foreach ($values as $result) {
40
      $uids[] = $this->get_value($result);
41 42 43 44 45 46 47 48 49 50 51 52 53 54
    }

    if ($uids) {
      // Get a list of all the modules implementing a hook_permission() and sort by
      // display name.
      $module_info = system_get_info('module');
      $modules = array();
      foreach (module_implements('permission') as $module) {
        $modules[$module] = $module_info[$module]['name'];
      }
      asort($modules);

      $permissions = module_invoke_all('permission');

55 56 57 58 59 60 61 62
      $query = db_select('role_permission', 'rp');
      $query->join('users_roles', 'u', 'u.rid = rp.rid');
      $query->fields('u', array('uid', 'rid'));
      $query->addField('rp', 'permission');
      $query->condition('u.uid', $uids);
      $query->condition('rp.module', array_keys($modules));
      $query->orderBy('rp.permission');
      $result = $query->execute();
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84

      foreach ($result as $perm) {
        $this->items[$perm->uid][$perm->permission]['permission'] = $permissions[$perm->permission]['title'];
      }
    }
  }

  function render_item($count, $item) {
    return $item['permission'];
  }

  /*
  function document_self_tokens(&$tokens) {
    $tokens['[' . $this->options['id'] . '-role' . ']'] = t('The name of the role.');
    $tokens['[' . $this->options['id'] . '-rid' . ']'] = t('The role ID of the role.');
  }

  function add_self_tokens(&$tokens, $item) {
    $tokens['[' . $this->options['id'] . '-role' . ']'] = $item['role'];
    $tokens['[' . $this->options['id'] . '-rid' . ']'] = $item['rid'];
  }
  */
85

86
}