User Object addRoles vs setRoles

I encountered this code:

roles=['operator']
userObject.setRoles(roles)

But I already know this code:

userObject.addRoles(roles)

Similarly addContactInfo() vs setContactInfo() ?

I am confuse.
When do I use set vs add?
What is the difference?

I believe the difference is addRoles() appends to the current roles list, and setRoles() overrides the current list with the one provided to the method.

System user functions - Ignition - Inductive Automation Forum

2 Likes

That really make sense.

if user have existing roles, addRoles() will append to it, while setRoles() will overwrite it..
Same with addContactInfo and setContactInfo.

New User with no existing role, addRoles() and setRoles() will do the same thing.?!

1 Like

Yes.

1 Like

Thanks a lot.

Forgive me for being slow today. add vs set :squinting_face_with_tongue:

One more thing.

Quoted from ignition manual:

user.set("firstname", "Naomi")
user.set(user.LastName, "Nagata")
user.set("password", "1234567890")

is this also valid (i saw this code on existing)?
user.set('lastname', "Nagata")

Yes, they will work equivalently, because of some work we're doing under the hood. In theory, the user.LastName reference is more "stable" and safer from modifications we might have to do. In practice it's unlikely we'll ever break compatibility in a way that matters to you.

Thanks.

Can you create your own property as well (I saw this on existing code)??
like
userObject.set('home', home_list)

image

Yes, you can, but I probably wouldn't recommend it. I don't know if all the serialization machinery is going to respect arbitrary properties you add - they have to both be stored (and reconstituted) when going to the DB/user source/etc, and they have to be serialized/deserialized over RPC.

You can give it a try and see if it works; if it does then it should be fine as long as you stick to something simple like strings on both sides.

1 Like

I've done this by using notes to store a JSON string that can be parsed to get "custom properties." There may be a better way though :man_shrugging: