As is often the case, the point of this blog isn’t so much to explain how to do something complicated. The point is that I’m trying to explain something simple that should be easy to find an answer for, but was not. In this case my question was “what on earth is a UserTemplate (User Template) in Confluence”?
On the surface, it seems like creating a new user in Confluence should be a pretty straightforward process. There’s a UserAccessor class, and that class has a createUser() method. However, the expected inputs to that method are a User Template object and a Credentials object. From the class documentation:
-
createUser
ConfluenceUser createUser(com.atlassian.user.User userTemplate, com.atlassian.user.security.password.Credential password)
The import required to work with Credential is spelled out for us, but the userTemplate is a different story. There’s virtually no documentation on what that means, and no amount of Googling “Confluence User Template”, “UserTemplate”, “Confluence Create User Template” will actually tell you what to do. Part of the issue is that “template” means several different things in the context of Confluence, so that muddies the waters.
Let’s cut to the chase. Here’s the code that I eventually came up with:
import com.atlassian.confluence.api.model.people.User
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.confluence.user.UserAccessor
import com.atlassian.user.impl.DefaultUser
import com.atlassian.user.security.password.Credential
def userAccessor = ComponentLocator.getComponent(UserAccessor)
def user = userAccessor.createUser(new DefaultUser("kenm", "Ken McClean", "ken@email.com"), Credential.unencrypted("password"));
return user
The UserTemplate that we’re working with is the DefaultUser object. We import it using the com.atlassian.user.impl
package, of which Default User is the only class. So it’s not like the package is going to tell us how to work with other user templates.
When we call the createUser() method, we define the UserTemplate at the same time that we declare it. So we start with a base of the DefaultUser, and supply the necessary values to create a user.
You could also create a user with UserService.CreateUserRequest, but then you’ll need to figure out the validation. Not difficult, just a different approach.
Leave a Reply