How to create user programatically in jira plugin

Hi Experts,

How to create JIRA user programmatically in jira plugin. Please refer some reference code also.

There are two interfaces in the jira-api library that you should take a look at:

UserManager has this:

public ApplicationUser createUser(@Nonnull final UserDetails userData)
        throws CreateException, PermissionException;

UserService has this:

ApplicationUser createUser(CreateUserValidationResult createUserValidationResult)
        throws PermissionException, CreateException;

As per the conventions for interface naming, the UserService performs more validation for you, but is less convenient to use.

1 Like

By the way, the best reference code in the Universe will be the Jira source code, which is available to you for free with the purchase of a $10 Jira Server license.

Thanks for reference @david.pinn. Below code is working with UserService interface.

        // Create user 
		UserService userService = ComponentAccessor.getComponent(UserService.class);
		ApplicationUser user=ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
		final CreateUserRequest createUserRequest = CreateUserRequest.withUserDetails(user,
	               "dipesh", "dipesh123", "abcd123@gmail.com", "Dipesh Chouksey");
		CreateUserValidationResult result = userService.validateCreateUser(createUserRequest);
		 if(result.isValid())
		 {
		    try {
				userService.createUser(result);
				System.out.println("User is created successfully: abcd123@gmail.com");
			} catch (PermissionException | CreateException e) {
				System.out.println("Exception:   "+e.getMessage());
				e.printStackTrace();
			}
		 }else{
			 
			 System.out.println("Error: "+result.getErrorCollection());
		 }


Well done, @dchouksey89; that looks pretty good to me.

Consider injecting the UserService and JiraAuthenticationContext into your class rather than using ComponentAccessor. What you have done is perfectly fine, but using Spring Scanner to inject the components will make your code tidier. Other than that, you are golden.

1 Like

sure @david.pinn. I will do changes according to your suggestion. Thanks :relaxed: !

Hi @david.pinn

I also want to update user kindly refer me some sample code to update user like username and email address for user.

Note: I am using below code.

UserManager userManager = ComponentAccessor.getComponent(UserManager.class);
ApplicationUser applicationUser= userManager.getUserByKey(employeeDTO.getUserKey());
userManager.updateUser(applicationUser);

Here how to set the values in application user as there is no setter method. Please can you guide me to update username and email in user.

Hi @david.pinn.

Solved by this link:

Just a note for others, password can be null and according to the documentation, If empty a random password will be generated https://docs.atlassian.com/software/jira/docs/api/7.6.1/com/atlassian/jira/bc/user/UserService.CreateUserRequest.html