Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Script to create user account in Jira Internal Directory based on email provided in Jira field

kdickason
Contributor
November 17, 2021

We have set up a public screen for users to request a Jira account.  It collects their Full Name and Email.  I have access to Script Runner.  Can someone help me make a script I can kick off during the workflow that will create a user in Jira Internal Directory using the user's email as their Username in Jira?  I really need the full script -- I am not strong with ScriptRunner.  Thank you in advance so much for any help!!

Here is code I have pieced together.  I'm getting an error for the bolded line (line 22) saying, "unable to resolve class UserService.CreateUserRequest"


import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.bc.user.UserService

def user = ComponentAccessor.getUserManager().getUserByKey("anyUserKey")
def issueManager = ComponentAccessor.getIssueManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def userName = customFieldManager.getCustomFieldObject("customfield_10000")
String userNameValue = issue.getCustomFieldValue(userName).toString()
def mail = customFieldManager.getCustomFieldObject("customfield_10001")
String mailValue = issue.getCustomFieldValue(mail).toString()

def pass = ""
def fullName = "FullName"
def userService = ComponentAccessor.getComponent(UserService)


UserService.CreateUserRequest createUserRequest = UserService.CreateUserRequest.
withUserDetails(user, userNameValue, pass, mailValue, fullName)

UserService.CreateUserValidationResult result = userService.validateCreateUser(createUserRequest)
if(result.isValid())
userService.createUser(result)
else
result.getErrorCollection()

Can anyone please help me.  This is code I got from others, so it may be totally off!!

4 answers

0 votes
mfabris
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.
November 21, 2021

Hi there @kdickason 

One of my colleagues wrote exactly this script you need some times ago, I have it somewhere

In any case you definitely need an if statement in there, to check if the user already exists.

I am quite busy today but if I find a moment in my break I will share the code.

mfabris
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.
November 22, 2021

There you go

From what I understand you want to use this with an issue collector?

Maybe you could use the issue collector to open the issues in a specific project, and then bind the "Issue Created" event to the script with a Script Listener.

 

The script would be this: 

 

import com.atlassian.jira.bc.user.UserService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.event.type.EventDispatchOption

def userManager = ComponentAccessor.getUserManager()
def issueManager = ComponentAccessor.getIssueManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def userService = ComponentAccessor.getComponent(UserService)
def userUtil = ComponentAccessor.userUtil
def user = userManager.getUserByName("amdin-user@your-jira.com") //you need to specify here the admin user who will create the new users.
//def issue = issueManager.getIssueObject("ABC-001") this is a debugging line, you can test with a test ticket

def newUserObj = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_12345") //your custom field for the email of the new user
def newUserNameField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_23456") //yourcustom field for the name of the new user
assert newUserObj: "Could not find custom field with name $newUserObj"

def newUserVal = (String) issue.getCustomFieldValue(newUserObj)
def newUserName = (String) issue.getCustomFieldValue(newUserNameField)

if(newUserVal != null) {
assert newUserVal: " value of newUserVal is $newUserVal"
def newUser = userManager.getUserByName(newUserVal)

if ( newUser == null ) {
// notifications are sent by default, set to false to not send a notification
final boolean sendNotification = false
def newCreateRequest = UserService.CreateUserRequest.withUserDetails(user, newUserVal, null, newUserVal, newUserName).sendNotification(sendNotification)
def createValidationResult = userService.validateCreateUser(newCreateRequest)
assert createValidationResult.isValid() : createValidationResult.errorCollection
newUser = userService.createUser(createValidationResult)
// userUtil.addUserToGroup(ComponentAccessor.groupManager.getGroup("my-group"), newUser)
// this if you want to add the user to a group
// you can also use removeUserFromGroup if you want to remove the user from a group upon creation, for example jira-software-users
}

 

I could not test it but there should not be any problem; this because I made it modifying another script that a colleague of mine wrote, and he is really a groovy legend. 

So yes, do not thank me but thank my colleague :) 

kdickason
Contributor
November 22, 2021

Thank you so much!!! This was my planned implementation:  I have a public project with one Issue Type  to which an Anonymous user can Create an issue type I named Request Account.  The screen for that issue type prompts the user for their Full Name and email.  

I was planning to kick off the script as a post function when the user hits Create with the screen completed.  

I'm hoping the script would make a Jira Internal Directory entry with this inforamtion:  

JIra User Name = user's email from the Email field on my screen

JIra Password = randomly generated (we plan to have PKI login, so I don't believe this matters)

Jira Email = user's email from the Email field on my screen.

If the script could validate there is no user with that email already, that would be even better. 

Is this where you were going with the script above?  I'm sorry I did not supply all this sooner.  If you think it would be better for me to use a Listener I'll revise my thinking.

You are my hero!! @mfabris 

kdickason
Contributor
November 27, 2021

@mfabris I have been out of the office and unable to try your script--I will be able to on Monday 29 Nov.  Just wanted to verify we are on the same line of thinking with my explanation above. Please let me know if you are willing to take another few minutes. Thank you in advance.  

From my earlier post:  

This was my planned implementation:  I have a public project with one Issue Type  to which an Anonymous user can Create an issue type I named Request Account.  The screen for that issue type prompts the user for their Full Name and email.  

I was planning to kick off the script as a post function when the user hits Create with the screen completed.  

I'm hoping the script would make a Jira Internal Directory entry with this inforamtion:  

JIra User Name = user's email from the Email field on my screen

JIra Password = randomly generated (we plan to have PKI login, so I don't believe this matters)

Jira Email = user's email from the Email field on my screen.

If the script could validate there is no user with that email already, that would be even better. 

mfabris
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.
November 28, 2021

Hi there! yes, this is exactly how the script would work.

Check the inline comments, they explain how to use the fields.

The line 

if ( newUser == null ) 

Is exactly to check that the user does not exist already. If the user exists, then the script skips to the end.

kdickason
Contributor
November 30, 2021

@mfabris  I still need just another moment of your time, if you please would help me. 

I created a Listener for the Event "Issue Created" on my project.  When I enter your script in ScriptRunner to set up the Listener, ScriptRunner is showing an error for each of these lines:

def newUserVal = (String) issue.getCustomFieldValue(newUserObj)
def newUserName = (String) issue.getCustomFieldValue(newUserNameField)

The error is:  (Static type checking) - The variable [issue] is undeclared.

I read in this post to change issue to event.issue in the Listener.  I did that and these errors went away, but the script is failing when executed by the Listener.  The Jira logs show nothing of interest and truncate after 300 lines.

mfabris
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.
December 1, 2021

issue is undeclared in the script, but when it runs in an issue then it is given by the context.

 

If you want you can add at the beginning of your script 

Issue issue = context.issue as Issue;

That should clear the error.

0 votes
kdickason
Contributor
November 18, 2021

@mfabris Do you want to come to my rescue AGAIN with my above question??

0 votes
kdickason
Contributor
November 17, 2021

@Manuel Bastardo Castellano Would you be willing to post your entire script for me? I think I'm trying to do something very similar to you.

0 votes
kdickason
Contributor
November 17, 2021

@Aleksandr Zuevich I see you helped a user with a similar problem to this in the past.  Would you be willing to help me?

Suggest an answer

Log in or Sign up to answer