Getting a list of users that belong to a specific role in Drupal multisite

Events happening in the community are now at Drupal community events on www.drupal.org.
humansky's picture

I'm trying to get a list of user emails that belong to the role 'site administrators' from a remote multisite Drupal environment. When I run the following command:

drush @remote_site sql-query 'SELECT DISTINCT u.mail FROM users AS u LEFT JOIN users_roles AS ur ON u.uid=ur.uid LEFT JOIN role AS r ON ur.rid=r.rid WHERE r.name = "site administrator"'

The SQL query works, however, it only works for the "default" site. Is there anyway to run this command for all the sites in my environment? I tried adding "@sites" after the remote alias, but it still only points to the default folder. Any ideas?

Thank you,
Henry

Comments

More quotes

eporama's picture

You can run remote drush commands and quote the command (including the @sites portion). You'll just have to escape the quotes necessary for the r.name string.


drush @remote "@sites sqlq 'SELECT DISTINCT u.mail FROM users AS u LEFT JOIN users_roles AS ur ON u.uid=ur.uid LEFT JOIN role AS r ON ur.rid=r.rid WHERE r.name = \"site administrator\"'"

That should work to get you what you need.

@eporama, I tried this exact

humansky's picture

@eporama, I tried this exact command, and it sorta worked. It looped through the same number of sites I have, so I know @sites is working, but it spit out the same exact response for each site. After I added the "-d" attribute and noticed that for each loop, the "--uri" attribute defaults to "http://default" instead of the uri for the site. I'm currently hosting on Acquia, so I wonder if there is something different in the way the @sites alias works. Here is my -d output with sensitive info redacted:

/usr/local/php5.5/bin/php  -d magic_quotes_gpc=Off -d magic_quotes_runtime=Off -d magic_quotes_sybase=Off /usr/local/drush6/drush.php --php=/usr/local/php5.5/bin/php --php-options=' -d magic_quotes_gpc=Off -d magic_quotes_runtime=Off -d magic_quotes_sybase=Off'  --backend=2 --debug --root=/var/www/html/XXXXXX.dev/platforms/XXXXX --verbose --uri=http://default --reserve-margin=79  sqlq 'SELECT DISTINCT u.mail FROM users AS u LEFT JOIN users_roles AS ur ON u.uid=ur.uid LEFT JOIN role AS r ON ur.rid=r.rid WHERE r.name = "site administrator"'

I had to change my --root, since my Drupal codebase is not in the docroot folder, but other than that, it should be like any other Acquia Drupal install.

Any thought? Perhaps I should open a ticket with Acquia?

What a coincidence

eporama's picture

Interestingly enough, I am a manager in the Acquia Support department ;-) So I would definitely suggest that a ticket could be warranted.

I would say that the remote aliases we provide don't modify much in terms of what is being passed. However, we do specify a --uri in the alias pointing to your *.acquia-sites.com domain. I'm going to see if I can replicate this issue on a multisite that I have on our infrastructure.

And for everyone else, if we do discuss this in an internal ticket, we'll be sure to post whatever relevant answers we can once this is figured out.

sql query

eporama's picture

You might also get faster results from


SELECT u.mail FROM users u INNER JOIN users_roles ur ON u.uid=ur.uid INNER JOIN role r ON ur.rid=r.rid AND r.name = "site administrator";

If there are a lot of users/roles, then this will be a much faster result.

Alternate sandbox drush command

eporama's picture

There's also a sandbox of a Drush command "user-list" that could be useful, but it's not in core drush.

https://www.drupal.org/sandbox/jbickar/2493771

Multisite

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds:

Hot content this week