Get current username / Session in scripting in perspective

Hi,

Our projects use extensive jython scripting to do databasing etc. In our database there are certain fields that provide information like : Created_by, Modified_By. In order to fill these fields we need to get the current username of the active session.

I know it is possible to use session.props to get the current user. But from our jython code we don’t have access to this property. Is there anyway we can access this property through a scripting method? I would prefer not to pass self or session to the business logic (to avoid cluttering of the code)

I guess scripts like system.perspective.IsAuthorized() need to have access to the current user in order to perform there logic. So probably there should be a way to do this?

Any ideas / help would greatly be appreciated.

Thanks

1 Like

You should get perspective session username from: system.perspective.getSessionInfo - Ignition User Manual 8.1 - Ignition Documentation
image

Hi,

Thanks, This returns a list of all sessions on the gateway. I’m specifically looking for the current user or the current session in the context that the script has been called.

Thanks,

Meaning what? Are these project / gateway scoped scripts?

Add a session argument to the functions in your script modules. Pass the session variable from the session events that call them. Anything else is going to be ugly and unsupported.

1 Like

These are jython files that we copy inside of the pylib folder and thus making them accessible from everywhere in Ignition. This can be project or gateway scoped.

Due to the new nature of Perspective, I guess everything is gatewayscoped right now.

We have this ORM mapper we use (a clone of peewee), that makes it possible to intercept certain calls. So on every save of an object, we are able to inject the username of the current user. This makes the code very clean.

So we can create code like this:

    def test_enterprise_creation(self):
        Enterprise.create(code="ENTERPRISE-1" , description="Enterprise 1")        
        ent = Enterprise.select().where(Enterprise.code == "ENTERPRISE-1").get()       
        self.assertEqual(ent.code, "ENTERPRISE-1")
        ent.code = "ENTERPRISE-2"
        ent.save()    

Under the hood, SQL statements are created and executed and objects are created. I would prefer not to pass the session over here. It would introduce more clutter and it would make our unittests less readable.

Thanks

I was trying this myself and was in the same thought process originally. Ultimately modified to pass in the session object into my classes/functions like Phil said. Its the right call and gives you more functionality to interact bi-directionally with the perspective session in the long term.

The ugly, hacky, unsupported, completely-liable-to-break-on-any-upgrade way to get magical access to the current session is using a public static ThreadLocal, as mentioned here:

As my first sentence alludes - this is a bad practice, liable to break at any time.