Clean code for REST API.
This commit is contained in:
parent
11ae76f4fb
commit
ee3d25d9fa
@ -51,7 +51,6 @@ use \Luracast\Restler\Defaults;
|
||||
$api = new DolibarrApi($db);
|
||||
|
||||
$api->r->addAPIClass('Luracast\\Restler\\Resources'); //this creates resources.json at API Root
|
||||
$api->r->addAPIClass('DolibarrApiInit',''); // Just for url root page
|
||||
$api->r->setSupportedFormats('JsonFormat', 'XmlFormat');
|
||||
$api->r->addAuthenticationClass('DolibarrApiAccess','');
|
||||
|
||||
@ -149,7 +148,6 @@ llxHeader();
|
||||
$linkback='<a href="'.DOL_URL_ROOT.'/admin/modules.php">'.$langs->trans("BackToModuleList").'</a>';
|
||||
print load_fiche_titre($langs->trans("ApiSetup"),$linkback,'title_setup');
|
||||
|
||||
|
||||
// Define $urlwithroot
|
||||
$urlwithouturlroot=preg_replace('/'.preg_quote(DOL_URL_ROOT,'/').'$/i','',trim($dolibarr_main_url_root));
|
||||
$urlwithroot=$urlwithouturlroot.DOL_URL_ROOT; // This is to use external domain name found into config file
|
||||
@ -165,20 +163,35 @@ print $message;
|
||||
print '<br>';
|
||||
print '<br>';
|
||||
|
||||
$oldclass='';
|
||||
|
||||
print $langs->trans("ListOfAvailableAPIs").':<br>';
|
||||
foreach($listofapis['v1'] as $key => $val)
|
||||
{
|
||||
if ($key == 'login') continue;
|
||||
if ($key)
|
||||
{
|
||||
//print $key.' - '.$val['classname'].' - '.$val['fullpath']." - ".DOL_MAIN_URL_ROOT.'/api/index.php/'.strtolower(preg_replace('/Api$/','',$val['classname']))."/xxx<br>\n";
|
||||
$url=$urlwithroot.'/api/index.php/'.$key;
|
||||
$url.='?api_key=token';
|
||||
print img_picto('','object_globe.png').' <a href="'.$url.'" target="_blank">'.$url."</a><br>\n";
|
||||
|
||||
foreach($val as $method => $val2)
|
||||
{
|
||||
$newclass=$val2['className'];
|
||||
if ($oldclass != $newclass)
|
||||
{
|
||||
print "\n<br>\n".$langs->trans("Class").': '.$newclass.'<br>'."\n";
|
||||
$oldclass = $newclass;
|
||||
}
|
||||
//print $key.' - '.$val['classname'].' - '.$val['fullpath']." - ".DOL_MAIN_URL_ROOT.'/api/index.php/'.strtolower(preg_replace('/Api$/','',$val['classname']))."/xxx<br>\n";
|
||||
$url=$urlwithroot.'/api/index.php/'.$key;
|
||||
$url.='?api_key=token';
|
||||
print img_picto('','object_globe.png').' '.$method.' <a href="'.$url.'" target="_blank">'.$url."</a><br>\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
print '<br>';
|
||||
print '<br>';
|
||||
print $langs->trans("OnlyActiveElementsAreExposed", DOL_URL_ROOT.'/admin/modules.php');
|
||||
|
||||
|
||||
llxFooter();
|
||||
$db->close();
|
||||
|
||||
|
||||
@ -100,19 +100,6 @@ print '<u>'.$langs->trans("ApiExporerIs").':</u><br>';
|
||||
$url=DOL_MAIN_URL_ROOT.'/api/admin/explorer.php';
|
||||
print img_picto('','object_globe.png').' <a href="'.$url.'" target="_blank">'.$url."</a><br>\n";
|
||||
|
||||
// API endpoint
|
||||
/*print '<u>'.$langs->trans("ApiEndPointIs").':</u><br>';
|
||||
$url=DOL_MAIN_URL_ROOT.'/api/index.php/xxx/list';
|
||||
print img_picto('','object_globe.png').' <a href="'.$url.'" target="_blank">'.$url."</a><br>\n";
|
||||
$url=DOL_MAIN_URL_ROOT.'/api/xxx/list.json';
|
||||
print img_picto('','object_globe.png').' <a href="'.$url.'" target="_blank">'.$url."</a><br>\n";
|
||||
*/
|
||||
|
||||
print '<br>';
|
||||
print '<br>';
|
||||
print $langs->trans("OnlyActiveElementsAreExposed", DOL_URL_ROOT.'/admin/modules.php');
|
||||
|
||||
|
||||
|
||||
llxFooter();
|
||||
$db->close();
|
||||
|
||||
@ -140,89 +140,3 @@ class DolibarrApi
|
||||
return checkUserAccessToObject(DolibarrApiAccess::$user, $featuresarray,$resource_id,$dbtablename,$feature2,$dbt_keyfield,$dbt_select);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* API init
|
||||
*
|
||||
*/
|
||||
class DolibarrApiInit extends DolibarrApi
|
||||
{
|
||||
|
||||
function __construct() {
|
||||
global $db;
|
||||
$this->db = $db;
|
||||
}
|
||||
|
||||
/**
|
||||
* Login
|
||||
*
|
||||
* Log user with username and password
|
||||
*
|
||||
* @param string $login Username
|
||||
* @param string $password User password
|
||||
* @param int $entity User entity
|
||||
* @return array Response status and user token
|
||||
*
|
||||
* @throws RestException
|
||||
*/
|
||||
public function login($login, $password, $entity = 0) {
|
||||
|
||||
global $conf, $dolibarr_main_authentication, $dolibarr_auto_user;
|
||||
|
||||
// Authentication mode
|
||||
if (empty($dolibarr_main_authentication))
|
||||
$dolibarr_main_authentication = 'http,dolibarr';
|
||||
// Authentication mode: forceuser
|
||||
if ($dolibarr_main_authentication == 'forceuser' && empty($dolibarr_auto_user))
|
||||
$dolibarr_auto_user = 'auto';
|
||||
// Set authmode
|
||||
$authmode = explode(',', $dolibarr_main_authentication);
|
||||
|
||||
include_once DOL_DOCUMENT_ROOT . '/core/lib/security2.lib.php';
|
||||
$login = checkLoginPassEntity($login, $password, $entity, $authmode);
|
||||
if (empty($login))
|
||||
{
|
||||
throw new RestException(403, 'Access denied');
|
||||
}
|
||||
|
||||
// Generate token for user
|
||||
$token = dol_hash($login.uniqid().$conf->global->MAIN_API_KEY,1);
|
||||
|
||||
// We store API token into database
|
||||
$sql = "UPDATE ".MAIN_DB_PREFIX."user";
|
||||
$sql.= " SET api_key = '".$this->db->escape($token)."'";
|
||||
$sql.= " WHERE login = '".$this->db->escape($login)."'";
|
||||
|
||||
dol_syslog(get_class($this)."::login", LOG_DEBUG); // No log
|
||||
$result = $this->db->query($sql);
|
||||
if (!$result)
|
||||
{
|
||||
throw new RestException(500, 'Error when updating user :'.$this->db->error_msg);
|
||||
}
|
||||
|
||||
//return token
|
||||
return array(
|
||||
'success' => array(
|
||||
'code' => 200,
|
||||
'token' => $token,
|
||||
'message' => 'Welcome ' . $login
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get status (Dolibarr version)
|
||||
*
|
||||
* @access protected
|
||||
* @class DolibarrApiAccess {@requires admin}
|
||||
*/
|
||||
function status() {
|
||||
require_once DOL_DOCUMENT_ROOT . '/core/lib/functions.lib.php';
|
||||
return array(
|
||||
'success' => array(
|
||||
'code' => 200,
|
||||
'dolibarr_version' => DOL_VERSION
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
110
htdocs/api/class/api_generic.class.php
Normal file
110
htdocs/api/class/api_generic.class.php
Normal file
@ -0,0 +1,110 @@
|
||||
<?php
|
||||
/* Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
use Luracast\Restler\Restler;
|
||||
use Luracast\Restler\RestException;
|
||||
use Luracast\Restler\Defaults;
|
||||
|
||||
require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
|
||||
require_once DOL_DOCUMENT_ROOT.'/api/class/api.class.php';
|
||||
|
||||
|
||||
/**
|
||||
* API generic (login, status, ...)
|
||||
*
|
||||
*/
|
||||
class GenericApi extends DolibarrApi
|
||||
{
|
||||
|
||||
function __construct() {
|
||||
global $db;
|
||||
$this->db = $db;
|
||||
}
|
||||
|
||||
/**
|
||||
* Login
|
||||
*
|
||||
* Log user with username and password
|
||||
*
|
||||
* @param string $login Username
|
||||
* @param string $password User password
|
||||
* @param int $entity User entity
|
||||
* @return array Response status and user token
|
||||
*
|
||||
* @throws RestException
|
||||
*/
|
||||
public function login($login, $password, $entity = 0) {
|
||||
|
||||
global $conf, $dolibarr_main_authentication, $dolibarr_auto_user;
|
||||
|
||||
// Authentication mode
|
||||
if (empty($dolibarr_main_authentication))
|
||||
$dolibarr_main_authentication = 'http,dolibarr';
|
||||
// Authentication mode: forceuser
|
||||
if ($dolibarr_main_authentication == 'forceuser' && empty($dolibarr_auto_user))
|
||||
$dolibarr_auto_user = 'auto';
|
||||
// Set authmode
|
||||
$authmode = explode(',', $dolibarr_main_authentication);
|
||||
|
||||
include_once DOL_DOCUMENT_ROOT . '/core/lib/security2.lib.php';
|
||||
$login = checkLoginPassEntity($login, $password, $entity, $authmode);
|
||||
if (empty($login))
|
||||
{
|
||||
throw new RestException(403, 'Access denied');
|
||||
}
|
||||
|
||||
// Generate token for user
|
||||
$token = dol_hash($login.uniqid().$conf->global->MAIN_API_KEY,1);
|
||||
|
||||
// We store API token into database
|
||||
$sql = "UPDATE ".MAIN_DB_PREFIX."user";
|
||||
$sql.= " SET api_key = '".$this->db->escape($token)."'";
|
||||
$sql.= " WHERE login = '".$this->db->escape($login)."'";
|
||||
|
||||
dol_syslog(get_class($this)."::login", LOG_DEBUG); // No log
|
||||
$result = $this->db->query($sql);
|
||||
if (!$result)
|
||||
{
|
||||
throw new RestException(500, 'Error when updating user :'.$this->db->error_msg);
|
||||
}
|
||||
|
||||
//return token
|
||||
return array(
|
||||
'success' => array(
|
||||
'code' => 200,
|
||||
'token' => $token,
|
||||
'message' => 'Welcome ' . $login
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get status (Dolibarr version)
|
||||
*
|
||||
* @access protected
|
||||
* @class DolibarrApiAccess {@requires admin}
|
||||
*/
|
||||
function status() {
|
||||
require_once DOL_DOCUMENT_ROOT . '/core/lib/functions.lib.php';
|
||||
return array(
|
||||
'success' => array(
|
||||
'code' => 200,
|
||||
'dolibarr_version' => DOL_VERSION
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -60,7 +60,6 @@ use \Luracast\Restler\Defaults;
|
||||
$api = new DolibarrApi($db);
|
||||
|
||||
$api->r->addAPIClass('Luracast\\Restler\\Resources'); //this creates resources.json at API Root
|
||||
$api->r->addAPIClass('DolibarrApiInit',''); // Just for url root page
|
||||
$api->r->setSupportedFormats('JsonFormat', 'XmlFormat');
|
||||
$api->r->addAuthenticationClass('DolibarrApiAccess','');
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user