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.
Community moderators have prevented the ability to post new answers.
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()
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}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
import com.atlassian.crowd.model.user.UserTemplate; You could also use Matt Boesch's solution via ImmutableUser.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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]
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.