Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I can do loads of user-related stuff with Drush, but I can't see how to create new user roles. Is it possible to do this with Drush?

share|improve this question

3 Answers 3

up vote 7 down vote accepted

This functionality is available in Drush 8.x-6.x:

 role-add-perm (rap)   Grant a specified permission to a role.                  
 role-create (rcrt)    Create a new role.                                       
 role-delete (rdel)    Delete a role.                                           
 role-list (rls)       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.                                                  
 role-remove-perm      Remove a specified permission from a role.               
 (rmp)                                                                          

Unfortunately, this has not been backported to the 7.x-5.x branch, so it probably won't be in the soon-to-be-released Drush-5.9, and isn't in Drush-5.8. For now, you'll have to use the dev branch to get it.

share|improve this answer

To get a list of all drush based commands just type drush at the command prompt and it will list all the set of commands available.

drush help [command] to view command-specific help.

Run drush topic to read even more documentation.

Drush user specific commands

User commands: (user)

user-add-role (urol) Add a role to the specified user accounts.

user-block (ublk) Block the specified user(s).

user-cancel (ucan) Cancel a user account with the specified name.

user-create (ucrt) Create a user account with the specified name.

user-information Print information about the specified user(s). (uinf)

user-login (uli) Display a one time login link for the given user account (defaults to uid 1).

user-password (upwd) (Re)Set the password for the user account with the specified name.

user-remove-role Remove a role from the specified user accounts. (urrol)

user-unblock (uublk) Unblock the specified user(s).

share|improve this answer
    
Can you add in the syntax that generate this? –  MPD Jan 10 '13 at 19:07
    
drush help --filter=user –  greg_1_anderson Jan 11 '13 at 20:58

Here's a Drush script I made to use with Drush 5.9. Just name as user_roles.drush.inc under ~/.drush and then drush cc drush.

<?php
// user_roles.drush.inc
// author: yuvals

function user_roles_drush_command() {
        $items = array();

        $items['roles-delete'] = array(
                'description' => 'Delete one or more user roles.',
                'arguments' => array(
                        'ids' => 'Comma delimited list of user role IDs (rid).',
                ),
                'examples' => array(
                        'drush roles-delete 30,31,32' => 'Deletes the specified user roles.',
                        'drush roles-delete' => 'Prompts for selection of user roles to delete.',
                ),
                'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
        );

        $items['roles-list'] = array(
                'description' => 'Lists user roles.',
                'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
        );

        return $items;
}

function drush_user_roles_roles_list() {
        foreach (user_roles() as $rid=>$name) {
                        drush_print(sprintf("%-7d\t%s", $rid, $name));
        }
}

function drush_user_roles_roles_delete($ids = NULL) {

        $roles = user_roles();

        if ($ids == NULL) {
                $options = array();
                foreach ($roles as $rid=>$name) {
                        // keys must be string otherwise array_merge inside drush_choice_multiple overwrites them
                        $options['.'.$rid] = $name;
                }
                $selection = drush_choice_multiple($options, FALSE, 'Choose which user roles you would like to delete.', '!value');
                if ($selection == null) {
                        return;
                }
                $ids = array();
                foreach ($selection as $id_string) {
                        $ids[] = intval(substr($id_string, 1)); // hack - remove '.' prefix; see above
                }
        }
        else {
                $valid_ids = array();
                $ids = explode(',', $ids);
                foreach ($ids as $id) {
                        $id = intval($id);
                        if (isset($roles[$id])) {
                                $valid_ids[] = $id;
                        }
                }
                $ids = $valid_ids;
        }

        if (count($ids) == 0) {
                return drush_log(dt('No user roles to delete.'));
        }
        $message = dt("The following @number user roles will be deleted:\n", array('@number' => count($ids)));
        $role_table = sprintf("%-7s%s\n", dt('rid'), dt('name'));
        foreach ($ids as $id) {
                $role_table .= sprintf("%-7d%s\n", $id, $roles[$id]);
        }
        $role_table = sprintf("%-7s%s\n", dt('rid'), dt('name'));
        foreach ($ids as $id) {
                $role_table .= sprintf("%-7d%s\n", $id, $roles[$id]);
        }
        $message .= $role_table;

        if (drush_confirm($message)) {
                foreach ($ids as $rid) {
                        user_role_delete($rid);
                }
                drush_log(dt("@number user roles were deleted:\n@role_table", array('@number' => count($ids), '@role_table' => $role_table)));
        }

}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.