AnsweredAssumed Answered

Add New User w/Curl or some other API

Question asked by szeidner on Sep 16, 2008
Has anyone been able to use an API to add a new user account? I am trying to use curl to do this since the call will be from PHP. I have used Live HTTPHeaders (firefox plugin) to figure out what headers need to be sent to Alfresco to log in and create a new user. I am successfully able to log in, but I can't create a new user even though I am sending the exact same headers that Live HTTPHeaders gives me and am also carrying the cookies along through the session. Here is the error I get from alfresco when trying to create the new user:

javax.faces.FacesException: org.apache.jasper.JasperException
caused by:
org.apache.jasper.JasperException
caused by:
java.lang.NullPointerException

Here is my curl code (somewhat messy and redundant in spots since I've been trying to troubleshoot this thing, but you should get the idea). Does anyone have thoughts on why this might be happening, or is there a much better way to add a new user through an API?

<?php

$useragent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1";
$referrer="http://dot2network.com:8080/alfresco/";

// login information
$username="user";
$password="password";
$loginurl="http://myserver.com:8080/alfresco/faces/jsp/login.jsp";
$cookie_file_path = "/home/dot2net/public_html/test/cookie".rand();
$postfields = 'loginForm%3Auser-name='.$username.'&loginForm%3Auser-password='.$password.'&loginForm%3Alanguage=en_US&loginForm%3Asubmit=Login&loginForm_SUBMIT=1&loginForm%3A_idcl=&loginForm%3A_link_hidden_=&javax.faces.ViewState=rO0ABXVyABNbTGphdmEubGFuZy5PYmplY3Q7kM5YnxBzKWwCAAB4cAAAAAN0AAExcHQADi9qc3AvbG9naW4uanNw';

// request to first get cookies, etc…
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $loginurl);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
echo curl_exec($ch);
curl_close($ch);
// do the actual login
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $loginurl);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
echo curl_exec($ch);
curl_close($ch);



//———- Now lets's add the new user ———//
// set variables
$user_first = 'Cheese';
$user_last = 'Monkey';
$user_email = 'cheese%40monkey.com';
$user_username = 'cmonkey';
$user_password = 'cmonkeypass';
$newuserurl1 = 'http://myserver.com:8080/alfresco/faces/jsp/dashboards/container.jsp';
$newuserurl2 = 'http://myserver.com:8080/alfresco/faces/jsp/dialog/container.jsp';
$newuserurl3 = 'http://myserver.com:8080/alfresco/faces/jsp/wizard/container.jsp';

