# 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:** 1\
**Showing post:** 2

<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")

```

---

_[View the full topic](https://forum.inductiveautomation.com/t/automatically-selecting-a-role-in-user-management/111830)._
