# Get current username / Session in scripting in perspective

**URL:** <https://forum.inductiveautomation.com/t/get-current-username-session-in-scripting-in-perspective/47778>\
**Category:** Ignition\
**Tags:** perspective\
**Created:** [June 15, 2021, 11:53am UTC](https://forum.inductiveautomation.com/t/get-current-username-session-in-scripting-in-perspective/47778 "2021-06-15T11:53:41Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![thijs1](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/thijs1/32/14793_2.png) [@thijs1](https://forum.inductiveautomation.com/u/thijs1)\
**Post date:** [June 15, 2021, 11:53am UTC](https://forum.inductiveautomation.com/t/get-current-username-session-in-scripting-in-perspective/47778/1 "2021-06-15T11:53:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![calimero100582](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/calimero100582/32/18630_2.png) [@calimero100582](https://forum.inductiveautomation.com/u/calimero100582)\
**Post date:** [June 15, 2021, 12:11pm UTC](https://forum.inductiveautomation.com/t/get-current-username-session-in-scripting-in-perspective/47778/2 "2021-06-15T12:11:50Z")

</div>

You should get perspective session username from: [system.perspective.getSessionInfo - Ignition User Manual 8.1 - Ignition Documentation](https://docs.inductiveautomation.com/display/DOC81/system.perspective.getSessionInfo)  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/b/6/b69f88dacbabd6108d464a145fa8c669ebbe5cf7.png)

---

<div class="post-metadata">

**Author:** ![thijs1](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/thijs1/32/14793_2.png) [@thijs1](https://forum.inductiveautomation.com/u/thijs1)\
**Post date:** [June 15, 2021, 12:16pm UTC](https://forum.inductiveautomation.com/t/get-current-username-session-in-scripting-in-perspective/47778/3 "2021-06-15T12:16:12Z")

</div>

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,

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [June 15, 2021, 1:04pm UTC](https://forum.inductiveautomation.com/t/get-current-username-session-in-scripting-in-perspective/47778/4 "2021-06-15T13:04:54Z")

</div>

> [@thijs1](#):
>
> But from our jython code we don’t have access to this property.

Meaning what? Are these project / gateway scoped scripts?

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [June 15, 2021, 1:38pm UTC](https://forum.inductiveautomation.com/t/get-current-username-session-in-scripting-in-perspective/47778/5 "2021-06-15T13:38:35Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![thijs1](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/thijs1/32/14793_2.png) [@thijs1](https://forum.inductiveautomation.com/u/thijs1)\
**Post date:** [June 15, 2021, 2:38pm UTC](https://forum.inductiveautomation.com/t/get-current-username-session-in-scripting-in-perspective/47778/6 "2021-06-15T14:38:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![thijs1](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/thijs1/32/14793_2.png) [@thijs1](https://forum.inductiveautomation.com/u/thijs1)\
**Post date:** [June 15, 2021, 2:47pm UTC](https://forum.inductiveautomation.com/t/get-current-username-session-in-scripting-in-perspective/47778/7 "2021-06-15T14:47:54Z")

</div>

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:

```auto
    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

---

<div class="post-metadata">

**Author:** ![ryanjmclaughlin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ryanjmclaughlin/32/4318_2.png) [@ryanjmclaughlin](https://forum.inductiveautomation.com/u/ryanjmclaughlin)\
**Post date:** [June 15, 2021, 3:14pm UTC](https://forum.inductiveautomation.com/t/get-current-username-session-in-scripting-in-perspective/47778/8 "2021-06-15T15:14:35Z")

</div>

> [@thijs1](#):
>
> I would prefer not to pass the session over here.

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.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [June 15, 2021, 4:09pm UTC](https://forum.inductiveautomation.com/t/get-current-username-session-in-scripting-in-perspective/47778/9 "2021-06-15T16:09:14Z")

</div>

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:

> [@SDK transfert data from gateway to perspective](http://forum.inductiveautomation.com/t/sdk-transfert-data-from-gateway-to-perspective/36349/18):
>
> If you’re calling the InternalSession.SESSION threadlocal directly, then you don’t need to go through the rest of the Perspective context - you can directly call whatever you want to - ie, InternalSession.SESSION.get().getEventBus().post(); Also, a healthy disclaimer that while it’s unlikely we’ll change things, the InternalSession threadlocal is not considered part of our API surface, so it could break in some future version without notice.

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