Script to Set Component Security Checkboxes

I’m creating a popup template with configurable text and buttons on it, for use by a wide range of development and production engineers. Using system.security.getRoles() returns the roles of the active user, which I can use in a to set myButton.componentEnabled, but this is a less-than-perfect solution as it cannot inherit permissions or dynamically change the restrictions between “Access Denied Overlay” and “Disable” depending on the need of the engineer.

I cannot find a security property listed in https://docs.inductiveautomation.com/display/DOC79/Button, so perhaps it’s some hidden Java property?

I’m using Ignition v7.9.7.

You would need to use button.putClientProperty(key, permissions), where key is VisionComponent.COMPONENT_PERMISSIONS and permissions is an instance of ComponentPermissions. You’ll need to import both classes to use them.

Thank you, that got me 90% of the way with the code below!!! I could not, however, figure out how to set custom restrictions. Restrictions come as restriction@hexKey. How do I create my own restrictions including the key before doing putClientProperty?

There are five available restrictions acording to Interface ComponentRestriction, which are EnabledRestriction, EventScriptRestriction, FPMIWindow.DoNotOpenWindow, OverlayRestriction, and VisibilityRestriction. Doing myComponentPermissions.getRestrictions() gives me an array like array(com.inductiveautomation.vision.api.client.components.model.security.ComponentRestriction, [com.inductiveautomation.vision.api.client.components.model.security.EnabledRestriction@ed9809eb]), showing the hexKey I’m talking about.

from com.inductiveautomation.vision.api.client.components.model.security import ComponentPermissions
from com.inductiveautomation.vision.api.client.components.model import VisionComponent
from array import array
import java.lang.String as createJavaString

myComponent = system.gui.getWindow("Popups/OperatorAck").rootContainer.getComponent("Pushbuttons").getComponent("Pb1")

requestedSecurity = system.gui.getWindow("Popups/OperatorAck").rootContainer.AccessLevelBitArray

#Set Custom Security or Inherit Permissions?
inheritPermissions = not (bool)(requestedSecurity & 2**0)

#Construct Required Roles
requiredRoles = array(createJavaString)
if (requestedSecurity & 2**1):
     requiredRoles.append(u'Associate')
if (requestedSecurity & 2**2):
     requiredRoles.append(u'Designer')
if (requestedSecurity & 2**3):
     requiredRoles.append(u'Engineer')
if (requestedSecurity & 2**4):
     requiredRoles.append(u'GatewayAdmin')
if (requestedSecurity & 2**5):
     requiredRoles.append(u'Maintenance')
if (requestedSecurity & 2**6):
     requiredRoles.append(u'Supervisor')

#Construct Restrictions
myComponentPermissions = myComponent.getClientProperty(VisionComponent.COMPONENT_PERMISSIONS)
restrictions = myComponentPermissions.getRestrictions()

#Set Security
key = VisionComponent.COMPONENT_PERMISSIONS
permissions = ComponentPermissions(inheritPermissions, requiredRoles, restrictions)
myComponent.putClientProperty(key, permissions)

That's just Java's default toString representation - it's <classname@hashcode>. To set your own, use the static set method with your own constructed ComponentPermissions instance. Use the static fields on ComponentPermissions to reference the default security permissions, eg Access Denied.

Put together, setting a new permission would be something like:

from com.inductiveautomation.vision.api.client.components.model.security import ComponentPermissions

inheritPermissions = False
requiredRoles = ['Engineer']
restrictions = [ComponentPermissions.DISABLE, ComponentPermissions.ACCESS_DENIED_OVERLAY]

customPermissions = ComponentPermissions(inheritPermissions, requiredRoles, restrictions)

ComponentPermissions.set(myComponent, customPermissions)

Thank you, that solved it and let me simplify the requiredRoles as well:

####################################################
### INITIALIZE #####################################

#Import required classes
from com.inductiveautomation.vision.api.client.components.model.security import ComponentPermissions
from com.inductiveautomation.vision.api.client.components.model import VisionComponent

#Reference DINT from PLC to set security
requestedSecurity = system.gui.getWindow("Popups/OperatorAck").rootContainer.AccessLevelBitArray

#Choose which component security to modify
myComponent = system.gui.getWindow("Popups/OperatorAck").rootContainer.getComponent("Pushbuttons").getComponent("Pb1")

####################################################
### CONSTRUCT PERMISSIONS ##########################

#Set Custom Security or Inherit Permissions?
inheritPermissions = not (bool)(requestedSecurity & 2**0)

#Construct Required Roles
requiredRoles = []
if (requestedSecurity & 2**1):
     requiredRoles.append('Associate')
if (requestedSecurity & 2**2):
     requiredRoles.append('Designer')
if (requestedSecurity & 2**3):
     requiredRoles.append('Engineer')
if (requestedSecurity & 2**4):
     requiredRoles.append('GatewayAdmin')
if (requestedSecurity & 2**5):
     requiredRoles.append('Maintenance')
if (requestedSecurity & 2**6):
     requiredRoles.append('Supervisor')

#Construct Restrictions
restrictions = []
if (requestedSecurity & 2**7):
	restrictions.append(ComponentPermissions.ACCESS_DENIED_OVERLAY)
if (requestedSecurity & 2**8):
	restrictions.append(ComponentPermissions.DISABLE)
if (requestedSecurity & 2**9):
	restrictions.append(ComponentPermissions.DISABLE_SCRIPTS)
if (requestedSecurity & 2**10):
	restrictions.append(ComponentPermissions.HIDE)

#####################################################
### SET SECURITY ####################################
key = VisionComponent.COMPONENT_PERMISSIONS
permissions = ComponentPermissions(inheritPermissions, requiredRoles, restrictions)
myComponent.putClientProperty(key, permissions)
2 Likes