How to create users programmatically on Confluence Server?

Hi everyone,
How does one create new users on Confluence Server programmatically?

Hei @ChongJingHong

You can make use of the com.atlassian.confluence.user.UserAccessor#createUser method, see javadoc here.

Example imports, service constructor, and code usage would be :

import com.atlassian.confluence.setup.settings.SettingsManager;
import com.atlassian.confluence.user.ConfluenceUser;
import com.atlassian.confluence.user.UserAccessor;
import static com.atlassian.user.security.password.Credential.unencrypted;
import com.atlassian.user.impl.DefaultUser;
...

public SampleService(
            @ComponentImport final UserAccessor userAccessor,
            @ComponentImport final SettingsManager settingsManager
)
...
...
...
final DefaultUser defaultUser = new DefaultUser();
defaultUser.setFullName(format("%s %s", firstName, lastName));
defaultUser.setEmail(email);
defaultUser.setName(userName);

final ConfluenceUser confluenceUser = userAccessor.createUser(
      defaultUser,
      unencrypted(pass)
);

userAccessor.addMembership(
    settingsManager.getGlobalSettings().getDefaultUsersGroup(), 
    userName
);
...

This API might change in the future.

May I ask what your use case is ?

1 Like

Hi @viqueen-hasnae, thanks for your reply. I actually found this answer already but still thank you!