ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser() no longer working
Asked by Marc Jason Mutuc:

JIRA v9.15.2

JSM v5.15.2

Script Runner v8.29

The script works only if the user is not part of any JSM organization.

This was working before we upgraded the all 3 items above.

Code below:

*** ** * ** ***

import com.atlassian.jira.component.ComponentAccessor  
import static com.atlassian.jira.issue.IssueFieldConstants.\*

def childField0 = getFieldByName("Account Category")  
def childField1 = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()  
def childField2 = getFieldByName("Select User Role(s)")  
def childField3 = getFieldByName("Select ABC User Role(s)")  
def childField4 = getFieldByName("Select ABC Notifier Group(s)")  
def childField5 = getFieldByName("XYZ Organizations")  
def childField6 = getFieldByName("Reporter").getValue() as String  
def childField7 = getFieldByName("Organization")  
def desc = getFieldById("summary")

childField2.setHidden(true)  
childField3.setHidden(true)  
childField4.setHidden(true)  
childField5.setHidden(true)

def parentField = getFieldById(getFieldChanged())  
def selectedOption = parentField.getValue() as String

if ((selectedOption == "OPTION A")\|\|(selectedOption == "OPTION B") )  
{

//desc.setFormValue(childField7)

if (ComponentAccessor.getGroupManager().getGroupsForUser(childField1)?.find { it.name == "ABC" })

{   
childField2.setHidden(true)  
childField3.setHidden(false)  
childField4.setHidden(false)  
childField5.setHidden(true)  

childField2.setRequired(false)  
childField3.setRequired(true)  
childField4.setRequired(false)   
childField5.setRequired(false)

}

else if (ComponentAccessor.getGroupManager().getGroupsForUser(childField1)?.find { it.name == "XYZ" })   
{   
childField2.setHidden(false)  
childField3.setHidden(true)  
childField4.setHidden(true)  
childField5.setHidden(false)  

childField2.setRequired(true)  
childField3.setRequired(false)  
childField4.setRequired(false)   
childField5.setRequired(true)   
}   

else   
{  
childField2.setHidden(false)  
childField3.setHidden(true)  
childField4.setHidden(true)  
childField5.setHidden(true)  

childField2.setRequired(true)  
childField3.setRequired(false)  
childField4.setRequired(false)  
childField5.setRequired(false)   
}  
}

*** ** * ** ***


Answers:
author: Radek Dost?l
body: Rather curious what kind of code we're talking here.

The API has nothing to do with SR, and nothing to do with JSM, that's a JSW method, very old, very known, very used, used much, overused, used often, so I would be somewhat surprised if it got bugged out.

In general, if you have a job, an automation script, a listener, basically whenever there is a non-http thread somewhere - that's expected for #getLoggedInUser() to be null, because there is no underlying user for the running thread.

If I do a workflow transition, and a postfunction triggers, this all happens within the same thread, and the postfunction will get the logged in user fine.

If I do a scheduled job, the user will be null, because the job will execute outside of any user context.

Either, your code is running outside any user context; or, Jira changed something and decoupled some stuff (e.g. workflow functions executing in a separate thread, which would explain the null user).


author: Ram Kumar Aravindakshan _Adaptavist_
body: Hi Marc,

Instead of using the ComponentAccessor to invoke the loggedInUser method, please just use:-

```
Users.loggedInUser
```

