Workflow validator for current assignee is in projectrole

Is there any plugin that can validate if the current assignee is in a given project role when executing a transition?

Thank you,

5 answers

1 accepted

Below is the code for 4 files you will be using in the plugin

1. AssigneeInProjectRole

public class AssigneeInProjectRole implements Validator
{
    private static final Logger log = LoggerFactory.getLogger(AssigneeInProjectRole.class);
    public static final String PROJECT_ROLES="PROJECT_ROLES";
    public static final String SELECTED_PROJECT_ROLES="SELECTED_PROJECT_ROLES";
    private final ProjectRoleManager projectRoleManager;
    private final JiraAuthenticationContext authContext;

    public AssigneeInProjectRole(ProjectRoleManager projectRoleManager,
                              JiraAuthenticationContext authContext) {
        this.projectRoleManager = projectRoleManager;
        this.authContext = authContext;        
    }

    public void validate(Map transientVars, Map args, PropertySet ps) throws InvalidInputException
    {
        Issue issue = (Issue) transientVars.get("issue");
        User user = authContext.getLoggedInUser();
        String selectedProjectRoles = (String) args.get(SELECTED_PROJECT_ROLES);

        List<String> selectedProjectRolesList = Arrays.asList(selectedProjectRoles.split("\\s*,\\s*"));

        boolean userIsInProjectRole = false;
        
        for(String role:selectedProjectRolesList)
        {
            ProjectRole projectRole = projectRoleManager.getProjectRole(role);
            Project project = issue.getProjectObject();
            ProjectRoleActors projectRoleActors = projectRoleManager.getProjectRoleActors(projectRole, project);
            if(projectRoleActors.getUsers().contains(user)){
                userIsInProjectRole = true;
            }
        }

        if(!userIsInProjectRole) {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("User must be in one of the following project roles to perform this transition: ");
            for(String role:selectedProjectRolesList){stringBuilder.append(role+",");}
            throw new InvalidInputException(stringBuilder.substring(0,stringBuilder.toString().lastIndexOf(",")));
        }
    }
}

2. AssigneeInProjectRoleFactory

public class AssigneeInProjectRoleFactory extends AbstractWorkflowPluginFactory implements WorkflowPluginValidatorFactory
{
    public static final String PROJECT_ROLES="PROJECT_ROLES";
    public static final String SELECTED_PROJECT_ROLES="SELECTED_PROJECT_ROLES";
    private final ProjectRoleManager projectRoleManager;

    public AssigneeInProjectRoleFactory(ProjectRoleManager projectRoleManager) {
        this.projectRoleManager = projectRoleManager;
    }

    protected void getVelocityParamsForInput(Map velocityParams)
    {
        //the default message
        velocityParams.put(PROJECT_ROLES,projectRoleManager.getProjectRoles());
    }

    protected void getVelocityParamsForEdit(Map velocityParams, AbstractDescriptor descriptor)
    {
        getVelocityParamsForInput(velocityParams);
        getVelocityParamsForView(velocityParams, descriptor);
    }

    protected void getVelocityParamsForView(Map velocityParams, AbstractDescriptor descriptor)
    {
        if (!(descriptor instanceof ValidatorDescriptor))
        {
            throw new IllegalArgumentException("Descriptor must be a ValidatorDescriptor.");
        }

        ValidatorDescriptor validatorDescriptor = (ValidatorDescriptor) descriptor;

        velocityParams.put(SELECTED_PROJECT_ROLES, validatorDescriptor.getArgs().get(SELECTED_PROJECT_ROLES));
    }

    public Map getDescriptorParams(Map validatorParams)
    {
        // Process The map
        String[] value = (String[])validatorParams.get(SELECTED_PROJECT_ROLES);
        StringBuilder stringBuilder = new StringBuilder();
        for(String string:value){
            stringBuilder.append(string + ",");
        }
        return EasyMap.build(SELECTED_PROJECT_ROLES, stringBuilder.toString().substring(0,stringBuilder.lastIndexOf(",")));
    }
}


3. Edit view

<tr>
    <td class="fieldLabelArea">
        Project Role:
    </td>
    <td nowrap>
        <select name="SELECTED_PROJECT_ROLES" id="SELECTED_PROJECT_ROLES" height="100px;" multiple="multiple">
            #foreach ($role in $PROJECT_ROLES)
                <option value="$role"
                    #if ($SELECTED_PROJECT_ROLES.contains($role.name))
                        SELECTED
                    #end
                >$role</option>
            #end
        </select>
    </td>
</tr>

4. View

Assignee must be in one of the project roles to perform this transition: <br />
$SELECTED_PROJECT_ROLES

Do you have the jar for the plugin?

Doesn't allow me to upload jar files here. Give me your email id and I can share it

adolfo.casari at gmail.com

Thank you,

Can you send the jar to us? sysadmin@di2e.net . THANKS!

Best bet would be using jira script runner and writing a custom validator

Hi, if you can share the code i'd appreciate it.

Thank you.

Renjith Pillai Community Champion Oct 13, 2012

To extend what Vishwa said, you may use this code in the Script runner.

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.security.roles.ProjectRole
import com.atlassian.jira.security.roles.ProjectRoleManager

ComponentManager componentManager = ComponentManager.getInstance()
ProjectRoleManager projectRoleManager = ComponentManager.getComponentInstanceOfType(ProjectRoleManager.class) as ProjectRoleManager
ProjectRole devRole = projectRoleManager.getProjectRole("Developers")


projectRoleManager.isUserInProjectRole(issue.assignee, devRole, issue.projectObject)

Hi Adolfo,

I have a custom plugin that does this. Let me know if you want me to just post the code for the validatorFactory and Validator class

Hi,

Yes, if you can share the code i'd appreciate it.

Thank you.

Suggest an answer

Log in or Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

2,831 views 12 18
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot