5 abstract class RoleBase {
7 * Drupal 6 and Drupal 7:
9 * 'name' is machine name (e.g. 'anonymous user')
12 * 'rid' is machine name (e.g. 'anonymous')
13 * 'name' is human-readable name (e.g. 'Anonymous user').
15 * c.f. http://drupal.org/node/1619504
21 * This is initialized to the result of the user_roles()
22 * function, which returns an associative array of
28 * This constructor will allow the role to be selected either
29 * via the role id or via the role name.
31 public function __construct($rid = DRUPAL_ANONYMOUS_RID) {
32 $this->roles = user_roles();
33 if (!is_numeric($rid)) {
35 if (in_array($role_name, $this->roles)) {
36 $rid = array_search($role_name, $this->roles);
40 if (isset($this->roles[$rid])) {
42 // In D8+ Role is an object.
43 $this->name = is_object($this->roles[$rid]) ? $this->roles[$rid]->label() : $this->roles[$rid];
46 throw new RoleException(dt('Could not find the role: !role', array('!role' => $rid)));
51 * Get all perms for a given Role.
53 public function getPerms() {
58 * Get all perms for a given module.
60 public function getModulePerms($module) {
65 * Get all permissions site-wide.
67 public function getAllModulePerms() {
68 $permissions = array();
69 drush_include_engine('drupal', 'environment');
70 $module_list = drush_module_list();
72 foreach ($module_list as $module) {
73 if ($perms = $this->getModulePerms($module)) {
74 $permissions = array_merge($permissions, $perms);
80 public function role_create($role_machine_name, $role_human_readable_name = '') {
83 public function delete() {
86 public function add($perm) {
87 $perms = $this->getPerms();
88 if (!in_array($perm, $perms)) {
89 $this->grant_permissions(array($perm));
93 drush_log(dt('"!role" already has the permission "!perm"', array(
95 '!role' => $this->name,
101 public function remove($perm) {
102 $perms = $this->getPerms();
103 if (in_array($perm, $perms)) {
104 $this->revoke_permissions(array($perm));
108 drush_log(dt('"!role" does not have the permission "!perm"', array(
110 '!role' => $this->name,
116 public function grant_permissions($perms) {
119 public function revoke_permissions($perms) {