This [Users](https://docs.adaptavist.com/sr4js/latest/hapi/work-with-users) class is from [ScriptRunner's HAPI](https://docs.adaptavist.com/sr4js/latest/hapi) feature that simplifies the coding.

Try the approach above and see if you are still encountering the problem.

You mentioned:-
> The script works only if the user is not part of any JSM organization.
>
> This was working before we upgraded the all 3 items above.

Please clarify what are the items you upgraded.

I am looking forward to your feedback.

Thank you and Kind regards,  
Ram

comments: author: Marc Jason Mutuc
body: Checking the new code


author: Marc Jason Mutuc
body: Does not work unfortunately. Here is the code being used:

*** ** * ** ***

import com.atlassian.jira.component.ComponentAccessor  
import static com.atlassian.jira.issue.IssueFieldConstants.\*

def childField0 = getFieldByName("Account Category")  
//def childField1 = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()  
def childField1 = Users.loggedInUser  
def childField2 = getFieldByName("Select User Role(s)")  
def childField3 = getFieldByName("Select ABC User Role(s)")  
def childField4 = getFieldByName("Select ABC Notifier Group(s)")  
def childField5 = getFieldByName("XYZ Organizations")  
def childField6 = getFieldByName("Reporter").getValue() as String  
def childField7 = getFieldByName("Organization")  
def desc = getFieldById("summary")

childField2.setHidden(true)  
childField3.setHidden(true)  
childField4.setHidden(true)  
childField5.setHidden(true)

def parentField = getFieldById(getFieldChanged())  
def selectedOption = parentField.getValue() as String

if ((selectedOption == "OPT A")\|\|(selectedOption == "OPT B") )  
{

desc.setFormValue(childField1)

//if (ComponentAccessor.getGroupManager().getGroupsForUser(childField1)?.find { it.name == "ABC" })  
if (childField1.isMemberOfGroup("ABC"))

{   
childField2.setHidden(true)  
childField3.setHidden(false)  
childField4.setHidden(false)  
childField5.setHidden(true)  

childField2.setRequired(false)  
childField3.setRequired(true)  
childField4.setRequired(false)   
childField5.setRequired(false)

}

//else if (ComponentAccessor.getGroupManager().getGroupsForUser(childField1)?.find { it.name == "XYZ" })   
else if (childField1.isMemberOfGroup("XYZ"))   
{   
childField2.setHidden(false)  
childField3.setHidden(true)  
childField4.setHidden(true)  
childField5.setHidden(false)  

childField2.setRequired(true)  
childField3.setRequired(false)  
childField4.setRequired(false)   
childField5.setRequired(true)   
}   

else   
{  
childField2.setHidden(false)  
childField3.setHidden(true)  
childField4.setHidden(true)  
childField5.setHidden(true)  

childField2.setRequired(true)  
childField3.setRequired(false)  
childField4.setRequired(false)  
childField5.setRequired(false)   
}  
}


author: Ram Kumar Aravindakshan _Adaptavist_
body: Hi [@Marc Jason Mutuc](/t5/user/viewprofilepage/user-id/527585)

From the code you have provided, it appears you are using the Behaviour. Please share a screenshot of your Behaviour configuration so I can get a clearer picture and provide a better solution.

Thank you and Kind regards,  
Ram


author: Marc Jason Mutuc
body: ![image.png](https://community.atlassian.com/t5/image/serverpage/image-id/327439iE2B64268FD736AB2/image-size/large?v=v2&px=999 "image.png")


author: Marc Jason Mutuc
body: ![image.png](https://community.atlassian.com/t5/image/serverpage/image-id/327440i183CD7CB6BA4B149/image-size/large?v=v2&px=999 "image.png")


author: Marc Jason Mutuc
body: Please note that the script is working. Tested it for both JSM and Jira Software. However, for JSM, it only works for users that are not part of an Organization.


author: Ram Kumar Aravindakshan _Adaptavist_
body: Hi [@Marc Jason Mutuc](/t5/user/viewprofilepage/user-id/527585)

I have reviewed your code against the screenshots that you have shared, my first question is which field is this line of code supposed to represent:-

```
def parentField = getFieldById(getFieldChanged())
```

From the screenshot you shared, it appears that the Server-Side Behaviour has been configured for the Account Category field and you have declared a field called

```
def childField0 = getFieldByName("Account Category")
```

If the parentField refers to the Account Category field, please remove the childField0 line, as it is of no use.

Next, I can confirm your if/else conditions are not going to work.

If you intend to verify which group a User belongs to, you need to do something like this:-  

```
def adminGroup = Groups.getByName('jira-administrators')
...
...
...

if (sampleListValue in ['Option1','Option2']) { 
    if (loggedInUser.isMemberOfGroup(adminGroup)) {
        sampleTextField.hidden = false
        orgnanizations.hidden = false
     }
}

```

Below is a fully working sample code for your reference:-  

```
import com.adaptavist.hapi.jira.groups.Groups
import com.adaptavist.hapi.jira.users.Users
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours behaviours
def sampleList = getFieldById(fieldChanged)
def sampleListValue = sampleList.value.toString()

def sampleTextField = getFieldByName('Sample Text Field')
def orgnanizations = getFieldByName('Organizations')

def loggedInUser = Users.loggedInUser
def adminGroup = Groups.getByName('jira-administrators')
def jiraSoftwareGroup = Groups.getByName('jira-software-users')
def jsmGroup = Groups.getByName('jira-servicedesk-users')

sampleTextField.hidden = true
orgnanizations.hidden = true


if (sampleListValue in ['Option1','Option2']) {
    if (loggedInUser.isMemberOfGroup(adminGroup)) {
         sampleTextField.hidden = false
         orgnanizations.hidden = false
    } else if (loggedInUser.isMemberOfGroup(jsmGroup) && loggedInUser.isMemberOfGroup(jiraSoftwareGroup)) {
        orgnanizations.hidden = false
        sampleTextField.hidden = false
    } else if (loggedInUser.isMemberOfGroup(jsmGroup)) {
        orgnanizations.hidden = false
    } else if (loggedInUser.isMemberOfGroup(jiraSoftwareGroup)) {
        sampleTextField.hidden = false
    }
}
```

Please note that the sample working code above is not 100% exact to your environment. Hence, you must make the required modifications.

Below is a screenshot of the Server-Side Behaviour configuration for your reference:-

![behaviour_config.png](https://community.atlassian.com/t5/image/serverpage/image-id/327508i298E9D80D9C32F6E/image-size/large?v=v2&px=999 "behaviour_config.png")

If you observe the screenshot above, the Behaviour is configured for 2 projects, i.e. MOCK which is a Jira Software Project and ST which is a JSM project.

I have tested on both Jira software as well as on JSM and it is working as expected.

I hope this helps to solve your question. :-)  

Thank you and Kind regards,  
Ram


author: Marc Jason Mutuc
body: Thank you! I'll test it soon and let you know.


author: Marc Jason Mutuc
body: In your tests for the Portal (JSM), did you add the logged in user to a specific JSM Organization?

I used the provided code and it is still the same. Works internally, but on JSM it only works when the logged in user is not part of any Organization.


author: Ram Kumar Aravindakshan _Adaptavist_
body: Hi [@Marc Jason Mutuc](/t5/user/viewprofilepage/user-id/527585)

Yes, I added it to a JSM Organization.

Please confirm, if the User you are testing with only belongs to an Organization and not to any Group.

Thank you and Kind regards,  
Ram


author: Marc Jason Mutuc
body: The user belongs to the group and to an Organization on the JSM project.

Tested it several times and it does not seem to get the details of the reporter when he belongs to an Organization (threw what is being fetched as text on the summary to see it real time).

If the reporter does not belong to any Organization on the JSM project, then the fetched data is shown as \[Object\] \[object\] or similar.


author: Ram Kumar Aravindakshan _Adaptavist_
body: Hi Marc,

Unfortunately, I am not able to replicate this issue in my environment.

Below is the test that I have performed:-

1. I have created 2 organizations Org1 and Org2 as shown in the screenshot below:-

![organizations.png](https://community.atlassian.com/t5/image/serverpage/image-id/328349i474297F9FAE76485/image-size/large?v=v2&px=999 "organizations.png")

2. In each of the organizations, I added one user who is also a member of a Group.

For Org1, I added the user Ram Kumar, who is a member of the jira-software-users, jira-servicedesk-users and development group as shown in the screenshot below:-

![users_org1.png](https://community.atlassian.com/t5/image/serverpage/image-id/328351iDA59A53426FEA336/image-size/large?v=v2&px=999 "users_org1.png")

For Org2, I added the user Anoop Memon, who is a member of the jira-servicedesk-users and development group as shown in the screenshot below:-

![users_org2.png](https://community.atlassian.com/t5/image/serverpage/image-id/328353iC2A0BCB42F89D3D9/image-size/large?v=v2&px=999 "users_org2.png")

Below is a screenshot of the users according to their groups:-

![users_in_groups.png](https://community.atlassian.com/t5/image/serverpage/image-id/328354i554429CC7C17F227/image-size/large?v=v2&px=999 "users_in_groups.png")

I made some adjustments to the code to include the development group and added a few more conditions to the if /else statement. Below is the updated code for your reference:-  

```
import com.adaptavist.hapi.jira.groups.Groups
import com.adaptavist.hapi.jira.users.Users
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours behaviours
def sampleList = getFieldById(fieldChanged)
def sampleListValue = sampleList.value.toString()

def sampleTextField = getFieldByName('Sample Text Field')
def orgnanizations = getFieldByName('Organizations')
def projectCategory = getFieldByName('Project Category')

def loggedInUser = Users.loggedInUser
def adminGroup = Groups.getByName('jira-administrators')
def jiraSoftwareGroup = Groups.getByName('jira-software-users')
def jsmGroup = Groups.getByName('jira-servicedesk-users')
def devGroup = Groups.getByName('development')

sampleTextField.hidden = true
orgnanizations.hidden = true
projectCategory.hidden = true

def adminGroupMember = loggedInUser.isMemberOfGroup(adminGroup)
def jsmGroupMember = loggedInUser.isMemberOfGroup(jsmGroup)
def jiraSoftwareMember = loggedInUser.isMemberOfGroup(jiraSoftwareGroup)
def devMember = loggedInUser.isMemberOfGroup(devGroup)

if (sampleListValue in ['Option1','Option2']) {
    if (adminGroupMember || (jsmGroupMember && jiraSoftwareMember && devMember)) {
         sampleTextField.hidden = false
         orgnanizations.hidden = false
         projectCategory.hidden = false
    } else if (jsmGroupMember && jiraSoftwareMember) {
        orgnanizations.hidden = false
        sampleTextField.hidden = false
    } else if (jsmGroupMember && devMember) {
        orgnanizations.hidden = false
        projectCategory.hidden = false
    } else if (jiraSoftwareMember && devMember) {
        sampleTextField.hidden = false
        projectCategory.hidden = false
    } else if (loggedInUser.isMemberOfGroup(jsmGroup)) {
        orgnanizations.hidden = false
    } else if (loggedInUser.isMemberOfGroup(jiraSoftwareGroup)) {
        sampleTextField.hidden = false
    } else if (loggedInUser.isMemberOfGroup(devGroup)) {
        projectCategory.hidden = false
    }
}
```

Below are some test screenshots for your reference:-

1. Firstly, I have logged it as the user Ram Kumar into the Jira instance, accessed the Service Desk and Raised the Technical Support request as shown in the screenshots below:-

![test1.png](https://community.atlassian.com/t5/image/serverpage/image-id/328356i48AB8471334F2C81/image-size/large?v=v2&px=999 "test1.png")

![test2.png](https://community.atlassian.com/t5/image/serverpage/image-id/328357i464DE7A852D94B88/image-size/large?v=v2&px=999 "test2.png")

As shown in the screenshot above, when the Create page first loads and no option is selected from the Sample List, as expected, the fields are hidden.

2. Next, when I either select Option1 or Option2, as expected, since this user is all 3 groups, all the fields are visible as shown in the screenshot below:-

![test3.1.png](https://community.atlassian.com/t5/image/serverpage/image-id/328362i88EBFE9E5C982BA5/image-size/large?v=v2&px=999 "test3.1.png")

3. Next, I log out and login as the user Anoop Menon, who is only part of the jira-servicedesk-users group and development group. When I first load the request just like the previous user all the fields are hidden as expected as shown in the screenshot below:-

![test4.png](https://community.atlassian.com/t5/image/serverpage/image-id/328360i91FBD0CEFA957C77/image-size/large?v=v2&px=999 "test4.png")

4. Now, if I select either Option1 or Option2 only the Project Category and Organizations fields are visible as shown in the screenshot below:-

![test5.png](https://community.atlassian.com/t5/image/serverpage/image-id/328361i2EF66C3B37CD1EA8/image-size/large?v=v2&px=999 "test5.png")

I hope this helps to solve your question. :-)  

Thank you and Kind regards,  
Ram


author: Marc Jason Mutuc
body: Both your code and my code works for Jira Software. But on JSM Portal, it only works when the user/reporter is not a member of an Organization.

![image.png](https://community.atlassian.com/t5/image/serverpage/image-id/328548i1889842BC8AC4162/image-size/large?v=v2&px=999 "image.png")

I think, this new Groups is making an impact. We are not using it at the moment but I did test it and it is still the same. Only works for users/reporters that are not part of an Organization.

Anyway, I think it would be better to check if the user/reporter is part of a specific organization. Is there a code for this? Apologies for the lengthy inquiry and I am very thankful for your responses.


author: Ram Kumar Aravindakshan _Adaptavist_
body: Hi [@Marc Jason Mutuc](/t5/user/viewprofilepage/user-id/527585)

I'm not sure why, but the code works fine in my environment, i.e. both on Jirs Software and JSM, as I demonstrated in my previous comment.

The Groups class is available in ScriptRunner's HAPI feature to simplify the approach to invoking the Group details. It doesn't explain why you are unable to get your behaviour to on the JSM.

Please clarify: What version of JSM and ScriptRunner are you currently using.

Also, please share a full screenshot of your Behaviour configuration so I can double-check if anything is missing.

In addition to that, please confirm if you have any other Behaviour configurations applied to the sam JSM project you are testing with. If yes, temporarily disable them to avoid any conflict when performing the test to see if it makes any improvement.

Thank you and Kind regards,  
Ram


Tags: behaviours

data-center

jira-service-management

jira-service-management-data-center

scriptrunner