Get username in Update gateway event script

Hello.

I was viewing the documentation for system.security.getUsername() and saw one of the examples called this function in the Startup script. I tried using it in Update (when a user saves the designer) but I get the following error in the logs.

com.inductiveautomation.ignition.common.script.JythonExecException: Traceback (most recent call last): File "<[] Update Script>", line 4, in AttributeError: 'com.inductiveautomation.ignition.common.script.Imm' object has no attribute 'getUsername'

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._pyx10.f$0(<[] Update Script>:10)

at org.python.pycode._pyx10.call_function(<[] Update Script>)

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

at org.python.core.PyCode.call(PyCode.java:18)

at org.python.core.Py.runCode(Py.java:1614)

at com.inductiveautomation.ignition.common.script.ScriptManager.runCode(ScriptManager.java:778)

at com.inductiveautomation.ignition.gateway.project.ProjectScriptLifecycle$TrackingProjectScriptManager.runCode(ProjectScriptLifecycle.java:678)

at com.inductiveautomation.ignition.common.script.ScriptManager.runCode(ScriptManager.java:749)

at com.inductiveautomation.ignition.gateway.project.ProjectScriptLifecycle$TrackingProjectScriptManager.runCode(ProjectScriptLifecycle.java:669)

at com.inductiveautomation.ignition.common.script.ScriptManager.runCode(ScriptManager.java:711)

at com.inductiveautomation.ignition.gateway.project.ProjectScriptLifecycle$TrackingProjectScriptManager.runCode(ProjectScriptLifecycle.java:650)

at com.inductiveautomation.ignition.gateway.project.ProjectScriptLifecycle.runUpdateScript(ProjectScriptLifecycle.java:360)

at com.inductiveautomation.ignition.gateway.project.ProjectScriptLifecycle$1.projectUpdated(ProjectScriptLifecycle.java:110)

at com.inductiveautomation.ignition.gateway.project.BaseProjectManager.doProjectUpdatedNotification(BaseProjectManager.java:990)

at com.inductiveautomation.ignition.gateway.project.BaseProjectManager.lambda$fireProjectUpdated$24(BaseProjectManager.java:940)

at com.inductiveautomation.ignition.common.util.ExecutionQueue$PollAndExecute.run(ExecutionQueue.java:204)

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 "<[] Update Script>", line 4, in AttributeError: 'com.inductiveautomation.ignition.common.script.Imm' object has no attribute 'getUsername'

Is there something Im doing wrong? Is there a different way of getting the username of the user who saved their environment?

You’re using a client-scope function in a gateway script.

1 Like

Yeah that makes sense. Thank you. Do you know of a function that can accomplish this goal in a gateway script?

The gateway can run without a logged in user, or with many logged in users, so that function wouldn’t make a lot of sense.

I’m not really sure how I’d accomplish your goal via scripting like this. You should be able to achieve what you want by setting up Auditing in your gateway.

Have a peek here:
https://docs.inductiveautomation.com/display/DOC80/Audit+Log+and+Profiles

The Audit Log Display has the ability to show who and when a project was updated.

1 Like

This works great for audit logging purposes.
What if you are calling a batch script on the save button in the designer?
I want to commit to a git repository and I want the username of the user who saved the project to be in the commit message.
I could pass in as a parameter to that batch script that does the commits but I can’t find a way to access the username from within this Gateway Update Script.

1 Like

I’m trying to do this too. Have you found any workarounds?

You can retrieve the username from the audit log (system.util.queryAuditLog()). Filter on
actionFilter='project save',
targetFilter="projectName"

Maybe put some values in for the startDate and endDate that are just a few seconds apart from system.date.now()

I have not attempted to pass this as an argument to the bash script that runs to auto-commit changes on project save.