Users search by Group name?

609
4
Jump to solution
04-10-2019 02:09 PM
AllenScully
Occasional Contributor II

We have a new Portal site, need to copy users from an existing Portal, but only a subset of users, who belong to a specific group in the existing Portal.

Using the code samples provided, I have it mostly ready to run in Jupyter Notebooks (figured out how to make it work for Active Directory accounts), but I can't seem to have the users.search be filtered/queried for a specific group.  The example I'm working from has a query to filter out certain users, which is nice, but I'm not sure how to/if you can change that filter to reference a group:

source_users = source.users.search('!esri_ & !admin')
for user in source_users:
   print(user.username + "\t:\t" + str(user.role))

So what I'd like is to have something like (i know 'groupname' is not a real thing, this is just get the idea across):

source_users = source.users.search('groupname: My Group')
for user in source_users:
   print(user.username + "\t:\t" + str(user.role))

Thanks - 

Allen

0 Kudos
1 Solution

Accepted Solutions
StephenM
Occasional Contributor II

Maybe you could create a separate list of users by checking to see if they belong to the target group. Example:

source_users = source.users.search('!esri_ & !admin')

group_name = "My Group"

users_subset = []

for user in source_users:
if len(user.groups) > 0:
for group in user.groups:
if group.title == group_name:
users_subset.append(user)
print(user.username + "\t:\t" + str(user.role))

View solution in original post

4 Replies
Egge-JanPollé1
MVP Regular Contributor

Hi Allen Scully‌,

Just wondered: would this work for you? https://community.esri.com/people/EPolle_TensingInternational/blog/2019/03/21/a-quick-overview-of-gr... 

I wrote this script for ArcGIS Online, not sure it will work for Portal. It provides an overview of membership by group. You might modify it to filter out just one group.

Please let me know what you think.

Egge-Jan

AllenScully
Occasional Contributor II

Thanks Egge-Jan - 

This looks great - I'm still working through it, but essentially I'm modifying your script to append the group 0 or 1 flag to the user list and use that flag as a filter to only run the 'copy_users' function on those where the flag = 1.

I'll follow up with results and any useful code blocks.

Allen

StephenM
Occasional Contributor II

Maybe you could create a separate list of users by checking to see if they belong to the target group. Example:

source_users = source.users.search('!esri_ & !admin')

group_name = "My Group"

users_subset = []

for user in source_users:
if len(user.groups) > 0:
for group in user.groups:
if group.title == group_name:
users_subset.append(user)
print(user.username + "\t:\t" + str(user.role))

View solution in original post

AllenScully
Occasional Contributor II

The works perfectly - Thanks Stephen. 

The only odd thing I'm running up against is that the group of users we are 'cloning' has 83 people - under the default limit of 100 that the user search returns.   However when I run my modified version of your code above along with the Copy_users function, it stops after creating the first 60 users. I thought at first this was due to some bad data or similar related to the account of the next (61st in this example) user causing the function to stop, but actually the user search is just returning 60 users.  

Here's my final code using yours (slightly modified for our organization)  and also a modified version of the copy_users function found in the Python API examples (https://developers.arcgis.com/python/sample-notebooks/clone-portal-users-groups-and-content/)

NOTE: we use Active Directory security, so this example is set up for that - our username syntax is 'ADUSERNAME@DOMAIN' - so we split the user name on the '@' sign and use the first list item from that split to feed into the IDP_USERNAME input required for AD-level security:

Copy_users function for Active Directory (I took out some name validation and user updating code as it was not needed for us - the 'adname' variable is the active directory username generated later): NOTE - the order of these items in the target_portal.users.create function DOES seem to matter.  Also, obviously you need to define source_portal and target_portal using the URLS for each.  Also note that here these are level 2 users, so that simplifies the function because we hard-code that value for 'level'.

Here's where I used Stephen's code to get users only in the desired group, then generate the AD Name, and call the copy_users function - after running a user search creating the 'source_users' object:

Thanks - 

Allen