AnsweredAssumed Answered

How to make a newly created user auto join a site?

Question asked by driekhof on Aug 2, 2010
Just getting started with Alfresco, we're evaluating its features and performance.

After a lot of googling, I was finally able to write a Java application that calls the Alfresco web services and creates users on 3.3.  Now I'd like to make each user automatically join a site on Alfresco Share for some automated load testing (using Selenium JUnit stuff).  Could someone point me to some code or documentation for making a user join a site (using Java web service client like code below)?

Here's my code to read users from a properties file and create them.  It actually works, yay!  Thanks to Amita Bhandari whoever she is.


package loadtest.utils;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import org.alfresco.repo.webservice.authentication.AuthenticationFault;
import org.alfresco.webservice.types.NamedValue;
import org.alfresco.webservice.types.Store;
import org.alfresco.webservice.util.AuthenticationUtils;
import org.alfresco.webservice.util.Constants;
import org.alfresco.webservice.util.WebServiceFactory;
import org.alfresco.webservice.administration.NewUserDetails;
/**
* create user through webservice
* @author Amita Bhandari
* @author driekhof
*/
public class TestUserWebServices
{
    public static final String ALFRESCOURL = "http://localhost:8080/alfresco";
    public static Properties props;

    public static void main(String args[])
    {
        if ( args.length == 0 || args[0] == null )
        {
            System.out.println( "Error, missing properties file argument, exiting" );
            System.exit( 1 );
        }
        String propsFilename = args[0];
        try {
            startSession("admin", "admin");
            props = new Properties();
            FileInputStream fis = new FileInputStream( propsFilename );
            props.load(fis);
            fis.close();
            props.list( System.out );
            String[] users = props.getProperty( "users" ).split( ":" );

System.out.println( "number of users to create=" + users.length );

            for ( int u = 0; u < users.length; u++ )
            {
                TestUserWebServices test = new TestUserWebServices();
                test.createUser( users[u] );
            }
        } catch (AuthenticationFault e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private static String startSession(String user, String pass)
    {
        String result = "";
        try {
System.out.println("Connecting to: " + ALFRESCOURL);

            WebServiceFactory.setEndpointAddress(ALFRESCOURL+"/api");
            AuthenticationUtils.startSession( user, pass);
            result = AuthenticationUtils.getTicket();

System.out.println("ticket: " + result);

        } catch(Exception e) {
            System.err.println("Can not initiate session with Alfresco server.");
            e.printStackTrace();
            result = "failed";
        }
        return result;
    }

    private void createUser( String userInfo ) throws Exception
    {
System.out.println( "createUser(): userInfo=" + userInfo );

        String[] ua = userInfo.split( "," );

//testuser1,testuser1,test,user,1,testuser1@localhost.com,somecompany:\

        String userName = ua[0];
        String password = ua[1];
        Store store = new Store( Constants.WORKSPACE_STORE, "SpacesStore" );
        String homeFolder = store.getScheme() + "://" + store.getAddress() + "/app:company_home/app:user_homes"  ;
        NewUserDetails[] newUsers = new NewUserDetails[] {
                new NewUserDetails( userName, password,
                        createPersonProperties(homeFolder, ua[2], ua[3], ua[4], ua[5], ua[6] ))};
        // Create the new users
        WebServiceFactory.getAdministrationService().createUsers(newUsers);
    }

    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) };
    }

}

Outcomes