Limit online perspective users

Hi,

I’m using Ignition 8.3.3 Perspective.

I want to:

  • count online Perspective users who have the operator role

  • limit the number of operator users to 15

I tried using system.perspective.getSessionInfo() in a Gateway Timer Script, but the roles are missing / empty, so my counts always return 0.

Is it possible to reliably count online users by role.
If not, what is the recommended approach to enforce a max number of operator users?

Thanks.

From the documentation for system.perspective.getSessionInfo(), roles are not included in the result of this function. You’ll have to start with the usernames returned by that function and look up roles separately from there.

Start with something like this:

userSource = "YourUserSourceName" # Needed later for system.user.getUser()
operator_limit = 15
sessions = system.perspective.getSessionInfo()

operator_sessions = []
for s in sessions:

In the for loop, you’ll first have to grab each session’s username. Then you can use system.user.getUser(), because you have the user source and the username, and this will provide you with a user object. Then you can get the roles associated with that user using user.getRoles(). If operator appears in there, you can append that session to the operator_sessions list.

After this, you may determine how many operator sessions are running by using operator_count = len(operator_sessions).

If your only goal is to limit maximum operator sessions to 15, I believe you can move all of this code from a gateway timer script into a page startup script in your project(s) that you want limited (because system.perspective.getSessionInfo() can run in the Perspective session scope). Then put something like this at the bottom of that script:

if operator_count >= operator_limit:
    system.perspective.logout(
        sessionId = self.session.props.id,
        message = "Maximum operator sessions ({}) reached. You have been logged out.".format(operator_limit)
    )

This will run every time someone logs in (and only then) and will log operators out instantly if the limit is already reached. You might have to exclude the current user in the count described above if using this method. You might also have to account for an empty username at the beginning of your code, depending on how the security/authentication works for your project(s).

By using a timer script, you might run into an issue where operator #16 is able to log in and work in a project before the timer script fires again to log him out, which might be ok or might be bad. Hope this gets you where you need. Someone else can correct me if I got anything wrong above.