Dear community,
While using the Nuxeo PHP Automation Client I ran into some issues.
When not using PHP Composer, loading and using the Automation Client is troublesome.
**[SOLVED] by implementing the PSR-0 loader file as listed below. **
I was wondering how to test if a login was successful or not. I could find no description in blogs, documentation or example code (I also checked clients in other languages) how to do this.
See my various attempts below. Either you get an error or regardless of the correctness of credentials you get the a similar object returned.
QUESTION : how can I check this please?
client versus webbrowser issue:
At first I assumed calling a getSession($user,$pass) would suffice, but that seemed not to be the case, so I issued: $answer = $session->newRequest("User.Get")
This request would match with the commandline call: lynx http://localhost:8080/nuxeo/site/automation/User.Get (any webbrowser locally or remotely will do).
The webbrowser will provide a JSON object, the Automation Client provides an error.
QUESTION: why? Are properties needed, why, which ones?
error handling in general:
QUESTION: how are errors handled? Can I tackle them from code?
I see no exceptions being raised, or status calls being returned, or status variables in objects. The subject itself seems also not to be documented or being demonstrated in examples.
Am I missing something here?
autoloader_no_composer.php
</code></pre>
<pre><code><?php
/*
* PSR-0 loader
* auto_loader function when not using Composer
* see: http://stackoverflow.com/questions/22388647/how-to-use-a-php-library-with-namespacing-without-composer-as-dependency
*/
function __autoload($className)
{
$className = ltrim($className, '\\');
$fileName = '';
$namespace = '';
if ($lastNsPos = strripos($className, '\\')) {
$namespace = substr($className, 0, $lastNsPos);
$className = substr($className, $lastNsPos + 1);
$fileName = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
}
$fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
// $fileName .= $className . '.php'; //sometimes you need a custom structure
//require_once "library/class.php"; //or include a class manually
//echo "Attempting to load ".$fileName.PHP_EOL;
?>
test.php :
<?php
/*
*
* https://doc.nuxeo.com/display/NXDOC/PHP+Automation+Client
*
* http://nuxeo.github.io/api-playground/#/commands/User.Get
* http://explorer.nuxeo.com/nuxeo/site/distribution/Nuxeo%20DM-7.10/viewOperation/User.Get
*
* curl -X POST 'http://demo.nuxeo.com/nuxeo/site/automation/User.Get' -H 'Nuxeo-Transaction-Timeout: 3' -H 'X-NXproperties: *' -H 'X-NXRepository: default' -H 'X-NXVoidOperation: false' -d '{"params":{},"context":{}}'
* lynx http://localhost:8080/nuxeo/site/automation/User.Get
*
*/
require_once "autoloader_no_composer.php";
require_once 'Log.php';
$logdebug = true;
$logger = &Log::singleton('console', '',
'PHP',
array('buffering' => true),
PEAR_LOG_DEBUG);
use Nuxeo\Automation\Client;
function testconnection($user, $pass) {
$nuxurl = 'http://localhost:8080/nuxeo/site/automation' ;
$nuxusr = $user ; // prevent SQL injection somehow !!!
$nuxpwd = $pass ; // idem
global $logdebug,$logger;
if ($logdebug) {
$logger->log("login attempt with credentials: ".$nuxusr." | ".$nuxpwd );
}
try {
$client = new \Nuxeo\Automation\Client\NuxeoPhpAutomationClient($nuxurl);
}
catch (Exception $e) {
//echo $e->getMessage(), "\n";
//$logger->log("Nuxeo REST error: ".$e->getMessage());
}
$session = $client->getSession($nuxusr, $nuxpwd);
/*
* detect if the login was successful or not !
* yes : start session
* no : offer try again or offer to create new account
*/
$answer = $session->newRequest("User.Get")
->sendRequest();
// ->set('input', 'void')
//print_r($answer);
# http 200 : 200 Ok
# http 401 : 401 Unauthorized (RFC 7235)
//if (!isset($answer)) {
//if ($answer != 200) {
//if (empty($session)) {
if (!isset($_SERVER['PHP_AUTH_USER'])) {
//$statuscode = $session->getStatusCode();
//if ($statuscode != 200) {
$success = false ;
if ($logdebug) {
$logger->log("Nuxeo REST login failed due to wrong credentials");
//print "Nuxeo REST login failed due to wrong credentials".PHP_EOL;
//$logger->log("Session: ".$session);
//print_r($session);
}
}
else
{
$success = true ;
if ($logdebug) {
//$logger->log("Nuxeo REST login successful");
print "Nuxeo REST login successful".PHP_EOL ;
//$logger->log("Session: ".$session);
//print_r($session);
}
}
return($success);
}
function amisuccessful($status) {
if ($status) {
print "Connection successful";
}
else
{
print "Connection failed";
}
}
amisuccessful(testconnection("Administrator", "Administrator"));
amisuccessful(testconnection("jdfsfds", "dsfdsfskdhfk"));
?>