# Automatically selecting a role in User Management

**URL:** <https://forum.inductiveautomation.com/t/automatically-selecting-a-role-in-user-management/111830>\
**Category:** Ignition\
**Tags:** vision, scripting\
**Created:** [October 30, 2025, 3:20pm UTC](https://forum.inductiveautomation.com/t/automatically-selecting-a-role-in-user-management/111830 "2025-10-30T15:20:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![elise.henry](https://avatars.discourse-cdn.com/v4/letter/e/97f17d/32.png) [@elise.henry](https://forum.inductiveautomation.com/u/elise.henry)\
**Post date:** [October 30, 2025, 3:20pm UTC](https://forum.inductiveautomation.com/t/automatically-selecting-a-role-in-user-management/111830/1 "2025-10-30T15:20:16Z")

</div>

I’d like to add a function to the user management component to automatically select the “Viewer” role of a user if it wasn’t selected during creation. “Viewer” is the minimum required role for users to interact with our windows, but in the case that someone forgot to select that box, I still want it to append that role to the new user. I’m assuming this can be done with the onCreateUser or onSaveUser function in component scripting but I’ve tried a few things and can’t get it to work. Any ideas?

This is what I have right now that isn’t working:

```python
def onCreateUser(self, saveContext):
	user = saveContext.getUser()
	roles = list(user.getRoles())
	if "Viewer" not in roles:
		roles.append("Viewer")
		user.setRoles(roles)
	saveContext.setUser(user)

```

---

<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:** [October 30, 2025, 3:52pm UTC](https://forum.inductiveautomation.com/t/automatically-selecting-a-role-in-user-management/111830/2 "2025-10-30T15:52:37Z")

</div>

**Exactly** like that?

The Python language uses "semantic whitespace", meaning that the way your code is indented is _critical_ to its functionality (instead of adding curly braces around blocks as other languages do).

Your snippet won't do anything because it's not valid code unindented, but with the indentation fixed it _looks_ plausible:

```python
def onCreateUser(self, saveContext):
	user = saveContext.getUser()
	roles = list(user.getRoles())
	if "Viewer" not in roles:
		roles.append("Viewer")
	user.setRoles(roles)

	saveContext.setUser(user)

```

But now I'm further confused, because the `saveContext` does not have any get or set user function.

I'm going to go with a hunch here and say as a preemptive word of advice - don't trust LLMs for help with Ignition scripting, because they lack sufficient context to be helpful.  
You'll get, at best, way too much code (because they really like to write code) that _might_ accomplish your immediate goal but is going to lead you to problems down the line, and at worst _outright hallucinations_ that make you spin your wheels wasting your time.

The only capability you have within the `onCreateUser` extension function is to prevent the creation of the user. If you want to modify the user before it's created silently, you need to use `onSaveUser`.  
Your function becomes as simple as this:

```python
def onSaveUser(self, saveContext, user):
	if "Viewer" not in user.roles:
		user.addRole("Viewer")

```

---

<div class="post-metadata">

**Author:** ![elise.henry](https://avatars.discourse-cdn.com/v4/letter/e/97f17d/32.png) [@elise.henry](https://forum.inductiveautomation.com/u/elise.henry)\
**Post date:** [October 30, 2025, 5:26pm UTC](https://forum.inductiveautomation.com/t/automatically-selecting-a-role-in-user-management/111830/3 "2025-10-30T17:26:10Z")

</div>

Oops, I did have the indentation right on my end. It just didnt carry over when I did the preformatted text option on here. It’s now reflecting what I had in ignition. As far as LLMs go, they definitely lack a lot of context regarding ignition but have been extremely helpful with getting the skeletal functionality of what I'm looking for. Then I can usually figure it out from there. I always struggle to find proper documentation that can show _what_ exactly can be done to a certain function.

As far as the function goes, I added the one you included and it worked. Thank you so much
