Just a heads up: On March 24, 2025, starting at 4:30pm CDT / 19:30 UTC, the site will be undergoing scheduled maintenance for a few hours. During this time, the site might be unavailable for a short while. Thanks for your patience.

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

How do I get new user's application access in JIRA ScriptRunner(Listener)?

Jaesang Kim July 23, 2019

Hi all, 

I'm using JIRA and ScriptRunner. What I'm trying to do is get notification in the event of UserCreatedEvent and inform the name, email and Application access ( such as "Software" or "Service Desk" or "Confluence") to see if the new user consumed a license of any application.  I tried to use ScriptListner so that it can email me with those information. 

I got it work to send name, email address of the new user but somehow the application info is not sending the correct information. Even though the new user created has a software application access, it doesn't say that so I think I'm missing something.

This is the part of my code (in groovy) to get the application info of newly created user.

import com.atlassian.jira.event.AbstractEvent
import com.atlassian.crowd.event.user.UserCreatedEvent
import com.atlassian.jira.application.ApplicationAuthorizationService
import com.atlassian.jira.application.ApplicationKeys
import com.atlassian.jira.component.ComponentAccessor

def newUserEvent = event as UserCreatedEvent;
def newUser = newUserEvent.getUser();
def newUserEmail = newUser.getEmailAddress();

def joinedApp = "";

def applicationAuthorizationService = ComponentAccessor.getComponent(ApplicationAuthorizationService);
def userManager = ComponentAccessor.userManager;
def user = userManager.getUserByName(newUser.getName());

if (applicationAuthorizationService.canUseApplication(user, ApplicationKeys.SOFTWARE)) {
joinedApp = "JIRA Software";
}else {
joinedApp = "somethingelse";
}

I appreciate for your help in advance.

Thank you!

Jaesang Kim

1 answer

0 votes
PD Sheehan
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 10, 2021

I found this question when searching for some usage of ApplicationAuthorizationService.

This helped me.

But I think I might be able to help anyone else that has the same issue as the original poster. Here is my guess as to what is happening.

  • Application access is derived from group membership
  • Group membership can only be granted after the user is created
  • The userCreated event is fired immediately after the creation of the user and before the group membership has been assigned.

You could try to use one of the Membership related events, but I'm not sure that would work. For one thing, I don't know how you'd detect if the user was just created or not.

I have no idea if this is a dangerously bad practice, but you could try something like 

Thread.start{

//all your imports here
Thread.start{
Thread.sleep(3000) //3 seconds
/* the rest of your script here */
}

 This starts a new thread, then that thread waits a bit and then executes your code.

The nice thing is the event call itself terminates immediately and the new thread is started asynchronously.

Suggest an answer

Log in or Sign up to answer