Select to view content in your preferred language

Not Able to Get All Roles (Default Built-In) in Portal Using ArcGIS Python API

690
1
09-11-2024 10:47 AM
BHK
by
Regular Contributor

I am using this ArcGIS Python API to list all roles and their associated  Privileges with this Code but this is ONLY and ONLY listing custom Roles and not default roles like (Administrator, Publisher,Creator .. )

 

roles = gis.users.roles.all()
# Print roles
for role in roles:
    print(f"Role Name: {role['name']}, Role ID: {role['id']}, Role Description: {role['description']}")

Can you please let me know why this is happening? and how I can get all

Tags (1)
1 Reply
AaronKoelker
Frequent Contributor

@BHK I know this is ancient, but in case anyone else comes across this while surfing the web for answers—the default Admin, Publisher, and User roles are handled differently. Not sure why, probably legacy stuff from when ArcGIS Online was first launched. While custom roles and later default Esri roles (Viewer, Data Editor) have a random 16-character id and a RoleObject you can read, the ID for these three original roles are org_admin, org_publisher, and org_user.  They behave differently and you can't pull the same info on them through gis.users.roles.all() as you can with the others, which have associated role objects. You can see these if you pull up a specific user with one of these three roles and check their roleid (see example below).

If you want to pull the privileges for those 3 default roles, you can pull an existing user who has that account type and get it from there. I don't know of a way to get it without going through an existing user who has those default roles

from arcgis import gis
from arcgis.gis import GIS

gis = GIS(url = 'your_portal_url', username = 'your_username', password = 'your_password')

### Inspect org roleIds, remove the query to see all roles, and how these defaults compare with custom roles
for user in gis.users.search(query="role:org_admin"):
    print(user.roleId)

### If you try to get the roleobject for one of these default roles, you'll get None in return. Whereas if you put in the id of a custom role (like you are in your example) you can pull name, description, privileges, etc.
roleobject =  gis.users.roles.get_role('org_admin')
print(roleobject)

# Get all possible privileges via admin
for user in gis.users.search(query="role:org_admin", max_users=1):
    full_priv_list = user.privileges

# Get privileges for all 3 default roles
primary_default_roles = ['org_admin', 'org_publisher', 'org_user']
for dr in primary_default_roles:
    for user in gis.users.search(query = f'role:{dr}', max_users = 1):
        print(user.roleId)
        priv_list = user.privileges
        print(priv_list)

 

-Aaron
0 Kudos