copy teamsite user

April 2, 2012

To copy teamsite user permissions i’ve written a python script.

You’ll need to replace the path with the path of your teamsite’s bin directory:

#!/usr/bin/python
#Script Name: copyTeamSiteUser.py
#Use: ./copyTeamSiteUser.py -s (source_user) -d (destination_user)
#Note: the bin bath of teamsite was hardcoded, please replace /opt/iw-home/TeamSite/bin
#      path of your teamsite's bin directory
#Author: Steve Stonebraker

import fileinput
import sys, re, getopt, os
import subprocess, traceback, inspect, re

def main(argv):
	user1 = ''
	user2 = ''
	try:
		opts, args = getopt.getopt(argv,"hs:d:")
	except getopt.GetoptError:
		print ('copyTeamSiteUser.py -s (source_user) -d (destination_user)')
		sys.exit(2)
	for opt, arg in opts:
		if opt == '-h':
			print ('copyTeamSiteUser.py -s (source_user) -d (destination_user)')
			sys.exit()
		elif opt == '-s':
			user1 = arg
		elif opt == '-d':
			user2 = arg
	print("Source User: " + user1)
	print("Destination User: " + user2)

	#export permissions entries for user1
	subprocess.call("/opt/iw-home/TeamSite/bin/iwaccess all-permission-entries-of-user " + user1 + " > sourceUser.tmp", shell=True)

	#export permissions entries for user2
	subprocess.call("/opt/iw-home/TeamSite/bin/iwaccess all-permission-entries-of-user " + user2 + " > destUser.tmp", shell=True)
	
	#dedupe permissions entries that do not match
        subprocess.call("fgrep -x -f destUser.tmp -v sourceUser.tmp > permissionsToAdd.tmp", shell=True)
	
	scriptPath=os.getcwd()

	#list permissions to process
	print("Permissions to process:")
        subprocess.call("cat permissionsToAdd.tmp", shell=True)
	text_file = open("permissionsToAdd.tmp", "r")
	line=0
	
	#Switch to teamsite dir
	os.chdir("/opt/iw-home/TeamSite/bin")

	#process permissions to add
	for lineTxt in text_file.readlines():
		re.sub("\s\s+" , " ", lineTxt)
		thePath = lineTxt.split( )
		theItems = lineTxt.replace(thePath[0],"")
		cleanItems = theItems.replace("\t","")
		cleanItems = cleanItems.replace("\n","")	
		arrItems = cleanItems.split(",")
		line=line+1 

		#process permissions for specific line
		print "processing line: " + lineTxt
		for permission in arrItems:
			print ("processing item: " + permission) 
			cmd="./iwaccess add-permission-entry " + thePath[0] + " -user " + user2 + " -role " + "'" +  permission + "'"
			print (cmd)
			subprocess.call(cmd, shell=True)
	text_file.close()

	#Switch to script dir
	os.chdir(scriptPath)
	print("current dir: " + os.getcwd())
	subprocess.call("rm *.tmp", shell=True)

	#export permissions entries for user1
	subprocess.call("/opt/iw-home/TeamSite/bin/iwaccess all-permission-entries-of-user " + user1 + " > sourceUser.tmp", shell=True)

	#export permissions entries for user2
	subprocess.call("/opt/iw-home/TeamSite/bin/iwaccess all-permission-entries-of-user " + user2 + " > destUser.tmp", shell=True)
	
	#dedupe permissions entries that do not match
        subprocess.call("fgrep -x -f destUser.tmp -v sourceUser.tmp > permissionsToAdd.tmp", shell=True)

	print("Processing complete, comparing " + user1 + " to " + user2)
	if os.path.getsize(scriptPath + "/permissionsToAdd.tmp") > 0:
		print(user2 + " is missing the following permissions")
		subprocess.call("cat permissionsToAdd.tmp", shell=True)
		print("Check permissions for each entry manually")
		print("example:")
		print("# /opt/iw-home/TeamSite/bin/iwaccess all-permission-entries-of-user " + user2 + " | grep '/default/main/marlboro/cms'")

	else:
		print(user1 + " and " + user2 + " have equal permissions")

if __name__ == "__main__":
	main(sys.argv[1:])