Hello everybody,
I am building a windows-based-tool to delegate user-managament in Jira to a small team, that does not have admin-permissions in Jira. The tool speaks to a Jira-Scriptrunner-REST-endpoint. The script behind the endpoint does a user-context-switch, then runs as an jira-admin, does some admin-tasks, then switches back to the original-user.
I came up with this, with muchhelp from the community and the adaptavist scriptrunner library.
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.util.UserManager
import com.atlassian.jira.security.JiraAuthenticationContext
import com.atlassian.jira.user.ApplicationUser
// ... some endpoint-stuff clipped ...
final AdminUserName = "someadminusername"
UserManager userManager = ComponentAccessor.getUserManager()
JiraAuthenticationContext authContext = ComponentAccessor.getJiraAuthenticationContext();
// preserve user-context
ApplicationUser originalUser = authContext.getLoggedInUser();
try
{
// switch user-context
ApplicationUser adminUser = userManager.getUserByName(AdminUserName)
if(adminUser==null)
{
throw new Exception("Can't switch context to user ${AdminUserName}: User not found!")
}
authContext.setLoggedInUser(adminUser);
// do admin-work
// ...
}
catch(Exception e)
{
errorMessage = "Fehler: " + e.message;
}
finally
{
// restore user-context
authContext.setLoggedInUser(originalUser);
}
This works well.
Now my question: With scriptrunner for confluence I would like to do the same: Create an endpoint that internally does a user-context-switch.
But the confluence-Java-API is completely different. I couldn't find any samples to switch user-context.
Any pointers to how I might proceed?
Best regards
Axel