Sheila's Blog

Software Development

Sheila's Blog header image 2

Alfresco – Create User

April 6th, 2009 by Sheila

Creating a new alfresco user using a web service call requires the following jar files in your classpath: alfresco-web-service-client.jar axis-1.4.jar commons-discovery-0.4.jar commons-logging-1.1.jar jaxrpc.jar opensaml-1.1.jar wsdl4j-1.6.2.jar wss4j.jar xmlsec-1.4.1.jar

Create a java class called AlfrescoCreateUser with a main method public static void main(String[] args) throws Exception { AlfrescoCreateUser alfTest = new AlfrescoCreateUser(); alfTest.createUser(); }

Copy the createPersonProperties method from the AdministrationServiceSystemTest in the web service client code /** * Taken from AdministrationServiceSystemTest in the web service client. */ private NamedValue[] createPersonProperties(String homeFolder, String firstName, String middleName, String lastName, String email, String orgId) { // Create the new user objects return new NamedValue[] { new NamedValue(Constants.PROP_USER_HOMEFOLDER, false, homeFolder, null), new NamedValue(Constants.PROP_USER_FIRSTNAME, false, firstName, null), new NamedValue(Constants.PROP_USER_MIDDLENAME, false, middleName, null), new NamedValue(Constants.PROP_USER_LASTNAME, false, lastName, null), new NamedValue(Constants.PROP_USER_EMAIL, false, email, null), new NamedValue(Constants.PROP_USER_ORGID, false, orgId, null)}; }

You need to set the user properties and get an authentication token using the admin access details. The code below creates a user called test with a home space also called test under Company Home -> User Homes /** * Creates the user test with a home space in Company Home. */ public void createUser() { String login = "test"; String password = "password"; String homeFolder = "://app:company_home/cm:" + login; String userName = "test"; String userMiddlename = ""; String userSurname = "Test"; String userEmail = "test"; String userOrg = ""; try { AuthenticationUtils.startSession("admin", "admin"); AdministrationServiceSoapBindingStub administrationService = WebServiceFactory.getAdministrationService(); NewUserDetails newUser = new NewUserDetails(login, password, createPersonProperties(homeFolder, userName, userMiddlename, userSurname, userEmail, userOrg)); administrationService.createUsers(new NewUserDetails[] { newUser }); } catch (AuthenticationFault f) { System.out.println("Administration Fault"); f.printStackTrace(); } catch (RemoteException e) { System.out.println("RemoteException"); e.printStackTrace(); } finally { AuthenticationUtils.endSession();> } System.out.println("User created successfully"); }

Run the class and check that the user has been created successfully.

Tags:   · No Comments

Leave a Comment

0 responses so far ↓

There are no comments yet...Kick things off by filling out the form below.