Reading Session Properties in Script

I’m trying to read the authenticated session property in a script to determine if a button will be log in or log out. In the expression for the text, the binding was easy, but the script is proving to be a bit more challenging. What I have so far is below, but the getProperty script is returning ‘None’ and I tried without get property and it said it couldn’t find session.

	auth = system.util.getProperty("session.props.auth.authenticated")
	log=system.util.getLogger("Test Log")
	log.warn("%s"%auth)

	if(auth == 'true'):
		system.perspective.logout()
	else:
		system.perspective.login()

auth = self.session.props.auth.authenticated

Script doesn’t run with that option.

com.inductiveautomation.ignition.common.script.JythonExecException: Traceback (most recent call last): File "", line 2, in runAction NameError: global name 'session' is not define

Sorry, I skipped the “self.” part.

Also, you’ll want to have something more like

    auth = self.session.props.auth.authenticated
	log=system.util.getLogger("Test Log")
	log.warn("%s"%auth)

	if auth:
		system.perspective.logout()
	else:
		system.perspective.login()

because the value comes back as a bool - not a str.

That was it. Thanks.

Wasn’t sure what was the route to get up to session properties. I had tried adding a couple of parent levels in there last time and that didn’t work. For some reason skipped just ‘self.’

self.parent.session should still have worked… I believe each component/container has a concept of “session”.

Hmm…so it does. Must have had a typo when I tried just one parent level.

It does error if I do parent.parent since that is too many levels up. Guess my brain has already turned off for the weekend.

it depends on the depth you’re calling/invoking from; if you are invoking from a component placed in a root container, then self.parent.parent would error out because the root container has no parent.

Yeah, I was thinking parent would be the container, then parent of that might be the session when I had tried that. Which was not the case.