Skip to content
Snippets Groups Projects
Commit 396d31e1 authored by Xebax's avatar Xebax
Browse files

REST API: begin the refactoring.

Simplify the code that looks for the API classes and replace the
/contact/ and /user/ APIs by /contacts/ and /users/.
parent d6b9eb1a
No related branches found
No related tags found
No related merge requests found
......@@ -65,8 +65,6 @@ $api->r->addAPIClass('Luracast\\Restler\\Explorer');
$api->r->setSupportedFormats('JsonFormat', 'XmlFormat');
$api->r->addAuthenticationClass('DolibarrApiAccess','');
$listofapis = array();
$modulesdir = dolGetModulesDirs();
foreach ($modulesdir as $dir)
{
......@@ -80,25 +78,19 @@ foreach ($modulesdir as $dir)
{
while (($file = readdir($handle))!==false)
{
if (is_readable($dir.$file) && preg_match("/^(mod.*)\.class\.php$/i",$file,$reg))
if (is_readable($dir.$file) && preg_match("/^mod(.*)\.class\.php$/i",$file,$reg))
{
$modulename=$reg[1];
$module = $part = strtolower($reg[1]);
// Defined if module is enabled
$enabled=true;
$module=$part=$obj=strtolower(preg_replace('/^mod/i','',$modulename));
//if ($part == 'propale') $part='propal';
if ($module == 'societe') {
$obj = 'thirdparty';
}
if ($module == 'categorie') {
$part = 'categories';
$obj = 'category';
}
if ($module == 'facture') {
$part = 'compta/facture';
$obj = 'facture';
}
// Defined if module is enabled
$enabled=true;
if (empty($conf->$module->enabled)) $enabled=false;
if ($enabled)
......@@ -118,17 +110,14 @@ foreach ($modulesdir as $dir)
{
while (($file_searched = readdir($handle_part))!==false)
{
if (is_readable($dir_part.$file_searched) && preg_match("/^(api_.*)\.class\.php$/i",$file_searched,$reg))
if (is_readable($dir_part.$file_searched) && preg_match("/^api_(.*)\.class\.php$/i",$file_searched,$reg))
{
$classname=$reg[1];
$classname = str_replace('Api_','',ucwords($reg[1])).'Api';
$classname = ucfirst($classname);
$classname = ucwords($reg[1]);
require_once $dir_part.$file_searched;
if (class_exists($classname))
if (class_exists($classname))
{
dol_syslog("Found API classname=".$classname);
$api->r->addAPIClass($classname,'');
$listofapis[]=array('classname'=>$classname, 'fullpath'=>$file_searched);
$api->r->addAPIClass($classname);
}
}
}
......
......@@ -20,14 +20,12 @@ use Luracast\Restler\RestException;
//require_once DOL_DOCUMENT_ROOT . '/contact/class/contact.class.php';
/**
* API class for contact object
* API class for contacts
*
* @smart-auto-routing false
* @access protected
* @class DolibarrApiAccess {@requires user,external}
*
*/
class ContactApi extends DolibarrApi
class Contacts extends DolibarrApi
{
/**
*
......@@ -44,9 +42,6 @@ class ContactApi extends DolibarrApi
/**
* Constructor
*
* @url contact/
*
*/
function __construct() {
global $db, $conf;
......@@ -62,7 +57,6 @@ class ContactApi extends DolibarrApi
* @param int $id ID of contact
* @return array|mixed data without useless information
*
* @url GET contact/{id}
* @throws RestException
*/
function get($id) {
......@@ -96,15 +90,10 @@ class ContactApi extends DolibarrApi
* @param int $limit Limit for list
* @param int $page Page number
* @return array Array of contact objects
*
* @url GET /contact/list
* @url GET /contact/list/{socid}
* @url GET /thirdparty/{socid}/contacts
* @url GET /customer/{socid}/contacts
*
* @throws RestException
*/
function getList($socid = 0, $sortfield = "c.rowid", $sortorder = 'ASC', $limit = 0, $page = 0) {
function index($socid = 0, $sortfield = "c.rowid", $sortorder = 'ASC', $limit = 0, $page = 0) {
global $db, $conf;
$obj_ret = array();
......@@ -191,8 +180,6 @@ class ContactApi extends DolibarrApi
*
* @param array $request_data Request datas
* @return int ID of contact
*
* @url POST contact/
*/
function post($request_data = NULL) {
if (!DolibarrApiAccess::$user->rights->societe->contact->creer)
......@@ -215,8 +202,6 @@ class ContactApi extends DolibarrApi
* @param int $id Id of contact to update
* @param array $request_data Datas
* @return int
*
* @url PUT contact/{id}
*/
function put($id, $request_data = NULL) {
if (!DolibarrApiAccess::$user->rights->societe->contact->creer)
......@@ -251,11 +236,9 @@ class ContactApi extends DolibarrApi
*
* @param int $id Contact ID
* @return integer
*
* @url DELETE contact/{id}
*/
function delete($id) {
if (!DolibarrApiAccess::$user->rights->contact->supprimer)
if (!DolibarrApiAccess::$user->rights->societe->contact->supprimer)
{
throw new RestException(401);
}
......@@ -282,7 +265,7 @@ class ContactApi extends DolibarrApi
*/
function _validate($data) {
$contact = array();
foreach (ContactApi::$FIELDS as $field)
foreach (Contacts::$FIELDS as $field)
{
if (!isset($data[$field]))
throw new RestException(400, "$field field missing");
......
......@@ -20,14 +20,12 @@ use Luracast\Restler\RestException;
//require_once DOL_DOCUMENT_ROOT . '/contact/class/contact.class.php';
/**
* API class for user object
* API class for users
*
* @smart-auto-routing false
* @access protected
* @class DolibarrApiAccess {@requires user,external}
*
*/
class UserApi extends DolibarrApi
class Users extends DolibarrApi
{
/**
*
......@@ -44,9 +42,6 @@ class UserApi extends DolibarrApi
/**
* Constructor
*
* @url user/
*
*/
function __construct() {
global $db, $conf;
......@@ -62,7 +57,6 @@ class UserApi extends DolibarrApi
* @param int $id ID of user
* @return array|mixed data without useless information
*
* @url GET user/{id}
* @throws RestException
*/
function get($id) {
......@@ -85,6 +79,7 @@ class UserApi extends DolibarrApi
}
/**
* TODO move to the /contacts/ API
* Create useraccount object from contact
*
* @param int $contactid Id of contact
......@@ -133,8 +128,6 @@ class UserApi extends DolibarrApi
*
* @param array $request_data New user data
* @return int
*
* @url POST user/
*/
function post($request_data = NULL) {
// check user authorization
......@@ -170,8 +163,6 @@ class UserApi extends DolibarrApi
* @param int $id Id of account to update
* @param array $request_data Datas
* @return int
*
* @url PUT user/{id}
*/
function put($id, $request_data = NULL) {
//if (!DolibarrApiAccess::$user->rights->user->user->creer) {
......@@ -194,7 +185,7 @@ class UserApi extends DolibarrApi
$this->useraccount->$field = $value;
}
if ($this->useraccount->update($id, DolibarrApiAccess::$user, 1, '', '', 'update'))
if ($this->useraccount->update(DolibarrApiAccess::$user, 1))
return $this->get($id);
return false;
......@@ -207,9 +198,9 @@ class UserApi extends DolibarrApi
* @param int $group Group ID
* @return int
*
* @url GET user/{id}/setGroup/{group}
* @url GET {id}/setGroup/{group}
*/
function setGroup($id,$group) {
function setGroup($id, $group) {
//if (!DolibarrApiAccess::$user->rights->user->user->supprimer) {
//throw new RestException(401);
//}
......@@ -232,8 +223,6 @@ class UserApi extends DolibarrApi
*
* @param int $id Account ID
* @return array
*
* @url DELETE user/{id}
*/
function delete($id) {
//if (!DolibarrApiAccess::$user->rights->user->user->supprimer) {
......@@ -262,7 +251,7 @@ class UserApi extends DolibarrApi
*/
function _validate($data) {
$account = array();
foreach (UserApi::$FIELDS as $field)
foreach (Users::$FIELDS as $field)
{
if (!isset($data[$field]))
throw new RestException(400, "$field field missing");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment