role.drush.inc

  1. 8.0.x commands/core/role.drush.inc
  2. 6.x commands/core/role.drush.inc
  3. 7.x commands/core/role.drush.inc
  4. master commands/core/role.drush.inc

Functions

Namesort descending Description
drush_role_add_perm Add a permission to the specified role.
drush_role_create Create the specified role
drush_role_delete Create the specified role
drush_role_get_class Wrapper for drush_get_class; instantiates an instance of the version-specific DrushRole class suitable for the current version of Drupal.
drush_role_list Displays a list of roles
drush_role_perm Implement permission add / remove operations.
drush_role_remove_perm Remove a permission from the specified role.
role_drush_command Implementation of hook_drush_command().
role_drush_help Implementation of hook_drush_help().

File

commands/core/role.drush.inc
View source
  1. <?php
  2. /**
  3. * Implementation of hook_drush_help().
  4. */
  5. function role_drush_help($section) {
  6. switch ($section) {
  7. case 'meta:role:title':
  8. return dt('Role commands');
  9. case 'meta:role:summary':
  10. return dt('Interact with the role system.');
  11. }
  12. }
  13. /**
  14. * Implementation of hook_drush_command().
  15. */
  16. function role_drush_command() {
  17. $items['role-create'] = array(
  18. 'description' => 'Create a new role.',
  19. 'examples' => array(
  20. "drush role-create 'test role'" => "Create a new role 'test role' on D6 or D7; auto-assign the rid. On D8, 'test role' is the rid, and the human-readable name is set to 'Test role'.",
  21. "drush role-create 'test role' 'Test role'" => "Create a new role with a machine name of 'test role', and a human-readable name of 'Test role'. On D6 and D7, behaves as the previous example."
  22. ),
  23. 'arguments' => array(
  24. 'machine name' => 'The symbolic machine name for the role. Required.',
  25. 'human-readable name' => 'A descriptive name for the role. Optional; Drupal 8 only. Ignored in D6 and D7.',
  26. ),
  27. 'aliases' => array('rcrt'),
  28. );
  29. $items['role-delete'] = array(
  30. 'description' => 'Delete a role.',
  31. 'examples' => array(
  32. "drush role-delete 'test role'" => "Delete the role 'test role'.",
  33. ),
  34. 'arguments' => array(
  35. 'machine name' => 'The symbolic machine name for the role. Required. In D6 and D7, this may also be a numeric role ID.',
  36. ),
  37. 'aliases' => array('rdel'),
  38. );
  39. $items['role-add-perm'] = array(
  40. 'description' => 'Grant a specified permission to a role.',
  41. 'examples' => array(
  42. "drush role-add-perm 'anonymous user' 'post comments'" => 'Allow anonymous users to put their own advertisements on your site.',
  43. "drush role-add-perm 'authenticated user' --module=node" => 'Select a permission from "node" permissions to add to authenticated users.'
  44. ),
  45. 'arguments' => array(
  46. 'role' => 'The role to modify. Required.',
  47. 'permission' => 'The permission to grant. Required, unless the --module option is used.',
  48. ),
  49. 'required-arguments' => 1,
  50. 'options' => array(
  51. 'module' => 'Select the permission to modify from an interactive list of all permissions available in the specified module.',
  52. ),
  53. 'aliases' => array('rap'),
  54. );
  55. $items['role-remove-perm'] = array(
  56. 'description' => 'Remove a specified permission from a role.',
  57. 'examples' => array(
  58. "drush role-remove-perm 'anonymous user' 'access content'" => 'Hide content from anonymous users.',
  59. ),
  60. 'arguments' => array(
  61. 'role' => 'The role to modify.',
  62. 'permission' => 'The permission to remove. Required, unless the --module option is used.',
  63. ),
  64. 'required-arguments' => 1,
  65. 'options' => array(
  66. 'module' => 'Select the permission to modify from an interactive list of all permissions available in the specified module.',
  67. ),
  68. 'aliases' => array('rmp'),
  69. );
  70. $items['role-list'] = array(
  71. 'description' => 'Display a list of all roles defined on the system. If a role name is provided as an argument, then all of the permissions of that role will be listed. If a permission name is provided as an option, then all of the roles that have been granted that permission will be listed.',
  72. 'examples' => array(
  73. "drush role-list --filter='administer nodes'" => 'Display a list of roles that have the administer nodes permission assigned.',
  74. "drush role-list 'anonymous user'" => 'Display all of the permissions assigned to the anonymous user role.'
  75. ),
  76. 'arguments' => array(
  77. 'role' => 'The role to list. Optional; if specified, lists all permissions assigned to that role. If no role is specified, lists all of the roles available on the system.',
  78. ),
  79. 'options' => array(
  80. 'filter' => 'Limits the list of roles to only those that have been assigned the specified permission. Optional; may not be specified if a role argument is provided.',
  81. ),
  82. 'outputformat' => array(
  83. 'default' => 'table',
  84. 'pipe-format' => 'list',
  85. 'field-labels' => array('rid' => 'ID', 'role' => 'Role Name', 'perm' => "Permission"),
  86. 'output-data-type' => 'format-table',
  87. ),
  88. 'aliases' => array('rls'),
  89. );
  90. return $items;
  91. }
  92. /**
  93. * Create the specified role
  94. */
  95. function drush_role_create($rid, $role_name = '') {
  96. $role = drush_role_get_class();
  97. $result = $role->role_create($rid, $role_name);
  98. if ($result !== FALSE) {
  99. drush_log(dt('Created "!role"', array('!role' => $rid)), 'success');
  100. }
  101. return $result;
  102. }
  103. /**
  104. * Create the specified role
  105. */
  106. function drush_role_delete($rid) {
  107. $role = drush_role_get_class($rid);
  108. if ($role === FALSE) {
  109. return FALSE;
  110. }
  111. $result = $role->delete();
  112. if ($result !== FALSE) {
  113. drush_log(dt('Deleted "!role"', array('!role' => $rid)), 'success');
  114. }
  115. return $result;
  116. }
  117. /**
  118. * Add a permission to the specified role.
  119. */
  120. function drush_role_add_perm($rid, $permission = NULL) {
  121. $result = drush_role_perm('add', $rid, $permission);
  122. if ($result !== FALSE) {
  123. drush_log(dt('Added "!perm" to "!role"', array('!perm' => $permission, '!role' => $result->name)), 'success');
  124. drush_drupal_cache_clear_all();
  125. }
  126. return $result;
  127. }
  128. /**
  129. * Remove a permission from the specified role.
  130. */
  131. function drush_role_remove_perm($rid, $permission = NULL) {
  132. $result = drush_role_perm('remove', $rid, $permission);
  133. if ($result !== FALSE) {
  134. drush_log(dt('Removed "!perm" from "!role"', array('!perm' => $permission, '!role' => $result->name)), 'ok');
  135. drush_drupal_cache_clear_all();
  136. }
  137. return $result;
  138. }
  139. /**
  140. * Implement permission add / remove operations.
  141. */
  142. function drush_role_perm($action, $rid, $permission = NULL) {
  143. $role = drush_role_get_class($rid);
  144. if (!$role) {
  145. return FALSE;
  146. }
  147. // If a permission wasn't provided, but the module option is specified,
  148. // provide a list of permissions provided by that module.
  149. if (!$permission && $module = drush_get_option('module', FALSE)) {
  150. if (!module_exists($module)) {
  151. return drush_set_error('DRUSH_ROLE_ERROR', dt('!module not enabled!', array('!module' => $module)));
  152. }
  153. $module_perms = $role->getModulePerms($module);
  154. if (empty($module_perms)) {
  155. return drush_set_error('DRUSH_ROLE_NO_PERMISSIONS', dt('No permissions found for module !module', array('!module' => $module)));
  156. }
  157. $choice = drush_choice($module_perms, "Enter a number to choose which permission to $action.");
  158. if ($choice === FALSE) {
  159. return FALSE;
  160. }
  161. $permission = $module_perms[$choice];
  162. }
  163. else {
  164. $permissions = $role->getAllModulePerms();
  165. if (!in_array($permission, $permissions)) {
  166. return drush_set_error(dt('Could not find the permission: !perm', array('!perm' => $permission)));
  167. }
  168. }
  169. $role_perms = $role->getPerms();
  170. $result = $role->{$action}($permission);
  171. if ($result === FALSE) {
  172. return FALSE;
  173. }
  174. return $role;
  175. }
  176. /**
  177. * Wrapper for drush_get_class; instantiates an instance of the version-specific
  178. * DrushRole class suitable for the current version of Drupal.
  179. */
  180. function drush_role_get_class($role_name = DRUPAL_ANONYMOUS_RID) {
  181. return drush_get_class('DrushRole', array($role_name));
  182. }
  183. /**
  184. * Displays a list of roles
  185. */
  186. function drush_role_list($rid = '') {
  187. $result = array();
  188. if (empty($rid)) {
  189. drush_hide_output_fields(array('perm'));
  190. // get options passed
  191. $perm = drush_get_option('filter');
  192. $roles = array();
  193. // get all roles - if $perm is empty user_roles retrieves all roles
  194. $roles = user_roles(FALSE, $perm);
  195. if (empty($roles)) {
  196. return drush_set_error('DRUSH_NO_ROLES', dt("No roles found."));
  197. }
  198. foreach ($roles as $rid => $role_name) {
  199. $result[$role_name] = array(
  200. 'rid' => $rid,
  201. 'role' => $role_name,
  202. );
  203. }
  204. }
  205. else {
  206. drush_hide_output_fields(array('rid', 'role'));
  207. $role = drush_role_get_class($rid);
  208. if (!$role) {
  209. return FALSE;
  210. }
  211. $perms = $role->getPerms();
  212. foreach ($perms as $permission) {
  213. $result[$permission] = array(
  214. 'perm' => $permission);
  215. }
  216. }
  217. return $result;
  218. }