Number of Members in a Role

Hi there.

Wondering if there is a way to get the number of members in a role? I can see this number in the “User Management” component but I cannot find how to access this number. I would like to create an alarm if the number of members in a specific role exceeds 128. Reason being is our SMTP server limits the number of recipients to 128 and I have a report being emailed out based on role. If the number of recipients exceeds 128 then the report does not get emailed out.

Thanks

The way the user management component does it is actually as simple as ‘query all the users, then get all the roles each user has’ - which you can replicate pretty easily via builtin scripting functions:

from collections import defaultdict

users = system.user.getUsers("default")

roles = defaultdict(list)
for user in users:
	for role in user.roles:
		roles[role].append(user)
		
for (role, members) in roles.items():
	print role, len(members), members
1 Like

First get a list of of the users in a certain profile, then access the roles for each user and tally it.

count = 0	
profile = 'yourProfile'
users = system.user.getUsers(profile)
for user in users:
	roles = user.getRoles()
	if 'production' in roles:
		count += 1
print count

Note @PGriffith script is probably better

Thanks. Both solutions work.