Hi
I am writing a condition where web-item is shown only if the user is administrator or project lead. I have added project lead to project administrator role. I am looking for strings to check for the roles, I wrote the following code so far:
import java.util.SortedSet;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.plugin.webfragment.conditions.AbstractJiraCondition;
import com.atlassian.jira.plugin.webfragment.model.JiraHelper;
import com.atlassian.crowd.embedded.api.Group;
import com.atlassian.crowd.embedded.api.User;
import com.atlassian.jira.user.UserUtils;
import com.atlassian.jira.user.util.UserUtil;
public class SubComponentAccessCondition extends AbstractJiraCondition {
UserUtil userUtil;
@Override
public boolean shouldDisplay(User user, JiraHelper jiraHelper) {
userUtil = ComponentAccessor.getUserUtil();
SortedSet<Group> groups = userUtil.getGroupsForUser(user.getName());
return user != null && groups.contains("jira-developers");
}
}
Can you please let me how I check if user belongs to jira administrator or project administrator role. Also it shows me AbstractJiraCondition as depricated (I am using JIRA6) is there a better way to write this..
Please let me know.
Thanks
Abe
to check user in project role you can try this by doing little bit changes
SimpleErrorCollection errorCollection = new SimpleErrorCollection();
ProjectRole projectLeaders = projectRoleService.getProjectRoleByName(loggedInUser,"project Leaders",errorCollection);
ProjectRoleActors actors = projectRoleService.getProjectRoleActors( user, projectLeaders,project, errorCollection);
for (Iterator<RoleActor> iterator = actors.getRoleActors().iterator(); iterator.hasNext();) {
RoleActor roleActor = (RoleActor) iterator.next();
if (roleActor.getType().equals("atlassian-user-role-actor")) {
Set<User> users = roleActor.getUsers();
//remaining logic need to add here
}
}
Hello Prasad
Is it possible to send you some code in private for a opinion, I can't post it online. If ok please email me jiraabraham@gmail.com
Thanks
Abe
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Made some progress, just did not know how to get reference to project in the following line:
ProjectRoleActors actors = projectRoleService.getProjectRoleActors( user, projectLeaders,project, errorCollection);
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
you can get project object like this
Issue issue = (Issue) transientVars.get("issue");
Project project=issue.getProjectObject();
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Get the project from JiraHelper.
Rest is already given by @Rambanam
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Cant write comment so long so writing as answer:
Made some progress, just did not know how to get reference to project in the following line:
ProjectRoleActors actors = projectRoleService.getProjectRoleActors( user, projectLeaders,project, errorCollection);
Code written so far:
package com.collab.consulting.plugin.subcomponent.conditions;
import java.util.SortedSet;
import com.atlassian.jira.bc.projectroles.ProjectRoleService;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.plugin.webfragment.conditions.AbstractJiraCondition;
import com.atlassian.jira.plugin.webfragment.model.JiraHelper;
import com.atlassian.crowd.embedded.api.Group;
import com.atlassian.crowd.embedded.api.User;
import com.atlassian.jira.user.UserUtils;
import com.atlassian.jira.user.util.UserUtil;
import com.atlassian.jira.util.SimpleErrorCollection;
import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.project.Project;
import com.atlassian.jira.project.ProjectManager;
import com.atlassian.jira.security.roles.ProjectRole;
import com.atlassian.jira.security.roles.ProjectRoleActors;
import com.atlassian.jira.security.roles.ProjectRoleManager;
import com.atlassian.jira.security.roles.DefaultRoleActors;
import com.atlassian.jira.security.roles.ProjectRoleImpl;
import com.atlassian.jira.security.roles.RoleActor;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class SubComponentAccessCondition extends AbstractJiraCondition {
UserUtil userUtil;
@Override
public boolean shouldDisplay(User user, JiraHelper jiraHelper) {
boolean validated = false;
if (user == null) return false;
// Check if the user in project leader role
// Get all actors in the project leader role
ProjectRoleService projectRoleService = ComponentManager.getComponentInstanceOfType(ProjectRoleService.class);
SimpleErrorCollection errorCollection = new SimpleErrorCollection();
ProjectRole projectLeaders = projectRoleService.getProjectRoleByName(user,"project Leaders",errorCollection);
if (!errorCollection.hasAnyErrors()) {
ProjectRoleActors actors = projectRoleService.getProjectRoleActors( user, projectLeaders,project, errorCollection);
for (Iterator<RoleActor> iterator = actors.getRoleActors().iterator(); iterator.hasNext();) {
RoleActor roleActor = (RoleActor) iterator.next();
if (roleActor.getType().equals("atlassian-user-role-actor")) {
Set<User> roleUsers = roleActor.getUsers();
//remaining logic need to add here
for (Iterator<User> it = roleUsers.iterator(); it.hasNext(); ) {
User itUser = it.next();
if (itUser.getName().equalsIgnoreCase(user.getName())){
return true; // User name matched so returning that user has access
}
}
}
}
}
// Check if the user is in global admin role , get the groups to which user belongs and see jira-administrators is part of the group
userUtil = ComponentAccessor.getUserUtil();
SortedSet<Group> groups = userUtil.getGroupsForUser(user.getName());
return groups.contains("jira-administrators");
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.