Have the following:
import com.atlassian.confluence.user.UserAccessor;
import com.atlassian.crowd.embedded.api.CrowdDirectoryService;
import com.atlassian.crowd.embedded.api.CrowdService;
import com.atlassian.crowd.embedded.api.Directory;
import com.atlassian.crowd.embedded.api.OperationType;
import com.atlassian.crowd.embedded.impl.ImmutableUser;
import com.atlassian.user.User;
...
private Long findFirstWritableDirectoryId() {
List<Directory> directories = crowdDirectoryService.findAllDirectories();
for (int i=0; i<directories.size(); i++) {
Directory directory = directories.get(i);
if (directory!=null) {
boolean writable = false;
Set allowedOperations = directory.getAllowedOperations();
Iterator iter = allowedOperations.iterator();
while(iter.hasNext()) {
OperationType operationType = (OperationType)iter.next();
if (operationType == OperationType.CREATE_USER) {
writable = true;
return directory.getId();
}
}
}
}
return null;
}
public User addUser(String userName, String email, String fullName) {
User user = null;
try {
ImmutableUser.Builder userBuilder = new ImmutableUser.Builder();
userBuilder.active(true);
userBuilder.directoryId(findFirstWritableDirectoryId());
userBuilder.displayName(fullName);
userBuilder.emailAddress(email);
userBuilder.name(userName);
com.atlassian.crowd.embedded.api.User cUser = userBuilder.toUser();
String credential = null;
cUser = crowdService.addUser(cUser, credential);
user = userAccessor.getUser(cUser.getName());
} catch (Throwable t) {
log.error("Problem creating user '" + userName + "'", t);
}
return user;
}
But, during upload of the Confluence plugin using that in Confluence 3.5.5, it fails with the following:
ClassNotFoundException: com.atlassian.crowd.embedded.impl.ImmutableUser$Builder
The reason that I'm using userAccessor at the end is because most of my code and GroupManager, etc. requires com.atlassian.user.User.
Any idea how to create a user using the Confluence API or embedded Crowd API in Confluence 3.5.5? If interested, the latest version of plugin code is here.
Community moderators have prevented the ability to post new answers.
Need some more work, but bassicaly it should come to this. Not sure if the permission check is ignored now though ;-)
Also added a check if the current confluence instance is licensed to add more user, seems right!
public User addUser(String username, String email, String fullname) {
if (userAccessor.isLicensedToAddMoreUsers()) {
DefaultUser user = new DefaultUser(username, fullname, email);
User user = createUser(user, createRandomCredential());
personalInformationManager.createPersonalInformation(user);
userChecker.incrementRegisteredUserCount();
eventManager.publishEvent(new UserCreateEvent(this, user));
return user;
} else {
throw new LicensingException("You are not licensed to add any more users to this installation of Confluence. Please contact sales@atlassian.com");
}
}
private User createUser(User userTemplate, Credential credential) {
// add check to prevent duplicated UserNames
try {
crowdService.addUser(toUserTemplate(userTemplate), credential.getValue());
} catch (.....) {
}
return Conversions.TO_ATLASSIAN_USER.apply(crowdUser);
}
private Credential createRandomCredential() {
String randomPassword = SecureRandomStringUtils.getInstance().randomAlphanumericString(22);
return Credential.unencrypted(randomPassword);
}
private UserTemplate toUserTemplate(User atlassianUser) {
UserTemplate template = new UserTemplate(atlassianUser.getName());
template.setDisplayName(atlassianUser.getFullName());
template.setEmailAddress(atlassianUser.getEmail());
template.setActive(true);
return template;
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Honestly, I think the 'UserAccessor' takes care of this all (or I misunderstood your problem :-))
I added this to a simple macro and it seemed to work!
public User addUser(String username, String email, String fullname) {
DefaultUser user = new DefaultUser(username, fullname, email);
try {
User user = userAccessor.createUser(user, Credential.NONE);
userAccessor.addMembership(UserAccessor.GROUP_CONFLUENCE_USERS, username);
return user;
}
catch (LicensingException e) {
....
}
catch (InfrastructureException e) {
....
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.