# Replicating role assignation between sources

**URL:** <https://forum.inductiveautomation.com/t/replicating-role-assignation-between-sources/33764>\
**Category:** Ignition\
**Tags:** ignition80, scripting\
**Created:** [April 8, 2020, 2:21am UTC](https://forum.inductiveautomation.com/t/replicating-role-assignation-between-sources/33764 "2020-04-08T02:21:46Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![cesrom](https://avatars.discourse-cdn.com/v4/letter/c/ea666f/32.png) [@cesrom](https://forum.inductiveautomation.com/u/cesrom)\
**Post date:** [April 8, 2020, 2:21am UTC](https://forum.inductiveautomation.com/t/replicating-role-assignation-between-sources/33764/1 "2020-04-08T02:21:46Z")

</div>

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.

```auto
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](http://files.inductiveautomation.com/sdk/javadoc/ignition80/8.0.10/com/inductiveautomation/ignition/common/user/PyUser.html).

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [April 8, 2020, 2:59pm UTC](https://forum.inductiveautomation.com/t/replicating-role-assignation-between-sources/33764/2 "2020-04-08T14:59:54Z")

</div>

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.

```python
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)

```