// set the post strings (there are 3 parts to the user registration)
$newuserpost1 = 'dashboard%3Asearch%3A_option=0&dashboard%3Asearch=&dashboard_SUBMIT=1&id=&dashboard%3A_idJsp7=&dashboard%3Aact=dashboard%3Aalf_admin_console&dashboard%3Asidebar-body%3Anavigator=&dashboard%3Amodelist=&javax.faces.ViewState=rO0ABXVyABNbTGphdmEubGFuZy5PYmplY3Q7kM5YnxBzKWwCAAB4cAAAAAN0AAExcHQAGS9qc3AvZGlhbG9nL2NvbnRhaW5lci5qc3A%3D';
$newuserpost2 = 'dialog%3Asearch%3A_option=0&dialog%3Asearch=&dialog_SUBMIT=1&id=&dialog%3Aact=dialog%3Adialog-body%3A_idJsp13&dialog%3Asidebar-body%3Anavigator=&dialog%3A_link_hidden_=&dialog%3A_idcl=&dialog%3A_idJsp7=&dialog%3Amodelist=&javax.faces.ViewState=rO0ABXVyABNbTGphdmEubGFuZy5PYmplY3Q7kM5YnxBzKWwCAAB4cAAAAAN0AAEycHQAGS9qc3AvZGlhbG9nL2NvbnRhaW5lci5qc3A%3D';
$newuserpost3 = 'dialog%3Asearch%3A_option=0&dialog%3Asearch=&dialog%3Adialog-body%3Asearch-text=&dialog_SUBMIT=1&id=&dialog%3Aact=dialog%3Acreate_userid_2413&dialog%3Asidebar-body%3Anavigator=&dialog%3A_link_hidden_=&dialog%3A_idcl=&dialog%3A_idJsp7=&dialog%3Adialog-body%3Ausers-list%3Asort=&dialog%3Amodelist=&javax.faces.ViewState=rO0ABXVyABNbTGphdmEubGFuZy5PYmplY3Q7kM5YnxBzKWwCAAB4cAAAAAN0AAEzcHQAGS9qc3AvZGlhbG9nL2NvbnRhaW5lci5qc3A%3D';
$newuserpost4 = 'wizard%3Asearch%3A_option=0&wizard%3Asearch=&wizard%3Awizard-body%3AfirstName='.$user_first.'&wizard%3Awizard-body%3AlastName='.$user_last.'&wizard%3Awizard-body%3Aemail='.$user_email.'&wizard%3Awizard-body%3A_idJsp31=&wizard%3Awizard-body%3Aorganisation=&wizard%3Awizard-body%3Ajobtitle=&wizard%3Awizard-body%3Alocation=&wizard%3Awizard-body%3A_idJsp36=&wizard%3Awizard-body%3A_idJsp41=&wizard%3Awizard-body%3A_idJsp44=&wizard%3Awizard-body%3A_idJsp45=gigabyte&wizard%3Anext-button=Next&wizard_SUBMIT=1&id=&wizard%3Aact=&wizard%3Asidebar-body%3Anavigator=&wizard%3A_idJsp7=&wizard%3Amodelist=&wizard%3A_link_hidden_=&wizard%3A_idcl=&javax.faces.ViewState=rO0ABXVyABNbTGphdmEubGFuZy5PYmplY3Q7kM5YnxBzKWwCAAB4cAAAAAN0AAE0cHQAGS9qc3Avd2l6YXJkL2NvbnRhaW5lci5qc3A%3D';
$newuserpost5 = 'wizard%3Asearch%3A_option=0&wizard%3Asearch=&wizard%3Awizard-body%3AuserName='.$user_username.'&wizard%3Awizard-body%3Apassword='.$user_password.'&wizard%3Awizard-body%3Aconfirm='.$user_password.'&spaceSelector-value=workspace%3A%2F%2FSpacesStore%2Fb6007bf7-1353-45a5-ab7f-dcce23fcdbb5&wizard%3Awizard-body%3AhomeSpaceName=&wizard%3Anext-button=Next&wizard_SUBMIT=1&id=&wizard%3Aact=&wizard%3Asidebar-body%3Anavigator=&wizard%3A_idJsp7=&wizard%3Amodelist=&wizard%3A_link_hidden_=&wizard%3A_idcl=&javax.faces.ViewState=rO0ABXVyABNbTGphdmEubGFuZy5PYmplY3Q7kM5YnxBzKWwCAAB4cAAAAAN0AAE1cHQAGS9qc3Avd2l6YXJkL2NvbnRhaW5lci5qc3A%3D';
$newuserpost6 = 'wizard%3Asearch%3A_option=0&wizard%3Asearch=&wizard%3Afinish-button=Finish&wizard_SUBMIT=1&id=&wizard%3Aact=&wizard%3Asidebar-body%3Anavigator=&wizard%3A_idJsp7=&wizard%3Amodelist=&wizard%3A_link_hidden_=&wizard%3A_idcl=&javax.faces.ViewState=rO0ABXVyABNbTGphdmEubGFuZy5PYmplY3Q7kM5YnxBzKWwCAAB4cAAAAAN0AAE2cHQAGS9qc3Avd2l6YXJkL2NvbnRhaW5lci5qc3A%3D';

// Post 1
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $newuserurl1);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $newuserpost1);
echo curl_exec($ch);
curl_close($ch);


// Post 2
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $newuserurl2);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, rawurlencode(rawurldecode($newuserpost2)));
//echo curl_exec($ch);
curl_close($ch);

// Post 3
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $newuserurl2);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, rawurlencode(rawurldecode($newuserpost3)));
//echo curl_exec($ch);
curl_close($ch);

// Post 4
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $newuserurl3);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, rawurlencode(rawurldecode($newuserpost4)));
echo curl_exec($ch);
curl_close($ch);



// Post 5
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $newuserurl3);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $newuserpost5);
echo curl_exec($ch);
curl_close($ch);


// Post 6
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $newuserurl3);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $newuserpost6);
echo curl_exec($ch);
curl_close($ch);
//echo $postfields."<br/>".$newuserpost1."<br/>".$newuserpost2."<br/>".$newuserpost3."<br/>".$newuserpost4."<br/>".$newuserpost5."<br/>".$newuserpost6;
echo rawurldecode($postfields);

?>

Outcomes