Adding the users role based on what form they have submitted

Hi,
I am using this code to add new users…

# Get new user
	userToGet = system.user.getNewUser("Users", self.getSibling("txtUsername").props.text)
		  
		# Add some contact info
	userToGet.set("firstname", self.getSibling("txtFirstName").props.text)
	userToGet.set("lastname", self.getSibling("txtSecondName").props.text)
	
		
	contactInfo = {"email":self.getSibling("txtEmailAddress").props.text}
	userToGet.addContactInfo(contactInfo)
	userToGet.set("password", self.getSibling("txtPassword").props.text)
		
		  
		# Adds a user to the the usersource.
	system.user.addUser("Users", userToGet)

I have tried to set the users roles by trying to copy the way I have the first name and second name done but it isnt working. How would I be able to set user role to administrator in this bit of code?

After speaking with @PGriffith:

roles = ['RoleOne', 'RoleTwo']
userToGet.setRoles(roles)
system.user.addUser("Users", userToGet)
1 Like

That worked thanks.

1 Like

Hi, I am trying to use the same idea for letting users update their account details and I am getting the following error…

Error running action ‘component.onActionPerformed’ on UpdateAccount@D/root/btnChange: Traceback (most recent call last): File " ", line 22, in runAction AttributeError: ‘NoneType’ object has no attribute ‘set’

This is the code I am using…

userToChange = system.user.getUser("Users", self.session.props.auth.user.userName)
	 
	

	userToChange.set("firstname", self.getSibling("txtFirstName").props.text)
	userToChange.set("lastname", self.getSibling("txtSecondName").props.text)
	userToChange.set("password", self.getSibling("txtPassword").props.text)
	userToChange.set("username", self.getSibling("txtUsername").props.text)
	

	system.user.editUser("Users", userToChange)

I believe it was working before and has just recently stopped.

  1. Which of those lines is line 22? I want to assume userToChange.set("firstname", self.getSibling("t… but verification is the first step.
  2. While we always prefer formatted code like you’ve done here, indentation is incredibly important in Jython, and the difference in indentation concerns me. Could you add a screenshot of the code area in question so that I can verify the indentation is not the problem in some way?
  3. This is not the way to “update” users which already exist - you should use system.user.editUser for that.

code

Am I not using this example the right way?

userToChange = system.user.getUser("default", self.session.props.auth.user.userName)

Is “Users” a good user source? You have it configured?

Yes, I am using it for adding new users and that is working fine

Does the user already exist in the user source?

Yes, I am taking the users details from the session and displaying them in textboxs to be edited and submitted through a button.

You might need to contact support because I’m not seeing anything that would cause this based on your responses.

I have contacted support and I am waiting on a response. This is the error in my logs…


com.inductiveautomation.ignition.common.script.JythonExecException: Traceback (most recent call last): File “”, line 22, in runAction AttributeError: ‘NoneType’ object has no attribute ‘set’

at org.python.core.Py.AttributeError(Py.java:207)

at org.python.core.PyObject.noAttributeError(PyObject.java:1032)

at org.python.core.PyObject.getattr(PyObject.java:1027)

at org.python.pycode._pyx1217.runAction$1(:40)

at org.python.pycode._pyx1217.call_function()

at org.python.core.PyTableCode.call(PyTableCode.java:171)

at org.python.core.PyBaseCode.call(PyBaseCode.java:308)

at org.python.core.PyFunction.function___call__(PyFunction.java:471)

at org.python.core.PyFunction.call(PyFunction.java:466)

at org.python.core.PyFunction.call(PyFunction.java:461)

at com.inductiveautomation.ignition.common.script.ScriptManager.runFunction(ScriptManager.java:822)

at com.inductiveautomation.ignition.common.script.ScriptManager.runFunction(ScriptManager.java:806)

at com.inductiveautomation.ignition.gateway.project.ProjectScriptLifecycle$TrackingProjectScriptManager.runFunction(ProjectScriptLifecycle.java:687)

at com.inductiveautomation.ignition.common.script.ScriptManager$ScriptFunctionImpl.invoke(ScriptManager.java:965)

at com.inductiveautomation.ignition.gateway.project.ProjectScriptLifecycle$AutoRecompilingScriptFunction.invoke(ProjectScriptLifecycle.java:752)

at com.inductiveautomation.perspective.gateway.script.ScriptFunctionHelper.invoke(ScriptFunctionHelper.java:101)

at com.inductiveautomation.perspective.gateway.action.ScriptAction.runAction(ScriptAction.java:71)

at com.inductiveautomation.perspective.gateway.model.ActionCollection$ActionSequence$ExecuteActionsTask.lambda$call$0(ActionCollection.java:263)

at com.inductiveautomation.perspective.gateway.api.LoggingContext.mdc(LoggingContext.java:54)

at com.inductiveautomation.perspective.gateway.model.ActionCollection$ActionSequence$ExecuteActionsTask.call(ActionCollection.java:252)

at com.inductiveautomation.perspective.gateway.model.ActionCollection$ActionSequence$ExecuteActionsTask.call(ActionCollection.java:221)

at com.inductiveautomation.perspective.gateway.threading.BlockingTaskQueue$TaskWrapper.run(BlockingTaskQueue.java:154)

at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)

at java.base/java.util.concurrent.FutureTask.run(Unknown Source)

at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)

at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)

at java.base/java.lang.Thread.run(Unknown Source)

Caused by: org.python.core.PyException: Traceback (most recent call last): File “”, line 22, in runAction AttributeError: ‘NoneType’ object has no attribute ‘set’

… 27 common frames omitted

Seems pretty clear that userToChange is null. Meaning, your user lookup didn’t return a user for some reason. You have to deal with that case.

2 Likes

@pturmel is correct. Based on your responses, however, I’m not able to tell you why no user is being returned. If you are already waiting on support, you should hang tight and wait.

1 Like