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

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

8
namespace Drupal\user\Plugin\views\field;
9

10
use Drupal\Component\Annotation\PluginID;
11
use Drupal\views\Plugin\views\display\DisplayPluginBase;
12
use Drupal\views\ViewExecutable;
13
14
use Drupal\views\Plugin\views\field\PrerenderList;

15
16
17
18
/**
 * Field handler to provide a list of permissions.
 *
 * @ingroup views_field_handlers
19
 *
20
 * @PluginID("user_permissions")
21
22
 */
class Permissions extends PrerenderList {
23

24
  /**
25
   * Overrides Drupal\views\Plugin\views\field\FieldPluginBase::init().
26
   */
27
28
  public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
    parent::init($view, $display, $options);
29

30
31
32
    $this->additional_fields['uid'] = array('table' => 'users', 'field' => 'uid');
  }

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

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

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

    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');

58
59
60
61
62
63
64
65
      $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();
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82

      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.');
  }

83
  protected function addSelfTokens(&$tokens, $item) {
84
85
86
87
    $tokens['[' . $this->options['id'] . '-role' . ']'] = $item['role'];
    $tokens['[' . $this->options['id'] . '-rid' . ']'] = $item['rid'];
  }
  */
88

89
}