Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Compare a user to a group

CRS123456 June 25, 2022

Hello everyone,

I'll try to write a script with script runner in the Jira server.

I want to log in user has to check with the eRD-Leaders group and if the user exists the Project Plan field will be visible else Project Plan hide.

I wrote some codes but it's not working.

import com.atlassian.jira.component.ComponentAccessor
getFieldByName('Project Plan Delivery Date').setDescription('Select the date that the Project Plan will be delivered to PMT. Less than 60 days')
def ProjectPlan = getFieldManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def group = ComponentAccessor.groupManager.getUserNamesInGroup("eRD-Leaders").contains("user")
if ((user) == (group))
{
return getFieldByName('Project Plan Delivery Date')
}
else
{
    getFieldByName('Project Plan Delivery Date').setHidden(true)
}

1 answer

1 accepted

0 votes
Answer accepted
Nic Brough -Adaptavist-
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 25, 2022

Your logic goes wrong at the point where you define the "user" and "group" variables.

There's nothing wrong with getting either of them, but you've not looked at what they will actually return.

The code you've got will return

user: a user object

group: a true or false value, not a list of users.  (true if the user is in the group)

So, your "if user == group" question is total nonsense.  You're comparing a user with a boolean true or false.

You don't need to do that, your "group" variable already contains a true or false, so you should be saying "if (group == true)" (you could shorthand that to "if (group)"

The other problem is the use of "user" - your "def group =" is not looking through the group for a user, it's looking for a string.  You need to change your "user =" to something that will get the user as a string, not a user object.

CRS123456 June 25, 2022

@Nic Brough -Adaptavist- Thank you for your answer

I didn't know these you said. I'm a noob in script!!

I used to think CSS or Javascript is hard but now I understand these script's models are ridiculous. I don't understand even one line!!

so I changed the user section. is it true?

def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser().getUsername()

Also, I changed the if block but it's still not working.

Nic Brough -Adaptavist-
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 25, 2022

Ok, not a coder, that's fine.

Is this script supposed to be a Behaviour or a calculated field?  I ask because it looks like it has elements of both, and it's not clear what you are actually trying to achieve.  

Like CRS123456 likes this
CRS123456 June 25, 2022

@Nic Brough -Adaptavist- 

I wrote it in behavior.

I want to only the users who have login and member of the eRD-Leaders group could see the Project Plan field.

If those who have login don't member of the eRD-Leaders Project Plan field hide.

After this condition, if a user is a member of the eRD-Leaders they can't enter more than 60 days in the Project Plan field (it's a date picker). this is the second part and I didn't try this part because of the first condition.

CRS123456 June 25, 2022

@Nic Brough -Adaptavist- 

I checked these lines

def group = ComponentAccessor.groupManager.getUserNamesInGroup("eRD-Leaders")
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser().getUsername()
Apparently, they are fine because I see them in another field and their values are fine.
Now I think my problem is in the if. I don't know to compare command
Vikrant Yadav
Community Champion
June 25, 2022

Hi @CRS123456  Try something different here :- 

Instead of checking users in particular group, try to check logged in user groups , get all group name then apply condition.. User added in a group or not. If user is not added then hide field else field should be visible. 

import com.atlassian.jira.component.ComponentAccessor

def groupManager = ComponentAccessor.groupManager
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser

final username = loggedInUser.username

def groupNames = groupManager.getGroupNamesForUser(username) as List

if (groupNames.contains("eRD-Leaders")) {
return "user in group"
getFieldByName('Project Plan Delivery Date').setHidden(false)

}else {
return "user not in group"
getFieldByName('Project Plan Delivery Date').setHidden(true)
}

 Thanks

V.Y

Like CRS123456 likes this
CRS123456 June 25, 2022

@Vikrant Yadav 

Thank you

Are you sure about it?

It's not working. if my user is in eRD-Leaders or isn't, the field is still visible.

CRS123456 June 26, 2022

finally, I found it.

 

if (group.contains(user))
This if worked with that code I wrote the first time.
thank you very much, guys.
I hope can learn more about the script.
Like Vikrant Yadav likes this
Vikrant Yadav
Community Champion
June 26, 2022

Great! Glad to hear we are able help you in fixing the script :)

 

I have tested my code on my machine, it's working fine, i tested with attachment field not custom field andi have Added Script in Script Runner Behaviour >> Initialiser :- 

 

import com.atlassian.jira.component.ComponentAccessor

def groupManager = ComponentAccessor.groupManager

def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser

def attachment = getFieldById("attachment")

final username = loggedInUser.username

def groupNames = groupManager.getGroupNamesForUser(username) as List

if (groupNames.contains("AF US Consumer User App Spt")) {

attachment.setHidden(false)

}else {

attachment.setHidden(true)

}
Like CRS123456 likes this
CRS123456 June 26, 2022

Thanks, @Vikrant Yadav 

I can use these codes for other situations so it's good for those like me that don't know too much about scripts.

Suggest an answer

Log in or Sign up to answer