Nuevo usuario desde SHARE

cancel
Showing results for 
Search instead for 
Did you mean: 
bprados
Member II

Nuevo usuario desde SHARE

Hola,

tengo una clase que sobreescribe el doLogin de alfresco share, extendiendo a LoginController. En ella, valido si el usuario existe en un sistema externo y si el usuario existe, quiero que se cree automáticamente en alfresco, pero al ser SHARE no lo consigo; desde alfresco lo tengo listo haciendo uso de authenticationService pero para SHARE no tengo dicho servicio disponible.

¿alguien me orienta un poco sobre cómo hacerlo?
2 Replies
angelborroy
Alfresco Employee
bprados
Member II

Re: Nuevo usuario desde SHARE

Buenas, comparto cómo he conseguido crear un usuario en SHARE a través de webscript desde JAVA

import java.io.IOException;
import org.apache.axis.utils.StringUtils;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.google.gson.Gson;


public class ShareUserUtils {
   private static final Log logger = LogFactory.getLog(ShareUserUtils.class);
   private static String superAdminUser = "admin";
        private static String superAdminPassword = "admin";
   private static String ALFRESCO_URL = "http://localhost:8080/alfresco";
   public static void addUser(String userName, String password, String firstName, String lastName, String email, String disableAccount, String quota, String[] groups) {
      String ticket = "";
      try {
         ticket = login();

         HttpClient client = new HttpClient();
         String urlString = ALFRESCO_URL + "/service/api/people?alf_ticket=" + ticket;
         PostMethod mPost = new PostMethod(urlString);
         UserDetails objUserDetails = new UserDetails();

         objUserDetails.setUserName(userName);

         objUserDetails.setPassword(password);

         objUserDetails.setFirstName(firstName);

         objUserDetails.setLastName(lastName);

         objUserDetails.setEmail(email);

         objUserDetails.setGroups(groups);

         objUserDetails.setQuota(quota);

         StringRequestEntity requestEntity = new StringRequestEntity(

         getJSONString(objUserDetails),

         "application/json",

         "UTF-8");

         mPost.setRequestEntity(requestEntity);
         int statusCode = client.executeMethod(mPost);
         mPost.releaseConnection();

      } catch (Exception e) {
         System.out.println(e);
         System.out.println(e.getMessage());
      } finally {
         if (!StringUtils.isEmpty(ticket))
            logout(ticket);
      }
   }

   private static String getJSONString(Object obj)

   {

      Gson gson = new Gson();

      return gson.toJson(obj);

   }

   private static String login() {

      String urlString = ALFRESCO_URL + "/service/api/login?u=" + superAdminUser + "&pw=" + superAdminPassword;
      String ticket = "";
      int statusCode = -1;
      try {
         HttpClient client = new HttpClient();
         GetMethod method = new GetMethod(urlString);
         statusCode = client.executeMethod(method);
         if (statusCode == HttpStatus.SC_OK) {
            System.out.print(method.getResponseBody());
            System.out.print(method.getResponseBodyAsString());
            int inicio = method.getResponseBodyAsString().indexOf("TICKET_");
            int fin = method.getResponseBodyAsString().indexOf("</ticket>");
            ticket = method.getResponseBodyAsString().substring(inicio, fin);
         } else {
            logger.error("Se ha producido un error al hacer login. statusCode no es correcto" + statusCode);
         }
      } catch (HttpException e) {
         logger.error(e.getMessage());
      } catch (IOException e) {
         logger.error(e.getMessage());
      }
      return ticket;
   }

   private static String logout(String ticket) {

      String urlString = ALFRESCO_URL + "/service/api/login/ticket/" + ticket + "?alf_ticket=" + ticket;
      int statusCode = -1;
      try {
         HttpClient client = new HttpClient();
         GetMethod method = new GetMethod(urlString);
         statusCode = client.executeMethod(method);
         if (statusCode == HttpStatus.SC_OK) {
            logger.debug("Logout realizado");
         } else {
            logger.error("Se ha producido un error al hacer logout. statusCode no es correcto" + statusCode + method.getResponseBodyAsString());
         }
      } catch (HttpException e) {
         logger.error(e.getMessage());
      } catch (IOException e) {
         logger.error(e.getMessage());
      }
      return ticket;
   }

   public static void main(String args[]) throws IOException {

      String[] groups = new String[1];
      groups[0] = "GROUP_MI_GRUPO";
      addUser("testuser2", "testpassword2", "testfirst2", "testlast", "testemail@test.com", "false", "209715200", groups);
   }
   
}

class UserDetails {

   private String userName;

   private String password;

   private String firstName;

   private String lastName;

   private String email;

   private String disableAccount;

   private String quota;

   private String[] groups;

   public String getUserName() {

      return userName;

   }

   public void setUserName(String userName) {

      this.userName = userName;

   }

   public String getPassword() {

      return password;

   }

   public void setPassword(String password) {

      this.password = password;

   }

   public String getFirstName() {

      return firstName;

   }

   public void setFirstName(String firstName) {

      this.firstName = firstName;

   }

   public String getLastName() {

      return lastName;

   }

   public void setLastName(String lastName) {

      this.lastName = lastName;

   }

   public String getEmail() {

      return email;

   }

   public void setEmail(String email) {

      this.email = email;

   }

   public String getDisableAccount() {

      return disableAccount;

   }

   public void setDisableAccount(String disableAccount) {

      this.disableAccount = disableAccount;

   }

   public String getQuota() {

      return quota;

   }

   public void setQuota(String quota) {

      this.quota = quota;

   }

   public String[] getGroups() {


      return groups;

   }

   public void setGroups(String[] groups) {

      this.groups = groups;

   }

}


Son necesarias las librerías commons-httpclient 3.1 y com.google.code.gson 1.7.1