Creating/registering new user in Crowd with Crowd REST java client

Put the dependecy in your pom.xml

<dependency>
<groupId>com.atlassian.crowd.client</groupId>
<artifactId>atlassian-crowd-rest-client</artifactId>
<version>1.1</version>
</dependency>

In your java code:
  1. import com.atlassian.crowd.embedded.api.PasswordCredential;
  2. import com.atlassian.crowd.exception.ApplicationPermissionException;
  3. import com.atlassian.crowd.exception.GroupNotFoundException;
  4. import com.atlassian.crowd.exception.InvalidAuthenticationException;
  5. import com.atlassian.crowd.exception.OperationFailedException;
  6. import com.atlassian.crowd.model.user.*;
  7. import com.atlassian.crowd.exception.UserNotFoundException;
  8. import com.atlassian.crowd.service.client.CrowdClient;
  9. import com.atlassian.crowd.integration.rest.service.factory.RestCrowdClientFactory;
  10. import com.atlassian.crowd.service.client.ClientPropertiesImpl;
  11. import com.atlassian.crowd.service.client.ClientResourceLocator;

public static int registerNewUser(String userName, String emailAddress, String password) {
//your crowd server settings
   String url = "http://auth.staging.company.com/crowd/";//crowd base url
   String applicationName = "appincrowd";
   String applicationPass = "apppassword";
   String groupName = "usergroup";//a user group under the app
   

   CrowdClient client = new RestCrowdClientFactory().newInstance(url, applicationName, applicationPass);
//initialze crowd client

   UserTemplate ut = new UserTemplate(userName);

   ut.setActive(true);//make it active
   ut.setEmailAddress(emailAddress);
   ut.setName(userName);
   PasswordCredential p = new PasswordCredential(password, false);
   client.addUser(ut, p);
   client.addUserToGroup(userName, groupName);//now add user to that group
}


Setting Crowd configuration with properties :
Instead of hard-coded crowd configuration we should read them from a properties file:
Properties crowdProp=//properties from your bean
 
   ClientPropertiesImpl newCrowdClient=ClientPropertiesImpl.newInstanceFromProperties(crowdProp);
   CrowdClient client = new RestCrowdClientFactory().newInstance(newCrowdClient);
 

No comments:

Post a Comment