3 use Drush\Log\LogLevel;
4 use Drush\Role\RoleBase;
7 * Implementation of hook_drush_help().
9 function role_drush_help($section) {
11 case 'meta:role:title':
12 return dt('Role commands');
13 case 'meta:role:summary':
14 return dt('Interact with the role system.');
19 * Implements hook_drush_help_alter().
21 function role_drush_help_alter(&$command) {
22 // Drupal 8+ has changed role names.
23 if (in_array($command['command'] , array('role-add-perm', 'role-add-perm', 'role-list')) && drush_drupal_major_version() >= 8) {
24 foreach ($command['examples'] as $key => $val) {
25 $newkey = str_replace(array('anonymous user', 'authenticated user'), array('anonymous', 'authenticated'), $key);
26 $command['examples'][$newkey] = $val;
27 unset($command['examples'][$key]);
33 * Implementation of hook_drush_command().
35 function role_drush_command() {
36 $items['role-create'] = array(
37 'description' => 'Create a new role.',
39 "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'.",
40 "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."
43 'machine name' => 'The symbolic machine name for the role. Required.',
44 'human-readable name' => 'A descriptive name for the role. Optional; Drupal 8 only. Ignored in D6 and D7.',
46 'aliases' => array('rcrt'),
48 $items['role-delete'] = array(
49 'description' => 'Delete a role.',
51 "drush role-delete 'test role'" => "Delete the role 'test role'.",
54 'machine name' => 'The symbolic machine name for the role. Required. In D6 and D7, this may also be a numeric role ID.',
56 'aliases' => array('rdel'),
58 $items['role-add-perm'] = array(
59 'description' => 'Grant specified permission(s) to a role.',
61 "drush role-add-perm 'anonymous user' 'post comments'" => 'Allow anon users to post comments.',
62 "drush role-add-perm 'anonymous user' \"'post comments','access content'\"" => 'Allow anon users to post comments and access content.',
63 "drush role-add-perm 'authenticated user' --module=node" => 'Select a permission from "node" permissions to add to logged in users.'
66 'role' => 'The role to modify. Required.',
67 'permissions' => 'The list of permission to grant, delimited by commas. Required, unless the --module option is used.',
69 'required-arguments' => 1,
71 'module' => 'Select the permission to modify from an interactive list of all permissions available in the specified module.',
73 'global-options' => array(
76 'aliases' => array('rap'),
79 $items['role-remove-perm'] = array(
80 'description' => 'Remove specified permission(s) from a role.',
82 "drush role-remove-perm 'anonymous user' 'access content'" => 'Hide content from anon users.',
85 'role' => 'The role to modify.',
86 'permissions' => 'The list of permission to grant, delimited by commas. Required, unless the --module option is used.',
88 'required-arguments' => 1,
90 'module' => 'Select the permission to modify from an interactive list of all permissions available in the specified module.',
92 'global-options' => array(
95 'aliases' => array('rmp'),
98 $items['role-list'] = array(
99 '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.',
101 "drush role-list --filter='administer nodes'" => 'Display a list of roles that have the administer nodes permission assigned.',
102 "drush role-list 'anonymous user'" => 'Display all of the permissions assigned to the anon user role.'
104 'arguments' => array(
105 '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.',
108 '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.',
110 'outputformat' => array(
111 'default' => 'table',
112 'pipe-format' => 'list',
113 'field-labels' => array('rid' => 'ID', 'label' => 'Role Label', 'perm' => "Permission"),
114 'output-data-type' => 'format-table',
116 'aliases' => array('rls'),
123 * Create the specified role
125 function drush_role_create($rid, $role_name = '') {
126 $role = drush_role_get_class();
127 $result = $role->role_create($rid, $role_name);
128 if ($result !== FALSE) {
129 drush_log(dt('Created "!role"', array('!role' => $rid)), LogLevel::SUCCESS);
135 * Create the specified role
137 function drush_role_delete($rid) {
138 $role = drush_role_get_class($rid);
139 if ($role === FALSE) {
142 $result = $role->delete();
143 if ($result !== FALSE) {
144 drush_log(dt('Deleted "!role"', array('!role' => $rid)), LogLevel::SUCCESS);
150 * Add one or more permission(s) to the specified role.
152 * @param string $rid machine name for a role
153 * @param null|string $permissions machine names, delimited by commas.
157 function drush_role_add_perm($rid, $permissions = NULL) {
158 if (is_null($permissions)) {
159 // Assume --module is used thus inject a FALSE
160 $perms = array(FALSE);
163 $perms = _convert_csv_to_array($permissions);
168 foreach($perms as $perm) {
169 $result = drush_role_perm('add', $rid, $perm);
170 if ($result !== FALSE) {
172 drush_log(dt('Added "!perm" to "!role"', array('!perm' => $perm, '!role' => $result->name)), LogLevel::SUCCESS);
177 drush_drupal_cache_clear_all();
184 * Remove permission(s) from the specified role.
186 * @param string $rid machine name for a role
187 * @param null|string $permissions machine names, delimited by commas.
191 function drush_role_remove_perm($rid, $permissions = NULL) {
192 if (is_null($permissions)) {
193 // Assume --module is used thus inject a FALSE
194 $perms = array(FALSE);
197 $perms = _convert_csv_to_array($permissions);
200 $removed_perm = FALSE;
202 foreach($perms as $perm) {
203 $result = drush_role_perm('remove', $rid, $perm);
204 if ($result !== FALSE) {
205 $removed_perm = TRUE;
206 drush_log(dt('Removed "!perm" from "!role"', array('!perm' => $perm, '!role' => $result->name)), LogLevel::OK);
211 drush_drupal_cache_clear_all();
218 * Implement permission add / remove operations.
220 * @param string $action 'add' | 'remove'
222 * @param string|null $permission
224 * @return bool|RoleBase
226 * @see drush_set_error()
228 function drush_role_perm($action, $rid, $permission = NULL) {
229 $role = drush_role_get_class($rid);
234 // If a permission wasn't provided, but the module option is specified,
235 // provide a list of permissions provided by that module.
236 if (!$permission && $module = drush_get_option('module', FALSE)) {
237 drush_include_engine('drupal', 'environment');
238 if (!drush_module_exists($module)) {
239 return drush_set_error('DRUSH_ROLE_ERROR', dt('!module not enabled!', array('!module' => $module)));
241 $module_perms = $role->getModulePerms($module);
242 if (empty($module_perms)) {
243 return drush_set_error('DRUSH_ROLE_NO_PERMISSIONS', dt('No permissions found for module !module', array('!module' => $module)));
245 $choice = drush_choice($module_perms, "Enter a number to choose which permission to $action.");
246 if ($choice === FALSE) {
247 return drush_user_abort();
249 $permission = $module_perms[$choice];
252 $permissions = $role->getAllModulePerms();
253 if (!in_array($permission, $permissions)) {
254 return drush_set_error(dt('Could not find the permission: !perm', array('!perm' => $permission)));
258 $result = $role->{$action}($permission);
259 if ($result === FALSE) {
266 * Get core version specific Role handler class.
268 * @param string $role_name
271 * @see drush_get_class().
273 function drush_role_get_class($role_name = DRUPAL_ANONYMOUS_RID) {
274 return drush_get_class('Drush\Role\Role', func_get_args());
278 * Displays a list of roles
280 function drush_role_list($rid = '') {
283 drush_hide_output_fields(array('perm'));
284 // Get options passed.
285 $perm = drush_get_option('filter');
288 // Get all roles - if $perm is empty user_roles retrieves all roles.
289 $roles = user_roles(FALSE, $perm);
291 return drush_set_error('DRUSH_NO_ROLES', dt("No roles found."));
293 foreach ($roles as $rid => $value) {
294 $role = drush_role_get_class($rid);
295 $result[$role->name] = array(
297 'label' => $role->name,
302 drush_hide_output_fields(array('rid', 'label'));
303 $role = drush_role_get_class($rid);
307 $perms = $role->getPerms();
308 foreach ($perms as $permission) {
309 $result[$permission] = array(
310 'perm' => $permission);