Confluence Scheduled Job Impersonate User/Admin

Hey all, I am trying to create a scheduled job that runs an action that requires a user being logged in. From what i can tell the JobRunner runs as a null user and I am having difficulty finding the correct method to impersonate a user to be used while running the command.

Does anyone have an example of what the impersonate code for a service would look like in confluence? I have some experience doing this with JIRA but i cannot find the correct class to do the same in Confluence. Should I be using AbstractServiceCommand, RunAsUserCommand, ConfluenceUserManager, and should the user be “admin” or is there an easier way to assume the service to be run as an admin?

Thanks for any and all feedback!

1 Like
UserAccessor userAccessor = ComponentLocator.getComponent(UserAccessor.class);
// As Anonymous
AuthenticatedUserImpersonator.REQUEST_AGNOSTIC.asAnonymousUser(() -> {
    // DO STUFF
// As Admin
ConfluenceUser adminUser = userAccessor.getUserByName("admin");
System.out.println("adminUser is " + adminUser);
AuthenticatedUserImpersonator.REQUEST_AGNOSTIC.asUser(() -> {
    // DO STUFF
}, adminUser);

Thank you, thank you, thank you for this! This solution solved a problem I’ve been working on for the last 3 hours.

1 Like