System user functions

It exists.
I think I am using .append. I need to use something else?
I can't tell if setRoles wants a python list.


This doesn't mean anything to me.
It only has the one line, no examples.
Needed this done yesterday, such stress.

Should have called support.

1 Like

I thought they don't help us with our scripts.
I will do this, thanks.

They won't just write it for you on spec, but they generally try to help with anything. They are too generous IMO.

If you frame the ticket as "hey I've written this script to add roles to a user but I can't figure out why it's not working" I think they'd be able to help you, assuming that statement is actually true.

1 Like

This was a little off topic. I do think scripting help is in scope. But it's not unusual to see those guys power through and help with non-Ignition IT/windows/network problems as well. Or at least try for longer than I would.

1 Like

use this instead of appending to the role list

user.addRole("Pilot")
1 Like

First off, stop using ChatGPT. You'll be better off as it won't lead you in the wrong direction on this and you'll actually get a chance to learn debugging and troubleshooting on your own by trial and error.

Secondly, to help with troubleshooting and debugging, stop nesting functions inside functions for now and instead focus on breaking things down into single functions that get assigned to variables. You can print those variables when/where necessary, and sometimes you'll get a value, and other times you will get Java objects (you'll see the difference). Keep adding little bits of code and once you have a section working, you can clean it up and simplify it. Eventually you'll get done what you're wanting, then you can put it in a loop to process all the users/roles.

6 Likes

@Zach_Larson I found this example script fairly easily, check it out.
Setting user roles when creating a user via script - Ignition - Inductive Automation Forum

2 Likes

It works now!

It seem .setRoles does not work.
.addRole does.

I had tried importing java string, and then making my string for the role into a java string. Well it sort of worked, but then I got an error that some kind of python object could not be converted.

So then I went with addRole.

Thanks so much for all the help.

# 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 role if not already present
            if "roleThatExists" not in current_roles:
                print user                
                user.addRole("roleThatExists")
               
                response =system.user.editUser("default", user)
                print response.getErrors()

I was able to get .setRoles to work by making the new role list a java arrayList.

from java.util import ArrayList
roles = ArrayList()  
userRoles = ['Role1', 'Role2', 'Role3']  # type: List
for role in userRoles:
	roles.add(role)  # type: Java array list

Using your Aug 07 example:

# 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() #This is already a java array list
            
            # Append the "Pilot" role if not already present
            if "Pilot" not in current_roles:
                current_roles.add("Pilot") #correct method to add an element to a java.util.ArrayList
                
                # 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()}")