Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Script-Runner Post-Function: Permission Checking

Michael Michael March 28, 2012

Hey,

we use the following code as a script-runner post function:

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.opensymphony.user.Group
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.config.SubTaskManager
import org.ofbiz.core.entity.GenericValue
import com.opensymphony.user.User
import com.atlassian.core.user.UserUtils
import com.atlassian.jira.user.util.UserManager
import com.opensymphony.workflow.WorkflowContext
import com.atlassian.jira.security.util.GroupSelectorUtils
import com.atlassian.jira.util.ImportUtils

ComponentManager componentManager = ComponentManager.getInstance()
CustomFieldManager customFieldManager = componentManager.getCustomFieldManager()
def issueFactory = componentManager.getIssueFactory()
def issueManager = componentManager.getIssueManager()
def indexManager = componentManager.getIndexManager()
def groupCf = customFieldManager.getCustomFieldObjectByName("Kompetenzcluster") // name of group CF
def UserList = []
String currentUser = ((WorkflowContext) transientVars.get("context")).getCaller()
User currentUserObj = UserUtils.getUser(currentUser)
GroupSelectorUtils util = (GroupSelectorUtils) ComponentManager.getInstance().getContainer().getComponentInstanceOfType(GroupSelectorUtils.class)

//Group group = issue.getCustomFieldValue(groupCf) as Group
issue.getCustomFieldValue(groupCf).each {Object group ->

        if (group) {
            Group groupObj = util.getGroups((String) group)
            groupObj.getUsers().each {String user ->

                if(!UserList.contains(user) && user != currentUser) {

                        MutableIssue newIssue = issueFactory.getIssue()
                        newIssue.summary = "$user: " + issue.summary
                        newIssue.issueTypeId = '71'
                        newIssue.project = issue.project
                        newIssue.reporter = UserUtils.getUser(user)
                        newIssue.assignee = UserUtils.getUser(user)
                        newIssue.assigneeId = user
                        newIssue.priority = issue.priority

                        Map<String,Object> newIssueParams = ["issue":newIssue] as Map<String,Object>
                        GenericValue newIssueGv = issueManager.createIssue(currentUserObj, newIssueParams)

                        boolean wasIndexing = ImportUtils.isIndexIssues();
                        ImportUtils.setIndexIssues(true);

                        indexManager.reIndex(newIssueGv);

                        SubTaskManager subTaskManager = componentManager.getSubTaskManager()
                        subTaskManager.createSubTaskIssueLink(issue, newIssue, componentManager.getJiraAuthenticationContext().getUser())

                        indexManager.reIndex(newIssueGv);
                        indexManager.reIndex(issue);

                        ImportUtils.setIndexIssues(wasIndexing);

                        UserList.add(user)

                }
            }
        }
}

Now, we wanted to add a permission check. So before a subtask will be created, the script should check, whether the author of the ticket has the CREATE permission in this project (project shortcut OCRA). can you help us with this problem?

thank you very much in advance!

regards,
michael

1 answer

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

1 vote
Answer accepted
JamieA
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.
March 28, 2012

Given the code you have already, this line should return true or false (untested):

import com.atlassian.jira.security.Permissions
import com.atlassian.jira.user.UserUtils

...

def permissionManager = ComponentManager.instance.getPermissionManager()
def hasPerms = permissionManager.hasPermission(Permissions.CREATE_ISSUE, issue.project, UserUtils.getUser(currentUser))

Michael Michael March 28, 2012

it seems to work.

thanks for your fast response!!

TAGS
AUG Leaders

Atlassian Community Events