11

How can I set a user to a specific role via drush? In my case I want to give the role of administrator to the user.

16
drush help user-add-role
Add a role to the specified user accounts.

Examples:
 drush user-add-role "power user" 5,user3  Add the "power user" role to the accounts with name, id, or email 5 or user3, uids 2 
 --uid=2,3 --name=someguy,somegal          and 3, names someguy and somegal, and email address of billgates@microsoft.com       
 --mail=billgates@microsoft.com

Arguments:
 role                                      The name of the role to add                                                
 users                                     (optional) A comma delimited list of uids, user names, or email addresses.

Options:
 --mail=<me@example.com>                   A comma delimited list of user mail addresses of users to operate on. 
 --name=<foo>                              A comma delimited list of user names of users to operate on.          
 --uid=<3,5>                               A comma delimited list of uids of users to operate on.

Aliases: urol

So, for your use case:

drush user-add-role administrator USERNAME

| improve this answer | |
  • tats too easy! =P ... at least now i know how to do it via sql... lol – Patoshi パトシ Aug 13 '15 at 23:53
  • @duckx but that's like using a back-scratcher to scratch your front – Patrick Ryan Aug 14 '15 at 2:04
0

First you will need to get the user's id (UID) in the users table like this:

drush sqlq 'select * from users \Gamma'

My uid I wanted was uid = 444

Then get the role id that you want assigned to that user by doing this:

drush sqlq 'select * from role'

Administrator role is rid = 3

Now I have my uid and rid, I do the following:

drush sqlq 'insert into users_roles (uid, rid) values (444, 3);'
| improve this answer | |
  • 1
    Never do this! Do not get into the habit of manipulating your database, eventually something will break horribly when you skip a hook which would've been called if you used the right APIs. I think this answer should be deleted. – user49 Aug 19 '15 at 9:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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