How to get the current project the user is on?

java
jira-server

#1

Hi,

I am writing a condition for a web-item. The condition passes only if the current project is some X project. For all other projects the condition fails.

How do I get the current project the user is on? Is it possible to fetch similar to fetching the current logged in user from JiraAuthenticationContext?


#2

You should get passed a JiraHelper object which has a getProject method. But I believe exactly what is available depends on the location in which the web-item is placed.

If you have problems with that method you can get it from https://docs.atlassian.com/jira/7.2.2/com/atlassian/jira/user/UserProjectHistoryManager.html… although the first method is preferable as that should be null in places where there is no project context, eg some admin sections.


#3

Hi @jechlin,

Thanks for your answer. I would like to go with the second option. (UserProjectHistoryManager).

However, for the method getCurrentProject(int permission, ApplicationUser user), what would be the int value for permission?

I can see that there are different permission schemes for a project and different permissions like Administer Project, Browse Project etc. But I don’t find a way to get the “int” value they are mapped to.


#4

pass in a constant like com.atlassian.jira.security.Permissions.BROWSE


#5

@jechlin I have tried that. It throws below exception. I have passed Permissions.ADMINISTER constant. I have also verified that the user has all necessary permissions from the JIRA UI.

java.lang.IllegalArgumentException: No permission for id : 0 in system
[INFO] [talledLocalContainer] 	at com.atlassian.jira.security.plugin.ProjectPermissionKey.resolve(ProjectPermissionKey.java:62)
[INFO] [talledLocalContainer] 	at com.atlassian.jira.security.plugin.ProjectPermissionKey.<init>(ProjectPermissionKey.java:23)
[INFO] [talledLocalContainer] 	at com.atlassian.jira.user.DefaultUserProjectHistoryManager.getCurrentProject(DefaultUserProjectHistoryManager.java:57)
[INFO] [talledLocalContainer] 	at sun.reflect.GeneratedMethodAccessor602.invoke(Unknown Source)
[INFO] [talledLocalContainer] 	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[INFO] [talledLocalContainer] 	at java.lang.reflect.Method.invoke(Method.java:498)

Also, from the docs, I see that “com.atlassian.jira.security.Permissions” class is deprecated See here: https://developer.atlassian.com/static/javadoc/jira/reference/com/atlassian/jira/security/Permissions.html

Alternative of that class is “om.atlassian.jira.permission.ProjectPermissions”: https://developer.atlassian.com/static/javadoc/jira/reference/com/atlassian/jira/permission/ProjectPermissions.html

I got the same error with the new constant values as well,

[INFO] [talledLocalContainer] java.lang.IllegalArgumentException: No permission for id : 37735953 in system
[INFO] [talledLocalContainer] 	at com.atlassian.jira.security.plugin.ProjectPermissionKey.resolve(ProjectPermissionKey.java:62)
[INFO] [talledLocalContainer] 	at com.atlassian.jira.security.plugin.ProjectPermissionKey.<init>(ProjectPermissionKey.java:23)
[INFO] [talledLocalContainer] 	at com.atlassian.jira.user.DefaultUserProjectHistoryManager.getCurrentProject(DefaultUserProjectHistoryManager.java:57)
[INFO] [talledLocalContainer] 	at sun.reflect.GeneratedMethodAccessor602.invoke(Unknown Source)
[INFO] [talledLocalContainer] 	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[INFO] [talledLocalContainer] 	at java.lang.reflect.Method.invoke(Method.java:498)
[INFO] [talledLocalContainer] 	at com.atlassian.plugin.util.ContextClassLoaderSettingInvocationHandler.invoke(ContextClassLoaderSettingInvocationHandler.java:26)
[INFO] [talledLocalContainer] 	at com.sun.proxy.$Proxy165.getCurrentProject(Unknown Source)
[INFO] [talledLocalContainer] 	at sun.reflect.GeneratedMethodAccessor618.invoke(Unknown Source)
[INFO] [talledLocalContainer] 	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[INFO] [talledLocalContainer] 	at java.lang.reflect.Method.invoke(Method.java:498)


#6

Yeah I know it’s deprecated but they haven’t provided a method that takes a ProjectPermissionKey.

Don’t use ADMINISTER because it’s not a project permission, use PROJECT_ADMIN if that’s what you want.

Have a look at the source - com.atlassian.jira.permission.ProjectPermissions#systemProjectPermissionKeyByShortName


#7

Yeah. PROJECT_ADMIN worked. Thanks a lot for the help.