Unable to get list of users in a role

1470
4
12-11-2017 07:52 AM
Highlighted
New Contributor III

Our portal installation is configured with enterprise logins and allows users to auto create an account when they authenticate for the first time.  We also have a custom role called "Auto_Created" which is the default role for new user accounts.  I would like to get a list of users in this role and perform some nightly administrative tasks. I can get the role via the roleManager, but I'm unable to successfully perform a search for users with the role name as criteria (see attached screen grab). 

[EDIT] Yes, I am certain that more than 0 users in our portal are in the  "Auto_Created" role. 

Would appreciate any pointers.  Thanks!

4 Replies
Highlighted
Occasional Contributor III

Add an empty parameter first in your search method. See below for pseudo code.

I tested your screenshot code against our portal by passing 'role: custom_role' as the only parameter and returned 0 users. When I add an empty string parameter, it returns the proper count.

#import modules
from arcgis import *
                
#declare gis
gis = GIS('URL', 'admin', 'pass')

#return list of users in portal
users = gis.users.search('', 'role: Auto_Created')
print("Users returned: " + str(len(users)))
Reply
0 Kudos
Highlighted
New Contributor III

Thank you for taking the time to reply.  Unfortunately your example code didn't work for me, It's still not returning any users from that custom role.  I think the issue is that the role name may not be valid criteria for use in the query. For example, in searching for users in the 'Administrator' role nothing would be returned. If, however, I use 'org_admin', which I found buried in some documentation it works. This leads me to wonder what I'm supposed to use as criteria for searching by custom role and where I'm supposed to find it?

Docs concerning roles:

http://doc.arcgis.com/en/arcgis-online/reference/roles.htm

Docs were I found the 3 constants (org_user, org_publisher, org_admin) for built-in roles:

https://developers.arcgis.com/python/guide/accessing-and-managing-users/#about-user-roles

Example:

users = gis.users.search('role:Administrator')     # returns 0

users = gis.users.search('role:org_admin')         # returns actual count   

Highlighted
Occasional Contributor

Hi Mike Quetel‌ - I haven't figured a way to make a list of users in a role directly from a gis.users.search(), but I generated one using role_id values in this way. The users list in the second block of code returned the correct users in the portal I tested.  Will that work to get your list?

g = GIS("portal url", "username", "password")

role_mgr = arcgis.gis.RoleManager(g)
roles = role_mgr.all()
for role in roles:
print("Role ID: {}".format(role.role_id))

users = [user for user in g.users.search() if user.role == '<role.role_id value>']
for user in users:
print("User: {:<20}\tRole: {}".format(user.username, user.role))‍‍‍‍‍‍‍‍‍
Highlighted
New Contributor III

This also works. For some reason I was unable to return all users when using the code in line 1.

users = gis.users.search(query='role: <enter role id here>', sort_field='created', sort_order='asc', max_users=200, outside_org=False)‍
Reply
0 Kudos