Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser() no longer working

Marc Jason Mutuc
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 30, 2024

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)
}
}


 

2 answers

0 votes
Radek Dostál
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 31, 2024

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).

0 votes
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 31, 2024

Hi Marc,

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

Users.loggedInUser

 This Users class is from ScriptRunner's 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

Marc Jason Mutuc
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 2, 2024

Checking the new code

Marc Jason Mutuc
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 2, 2024

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)
}
}

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 2, 2024

Hi @Marc Jason Mutuc

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

Marc Jason Mutuc
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 3, 2024

image.png

Marc Jason Mutuc
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 3, 2024

image.png

Marc Jason Mutuc
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 3, 2024

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.

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 3, 2024

Hi @Marc Jason Mutuc 

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

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

 

Marc Jason Mutuc
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 4, 2024

Thank you! I'll test it soon and let you know.

Marc Jason Mutuc
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 5, 2024

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.

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 5, 2024

Hi @Marc Jason Mutuc

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

Marc Jason Mutuc
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 5, 2024

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.

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 6, 2024

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

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

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

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

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

 

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

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

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

 

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

Thank you and Kind regards,
Ram

Marc Jason Mutuc
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 6, 2024

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

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.

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 7, 2024

Hi @Marc Jason Mutuc

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

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events