2
0
forked from Wavyzz/dolibarr

Debug module builder for generation of APIs

This commit is contained in:
Laurent Destailleur
2019-05-02 12:03:14 +02:00
parent 3a8d645fb9
commit 4756499a81
7 changed files with 219 additions and 101 deletions

View File

@@ -36,14 +36,6 @@ dol_include_once('/mymodule/class/myobject.class.php');
*/
class MyModuleApi extends DolibarrApi
{
/**
* @var array $FIELDS Mandatory fields, checked when create and update object
*/
static $FIELDS = array(
'name',
);
/**
* @var MyObject $myobject {@type MyObject}
*/
@@ -75,7 +67,7 @@ class MyModuleApi extends DolibarrApi
*/
public function get($id)
{
if(! DolibarrApiAccess::$user->rights->myobject->read) {
if(! DolibarrApiAccess::$user->rights->mymodule->read) {
throw new RestException(401);
}
@@ -84,8 +76,8 @@ class MyModuleApi extends DolibarrApi
throw new RestException(404, 'MyObject not found');
}
if( ! DolibarrApi::_checkAccessToResource('myobject', $this->myobject->id)) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
if( ! DolibarrApi::_checkAccessToResource('myobject', $this->myobject->id, 'mymodule_myobject')) {
throw new RestException(401, 'Access to instance id='.$this->myobject->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
}
return $this->_cleanObjectDatas($this->myobject);
@@ -113,33 +105,37 @@ class MyModuleApi extends DolibarrApi
global $db, $conf;
$obj_ret = array();
$tmpobject = new MyObject($db);
if(! DolibarrApiAccess::$user->rights->bbb->read) {
throw new RestException(401);
}
$socid = DolibarrApiAccess::$user->societe_id ? DolibarrApiAccess::$user->societe_id : '';
$restictonsocid = 0; // Set to 1 if there is a field socid in table of object
$restrictonsocid = 0; // Set to 1 if there is a field socid in table of object
// If the internal user must only see his customers, force searching by him
$search_sale = 0;
if ($restictonsocid && ! DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) $search_sale = DolibarrApiAccess::$user->id;
if ($restrictonsocid && ! DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) $search_sale = DolibarrApiAccess::$user->id;
$sql = "SELECT t.rowid";
if ($restictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) $sql .= ", sc.fk_soc, sc.fk_user"; // We need these fields in order to filter by sale (including the case where the user can only see his prospects)
$sql.= " FROM ".MAIN_DB_PREFIX."myobject_mytable as t";
if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) $sql .= ", sc.fk_soc, sc.fk_user"; // We need these fields in order to filter by sale (including the case where the user can only see his prospects)
$sql.= " FROM ".MAIN_DB_PREFIX.$tmpobject->table_element." as t";
if ($restictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale
$sql.= " WHERE 1 = 1";
// Example of use $mode
//if ($mode == 1) $sql.= " AND s.client IN (1, 3)";
//if ($mode == 2) $sql.= " AND s.client IN (2, 3)";
$tmpobject = new MyObject($db);
if ($tmpobject->ismultientitymanaged) $sql.= ' AND t.entity IN ('.getEntity('myobject').')';
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 && $search_sale > 0) $sql.= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
if ($restrictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) $sql.= " AND t.fk_soc = sc.fk_soc";
if ($restrictonsocid && $socid) $sql.= " AND t.fk_soc = ".$socid;
if ($restrictonsocid && $search_sale > 0) $sql.= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
// Insert sale filter
if ($restictonsocid && $search_sale > 0) {
if ($restrictonsocid && $search_sale > 0) {
$sql .= " AND sc.fk_user = ".$search_sale;
}
if ($sqlfilters)
@@ -176,7 +172,7 @@ class MyModuleApi extends DolibarrApi
}
}
else {
throw new RestException(503, 'Error when retrieve myobject list');
throw new RestException(503, 'Error when retrieving myobject list: '.$db->lasterror());
}
if( ! count($obj_ret)) {
throw new RestException(404, 'No myobject found');
@@ -194,7 +190,7 @@ class MyModuleApi extends DolibarrApi
*/
public function post($request_data = null)
{
if(! DolibarrApiAccess::$user->rights->myobject->create) {
if(! DolibarrApiAccess::$user->rights->mymodule->write) {
throw new RestException(401);
}
// Check mandatory fields
@@ -220,7 +216,7 @@ class MyModuleApi extends DolibarrApi
*/
public function put($id, $request_data = null)
{
if(! DolibarrApiAccess::$user->rights->myobject->create) {
if(! DolibarrApiAccess::$user->rights->mymodule->write) {
throw new RestException(401);
}
@@ -229,8 +225,8 @@ class MyModuleApi extends DolibarrApi
throw new RestException(404, 'MyObject not found');
}
if( ! DolibarrApi::_checkAccessToResource('myobject', $this->myobject->id)) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
if( ! DolibarrApi::_checkAccessToResource('myobject', $this->myobject->id, 'mymodule_myobject')) {
throw new RestException(401, 'Access to instance id='.$this->myobject->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
}
foreach($request_data as $field => $value) {
@@ -254,11 +250,11 @@ class MyModuleApi extends DolibarrApi
* @param int $id MyObject ID
* @return array
*
* @url DELETE myobject/{id}
* @url DELETE myobjects/{id}
*/
public function delete($id)
{
if (! DolibarrApiAccess::$user->rights->myobject->delete) {
if (! DolibarrApiAccess::$user->rights->mymodule->delete) {
throw new RestException(401);
}
$result = $this->myobject->fetch($id);
@@ -266,8 +262,8 @@ class MyModuleApi extends DolibarrApi
throw new RestException(404, 'MyObject not found');
}
if (! DolibarrApi::_checkAccessToResource('myobject', $this->myobject->id)) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
if (! DolibarrApi::_checkAccessToResource('myobject', $this->myobject->id, 'mymodule_myobject')) {
throw new RestException(401, 'Access to instance id='.$this->myobject->id.' of object not allowed for login '.DolibarrApiAccess::$user->login);
}
if (! $this->myobject->delete(DolibarrApiAccess::$user))
@@ -317,7 +313,8 @@ class MyModuleApi extends DolibarrApi
private function _validate($data)
{
$myobject = array();
foreach (MyObjectApi::$FIELDS as $field) {
foreach ($this->myobject->fields as $field => $propfield) {
if (in_array($field, array('rowid', 'entity', 'date_creation', 'tms', 'fk_user_creat')) || $propfield['notnull'] != 1) continue; // Not a mandatory field
if (!isset($data[$field]))
throw new RestException(400, "$field field missing");
$myobject[$field] = $data[$field];