I am trying to add a session timeout script in perspective.
My goal is to create a script that takes a time input from range 1min - 10hrs and run the timeout script.
How is it possible?
I am trying to add a session timeout script in perspective.
My goal is to create a script that takes a time input from range 1min - 10hrs and run the timeout script.
How is it possible?
It’s not clear from your question what you are trying to do. Are you asking how a user can set the session timeout so that the application logs that same user out? Why? What would happen if two or more clients all set different timeouts? (The session timeout is a project property - not a client property.)
Session timeout is defined in Project | Project Properties | Perspective General | Session Timeout. It does not seem to be accessible via system.perspective so I doubt you can change it by script.
You could create three custom session variables, lastActivityTimestamp
, ‘idleTime’ and timeout
, update the first every time there is activity on the session and use an expression in the second to fire the logout.
now(1000) - {session.custom.lastActivityTimestamp} > {session.custom.timeout}
might work for you.
Actually there will be an admin which will set the timeout for the session. There will be no other clients to change the timeout.
I have little confusion regarding the script that you have provided. How to update the lastActivityTimestamp and what’s the role of timeout variable.
I am creating a perspective screen for an admin you will be having ability to change the timeout settings.
In the project settings option there is an inactivity option but its fixed and every time you need to open the project settings to update the inactivity timeout.
So, I was trying to create a script for that.
I have one more question that where this script will run? In the session events startup?
You would have to configure an event on every control on your application and in that have a script that did something like
self.session.custom.lastActivityTimestamp = system.date.now()
This would be a lot of work so I don’t recommend it.
timeout
sets the session timeout time. If an admin is setting it then you would use a memory tag to hold the value and reference that in the idletime
expression.
You didn’t explain why you are doing this.
Actually its a requirement of client. He asked to create a settings screen where application timeout can be changed when needed.
I created a screen with a time picker component in it.
I have written a simple script, created two custom session properties and one memory tag that holds the value of time selected from the time picker component.
The logout action is not working
Problem 1
idletime
should be an integer, not a date. It’s the number of milliseconds that the session should last.
If you want to use the hh:mm input then you need to bind that to the tag with a toMillis()
function or similar. You’ll need to check that it’s not adding in a date too. (Remember that day zero is 1970-01-01.)
Problem 2
A startup script only runs at startup. If the session timeout hasn’t timed out then (which it won’t have) then the script is never executed again. That’s why I suggested a timeout
tag which will turn true when the session has been idle for the maximum duration. You’ll use an onChange event (you’ll have to look it up) for that.
As long as you’re on version 8.1.8 or later, you can use session.props.lastActivity
to compare against the current time.
@schenk, cool! I hadn’t seen that!
@Ahad_Imtiaz,that means your timeout session variable becomes
now(1000) - {session.props.lastActivity} > {session.custom.timeout}
The lastActivity prop is null. What could be the reason?
The lastActivity prop is null. What could be the reason?
You're monitoring a session variable in Designer. The session probably doesn't exist there; it only exists in the browser.
Bind a label to session.props.lastActivity
and you should see when you launch the project in the browser.
I have shared a screenshot of perspective view.
Here in this screenshot, I have also mentioned what expression I have used. When seconds lapsed is greater than input seconds than the value of the label which is at the end of this picture gets 1.
Now how to transfer this value to a tag? Because in the expression tag it doesn’t allow to use the session prop. I have shared another screenshot for this.
Now the only thing that I am left with is to trigger the logout action where I am still stuck.
you dont want to do this in a tag, tags are not per session.
you’ll want a (onchange) script on your lastActivity session property or in a transform on a binding to that prop
something like that?
Please have a look on this.
I created a custom session prop with the name idletime and followed your script. But it didn’t worked for me. Did you session logged out?
idk i didnt test it, i have no login setup on my test server,
try with a lower value isntead of 50000 first and looking at it it might have to be < instead of >
@Transistor @schenk @victordcq
Thank you all for the support.
The script and the logout action are now working fine!
These are the steps that I have followed:
now(1000) - {session.props.lastActivity} > {this.custom.timeout}
and in change property script:
if currentValue.value == True:
system.perspective.closeSession()
else:
return currentValue
try to format your script in your post with </>
so that its easier to copy for others
This is a useless if
statement, you can just have a direct literal expression:
now(1000) - {session.props.lastActivity} > {this.custom.timeout}
I would also strongly encourage you to move the closeSession
logic to a property change script attached to this value, rather than a script transform. While script transforms can have side effects, because they're just arbitrary Python, it's not where they "should" go, and it's not where someone's going to look for logic like this.