# System user functions

**URL:** <https://forum.inductiveautomation.com/t/system-user-functions/94045>\
**Category:** Ignition\
**Tags:** scripting\
**Created:** [August 1, 2024, 2:41pm UTC](https://forum.inductiveautomation.com/t/system-user-functions/94045 "2024-08-01T14:41:52Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zach\_Larson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zach_larson/32/54388_2.png) [@Zach\_Larson](https://forum.inductiveautomation.com/u/Zach_Larson)\
**Post date:** [August 1, 2024, 2:41pm UTC](https://forum.inductiveautomation.com/t/system-user-functions/94045/1 "2024-08-01T14:41:52Z")

</div>

I was trying to figure out how to write a bunch of roles to a set of users.

I have a script that I was given.  
It has some functions like `getContactInfo()` and `getRoles()`.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/0/d/0d07a6b47abc34da51467c5698bc10cc6d4134aa.png)

I don't see `getContactInfo()` in the list

So I thought maybe there are functions that I don't know about that will help me that I should ask about.

I basically want to add some roles to a list of users in a script to save time.

I can get their emails with my current script.  
I don't now how to set their roles though.

---

<div class="post-metadata">

**Author:** ![aaronrai24](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/aaronrai24/32/87248_2.png) [@aaronrai24](https://forum.inductiveautomation.com/u/aaronrai24)\
**Post date:** [August 1, 2024, 2:48pm UTC](https://forum.inductiveautomation.com/t/system-user-functions/94045/2 "2024-08-01T14:48:10Z")

</div>

`system.user.getUser()` returns a [user](https://www.docs.inductiveautomation.com/docs/8.1/appendix/reference-pages/scripting-object-reference#users) object which has functions like `getContactInfo` and `getRoles`. Based on this [javadoc](https://files.inductiveautomation.com/sdk/javadoc/ignition80/8.0.12/com/inductiveautomation/ignition/common/user/PyUser.html) if you have a User object, you can use `setRoles(java.util.Collection<java.lang.String> roles)`.to add roles to a user.

---

<div class="post-metadata">

**Author:** ![Mark\_Minuth](https://avatars.discourse-cdn.com/v4/letter/m/d78d45/32.png) [@Mark\_Minuth](https://forum.inductiveautomation.com/u/Mark_Minuth)\
**Post date:** [August 1, 2024, 2:51pm UTC](https://forum.inductiveautomation.com/t/system-user-functions/94045/3 "2024-08-01T14:51:34Z")

</div>

It looks like once you have that user object, you could also use some combination of:

```python
system.user.addRole(userSource, role)
system.user.editRole(userSource, oldName, newName)
system.user.removeRole(userSource, role)

```

---

<div class="post-metadata">

**Author:** ![KathyApplebaum](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kathyapplebaum/32/7261_2.png) [@KathyApplebaum](https://forum.inductiveautomation.com/u/KathyApplebaum)\
**Post date:** [August 1, 2024, 2:53pm UTC](https://forum.inductiveautomation.com/t/system-user-functions/94045/4 "2024-08-01T14:53:52Z")

</div>

What Aaron and Mark said.

Additionally, the [docs section on user properties](https://docs.inductiveautomation.com/docs/8.1/appendix/reference-pages/scripting-object-reference#users) will be helpful.

---

<div class="post-metadata">

**Author:** ![Zach\_Larson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zach_larson/32/54388_2.png) [@Zach\_Larson](https://forum.inductiveautomation.com/u/Zach_Larson)\
**Post date:** [August 1, 2024, 2:54pm UTC](https://forum.inductiveautomation.com/t/system-user-functions/94045/5 "2024-08-01T14:54:01Z")

</div>

I think those change roles for the usersource, not the user.

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [August 1, 2024, 2:55pm UTC](https://forum.inductiveautomation.com/t/system-user-functions/94045/6 "2024-08-01T14:55:50Z")

</div>

Once you edit the User object you call `system.user.editUser` to save those changes.

> **[system.user.editUser | Ignition User Manual](https://docs.inductiveautomation.com/docs/8.1/appendix/scripting-functions/system-user/system-user-editUser)**
>
> Alters a specific user in a user source, replacing the previous data with the new data passed in.

---

<div class="post-metadata">

**Author:** ![Zach\_Larson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zach_larson/32/54388_2.png) [@Zach\_Larson](https://forum.inductiveautomation.com/u/Zach_Larson)\
**Post date:** [August 7, 2024, 6:11pm UTC](https://forum.inductiveautomation.com/t/system-user-functions/94045/7 "2024-08-07T18:11:44Z")

</div>

My best guess with ChatGPT help is below.

I tested it without the system.user.editUser call.  
Seems to print normal.

The nesting part is a bit confusing for me.

```python
# List of emails to check against
email_list = ["email1@example.com", "email2@example.com", "email3@example.com"]

# Retrieve all users
users = system.user.getUsers('default')

# Loop through each user
for user in users:
    # Get contact info for the user
    contacts = user.getContactInfo()
    
    # Check if any contact is an email in the email_list
    for contact in contacts:
        if contact.contactType == "email" and contact.value in email_list:
            # Get the user's current roles
            current_roles = user.getRoles()
            
            # Append the "Pilot" role if not already present
            if "Pilot" not in current_roles:
                current_roles.append("Pilot")
                
                # Update the user's roles
                user.setRoles(current_roles)
                
                # Save the changes
                system.user.editUser("default", user)

                print(f"Added 'Pilot' role to user: {user.getName()}")

```

---

<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:** [August 7, 2024, 6:13pm UTC](https://forum.inductiveautomation.com/t/system-user-functions/94045/8 "2024-08-07T18:13:35Z")

</div>

> [@Zach\_Larson](#):
>
> ```python
> print(f"Added 'Pilot' role to user: {user.getName()}")
> 
> ```

f strings don't exist in Python 2, and the print _function_ is only available behind a `from __future__ ` import. This code is going to throw a syntax error when it reaches this conditional, if it even compiles. You cannot trust ChatGPT et al for general purpose Ignition help, full stop.

---

<div class="post-metadata">

**Author:** ![Zach\_Larson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zach_larson/32/54388_2.png) [@Zach\_Larson](https://forum.inductiveautomation.com/u/Zach_Larson)\
**Post date:** [August 7, 2024, 6:15pm UTC](https://forum.inductiveautomation.com/t/system-user-functions/94045/9 "2024-08-07T18:15:41Z")

</div>

Okay, but other than that line?

When I print the user, I don't get a dictionary, and that part confuses me a bit.  
I think it works other than just not able to print out the username at the end.

---

<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:** [August 7, 2024, 6:39pm UTC](https://forum.inductiveautomation.com/t/system-user-functions/94045/10 "2024-08-07T18:39:52Z")

</div>

It looks approximately correct, but I don't have time to debug further.

---

<div class="post-metadata">

**Author:** ![Zach\_Larson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zach_larson/32/54388_2.png) [@Zach\_Larson](https://forum.inductiveautomation.com/u/Zach_Larson)\
**Post date:** [August 7, 2024, 7:45pm UTC](https://forum.inductiveautomation.com/t/system-user-functions/94045/11 "2024-08-07T19:45:05Z")

</div>

I attempted the system.user.editUser function on a user that exists, but is not a real user.

```python
# List of emails to check against
email_list = ["email1@example.com"]

# Retrieve all users
users = system.user.getUsers('default')

# Loop through each user
for user in users:
    # Get contact info for the user
    contacts = user.getContactInfo()
    
    
    # Check if any contact is an email in the email_list
    for contact in contacts:
        if contact.contactType == "email" and contact.value in email_list:
            # Get the user's current roles
            current_roles = user.getRoles()
            print 'current roles'
            print (current_roles)
            
            # Append the "Pilot" role if not already present
            if "Pilot" not in current_roles:
                current_roles.append("Pilot")
                
                # Update the user's roles
                user.setRoles(current_roles)
                print 'user'
                print (user)
                print (user.get('username'))
                system.user.editUser("default", user)

```

I got back an error.  
`com.inductiveautomation.ignition.common.messages.UIResponse@25f99185`

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [August 7, 2024, 7:54pm UTC](https://forum.inductiveautomation.com/t/system-user-functions/94045/12 "2024-08-07T19:54:18Z")

</div>

That is not necessarily an error, it is a `UIResponse` object (details [here](https://files.inductiveautomation.com/sdk/javadoc/ignition80/8.0.12/com/inductiveautomation/ignition/common/messages/UIResponse.html))

On that response you can call methods like `getErrors()`, `getInfos()`, `getWarns()`

---

<div class="post-metadata">

**Author:** ![Zach\_Larson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zach_larson/32/54388_2.png) [@Zach\_Larson](https://forum.inductiveautomation.com/u/Zach_Larson)\
**Post date:** [August 7, 2024, 8:02pm UTC](https://forum.inductiveautomation.com/t/system-user-functions/94045/13 "2024-08-07T20:02:41Z")

</div>

I searched for how to do what you are saying.  
added to my script, hope I did it right:

```python
from com.inductiveautomation.ignition.common.messages import UIResponse
......

print(UIResponse.getErrors(system.user.editUser("default", user)))

```

I got this to print:

```python
[class org.python.core.PyString cannot be cast to class java.lang.String (org.python.core.PyString is in unnamed module of loader 'app'; java.lang.String is in module java.base of loader 'bootstrap')]

```

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [August 7, 2024, 8:05pm UTC](https://forum.inductiveautomation.com/t/system-user-functions/94045/14 "2024-08-07T20:05:49Z")

</div>

you shouldn't need to import the class

```python
response = system.user.editUser("default",user)
print response.getErrors()
print response.getInfos()
print response.getWarns()

```

---

<div class="post-metadata">

**Author:** ![Zach\_Larson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zach_larson/32/54388_2.png) [@Zach\_Larson](https://forum.inductiveautomation.com/u/Zach_Larson)\
**Post date:** [August 7, 2024, 8:18pm UTC](https://forum.inductiveautomation.com/t/system-user-functions/94045/15 "2024-08-07T20:18:42Z")

</div>

Is it upset that I tried to append "Pilot"?

I think it is upset that I tried to pass a string into the sequence.  
The tuple prints fine.  
I don't think I matched the expectations of the object though.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [August 7, 2024, 8:21pm UTC](https://forum.inductiveautomation.com/t/system-user-functions/94045/16 "2024-08-07T20:21:38Z")

</div>

No, you tried to run the methods on the **class** you imported, instead of the **instance** you received.

---

<div class="post-metadata">

**Author:** ![Zach\_Larson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zach_larson/32/54388_2.png) [@Zach\_Larson](https://forum.inductiveautomation.com/u/Zach_Larson)\
**Post date:** [August 7, 2024, 8:26pm UTC](https://forum.inductiveautomation.com/t/system-user-functions/94045/17 "2024-08-07T20:26:47Z")

</div>

That went over my head.

Do you mean the UIResponse methods or you mean the User methods?

Like I am getting the wrong error information or the error information says that my User methods are wrong?

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [August 7, 2024, 8:55pm UTC](https://forum.inductiveautomation.com/t/system-user-functions/94045/18 "2024-08-07T20:55:19Z")

</div>

> [@Zach\_Larson](#):
>
> the UIResponse methods

You imported that class unnecessarily. The class's methods are placeholders. The response object **instance** has the usable methods.

---

<div class="post-metadata">

**Author:** ![Zach\_Larson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zach_larson/32/54388_2.png) [@Zach\_Larson](https://forum.inductiveautomation.com/u/Zach_Larson)\
**Post date:** [August 8, 2024, 12:51pm UTC](https://forum.inductiveautomation.com/t/system-user-functions/94045/19 "2024-08-08T12:51:10Z")

</div>

Thanks

I got the same error both ways.

Says I can't cast a PyString as a Java String I think. I don't know what it wants me to do though.

I think I have a syntax error that I don't recognize.

Doesn't everyone have a script for setting roles and could easily help me?  
Setting roles on the gateway takes like 20 seconds per person when pressing the save button.

I feel I am struggling with something that should just be a code snippet on the manual, that setting user roles via script is the only way to effectively set roles.  
I don't think it is something I would know by reading programming books.  
I think it is something obvious to maybe someone who worked with 7.9 and before though. I think I am new in 8.1, and need help with knowing what I am doing wrong in the script.

I am looking over the java doc for the user object. This has wasted a lot of my time, very frustrating. I am nearly certain the error is obvious to another person.

* * *

ChatGPT said I might be copying the older user object and need to create a new one. Testing it.

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [August 8, 2024, 1:20pm UTC](https://forum.inductiveautomation.com/t/system-user-functions/94045/20 "2024-08-08T13:20:12Z")

</div>

I'm fairly certain you are getting the error because `Pilot` does not already exist as a role. Make sure `Pilot` is an available role before trying to assign it to a user.

[Next page](https://forum.inductiveautomation.com/t/system-user-functions/94045.md?page=2)
