How to check if user has right to write a variable from button event script

Hello,

I have an application where security is managed through tags. The tags have different security levels depending on who can write to them.

My problem is that I have buttons that write to these tags via system.tag.writeAsync. The security levels work, but there is no confirmation when nothing happens because the user doesn't have the correct security level.

I could manually assign security levels to the scripts, but that would be duplicating work (tag + event script). Additionally, I would like to only change one place if we want to add user categories that can change a tag, rather than changing all the different places where this tag is written.

Is it possible in event scripts to easily detect if the user can write to a tag?

Thank you and have a great day!

Have you checked the results you get from the writeAsync via the callback function? Via the documentation - system.tag.writeAsync | Ignition User Manual

paths = ["[default]Tag1", "[default]Folder/Tag2"]
values = [10, 10]
 
def myCallback(asyncReturn):
    for result in asyncReturn:
        # Do something if a bad qualified value was returned
        if not result.good:
            print result
 
system.tag.writeAsync(paths, values, myCallback)

Callback function gets called when the Gateway tells client what the result of the tag write is and then if not result.good I would do a system.util.sendMessage to make a popup with the result.diagnosticMessage to appear.

If it's not actually important to have it async though, you can do it with blocking code via writeBlocking. If you don't actually need async I would recommend using blocking code.

tags = ['someTag1','someTag2']
values = [1,2]
results = system.tag.writeBlocking(tags, values)
errMsg = ''
for i, result in enumerate(results):
    if not result.isGood:
        # Curate err messages 
        errMsg += "Writing value %s to tag %s had issue %s %s"%(str(values[i]), tags[i], str(result), result.getDiagnosticMessage())
# Now show errMsgs to user
2 Likes

Hello, yes that looks like what I try to achieve ! I was not happy with the callback function of the writeAsync but the writeBlocking is doing what I want ! I think I was just using the Async thinking the Blocking was working the same way.. Anyway, thank you!

1 Like

Oh great then I would highly recommend starting with writeBlocking always and only transfer to writeAsync if the situation really calls for it.

1 Like