DrushRole.php

Classes

File

classes/DrushRole.php
View source
  1. <?php
  2. abstract class DrushRole {
  3. /**
  4. * Drupal 6 and Drupal 7:
  5. * 'rid' is numeric
  6. * 'name' is machine name (e.g. 'anonymous user')
  7. *
  8. * Drupal 8:
  9. * 'rid' is machine name (e.g. 'anonymous')
  10. * 'name' is human-readable name (e.g. 'Anonymous user').
  11. *
  12. * c.f. http://drupal.org/node/1619504
  13. */
  14. public $name;
  15. public $rid;
  16. /**
  17. * This is initialized to the result of the user_roles()
  18. * function, which returns an associative array of
  19. * rid => name pairs.
  20. */
  21. public $roles;
  22. /**
  23. * This constructor will allow the role to be selected either
  24. * via the role id or via the role name.
  25. */
  26. public function __construct($rid = DRUPAL_ANONYMOUS_RID) {
  27. $this->roles = user_roles();
  28. if (!is_numeric($rid)) {
  29. $role_name = $rid;
  30. if (in_array($role_name, $this->roles)) {
  31. $rid = array_search($role_name, $this->roles);
  32. }
  33. }
  34. if (isset($this->roles[$rid])) {
  35. $this->rid = $rid;
  36. $this->name = $this->roles[$rid];
  37. }
  38. else {
  39. throw new DrushRoleException(dt('Could not find the role: !role', array('!role' => $rid)));
  40. }
  41. }
  42. public function getPerms() {
  43. return array();
  44. }
  45. public function getModulePerms($module) {
  46. return array();
  47. }
  48. public function getAllModulePerms() {
  49. $permissions = array();
  50. $module_list = module_list();
  51. ksort($module_list);
  52. foreach ($module_list as $module) {
  53. if ($perms = $this->getModulePerms($module)) {
  54. $permissions = array_merge($permissions, $perms);
  55. }
  56. }
  57. return $permissions;
  58. }
  59. public function role_create($role_machine_name, $role_human_readable_name = '') {
  60. }
  61. public function delete() {
  62. }
  63. public function add($perm) {
  64. $perms = $this->getPerms();
  65. if (!in_array($perm, $perms)) {
  66. $this->grant_permissions(array($perm));
  67. return TRUE;
  68. }
  69. else {
  70. drush_log(dt('"!role" already has the permission "!perm"', array(
  71. '!perm' => $perm,
  72. '!role' => $this->name,
  73. )), 'ok');
  74. return FALSE;
  75. }
  76. }
  77. public function remove($perm) {
  78. $perms = $this->getPerms();
  79. if (in_array($perm, $perms)) {
  80. $this->revoke_permissions(array($perm));
  81. return TRUE;
  82. }
  83. else {
  84. drush_log(dt('"!role" does not have the permission "!perm"', array(
  85. '!perm' => $perm,
  86. '!role' => $this->name,
  87. )), 'ok');
  88. return FALSE;
  89. }
  90. }
  91. public function grant_permissions($perms) {
  92. }
  93. public function revoke_permissions($perms) {
  94. }
  95. }
  96. class DrushRole6 extends DrushRole {
  97. public $perms = array();
  98. public function getPerms() {
  99. if (empty($this->perms)) {
  100. $perms = db_result(db_query("SELECT perm FROM {permission} pm LEFT JOIN {role} r ON r.rid = pm.rid WHERE r.rid = '%d'", $this->rid));
  101. $role_perms = explode(", ", $perms);
  102. $this->perms = array_filter($role_perms);
  103. }
  104. return $this->perms;
  105. }
  106. public function getModulePerms($module) {
  107. return module_invoke($module, 'perm');
  108. }
  109. public function role_create($role_machine_name, $role_human_readable_name = '') {
  110. $this->_admin_user_role_op($role_machine_name, t('Add role'));
  111. return TRUE;
  112. }
  113. public function delete() {
  114. $this->_admin_user_role_op($this->rid, t('Delete role'));
  115. }
  116. function _admin_user_role_op($role_machine_name, $op) {
  117. // c.f. http://drupal.org/node/283261
  118. require_once(drupal_get_path('module', 'user') . "/user.admin.inc");
  119. $form_id = "user_admin_new_role";
  120. $form_values = array();
  121. $form_values["name"] = $role_machine_name;
  122. $form_values["op"] = $op;
  123. $form_state = array();
  124. $form_state["values"] = $form_values;
  125. drupal_execute($form_id, $form_state);
  126. }
  127. public function grant_permissions($perms_to_add) {
  128. $perms = $this->getPerms();
  129. $this->perms = array_unique(array_merge($this->perms, $perms_to_add));
  130. $this->updatePerms();
  131. }
  132. public function revoke_permissions($perms_to_remove) {
  133. $perms = $this->getPerms();
  134. $this->perms = array_diff($this->perms, $perms_to_remove);
  135. $this->updatePerms();
  136. }
  137. function updatePerms() {
  138. $new_perms = implode(", ", $this->perms);
  139. drush_op('db_query', "UPDATE {permission} SET perm = '%s' WHERE rid = %d", $new_perms, $this->rid);
  140. }
  141. }
  142. class DrushRole7 extends DrushRole {
  143. public function getPerms() {
  144. $perms = user_role_permissions(array($this->rid => $this->name));
  145. return array_keys($perms[$this->rid]);
  146. }
  147. public function getModulePerms($module) {
  148. $perms = module_invoke($module, 'permission');
  149. return $perms ? array_keys($perms) : array();
  150. }
  151. public function role_create($role_machine_name, $role_human_readable_name = '') {
  152. return user_role_save((object)array('name' => $role_machine_name));
  153. }
  154. public function delete() {
  155. user_role_delete($this->rid);
  156. }
  157. public function grant_permissions($perms) {
  158. return drush_op('user_role_grant_permissions', $this->rid, $perms);
  159. }
  160. public function revoke_permissions($perms) {
  161. return drush_op('user_role_revoke_permissions', $this->rid, $perms);
  162. }
  163. }
  164. class DrushRole8 extends DrushRole7 {
  165. public function role_create($role_machine_name, $role_human_readable_name = '') {
  166. // In D6 and D7, when we create a new role, the role
  167. // machine name is specified, and the numeric rid is
  168. // auto-assigned (next available id); in D8, when we
  169. // create a new role, we need to specify both the rid,
  170. // which is now the role machine name, and also a human-readable
  171. // role name. If the client did not provide a human-readable
  172. // name, then we'll use the role machine name in its place.
  173. if (empty($role_human_readable_name)) {
  174. $role_human_readable_name = ucfirst($role_machine_name);
  175. }
  176. return user_role_save((object)array('name' => $role_human_readable_name, 'rid' => $role_machine_name));
  177. }
  178. }
  179. class DrushRoleException extends Exception {}