Set a user inactive programmatically

Francesco R
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 11, 2024

Hi all.
on a Jira Core server instance, I'm trying to set a user as inactive programmatically (using Java, no scripts) .
But  the 


UpdateUserValidationResult updateUserValidationResult = userService.validateUpdateUser(updateUser);


checks the jira authenticated user. But I'm trying to do this using a cron service.
How can I set a user inactive programmatically ?
Is this allowed ?

Thank you in advance
Francesco

1 answer

0 votes
Radek Dostál
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 11, 2024

If the underlying API uses the logged-in user, you can workaround this using: https://docs.atlassian.com/software/jira/docs/api/9.4.17/com/atlassian/jira/security/JiraAuthenticationContext.html#setLoggedInUser-com.atlassian.jira.user.ApplicationUser-

 

I.e. get the "automation user" via usermanager, set it via JiraAuthenticationContext, continue with rest of the code.

 

Assuming the cron job is using any of Jira's threadpools, you'll probably want to set the logged in user back just in case it could leak to other jobs/tasks. Not really a problem with standalone runnables but theoretically might be if the underlying thread will be re-used by something else, so I'd probably just store whatever current user there is, run the code, then switch it back in finally block.

Francesco R
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, 2024

That's perfect, it works. Thank you very much. My problem was the null authenticatedUser. Now I set it to a service user allowed to do the operation and then, just for cleanliness, I put the null back.

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
8.20.1
TAGS
AUG Leaders

Atlassian Community Events