2
0
forked from Wavyzz/dolibarr

Fix API explorer was broken due to corrupted API

This commit is contained in:
Laurent Destailleur
2019-03-06 18:04:36 +01:00
parent 0c2a0c643b
commit 401ad65975
3 changed files with 55 additions and 58 deletions

View File

@@ -18,12 +18,11 @@
use Luracast\Restler\RestException; use Luracast\Restler\RestException;
dol_include_once('/bom/class/bom.class.php'); require_once DOL_DOCUMENT_ROOT.'/bom/class/bom.class.php';
/** /**
* \file bom/class/api_bom.class.php * \file bom/class/api_boms.class.php
* \ingroup bom * \ingroup bom
* \brief File for API management of bom. * \brief File for API management of bom.
*/ */
@@ -31,36 +30,32 @@ dol_include_once('/bom/class/bom.class.php');
/** /**
* API class for bom bom * API class for bom bom
* *
* @smart-auto-routing false
* @access protected * @access protected
* @class DolibarrApiAccess {@requires user,external} * @class DolibarrApiAccess {@requires user,external}
*/ */
class BillOfMaterialsApi extends DolibarrApi class BOMs extends DolibarrApi
{ {
/** /**
* @var array $FIELDS Mandatory fields, checked when create and update object * @var array $FIELDS Mandatory fields, checked when create and update object
*/ */
static $FIELDS = array( static $FIELDS = array(
'name', 'label'
); );
/** /**
* @var BillOfMaterials $bom {@type BillOfMaterials} * @var BOM $bom {@type BOM}
*/ */
public $bom; public $bom;
/** /**
* Constructor * Constructor
*
* @url GET /
*
*/ */
public function __construct() public function __construct()
{ {
global $db, $conf; global $db, $conf;
$this->db = $db; $this->db = $db;
$this->bom = new BillOfMaterials($this->db); $this->bom = new BOM($this->db);
} }
/** /**
@@ -71,7 +66,7 @@ class BillOfMaterialsApi extends DolibarrApi
* @param int $id ID of bom * @param int $id ID of bom
* @return array|mixed data without useless information * @return array|mixed data without useless information
* *
* @url GET boms/{id} * @url GET {id}
* @throws RestException * @throws RestException
*/ */
public function get($id) public function get($id)
@@ -82,7 +77,7 @@ class BillOfMaterialsApi extends DolibarrApi
$result = $this->bom->fetch($id); $result = $this->bom->fetch($id);
if( ! $result ) { if( ! $result ) {
throw new RestException(404, 'BillOfMaterials not found'); throw new RestException(404, 'BOM not found');
} }
if( ! DolibarrApi::_checkAccessToResource('bom', $this->bom->id)) { if( ! DolibarrApi::_checkAccessToResource('bom', $this->bom->id)) {
@@ -106,8 +101,6 @@ class BillOfMaterialsApi extends DolibarrApi
* @return array Array of order objects * @return array Array of order objects
* *
* @throws RestException * @throws RestException
*
* @url GET /boms/
*/ */
public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '') public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '')
{ {
@@ -134,7 +127,7 @@ class BillOfMaterialsApi extends DolibarrApi
//if ($mode == 1) $sql.= " AND s.client IN (1, 3)"; //if ($mode == 1) $sql.= " AND s.client IN (1, 3)";
//if ($mode == 2) $sql.= " AND s.client IN (2, 3)"; //if ($mode == 2) $sql.= " AND s.client IN (2, 3)";
$tmpobject = new BillOfMaterials($db); $tmpobject = new BOM($db);
if ($tmpobject->ismultientitymanaged) $sql.= ' AND t.entity IN ('.getEntity('bom').')'; if ($tmpobject->ismultientitymanaged) $sql.= ' AND t.entity IN ('.getEntity('bom').')';
if ($restictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) $sql.= " AND t.fk_soc = sc.fk_soc"; if ($restictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) $sql.= " AND t.fk_soc = sc.fk_soc";
if ($restictonsocid && $socid) $sql.= " AND t.fk_soc = ".$socid; if ($restictonsocid && $socid) $sql.= " AND t.fk_soc = ".$socid;
@@ -172,7 +165,7 @@ class BillOfMaterialsApi extends DolibarrApi
while ($i < $num) while ($i < $num)
{ {
$obj = $db->fetch_object($result); $obj = $db->fetch_object($result);
$bom_static = new BillOfMaterials($db); $bom_static = new BOM($db);
if($bom_static->fetch($obj->rowid)) { if($bom_static->fetch($obj->rowid)) {
$obj_ret[] = $this->_cleanObjectDatas($bom_static); $obj_ret[] = $this->_cleanObjectDatas($bom_static);
} }
@@ -193,8 +186,6 @@ class BillOfMaterialsApi extends DolibarrApi
* *
* @param array $request_data Request datas * @param array $request_data Request datas
* @return int ID of bom * @return int ID of bom
*
* @url POST boms/
*/ */
public function post($request_data = null) public function post($request_data = null)
{ {
@@ -208,7 +199,7 @@ class BillOfMaterialsApi extends DolibarrApi
$this->bom->$field = $value; $this->bom->$field = $value;
} }
if( ! $this->bom->create(DolibarrApiAccess::$user)) { if( ! $this->bom->create(DolibarrApiAccess::$user)) {
throw new RestException(500); throw new RestException(500, "Error creating BOM", array_merge(array($this->bom->error), $this->bom->errors));
} }
return $this->bom->id; return $this->bom->id;
} }
@@ -218,9 +209,8 @@ class BillOfMaterialsApi extends DolibarrApi
* *
* @param int $id Id of bom to update * @param int $id Id of bom to update
* @param array $request_data Datas * @param array $request_data Datas
* @return int
* *
* @url PUT boms/{id} * @return int
*/ */
public function put($id, $request_data = null) public function put($id, $request_data = null)
{ {
@@ -230,7 +220,7 @@ class BillOfMaterialsApi extends DolibarrApi
$result = $this->bom->fetch($id); $result = $this->bom->fetch($id);
if( ! $result ) { if( ! $result ) {
throw new RestException(404, 'BillOfMaterials not found'); throw new RestException(404, 'BOM not found');
} }
if( ! DolibarrApi::_checkAccessToResource('bom', $this->bom->id)) { if( ! DolibarrApi::_checkAccessToResource('bom', $this->bom->id)) {
@@ -238,46 +228,49 @@ class BillOfMaterialsApi extends DolibarrApi
} }
foreach($request_data as $field => $value) { foreach($request_data as $field => $value) {
if ($field == 'id') continue;
$this->bom->$field = $value; $this->bom->$field = $value;
} }
if($this->bom->update($id, DolibarrApiAccess::$user)) if($this->bom->update($id, DolibarrApiAccess::$user) > 0)
{
return $this->get($id); return $this->get($id);
}
return false; else
{
throw new RestException(500, $this->commande->error);
}
} }
/** /**
* Delete bom * Delete bom
* *
* @param int $id BillOfMaterials ID * @param int $id BOM ID
* @return array * @return array
*
* @url DELETE bom/{id}
*/ */
public function delete($id) public function delete($id)
{ {
if(! DolibarrApiAccess::$user->rights->bom->delete) { if (! DolibarrApiAccess::$user->rights->bom->delete) {
throw new RestException(401); throw new RestException(401);
} }
$result = $this->bom->fetch($id); $result = $this->bom->fetch($id);
if( ! $result ) { if (! $result) {
throw new RestException(404, 'BillOfMaterials not found'); throw new RestException(404, 'BOM not found');
} }
if( ! DolibarrApi::_checkAccessToResource('bom', $this->bom->id)) { if (! DolibarrApi::_checkAccessToResource('bom', $this->bom->id)) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
} }
if( !$this->bom->delete(DolibarrApiAccess::$user, 0)) if (! $this->bom->delete(DolibarrApiAccess::$user))
{ {
throw new RestException(500); throw new RestException(500, 'Error when deleting BOM : '.$this->bom->error);
} }
return array( return array(
'success' => array( 'success' => array(
'code' => 200, 'code' => 200,
'message' => 'BillOfMaterials deleted' 'message' => 'BOM deleted'
) )
); );
} }
@@ -287,8 +280,8 @@ class BillOfMaterialsApi extends DolibarrApi
/** /**
* Clean sensible object datas * Clean sensible object datas
* *
* @param object $object Object to clean * @param object $object Object to clean
* @return array Array of cleaned object properties * @return array Array of cleaned object properties
*/ */
protected function _cleanObjectDatas($object) protected function _cleanObjectDatas($object)
{ {
@@ -308,15 +301,15 @@ class BillOfMaterialsApi extends DolibarrApi
/** /**
* Validate fields before create or update object * Validate fields before create or update object
* *
* @param array $data Data to validate * @param array $data Array of data to validate
* @return array * @return array
* *
* @throws RestException * @throws RestException
*/ */
private function _validate($data) private function _validate($data)
{ {
$bom = array(); $bom = array();
foreach (BillOfMaterialsApi::$FIELDS as $field) { foreach (BOMs::$FIELDS as $field) {
if (!isset($data[$field])) if (!isset($data[$field]))
throw new RestException(400, "$field field missing"); throw new RestException(400, "$field field missing");
$bom[$field] = $data[$field]; $bom[$field] = $data[$field];

View File

@@ -16,9 +16,9 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
use Luracast\Restler\RestException; use Luracast\Restler\RestException;
require_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php'; require_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
/** /**
* API class for orders * API class for orders
@@ -544,7 +544,7 @@ class Orders extends DolibarrApi
} }
if( ! $this->commande->delete(DolibarrApiAccess::$user)) { if( ! $this->commande->delete(DolibarrApiAccess::$user)) {
throw new RestException(500, 'Error when delete order : '.$this->commande->error); throw new RestException(500, 'Error when deleting order : '.$this->commande->error);
} }
return array( return array(

View File

@@ -31,7 +31,6 @@ dol_include_once('/mymodule/class/myobject.class.php');
/** /**
* API class for mymodule myobject * API class for mymodule myobject
* *
* @smart-auto-routing false
* @access protected * @access protected
* @class DolibarrApiAccess {@requires user,external} * @class DolibarrApiAccess {@requires user,external}
*/ */
@@ -205,7 +204,7 @@ class MyModuleApi extends DolibarrApi
$this->myobject->$field = $value; $this->myobject->$field = $value;
} }
if( ! $this->myobject->create(DolibarrApiAccess::$user)) { if( ! $this->myobject->create(DolibarrApiAccess::$user)) {
throw new RestException(500); throw new RestException(500, "Error creating MyObject", array_merge(array($this->myobject->error), $this->myobject->errors));
} }
return $this->myobject->id; return $this->myobject->id;
} }
@@ -235,13 +234,18 @@ class MyModuleApi extends DolibarrApi
} }
foreach($request_data as $field => $value) { foreach($request_data as $field => $value) {
if ($field == 'id') continue;
$this->myobject->$field = $value; $this->myobject->$field = $value;
} }
if($this->myobject->update($id, DolibarrApiAccess::$user)) if ($this->myobject->update($id, DolibarrApiAccess::$user) > 0)
{
return $this->get($id); return $this->get($id);
}
return false; else
{
throw new RestException(500, $this->myobject->error);
}
} }
/** /**
@@ -254,21 +258,21 @@ class MyModuleApi extends DolibarrApi
*/ */
public function delete($id) public function delete($id)
{ {
if(! DolibarrApiAccess::$user->rights->myobject->delete) { if (! DolibarrApiAccess::$user->rights->myobject->delete) {
throw new RestException(401); throw new RestException(401);
} }
$result = $this->myobject->fetch($id); $result = $this->myobject->fetch($id);
if( ! $result ) { if (! $result) {
throw new RestException(404, 'MyObject not found'); throw new RestException(404, 'MyObject not found');
} }
if( ! DolibarrApi::_checkAccessToResource('myobject', $this->myobject->id)) { if (! DolibarrApi::_checkAccessToResource('myobject', $this->myobject->id)) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
} }
if( !$this->myobject->delete(DolibarrApiAccess::$user, 0)) if (! $this->myobject->delete(DolibarrApiAccess::$user))
{ {
throw new RestException(500); throw new RestException(500, 'Error when deleting MyObject : '.$this->myobject->error);
} }
return array( return array(
@@ -305,10 +309,10 @@ class MyModuleApi extends DolibarrApi
/** /**
* Validate fields before create or update object * Validate fields before create or update object
* *
* @param array $data Data to validate * @param array $data Array of data to validate
* @return array * @return array
* *
* @throws RestException * @throws RestException
*/ */
private function _validate($data) private function _validate($data)
{ {