Forums

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

How to get username of person who had hit the custom build Rest API in my Java code?

Deleted user March 17, 2022

Hi Team,

I have developed a Rest API to fetch data saved in Active Objects database tables.

// To delete all the rules present in database table
@DELETE
@Path("/debug/delete/rules")
public Response deleteAllRules() {

boolean result = rules.deleteAllStoredRules();
if (result) {
     return Response.noContent().build();
}
return Response.status(205).build();
}

Can anyone please help me to find a way to fetch username in my code who had hit this API via postman/any other way?

2 answers

1 accepted

0 votes
Answer accepted
Tuncay Senturk _Snapbytes_
Community Champion
March 18, 2022

Hi @[deleted] 

You can get the logged user as below.

import com.atlassian.jira.security.JiraAuthenticationContext;

private final JiraAuthenticationContext authenticationContext;
ApplicationUser currentUser = authenticationContext.getLoggedInUser();

 Then you can get the user name and store it in a database table for auditing.

Please let me know if I am missing something.

Deleted user March 25, 2022

Hi @Tuncay Senturk _Snapbytes_ ,

Thank you for your reply, Using the above code I am able to get the current user. Can you please help me further in how to check if current user is system admin or not?

Tuncay Senturk _Snapbytes_
Community Champion
March 25, 2022

 

You can use UserManager's isSystemAdmin method to see whether it is a system manager.

See reference: https://docs.atlassian.com/sal-api/3.1.0/sal-api/apidocs/index.html?com/atlassian/sal/api/user/UserManager.html

Tuncay Senturk _Snapbytes_
Community Champion
March 25, 2022

Please mark the answer as accepted if it solved your problem. Thanks.

Deleted user March 25, 2022

Yeah sure. Thank you @Tuncay Senturk _Snapbytes_ 

1 vote
Fabio Racobaldo _Catworkx_
Community Champion
March 17, 2022

Hi @[deleted] ,

in my opinion, the best way is to add your custom AO containg all information about user and data and add in your code AO management so that user that invoked that rest API will be added to AO table.

In your app, moreover, you can add a menu that, at administration level, will display history of REST api calls.

Hope this helps,

Fabio

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events