5e19d4282ff06be6a2d9e95e4557661de5ce4c28
[yaffs-website] / vendor / drush / drush / lib / Drush / Role / RoleBase.php
1 <?php
2
3 namespace Drush\Role;
4
5 abstract class RoleBase {
6   /**
7    * Drupal 6 and Drupal 7:
8    *   'rid' is numeric
9    *   'name' is machine name (e.g. 'anonymous user')
10    *
11    * Drupal 8:
12    *   'rid' is machine name (e.g. 'anonymous')
13    *   'name' is human-readable name (e.g. 'Anonymous user').
14    *
15    * c.f. http://drupal.org/node/1619504
16    */
17   public $name;
18   public $rid;
19
20   /**
21    * This is initialized to the result of the user_roles()
22    * function, which returns an associative array of
23    * rid => name pairs.
24    */
25   public $roles;
26
27   /**
28    * This constructor will allow the role to be selected either
29    * via the role id or via the role name.
30    */
31   public function __construct($rid = DRUPAL_ANONYMOUS_RID) {
32     $this->roles = user_roles();
33     if (!is_numeric($rid)) {
34       $role_name = $rid;
35       if (in_array($role_name, $this->roles)) {
36         $rid = array_search($role_name, $this->roles);
37       }
38     }
39
40     if (isset($this->roles[$rid])) {
41       $this->rid = $rid;
42       // In D8+ Role is an object.
43       $this->name = is_object($this->roles[$rid]) ? $this->roles[$rid]->label() : $this->roles[$rid];
44     }
45     else {
46       throw new RoleException(dt('Could not find the role: !role', array('!role' => $rid)));
47     }
48   }
49
50   /*
51    * Get all perms for a given Role.
52    */
53   public function getPerms() {
54     return array();
55   }
56
57   /*
58    * Get all perms for a given module.
59    */
60   public function getModulePerms($module) {
61     return array();
62   }
63
64   /*
65    * Get all permissions site-wide.
66    */
67   public function getAllModulePerms() {
68     $permissions = array();
69     drush_include_engine('drupal', 'environment');
70     $module_list = drush_module_list();
71     ksort($module_list);
72     foreach ($module_list as $module) {
73       if ($perms = $this->getModulePerms($module)) {
74         $permissions = array_merge($permissions, $perms);
75       }
76     }
77     return $permissions;
78   }
79
80   public function role_create($role_machine_name, $role_human_readable_name = '') {
81   }
82
83   public function delete() {
84   }
85
86   public function add($perm) {
87     $perms = $this->getPerms();
88     if (!in_array($perm, $perms)) {
89       $this->grant_permissions(array($perm));
90       return TRUE;
91     }
92     else {
93       drush_log(dt('"!role" already has the permission "!perm"', array(
94         '!perm' => $perm,
95         '!role' => $this->name,
96       )), 'ok');
97       return FALSE;
98     }
99   }
100
101   public function remove($perm) {
102     $perms = $this->getPerms();
103     if (in_array($perm, $perms)) {
104       $this->revoke_permissions(array($perm));
105       return TRUE;
106     }
107     else {
108       drush_log(dt('"!role" does not have the permission "!perm"', array(
109         '!perm' => $perm,
110         '!role' => $this->name,
111       )), 'ok');
112       return FALSE;
113     }
114   }
115
116   public function grant_permissions($perms) {
117   }
118
119   public function revoke_permissions($perms) {
120   }
121 }