How to create a login on a Menu Tree?

Hey guys,
I have a project that does not require a login, because it is an anonymous survey, but for those that need to get the data I created this:

The "Reporting" item is hidden unless I am logged in, and the "Logout" is "Login" until authenticated. All I have so far is an event on the menu that sets a custom prop to true if the user is authenticated, and a script that runs every time the menu is clicked.

def runAction(self, event):
	if self.custom.auth == True:
		system.perspective.logout()
	else: 
		system.perspective.login()

This does not work very well, because on each click event I am asked to login. So, what needs to happen to make this work?

Can I get the menu item that was clicked, such as the "Login/out" item? Which is index 2 in this case.

1 Like

try this

itemPath = [int(x) for x in list(event.itemPath)]

You can compare this to the item path for your logout node and only run your script when they match

I would suspect that your auth property isn't actually a boolean, and therefore never equal to true.

Consider using the session auth properties to drive your display and logic.

I think I just found it. I can use event.path in the onItemClicked script.

No need for a loop.

It is boolean, the event script just needs some fixin'.

Then it's just
if self.custom.auth:

Oh, right, I forget the shorthand sometimes.

Thinking this might work:

	if not self.custom.auth and event.path == 2:
		system.perspective.login()
	elif self.custom.auth and event.path == 2:
		system.perspective.logout()

Except, "Reporting" is not visible until logged in, therefore the event.path = 2 will not work as 2 does not exist at that time...

So,


	if not self.custom.auth and event.label == 'Login':
		system.perspective.login()
	elif self.custom.auth and event.label == 'Logout':
		system.perspective.logout()

This works. Is this an acceptable practice for allowing a login/out for something that would not normally require authentication?

Edit: Was updating when new post came in... See below...

This is more complex than it needs to be. You do not need self.custom.auth at all.
Put a binding on your login item like so


Then in the onItemClicked event do this, and always check if the item is enabled, it is good practice.

if event.enabled:
	if event.label == 'Login':
		system.perspective.login()
	elif event.label == 'Logout':
		system.perspective.logout()
1 Like

Yeah, I like it. Never used the map transform before. Thank you.

1 Like