(Vision) User management script (onDeleteUser)

Hello everyone! Do you think I can get the numbers circled in red?
My goal is to make user management such that it’s not possible to delete a user if he is the last of his role.
Thanks for your attention!

Immagine

Something like this should work.

allUsers = system.user.getUsers('')

# Create dictionary with user counts of roles
roleCount = {}
for userObj in allUsers:
	for role in userObj.roles:
		if role not in roleCount.keys():
			roleCount[role] = 1
		else:
			roleCount[role] += 1


rejectFlag = False

# Check to see if user is only one in the role. Set flag if true
for role in user.roles:
	if roleCount[role] == 1:
		rejectFlag = True

if rejectFlag == True:
	saveContext.rejectSave('Cannot delete user when it is only one in role.')
2 Likes

It works! Thank you so much!!