How to correctly define the project in the condition of script fragment?

Айри Мизуно September 18, 2020

Hello, everyone

I added new custom web item via sciptrunner in jira. It should be shown only when current user didn't have permission to browse project.

My condition (is not work):

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.plugin.ProjectPermissionKey
import com.atlassian.jira.project.Project
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
Project project = jiraHelper.project
def permissionManager = ComponentAccessor.getPermissionManager()
ProjectPermissionKey browse_projects = new ProjectPermissionKey("BROWSE_PROJECTS")
def hasPerm = permissionManager.hasPermission(browse_projects, project, currentUser)
(!hasPerm)

I guess the problem is  jiraHelper return the last project that user used/browsed when user has not permission to browse current project .

Is it any other solution I can use in the condition to define project correctly?

1 answer

0 votes
Martin Bayer _MoroSystems_ s_r_o__
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 18, 2020

Hi @Айри Мизуно where do you want to display this Web Item? What context is set?

Айри Мизуно September 20, 2020

Hi @Martin Bayer _MoroSystems_ s_r_o__ 

On the system.top.navigation.bar. I added custom web item (button) that run code and display dialog. The code is the rest endpoint indicated on link. Maybe it could be define the current project in the rest endpoint?

Martin Bayer _MoroSystems_ s_r_o__
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 20, 2020

Did you try to log "project" value? I'm not sure that "system.top.navigation.bar" has Project in its context.

What should your action do? Is it really so 'genera' action that it make sense to have it as part of the top bar?

But first, I would add logging and you can check that project value is present.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.plugin.ProjectPermissionKey
import com.atlassian.jira.project.Project
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
Project project = jiraHelper.project

log.error(project)

def permissionManager = ComponentAccessor.getPermissionManager()
ProjectPermissionKey browse_projects = new ProjectPermissionKey("BROWSE_PROJECTS")
def hasPerm = permissionManager.hasPermission(browse_projects, project, currentUser)
Айри Мизуно September 25, 2020

Hi, @Martin Bayer _MoroSystems_ s_r_o__ 

Yes, I tried to log before. I passed the value of project as parameter in the link to REST endpoint and logs the value of parameter in the code of endpoint.

So I came to the conclusion that every time when I open project which user can't browse jiraHelper return the last project that user browsed. It seems like jiraHelper can't get project if user can't browse project and returns previous value.

Screenshot_8.png

I added logs as you advised. Result is the same.

Айри Мизуно September 25, 2020

Hi, @Martin Bayer _MoroSystems_ s_r_o__ 

Yes, I tried to log before. I passed the value of project as parameter in the link to REST endpoint and logs the value of parameter in the code of endpoint.

So I came to conclusion that every time when user open project which he can't browse jiraHelper return the last project that user browsed. It seems like jiraHelper can't get project if user can't browse project and returns previous value.

Screenshot_8.png

I added logs as you advised. Result is the same.

Martin Bayer _MoroSystems_ s_r_o__
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 27, 2020

Hi @Айри Мизуно what is the use case? Does it make sense to invoke condtition based on the project on top bar? It is safer to have functionality for "project" in "project context"

Айри Мизуно September 27, 2020

Hi, 

I'm not sure that web item shown for  "project" in "project context" if user  couldn't browse project. I'll check.

Thank you.

Martin Bayer _MoroSystems_ s_r_o__
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 27, 2020

You are right, it won't work probably. CAn you describe your use case please? It's hard to think about it when I don't have context.

Thank you :)

Айри Мизуно September 28, 2020

The condition isn't matter, the web item will shown anyway. Now I need to define the current project in the web item and pass it to the REST endpoint or in the code of REST endpoint even if the user has no permission to browse.

Web item calls REST endpoint,  REST endpoint returns dialog window.

In the dialog window will be a link on which user can go and create issue. One of the custom fields of issue is should be equal to the "defined project". I would like to preset the value of this field.

Suggest an answer

Log in or Sign up to answer