# Pass AD List to Shared Script

**URL:** <https://forum.inductiveautomation.com/t/pass-ad-list-to-shared-script/32147>\
**Category:** Ignition\
**Created:** [February 11, 2020, 9:19pm UTC](https://forum.inductiveautomation.com/t/pass-ad-list-to-shared-script/32147 "2020-02-11T21:19:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![LandrethEdward](https://avatars.discourse-cdn.com/v4/letter/l/7bcc69/32.png) [@LandrethEdward](https://forum.inductiveautomation.com/u/LandrethEdward)\
**Post date:** [February 11, 2020, 9:19pm UTC](https://forum.inductiveautomation.com/t/pass-ad-list-to-shared-script/32147/1 "2020-02-11T21:19:49Z")

</div>

Hello,  
I am attempting to convert a security login project script that we have to be utilized as a global script with a parameter passed in so it can be shared more easily across projects. It seems like the list of AD groups that I’m trying to pass into the script may not be getting received properly so I’d appreciate any thoughts on this.

Here is a layout of what the shared script definition looks like:

```auto
def rolesCheck(groupRoles):
	userRoles = system.security.getRoles()
	userRoles = list(userRoles)
	for i in range(len(groupRoles)-1,-1,-1):
		if any(s.upper() in userRoles for s in groupRoles[i]):
			system.gui.messageBox(groupRoles[i])
			return i

On a button press we run the script below to switch the system user and pass the AD groups to be checked for:

user = event.source.parent.getComponent('Username').text
password = event.source.parent.getComponent('Password').text
groupRoles = [['AD_GROUP_1'] 
,['AD_GROUP_2'] 
,['AD_GROUP_3A','AD_GROUP_3B'] 
,['AD_GROUP_4'] 
,['AD_GROUP_5A','AD_GROUP_5B'] 
]
system.security.switchUser(user,password,event)
system.tag.write("[Client]SecurityLevel",shared.Security.rolesCheck(groupRoles))

```

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [February 11, 2020, 9:41pm UTC](https://forum.inductiveautomation.com/t/pass-ad-list-to-shared-script/32147/2 "2020-02-11T21:41:26Z")

</div>

Please edit your post to place the code in a code block. (: Just put triple back-quotes (these: ```) above and below the pasted code (on lines of their own).

---

<div class="post-metadata">

**Author:** ![LandrethEdward](https://avatars.discourse-cdn.com/v4/letter/l/7bcc69/32.png) [@LandrethEdward](https://forum.inductiveautomation.com/u/LandrethEdward)\
**Post date:** [February 19, 2020, 5:47pm UTC](https://forum.inductiveautomation.com/t/pass-ad-list-to-shared-script/32147/3 "2020-02-19T17:47:13Z")

</div>

Wow that is awesome didn’t realize you could do that! Thanks please let me know if you need anything else to look at this I appreciate it.

---

<div class="post-metadata">

**Author:** ![Tim](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/tim/32/3361_2.png) [@Tim](https://forum.inductiveautomation.com/u/Tim)\
**Post date:** [February 19, 2020, 9:15pm UTC](https://forum.inductiveautomation.com/t/pass-ad-list-to-shared-script/32147/4 "2020-02-19T21:15:17Z")

</div>

I think you’re after something like this.

```auto
userSource = 'usersource'
user = 'user'
password = 'password'
groupRoles = [
'AD_GROUP_1'
,'AD_GROUP_2'
,'AD_GROUP_3A'
,'AD_GROUP_3B' 
,'AD_GROUP_4'
,'AD_GROUP_5A'
,'AD_GROUP_5B' 
]

userRoles = system.security.getUserRoles(user, password, userSource)

i = 0	
for item in groupRoles:
	i += 1
	if item in userRoles: 
		securityLevel = i

print i
print securityLevel
print userRoles
print groupRoles

system.security.switchUser(user,password)
system.tag.write("[Client]SecurityLevel",securityLevel)

```

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [February 19, 2020, 9:27pm UTC](https://forum.inductiveautomation.com/t/pass-ad-list-to-shared-script/32147/5 "2020-02-19T21:27:11Z")

</div>

Your code is pretty much working. I made a few minor changes to make it easier to test, but the logic is basically fine. The main changes I made were to make it a bit more “Pythonic”, ie `for i, roles in enumerate(reversed(groupRoles)):`

```python
def rolesCheck(groupRoles, userRoles=None):
	if userRoles is None:
		userRoles = list(system.security.getRoles())
	for i, roles in enumerate(reversed(groupRoles)):
		if any(s.upper() in userRoles for s in roles):
# system.gui.messageBox(str(roles))
			return i
	return -1

# On a button press we run the script below to switch the system user and pass the AD groups to be checked for:

#user = event.source.parent.getComponent('Username').text
#password = event.source.parent.getComponent('Password').text
groupRoles = [['AD_GROUP_1'] 
,['AD_GROUP_2'] 
,['AD_GROUP_3A','AD_GROUP_3B'] 
,['AD_GROUP_4'] 
,['AD_GROUP_5A','AD_GROUP_5B'] 
]
#system.security.switchUser(user,password,event)
#system.tag.write("[Client]SecurityLevel",shared.Security.rolesCheck(groupRoles))

print rolesCheck(groupRoles, userRoles=['AD_GROUP_4', 'AD_GROUP_1'])

```

---

<div class="post-metadata">

**Author:** ![LandrethEdward](https://avatars.discourse-cdn.com/v4/letter/l/7bcc69/32.png) [@LandrethEdward](https://forum.inductiveautomation.com/u/LandrethEdward)\
**Post date:** [February 20, 2020, 8:38pm UTC](https://forum.inductiveautomation.com/t/pass-ad-list-to-shared-script/32147/6 "2020-02-20T20:38:44Z")

</div>

Thanks very much for your help. While trying out the code from PGriffith I was still having a problem so I tried putting in another AD group and that one worked. Upon further inspection from printing the returned user roles I noticed that the one I had been testing with has some under case letters and the code was looking for the uppercase version. So I upper cased the user roles and it worked. Thanks again for your help with this!
