Community.
I have two different User Sources, with the same users and roles, and I would like to copy the role assignation.
I tried with a simple script, but since it timed out, I created the following.
user_source_a = 'User Source A' # Source.
user_source_b = 'User Source B' # Destination
def assign_roles():
import system
users = system.user.getUsers(user_source_a)
for user_a in users:
username = user_a.get('username')
print 'Checking user: %s' % username
roles = sorted(user_a.getRoles())
if len(roles) > 0:
user_b = system.user.getUser(user_source_b, username)
user_b.addRoles(roles)
system.user.editUser(user_source_b, user_b)
def send_back():
system.gui.messageBox('Done!', 'Message')
system.util.invokeLater(send_back)
system.util.invokeAsynchronous(assign_roles)
So I just need your help with the following. Calling addRoles
was just a lucky guess because system.user.editUser
shows only an example with addContactInfo
. So please point me to the right location where I can find documentation on the User
object.
Edit: I think I’ve found the User documentation. PyUser.
I wrote this while I was working on migrating from one source to another. Allows for new users added to the original source, and to update the target roles with source roles, if they don’t match. (It happened a few times during migration). Hope there’s something useful in there you can use.
def printResponse(response):
warnings = response.getWarns()
errors = response.getErrors()
infos = response.getInfos()
if len(warnings) > 0:
print 'Warnings:'
for warning in warnings:
print "", warning
if len(errors) > 0:
print 'Errors:'
for error in errors:
print "", error
if len(infos) > 0:
print 'Infos:'
for info in infos:
print "", info
print '---------------------'
sourceUserSource = 'User Source A'
targetUserSource = 'UserSource B'
defaultPassword = 'password'
sourceUsers = system.user.getUsers(sourceUserSource)
sourceRoles = set(system.user.getRoles(sourceUserSource))
sourceUserDict = {}
for user in sourceUsers:
sourceUserDict[user.get('username')] = {'firstname' : user.get('firstname'),
'lastname' : user.get('lastname'),
'roles' : sorted(user.getRoles())
}
targetUsers = system.user.getUsers(targetUserSource)
targetRoles = set(system.user.getRoles(targetUserSource))
targetUserDict = {}
for user in targetUsers:
targetUserDict[user.get('username')] = {'firstname' : user.get('firstname'),
'lastname' : user.get('lastname'),
'roles' : sorted(user.getRoles())
}
# Add new roles to target source
rolesToProcess = sourceRoles - targetRoles
for role in rolesToProcess:
system.user.addRole(targetUserSource, role)
sourceUserSet = set(sourceUserDict.keys())
targetUserSet = set(targetUserDict.keys())
newUsers = sourceUserSet - targetUserSet
modifyUsers = sourceUserSet.intersection(targetUserSet)
for userName in newUsers:
user = system.user.getNewUser(targetUserSource, userName)
user.set('password', defaultPassword)
if sourceUserDict[userName]['firstname'] is not None:
user.set('firstname',sourceUserDict[userName]['firstname'])
if sourceUserDict[userName]['lastname'] is not None:
user.set('lastname',sourceUserDict[userName]['lastname'])
user.addRoles(sourceUserDict[userName]['roles'])
print userName
response = system.user.addUser(targetUserSource, user)
printResponse(response)
for userName in modifyUsers:
changeFlag = False
if sourceUserDict[userName]['roles'] != targetUserDict[userName]['roles']:
user = system.user.getUser(targetUserSource, userName)
user.setRoles(sourceUserDict[userName]['roles'])
response = system.user.editUser(targetUserSource, user)
printResponse(response)
1 Like