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:
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:
def onSaveUser(self, saveContext, user):
if "Viewer" not in user.roles:
user.addRole("Viewer")