Work with Users

With HAPI, we've made it easy for you to work with users. 

Running code as another user

You can run code as another user with the  Users  top-level object. For example, you may wish to create an issue in a post-function in a project that the current user may not have create issue permissions in.

Run the following script from the script console:

In this example we're creating an issue as another user.

            def issue = Users.runAs('anuser') {
                Issues.create('SR', 'Bug') {
                    setSummary('created by another user')
                }
            }
            
            assert issue.reporter.name == 'anuser'            

Image showing you how to run code as another user

Creating and modifying users

With HAPI we've made it easy for you to create and modify users. 

Create a new user

You can create a user as follows:

            Users.create('jdoe', 'jdoe@example.com', 'Jane Doe')            

Image showing you how to create a new user with HAPI

You can control the user creation process further by specifying a closure:

In this example we're creating a user and adding a password, directory ID and application access.

            import com.atlassian.jira.application.ApplicationKeys
            
            Users.create('jdoe', 'jdoe@example.com', 'Jane Doe') {
                password = 'secret'
                directoryId = 10_001
                withApplicationAccess(ApplicationKeys.SERVICE_DESK, ApplicationKeys.SOFTWARE)
            }        

Delete a user

You can delete a user as follows:

            def user = Users.getByName('jdoe')
            user.delete()

Deactivate and activate a user

You can deactivate or activate a user as follows:

            def user = Users.getByName('jdoe')
            
            user.deactivate()
            user.activate()

User permissions and role membership

Extension methods on com.atlassian.jira.user.ApplicationUser make it easy to:

  1. Retrieve whether the user has a given permission in a project
  2. Retrieve whether the user has a given global permission
  3. Retrieve whether the user has a particular project role membership in a project

Retrieving project permissions

You can retrieve project permissions as follows:

            import com.atlassian.jira.permission.ProjectPermissions 
            
            // can this user create issues in project SR         
            user.canCreateIssues(Projects.getByKey('SR'))
            
            // which is equivalent to 
            user.hasPermission(ProjectPermissions.BROWSE_PROJECTS, Projects.getByKey('SR'))
            
            // does this user have permission to move issues in project SR         
            user.hasPermission(ProjectPermissions.MOVE_ISSUES, Projects.getByKey('SR'))
            
            // can this user view issues in SR
            user.canView(Projects.getByKey('SR'))
            
            // can this user administer the SR project         
            user.canAdminister(Projects.getByKey('SR'))

Image showing you how to retrieve user permissions using HAPI

Retrieving issue permissions

You can retrieve issue permissions as follows:

            import com.atlassian.jira.permission.ProjectPermissions 
            
            // does this user have permission to move issue SR-1         
            user.hasPermission(ProjectPermissions.MOVE_ISSUES, Issues.getByKey('SR-1'))
            
            // can this user view issue SR-1         
            user.canView(Issues.getByKey('SR-1'))

Image showing you how to see if a user has permissions using HAPI

Working with role memberships

You can work with role memberships as follows:

            import com.atlassian.jira.component.ComponentAccessor
            import com.atlassian.jira.security.roles.ProjectRoleManager
            
            // does this user have membership of the Developers role in the SR project         
            user.isMemberOfRole('Developers', Projects.getByKey('SR'))
            
            // or using a ProjectRole instance
            def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager)
            def projectRole = projectRoleManager.getProjectRole('Developers')
            user.isMemberOfRole(projectRole, Projects.getByKey('SR'))

Image showing how to check if a user has membership of a role

Working with group membership

You can work with group memberships as follows:

            // is the user a member of the jira-administrators group     
            user.isMemberOfGroup('jira-administrators')
            
            // or using a Group instance
            def group = Groups.getByName('jira-administrators')
            user.isMemberOfGroup(group)
            
            // checking membership from the group works too
            group.contains(user)
            
            // as well as with usernames
            group.contains('bob')

For more information on working with groups, see this page



Related content

On this page