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

How can I update a JIRA user's email address in a plugin?

Matt Boesch March 6, 2015

Question:  How do I update the email address on a User object in an email handler plugin?

Background: I created an email handler plugin in JIRA 6.3.11 for a private JIRA instance.  I check to see if the user exists by calling the MessageUserProcessor.getAuthorFromSender(message), which looks for a user based on the email address equaling the the user's email address or the username.

User reporter = processor.getAuthorFromSender(message);

Our usernames are the part of the email address before the "@" sign, so I add second chance logic to parse out the username from the email address of the email being processed and try to get the user based on the username by calling MessageUserProcessor..findUserByUsername(username).

reporter = processor.findUserByUsername(username);

We recently had a change in MS Exchange where our contractors email addresses changed from username@company.com to username@associates.company.com, so their email in JIRA may have an old email address for them.  What I want to do is once I get the user from the username, I want to compare the email address from the user object returned from JIRA with the email address from the email being processed.  If they are different, I would like to update the JIRA user with email address from the email being processed.  Based on my imports, the User object is com.atlassian.crowd.embedded.api.User.  I am not sure how to update the email address on the User in my Java code, since I do not see a method for this in the API in order to update the user in JIRA.  I also would like to confirm once the user object is updated with the correct email address, how to update that user in JIRA.

1 answer

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

0 votes
Answer accepted
Dmitrii Apanasevich
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 12, 2015

Hi!

You should inject UserManager class (class of userManager in code below) and do something like this:

 

final UserTemplate userTemplate = new UserTemplate(user);
userTemplate.setEmailAddress("qqq@qqq.com");
userManager.updateUser(userTemplate);

 

Upd: you can also access UserManager via ComponentAccessor.getUserManager()

Matt Boesch March 13, 2015

Thanks Dmitrii. I ended up doing the following the other day based on https://answers.atlassian.com/questions/180474. It was not exactly what he did, but I got some ideas from it. I use the same userManager.updateUser you suggested. I used ImmutableUser instead of UserTemplate. I am not sure what the advantages are of one vs the other. {code} import com.atlassian.crowd.embedded.impl.ImmutableUser; import com.atlassian.crowd.embedded.api.User; User reporter = processor.getAuthorFromSender(message); if (reporter == null) { //Executed some logic (not shown here) to get the username from the incoming email address and then try to get the user based on username as second chance logic reporter = processor.findUserByUsername(username); if (!reporter == null) { if (!reporterEmail.equals(reporter.getEmailAddress())) { User updateUser = ImmutableUser.newUser(reporter).emailAddress(reporterEmail).toUser(); if (userManager.hasWritableDirectory()) { try { userManager.updateUser(updateUser); } catch (Exception e) { log.error("Failed trying to update user's email address from " + reporter.getEmailAddress() + " to " + reporterEmail); } } } } } {code}

MB April 15, 2015

My IDE cannot find reference for UserTemplate. What's that package?

Dmitrii Apanasevich
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.
April 15, 2015

import com.atlassian.crowd.model.user.UserTemplate; You could also use Matt Boesch's solution via ImmutableUser.

MB April 16, 2015

Thanks for quick response. Do I have to add some new dependency to pom? If yes, which one?

Dmitrii Apanasevich
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.
April 16, 2015

No, you don't need any additional dependencies. I use <dependency> <groupId>com.atlassian.jira</groupId> <artifactId>atlassian-jira</artifactId> <version>${jira.version}</version> <scope>provided</scope> </dependency> which, in turn, depends on several libs with <groupId>com.atlassian.crowd</groupId>. Which JIRA version do you use?

MB April 16, 2015

We use <jira.version>6.2.2</jira.version> <amps.version>4.2.20</amps.version> but I've try it also with <jira.version>6.4.2</jira.version> <amps.version>5.0.13</amps.version> I don't have package com.atlassian.crowd.model in my repo.

Dmitrii Apanasevich
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.
April 16, 2015

It's strange, package is valid: https://docs.atlassian.com/atlassian-crowd/latest/com/atlassian/crowd/model/user/package-summary.html. Try to use direct dependency (for <jira.version>6.2.2</jira.version>): <dependency> <groupId>com.atlassian.crowd</groupId> <artifactId>atlassian-crowd-components</artifactId> <version>2.8.0-OD-4</version> </dependency> Or try to use ImmutableUser.

MB April 16, 2015

With ImmutableUser; it's pretty same story. In package com.atlassian.crowd.embedded.impl is no class ImmutableUser in my repo.

MB April 16, 2015

Yeah, and the additional dependency didn't help :-( I'm going to try different environment.

Dmitrii Apanasevich
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.
April 16, 2015

Are your dependencies ok? Try <jira.version>6.0-rotp3</jira.version> I can't donload <jira.version>6.2.2</jira.version> at the moment

Matt Boesch April 17, 2015

I have the following dependencies for my plugin, but I am not actually sure what I needed. groupId - com.atlassian.jira, artifactId - jira-api, version - ${jira.version}, scope - provided groupId - com.atlassian.jira, artifactId - jira-core, version - ${jira.version}, scope - provided

Royce Wong
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.
June 8, 2015

Hi Martin B and Matt Boesch, I have the same problem, wondering if you figured out why these packages are missing: - com.atlassian.crowd.model.user.UserTemplate - com.atlassian.crowd.embedded.impl.ImmutableUser I have these settings in pom.xml: <properties> <jira.version>6.2.2</jira.version> <amps.version>5.0.13</amps.version> <plugin.testrunner.version>1.2.3</plugin.testrunner.version> <!-- TestKit version 5.x for JIRA 5.x, 6.x for JIRA 6.x --> <testkit.version>5.2.26</testkit.version> </properties> I also have <dependency> <groupId>com.atlassian.jira</groupId> <artifactId>jira-api</artifactId> <version>${jira.version}</version> <scope>provided</scope> </dependency> but no jira-core <!-- <dependency> <groupId>com.atlassian.jira</groupId> <artifactId>jira-core</artifactId> <version>${jira.version}</version> <scope>provided</scope> </dependency> --> Thanks in advance.

Royce Wong
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.
June 10, 2015

Continue... And if I add this dependency to my pom file: <dependency> <groupId>com.atlassian.crowd</groupId> <artifactId>embedded-crowd-core</artifactId> <version>2.7.1</version> </dependency> Then the IDE is able to resolve the two packages and complile the code, but failed to build/load when I do atlas-run and gave this error message: 'com.mycompany.jira.plugins.MyPlugin' - 'ATTUserDeactivation' failed to load. Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/osgi] Offending resource: URL [bundle://142.0:0/META-INF/spring/atlassian-plugins-components.xml]

Royce Wong
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.
June 10, 2015

I ended up having to clean up the target dir. with maven clean and start again. But this time add: <dependency> <groupId>com.atlassian.crowd</groupId> <artifactId>embedded-crowd-core</artifactId> <version>2.7.1</version> <scope>provided</scope> </dependency> The difference is the "<scope>provided</scope>". Then the plugin compile and atlas-run install the plugin with no issue. Reference: https://developer.atlassian.com/docs/faq/troubleshooting/dependency-issues-during-plugin-initialisation

TAGS
AUG Leaders

Atlassian Community Events