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 ascending Description
role_drush_help_alter Implements hook_drush_help_alter().
role_drush_help Implementation of hook_drush_help().
role_drush_command Implementation of hook_drush_command().
drush_role_remove_perm Remove permission(s) from the specified role.
drush_role_perm Implement permission add / remove operations.
drush_role_list Displays a list of roles
drush_role_get_class Get core version specific Role handler class.
drush_role_delete Create the specified role
drush_role_create Create the specified role
drush_role_add_perm Add one or more permission(s) to the specified role.

File

commands/core/role.drush.inc
View source
  1. <?php
  2. use Drush\Log\LogLevel;
  3. use Drush\Role\RoleBase;
  4. /**
  5. * Implementation of hook_drush_help().
  6. */
  7. function role_drush_help($section) {
  8. switch ($section) {
  9. case 'meta:role:title':
  10. return dt('Role commands');
  11. case 'meta:role:summary':
  12. return dt('Interact with the role system.');
  13. }
  14. }
  15. /**
  16. * Implements hook_drush_help_alter().
  17. */
  18. function role_drush_help_alter(&$command) {
  19. // Drupal 8+ has changed role names.
  20. if (in_array($command['command'] , array('role-add-perm', 'role-add-perm', 'role-list')) && drush_drupal_major_version() >= 8) {
  21. foreach ($command['examples'] as $key => $val) {
  22. $newkey = str_replace(array('anonymous user', 'authenticated user'), array('anonymous', 'authenticated'), $key);
  23. $command['examples'][$newkey] = $val;
  24. unset($command['examples'][$key]);
  25. }
  26. }
  27. }
  28. /**
  29. * Implementation of hook_drush_command().
  30. */
  31. function role_drush_command() {
  32. $items['role-create'] = array(
  33. 'description' => 'Create a new role.',
  34. 'examples' => array(
  35. "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'.",
  36. "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."
  37. ),
  38. 'arguments' => array(
  39. 'machine name' => 'The symbolic machine name for the role. Required.',
  40. 'human-readable name' => 'A descriptive name for the role. Optional; Drupal 8 only. Ignored in D6 and D7.',
  41. ),
  42. 'aliases' => array('rcrt'),
  43. );
  44. $items['role-delete'] = array(
  45. 'description' => 'Delete a role.',
  46. 'examples' => array(
  47. "drush role-delete 'test role'" => "Delete the role 'test role'.",
  48. ),
  49. 'arguments' => array(
  50. 'machine name' => 'The symbolic machine name for the role. Required. In D6 and D7, this may also be a numeric role ID.',
  51. ),
  52. 'aliases' => array('rdel'),
  53. );
  54. $items['role-add-perm'] = array(
  55. 'description' => 'Grant specified permission(s) to a role.',
  56. 'examples' => array(
  57. "drush role-add-perm 'anonymous user' 'post comments'" => 'Allow anon users to post comments.',
  58. "drush role-add-perm 'anonymous user' \"'post comments','access content'\"" => 'Allow anon users to post comments and access content.',
  59. "drush role-add-perm 'authenticated user' --module=node" => 'Select a permission from "node" permissions to add to logged in users.'
  60. ),
  61. 'arguments' => array(
  62. 'role' => 'The role to modify. Required.',
  63. 'permissions' => 'The list of permission to grant, delimited by commas. Required, unless the --module option is used.',
  64. ),
  65. 'required-arguments' => 1,
  66. 'options' => array(
  67. 'module' => 'Select the permission to modify from an interactive list of all permissions available in the specified module.',
  68. ),
  69. 'global-options' => array(
  70. 'cache-clear',
  71. ),
  72. 'aliases' => array('rap'),
  73. );
  74. $items['role-remove-perm'] = array(
  75. 'description' => 'Remove specified permission(s) from a role.',
  76. 'examples' => array(
  77. "drush role-remove-perm 'anonymous user' 'access content'" => 'Hide content from anon users.',
  78. ),
  79. 'arguments' => array(
  80. 'role' => 'The role to modify.',
  81. 'permissions' => 'The list of permission to grant, delimited by commas. Required, unless the --module option is used.',
  82. ),
  83. 'required-arguments' => 1,
  84. 'options' => array(
  85. 'module' => 'Select the permission to modify from an interactive list of all permissions available in the specified module.',
  86. ),
  87. 'global-options' => array(
  88. 'cache-clear',
  89. ),
  90. 'aliases' => array('rmp'),
  91. );
  92. $items['role-list'] = array(
  93. '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.',
  94. 'examples' => array(
  95. "drush role-list --filter='administer nodes'" => 'Display a list of roles that have the administer nodes permission assigned.',
  96. "drush role-list 'anonymous user'" => 'Display all of the permissions assigned to the anon user role.'
  97. ),
  98. 'arguments' => array(
  99. '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.',
  100. ),
  101. 'options' => array(
  102. '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.',
  103. ),
  104. 'outputformat' => array(
  105. 'default' => 'table',
  106. 'pipe-format' => 'list',
  107. 'field-labels' => array('rid' => 'ID', 'label' => 'Role Label', 'perm' => "Permission"),
  108. 'output-data-type' => 'format-table',
  109. ),
  110. 'aliases' => array('rls'),
  111. );
  112. return $items;
  113. }
  114. /**
  115. * Create the specified role
  116. */
  117. function drush_role_create($rid, $role_name = '') {
  118. $role = drush_role_get_class();
  119. $result = $role->role_create($rid, $role_name);
  120. if ($result !== FALSE) {
  121. drush_log(dt('Created "!role"', array('!role' => $rid)), LogLevel::SUCCESS);
  122. }
  123. return $result;
  124. }
  125. /**
  126. * Create the specified role
  127. */
  128. function drush_role_delete($rid) {
  129. $role = drush_role_get_class($rid);
  130. if ($role === FALSE) {
  131. return FALSE;
  132. }
  133. $result = $role->delete();
  134. if ($result !== FALSE) {
  135. drush_log(dt('Deleted "!role"', array('!role' => $rid)), LogLevel::SUCCESS);
  136. }
  137. return $result;
  138. }
  139. /**
  140. * Add one or more permission(s) to the specified role.
  141. *
  142. * @param string $rid machine name for a role
  143. * @param null|string $permissions machine names, delimited by commas.
  144. *
  145. * @return bool
  146. */
  147. function drush_role_add_perm($rid, $permissions = NULL) {
  148. if (is_null($permissions)) {
  149. // Assume --module is used thus inject a FALSE
  150. $perms = array(FALSE);
  151. }
  152. else {
  153. $perms = _convert_csv_to_array($permissions);
  154. }
  155. $added_perm = FALSE;
  156. foreach($perms as $perm) {
  157. $result = drush_role_perm('add', $rid, $perm);
  158. if ($result !== FALSE) {
  159. $added_perm = TRUE;
  160. drush_log(dt('Added "!perm" to "!role"', array('!perm' => $perm, '!role' => $result->name)), LogLevel::SUCCESS);
  161. }
  162. }
  163. if ($added_perm) {
  164. drush_drupal_cache_clear_all();
  165. }
  166. return $result;
  167. }
  168. /**
  169. * Remove permission(s) from the specified role.
  170. *
  171. * @param string $rid machine name for a role
  172. * @param null|string $permissions machine names, delimited by commas.
  173. *
  174. * @return bool
  175. */
  176. function drush_role_remove_perm($rid, $permissions = NULL) {
  177. if (is_null($permissions)) {
  178. // Assume --module is used thus inject a FALSE
  179. $perms = array(FALSE);
  180. }
  181. else {
  182. $perms = _convert_csv_to_array($permissions);
  183. }
  184. $removed_perm = FALSE;
  185. foreach($perms as $perm) {
  186. $result = drush_role_perm('remove', $rid, $perm);
  187. if ($result !== FALSE) {
  188. $removed_perm = TRUE;
  189. drush_log(dt('Removed "!perm" from "!role"', array('!perm' => $perm, '!role' => $result->name)), LogLevel::OK);
  190. }
  191. }
  192. if ($removed_perm) {
  193. drush_drupal_cache_clear_all();
  194. }
  195. return $result;
  196. }
  197. /**
  198. * Implement permission add / remove operations.
  199. *
  200. * @param string $action 'add' | 'remove'
  201. * @param string $rid
  202. * @param string|null $permission
  203. *
  204. * @return bool|RoleBase
  205. *
  206. * @see drush_set_error()
  207. */
  208. function drush_role_perm($action, $rid, $permission = NULL) {
  209. $role = drush_role_get_class($rid);
  210. if (!$role) {
  211. return FALSE;
  212. }
  213. // If a permission wasn't provided, but the module option is specified,
  214. // provide a list of permissions provided by that module.
  215. if (!$permission && $module = drush_get_option('module', FALSE)) {
  216. drush_include_engine('drupal', 'environment');
  217. if (!drush_module_exists($module)) {
  218. return drush_set_error('DRUSH_ROLE_ERROR', dt('!module not enabled!', array('!module' => $module)));
  219. }
  220. $module_perms = $role->getModulePerms($module);
  221. if (empty($module_perms)) {
  222. return drush_set_error('DRUSH_ROLE_NO_PERMISSIONS', dt('No permissions found for module !module', array('!module' => $module)));
  223. }
  224. $choice = drush_choice($module_perms, "Enter a number to choose which permission to $action.");
  225. if ($choice === FALSE) {
  226. return drush_user_abort();
  227. }
  228. $permission = $module_perms[$choice];
  229. }
  230. else {
  231. $permissions = $role->getAllModulePerms();
  232. if (!in_array($permission, $permissions)) {
  233. return drush_set_error(dt('Could not find the permission: !perm', array('!perm' => $permission)));
  234. }
  235. }
  236. $result = $role->{$action}($permission);
  237. if ($result === FALSE) {
  238. return FALSE;
  239. }
  240. return $role;
  241. }
  242. /**
  243. * Get core version specific Role handler class.
  244. *
  245. * @param string $role_name
  246. * @return RoleBase
  247. *
  248. * @see drush_get_class().
  249. */
  250. function drush_role_get_class($role_name = DRUPAL_ANONYMOUS_RID) {
  251. return drush_get_class('Drush\Role\Role', func_get_args());
  252. }
  253. /**
  254. * Displays a list of roles
  255. */
  256. function drush_role_list($rid = '') {
  257. $result = array();
  258. if (empty($rid)) {
  259. drush_hide_output_fields(array('perm'));
  260. // Get options passed.
  261. $perm = drush_get_option('filter');
  262. $roles = array();
  263. // Get all roles - if $perm is empty user_roles retrieves all roles.
  264. $roles = user_roles(FALSE, $perm);
  265. if (empty($roles)) {
  266. return drush_set_error('DRUSH_NO_ROLES', dt("No roles found."));
  267. }
  268. foreach ($roles as $rid => $value) {
  269. $role = drush_role_get_class($rid);
  270. $result[$role->name] = array(
  271. 'rid' => $rid,
  272. 'label' => $role->name,
  273. );
  274. }
  275. }
  276. else {
  277. drush_hide_output_fields(array('rid', 'label'));
  278. $role = drush_role_get_class($rid);
  279. if (!$role) {
  280. return FALSE;
  281. }
  282. $perms = $role->getPerms();
  283. foreach ($perms as $permission) {
  284. $result[$permission] = array(
  285. 'perm' => $permission);
  286. }
  287. }
  288. return $result;
  289. }