Skip to content
Snippets Groups Projects
Commit da329131 authored by Laurent Destailleur's avatar Laurent Destailleur
Browse files

Merge pull request #4959 from nka11/api_dev

API Improvements
parents d40e659b 39948968
No related branches found
No related tags found
No related merge requests found
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
use Luracast\Restler\RestException; use Luracast\Restler\RestException;
require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php'; require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
require_once DOL_DOCUMENT_ROOT.'/societe/class/client.class.php';
/** /**
* API class for category object * API class for category object
...@@ -162,6 +163,86 @@ class CategoryApi extends DolibarrApi ...@@ -162,6 +163,86 @@ class CategoryApi extends DolibarrApi
} }
return $obj_ret; return $obj_ret;
} }
/**
* List categories of an entity
*
* Get a list of categories
*
* @param string $type Type of category ('member', 'customer', 'supplier', 'product', 'contact')
* @param string $sortfield Sort field
* @param string $sortorder Sort order
* @param int $limit Limit for list
* @param int $page Page number
* @param int $item Id of the item to get categories for
* @return array Array of category objects
*
* @url GET /product/{item}/categories
*/
function getListForItem($type='product', $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0, $item = 0) {
global $db, $conf;
$obj_ret = array();
if(! DolibarrApiAccess::$user->rights->categorie->lire) {
throw new RestException(401);
}
//if ($type == "") {
//$type="product";
//}
$sub_type = $type;
$subcol_name = "fk_".$type;
if ($type=="customer" || $type=="supplier") {
$sub_type="societe";
$subcol_name="fk_soc";
}
$sql = "SELECT s.rowid";
$sql.= " FROM ".MAIN_DB_PREFIX."categorie as s";
$sql.= " , ".MAIN_DB_PREFIX."categorie_".$sub_type." as sub ";
$sql.= ' WHERE s.entity IN ('.getEntity('categorie', 1).')';
$sql.= ' AND s.type='.array_search($type,CategoryApi::$TYPES);
$sql.= ' AND s.rowid = sub.fk_categorie';
$sql.= ' AND sub.'.$subcol_name.' = '.$item;
$nbtotalofrecords = 0;
if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST))
{
$result = $db->query($sql);
$nbtotalofrecords = $db->num_rows($result);
}
$sql.= $db->order($sortfield, $sortorder);
if ($limit) {
if ($page < 0)
{
$page = 0;
}
$offset = $limit * $page;
$sql.= $db->plimit($limit + 1, $offset);
}
$result = $db->query($sql);
if ($result)
{
$num = $db->num_rows($result);
while ($i < $num)
{
$obj = $db->fetch_object($result);
$category_static = new Categorie($db);
if($category_static->fetch($obj->rowid)) {
$obj_ret[] = parent::_cleanObjectDatas($category_static);
}
$i++;
}
}
else {
throw new RestException(503, 'Error when retrieve category list : '.$category_static->error);
}
if( ! count($obj_ret)) {
throw new RestException(404, 'No category found');
}
return $obj_ret;
}
/** /**
* Get member categories list * Get member categories list
...@@ -193,6 +274,56 @@ class CategoryApi extends DolibarrApi ...@@ -193,6 +274,56 @@ class CategoryApi extends DolibarrApi
function getListCategoryCustomer($sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0) { function getListCategoryCustomer($sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0) {
return $this->getList('customer', $sortfield, $sortorder, $limit, $page); return $this->getList('customer', $sortfield, $sortorder, $limit, $page);
} }
/**
* Get categories for a customer
*
* @param int $cusid Customer id filter
* @param string $sortfield Sort field
* @param string $sortorder Sort order
* @param int $limit Limit for list
* @param int $page Page number
*
* @return mixed
*
* @url GET /customer/{cusid}/categories
*/
function getListCustomerCategories($cusid, $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0) {
return $this->getListForItem('customer', $sortfield, $sortorder, $limit, $page, $cusid);
}
/**
* Add category to customer
*
* @param int $cusid Id of customer
* @param int $catid Id of category
*
* @return mixed
*
* @url GET /customer/{cusid}/addCategory/{catid}
*/
function addCustomerCategory($cusid,$catid) {
if(! DolibarrApiAccess::$user->rights->societe->creer) {
throw new RestException(401);
}
$customer = new Client($this->db);
$customer->fetch($cusid);
if( ! $customer ) {
throw new RestException(404, 'customer not found');
}
$result = $this->category->fetch($catid);
if( ! $result ) {
throw new RestException(404, 'category not found');
}
if( ! DolibarrApi::_checkAccessToResource('societe',$customer->id)) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
}
if( ! DolibarrApi::_checkAccessToResource('category',$this->category->id)) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
}
$this->category->add_type($customer,'customer');
return $customer;
}
/** /**
* Get supplier categories list * Get supplier categories list
......
...@@ -102,16 +102,17 @@ class CommandeApi extends DolibarrApi ...@@ -102,16 +102,17 @@ class CommandeApi extends DolibarrApi
* @param string $sortorder Sort order * @param string $sortorder Sort order
* @param int $limit Limit for list * @param int $limit Limit for list
* @param int $page Page number * @param int $page Page number
* @param string $societe Societe filter field
* *
* @url GET /order/list * @url GET /order/list
* @return array Array of order objects * @return array Array of order objects
*/ */
function getList($mode=0, $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0) { function getList($mode=0, $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0, $societe = 0) {
global $db, $conf; global $db, $conf;
$obj_ret = array(); $obj_ret = array();
// case of external user, $societe param is ignored and replaced by user's socid
$socid = DolibarrApiAccess::$user->societe_id ? DolibarrApiAccess::$user->societe_id : ''; $socid = DolibarrApiAccess::$user->societe_id ? DolibarrApiAccess::$user->societe_id : $societe;
// If the internal user must only see his customers, force searching by him // If the internal user must only see his customers, force searching by him
if (! DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) $search_sale = DolibarrApiAccess::$user->id; if (! DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) $search_sale = DolibarrApiAccess::$user->id;
...@@ -179,6 +180,22 @@ class CommandeApi extends DolibarrApi ...@@ -179,6 +180,22 @@ class CommandeApi extends DolibarrApi
return $obj_ret; return $obj_ret;
} }
/**
* List orders for specific thirdparty
*
* Get a list of orders
*
* @param int $socid Id of customer
*
* @url GET /customer/{socid}/order/list
* @url GET /thirdparty/{socid}/order/list
* @return array Array of order objects
*/
function getListForSoc($socid = 0) {
return getList(0,"s.rowid","ASC",0,0,$socid);
}
/** /**
* Create order object * Create order object
* *
...@@ -186,7 +203,7 @@ class CommandeApi extends DolibarrApi ...@@ -186,7 +203,7 @@ class CommandeApi extends DolibarrApi
* *
* @url POST order/ * @url POST order/
* *
* @return string ID of commande * @return int ID of commande
*/ */
function post($request_data = NULL) function post($request_data = NULL)
{ {
...@@ -199,15 +216,199 @@ class CommandeApi extends DolibarrApi ...@@ -199,15 +216,199 @@ class CommandeApi extends DolibarrApi
foreach($request_data as $field => $value) { foreach($request_data as $field => $value) {
$this->commande->$field = $value; $this->commande->$field = $value;
} }
if (isset($request_data["lines"])) {
$lines = array();
foreach ($request_data["lines"] as $line) {
array_push($lines, (object) $line);
}
$this->commande->lines = $lines;
}
if(! $this->commande->create(DolibarrApiAccess::$user) ) { if(! $this->commande->create(DolibarrApiAccess::$user) ) {
throw new RestException(401); throw new RestException(401);
} }
return $this->commande->ref; return $this->commande->id;
}
/**
* Get lines of an order
*
*
* @param int $id Id of order
*
* @url GET order/{id}/line/list
*
* @return int
*/
function getLines($id) {
if(! DolibarrApiAccess::$user->rights->commande->lire) {
throw new RestException(401);
}
$result = $this->commande->fetch($id);
if( ! $result ) {
throw new RestException(404, 'Commande not found');
} }
if( ! DolibarrApi::_checkAccessToResource('commande',$this->commande->id)) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
}
$this->commande->getLinesArray();
$result = array();
foreach ($this->commande->lines as $line) {
array_push($result,$this->_cleanObjectDatas($line));
}
return $result;
}
/** /**
* Update order * Add a line to given order
*
*
* @param int $id Id of commande to update
* @param array $request_data Orderline data
*
* @url POST order/{id}/line
*
* @return int
*/
function postLine($id, $request_data = NULL) {
if(! DolibarrApiAccess::$user->rights->commande->creer) {
throw new RestException(401);
}
$result = $this->commande->fetch($id);
if( ! $result ) {
throw new RestException(404, 'Commande not found');
}
if( ! DolibarrApi::_checkAccessToResource('commande',$this->commande->id)) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
}
$request_data = (object) $request_data;
$updateRes = $this->commande->addline(
$request_data->desc,
$request_data->subprice,
$request_data->qty,
$request_data->tva_tx,
$request_data->localtax1_tx,
$request_data->localtax2_tx,
$request_data->fk_product,
$request_data->remise_percent,
$request_data->info_bits,
$request_data->fk_remise_except,
'HT',
0,
$request_data->date_start,
$request_data->date_end,
$request_data->product_type,
$request_data->rang,
$request_data->special_code,
$fk_parent_line,
$request_data->fk_fournprice,
$request_data->pa_ht,
$request_data->label,
$request_data->array_options,
$request_data->fk_unit,
$this->element,
$request_data->id
);
if ($updateRes > 0) {
return $this->get($id)->line->rowid;
}
return false;
}
/**
* Update a line to given order
*
*
* @param int $id Id of commande to update
* @param int $lineid Id of line to update
* @param array $request_data Orderline data
*
* @url PUT order/{id}/line/{lineid}
*
* @return object
*/
function putLine($id, $lineid, $request_data = NULL) {
if(! DolibarrApiAccess::$user->rights->commande->creer) {
throw new RestException(401);
}
$result = $this->commande->fetch($id);
if( ! $result ) {
throw new RestException(404, 'Commande not found');
}
if( ! DolibarrApi::_checkAccessToResource('commande',$this->commande->id)) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
}
$request_data = (object) $request_data;
$updateRes = $this->commande->updateline(
$lineid,
$request_data->desc,
$request_data->subprice,
$request_data->qty,
$request_data->remise_percent,
$request_data->tva_tx,
$request_data->localtax1_tx,
$request_data->localtax2_tx,
'HT',
$request_data->info_bits,
$request_data->date_start,
$request_data->date_end,
$request_data->product_type,
$request_data->fk_parent_line,
0,
$request_data->fk_fournprice,
$request_data->pa_ht,
$request_data->label,
$request_data->special_code,
$request_data->array_options,
$request_data->fk_unit
);
if ($updateRes > 0) {
$result = $this->get($id);
unset($result->line);
return $this->_cleanObjectDatas($result);
}
return false;
}
/**
* Delete a line to given order
*
*
* @param int $id Id of commande to update
* @param int $lineid Id of line to delete
*
* @url DELETE order/{id}/line/{lineid}
*
* @return int
*/
function delLine($id, $lineid) {
if(! DolibarrApiAccess::$user->rights->commande->creer) {
throw new RestException(401);
}
$result = $this->commande->fetch($id);
if( ! $result ) {
throw new RestException(404, 'Commande not found');
}
if( ! DolibarrApi::_checkAccessToResource('commande',$this->commande->id)) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
}
$request_data = (object) $request_data;
$updateRes = $this->commande->deleteline($lineid);
if ($updateRes == 1) {
return $this->get($id);
}
return false;
}
/**
* Update order general fields (won't touch lines of order)
* *
* @param int $id Id of commande to update * @param int $id Id of commande to update
* @param array $request_data Datas * @param array $request_data Datas
...@@ -216,8 +417,7 @@ class CommandeApi extends DolibarrApi ...@@ -216,8 +417,7 @@ class CommandeApi extends DolibarrApi
* *
* @return int * @return int
*/ */
function put($id, $request_data = NULL) function put($id, $request_data = NULL) {
{
if(! DolibarrApiAccess::$user->rights->commande->creer) { if(! DolibarrApiAccess::$user->rights->commande->creer) {
throw new RestException(401); throw new RestException(401);
} }
...@@ -230,7 +430,6 @@ class CommandeApi extends DolibarrApi ...@@ -230,7 +430,6 @@ class CommandeApi extends DolibarrApi
if( ! DolibarrApi::_checkAccessToResource('commande',$this->commande->id)) { if( ! DolibarrApi::_checkAccessToResource('commande',$this->commande->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);
} }
foreach($request_data as $field => $value) { foreach($request_data as $field => $value) {
$this->commande->$field = $value; $this->commande->$field = $value;
} }
...@@ -329,6 +528,7 @@ class CommandeApi extends DolibarrApi ...@@ -329,6 +528,7 @@ class CommandeApi extends DolibarrApi
if (!isset($data[$field])) if (!isset($data[$field]))
throw new RestException(400, "$field field missing"); throw new RestException(400, "$field field missing");
$commande[$field] = $data[$field]; $commande[$field] = $data[$field];
} }
return $commande; return $commande;
} }
......
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
use Luracast\Restler\RestException; use Luracast\Restler\RestException;
require_once DOL_DOCUMENT_ROOT . '/contact/class/contact.class.php'; //require_once DOL_DOCUMENT_ROOT . '/contact/class/contact.class.php';
/** /**
* API class for contact object * API class for contact object
...@@ -100,6 +100,7 @@ class ContactApi extends DolibarrApi ...@@ -100,6 +100,7 @@ class ContactApi extends DolibarrApi
* @url GET /contact/list * @url GET /contact/list
* @url GET /contact/list/{socid} * @url GET /contact/list/{socid}
* @url GET /thirdparty/{socid}/contacts * @url GET /thirdparty/{socid}/contacts
* @url GET /customer/{socid}/contacts
* *
* @throws RestException * @throws RestException
*/ */
......
...@@ -40,6 +40,10 @@ class ThirdpartyApi extends DolibarrApi ...@@ -40,6 +40,10 @@ class ThirdpartyApi extends DolibarrApi
* @var Societe $company {@type Societe} * @var Societe $company {@type Societe}
*/ */
public $company; public $company;
/**
* @var Customer $customer {@type Client}
*/
public $customer;
/** /**
* Constructor * Constructor
...@@ -52,12 +56,60 @@ class ThirdpartyApi extends DolibarrApi ...@@ -52,12 +56,60 @@ class ThirdpartyApi extends DolibarrApi
global $db, $conf; global $db, $conf;
$this->db = $db; $this->db = $db;
$this->company = new Societe($this->db); $this->company = new Societe($this->db);
$this->customer = new Client($this->db);
if (! empty($conf->global->SOCIETE_MAIL_REQUIRED)) { if (! empty($conf->global->SOCIETE_MAIL_REQUIRED)) {
static::$FIELDS[] = 'email'; static::$FIELDS[] = 'email';
} }
} }
/**
* Get properties of a customer object
*
* Return an array with customer informations
*
* @param int $id ID of customer
* @return array|mixed data without useless information
*
* @url GET customer/{id}
* @throws RestException
*/
function getCustomer($id)
{
if(! DolibarrApiAccess::$user->rights->societe->lire) {
throw new RestException(401);
}
$result = $this->customer->fetch($id);
if( ! $result ) {
throw new RestException(404, 'Customer not found');
}
if( ! DolibarrApi::_checkAccessToResource('societe',$this->customer->id)) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
}
return $this->_cleanObjectDatas($this->customer);
}
/**
* Search customer by email
*
* @param string $email email id
*
* @return object client with given email
*
* @url GET customer/byemail/{email}
*/
function getByEmail($email) {
$res = $this->getList(1,$email);
if (count($res) == 1) {
$customer = $res[0];
return $customer;
}
return $res;
}
/** /**
* Get properties of a thirdparty object * Get properties of a thirdparty object
* *
...@@ -95,6 +147,7 @@ class ThirdpartyApi extends DolibarrApi ...@@ -95,6 +147,7 @@ class ThirdpartyApi extends DolibarrApi
* @param int $mode Set to 1 to show only customers * @param int $mode Set to 1 to show only customers
* Set to 2 to show only prospects * Set to 2 to show only prospects
* Set to 3 to show only those are not customer neither prospect * Set to 3 to show only those are not customer neither prospect
* @param Text $email Search by email filter
* @param string $sortfield Sort field * @param string $sortfield Sort field
* @param string $sortorder Sort order * @param string $sortorder Sort order
* @param int $limit Limit for list * @param int $limit Limit for list
...@@ -104,7 +157,7 @@ class ThirdpartyApi extends DolibarrApi ...@@ -104,7 +157,7 @@ class ThirdpartyApi extends DolibarrApi
* @url GET /thirdparty/list * @url GET /thirdparty/list
* *
*/ */
function getList($mode=0, $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0) { function getList($mode=0, $email=NULL, $sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0) {
global $db, $conf; global $db, $conf;
$obj_ret = array(); $obj_ret = array();
...@@ -126,6 +179,7 @@ class ThirdpartyApi extends DolibarrApi ...@@ -126,6 +179,7 @@ class ThirdpartyApi extends DolibarrApi
if ($mode == 3) $sql.= " AND s.client IN (0)"; if ($mode == 3) $sql.= " AND s.client IN (0)";
$sql.= ' AND s.entity IN ('.getEntity('societe', 1).')'; $sql.= ' AND s.entity IN ('.getEntity('societe', 1).')';
if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) $sql.= " AND s.rowid = sc.fk_soc"; if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) $sql.= " AND s.rowid = sc.fk_soc";
if ($email != NULL) $sql.= " AND s.email = \"".$email."\"";
if ($socid) $sql.= " AND s.rowid = ".$socid; if ($socid) $sql.= " AND s.rowid = ".$socid;
if ($search_sale > 0) $sql.= " AND s.rowid = sc.fk_soc"; // Join for the needed table to filter by sale if ($search_sale > 0) $sql.= " AND s.rowid = sc.fk_soc"; // Join for the needed table to filter by sale
...@@ -183,6 +237,7 @@ class ThirdpartyApi extends DolibarrApi ...@@ -183,6 +237,7 @@ class ThirdpartyApi extends DolibarrApi
* @return array List of customers * @return array List of customers
* *
* @url GET /thirdparty/list/customers * @url GET /thirdparty/list/customers
* @url GET /customer/list
*/ */
function getListCustomers() { function getListCustomers() {
return $this->getList(1); return $this->getList(1);
...@@ -196,7 +251,7 @@ class ThirdpartyApi extends DolibarrApi ...@@ -196,7 +251,7 @@ class ThirdpartyApi extends DolibarrApi
* @url GET /thirdparty/list/prospects * @url GET /thirdparty/list/prospects
*/ */
function getListProspects() { function getListProspects() {
return $this->getList('',1); return $this->getList(2);
} }
/** /**
...@@ -207,7 +262,7 @@ class ThirdpartyApi extends DolibarrApi ...@@ -207,7 +262,7 @@ class ThirdpartyApi extends DolibarrApi
* @url GET /thirdparty/list/others * @url GET /thirdparty/list/others
*/ */
function getListOthers() { function getListOthers() {
return $this->getList('','',1); return $this->getList(3);
} }
/** /**
...@@ -232,6 +287,21 @@ class ThirdpartyApi extends DolibarrApi ...@@ -232,6 +287,21 @@ class ThirdpartyApi extends DolibarrApi
return $this->company->create(DolibarrApiAccess::$user); return $this->company->create(DolibarrApiAccess::$user);
} }
/**
* Create customer object
*
* @param array $request_data Request datas
* @return int ID of thirdparty
*
* @url POST customer/
*/
function postCustomer($request_data) {
$this->post($request_data);
$this->company->set_as_client();
return $this->company->id;
}
/** /**
* Update thirdparty * Update thirdparty
* *
...@@ -265,14 +335,45 @@ class ThirdpartyApi extends DolibarrApi ...@@ -265,14 +335,45 @@ class ThirdpartyApi extends DolibarrApi
return false; return false;
} }
/**
* Update customer
*
* @param int $id Id of thirdparty to update
* @param array $request_data Datas
* @return int
*
* @url PUT customer/{id}
*/
function putClient($id, $request_data = NULL) {
if(! DolibarrApiAccess::$user->rights->societe->creer) {
throw new RestException(401);
}
$result = $this->customer->fetch($id);
if( ! $result ) {
throw new RestException(404, 'Customer not found');
}
if( ! DolibarrApi::_checkAccessToResource('societe',$this->customer->id)) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
}
foreach($request_data as $field => $value) {
$this->customer->$field = $value;
}
if($this->customer->update($id, DolibarrApiAccess::$user,1,'','','update'))
return $this->get ($id);
return false;
}
/** /**
* Delete thirdparty * Delete thirdparty
* *
* @param int $id Thirparty ID * @param int $id Thirparty ID
* @return integer * @return type
* *
* @url DELETE thirdparty/{id} * @url DELETE thirdparty/{id}
* @url DELETE customer/{id}
*/ */
function delete($id) function delete($id)
{ {
...@@ -283,11 +384,9 @@ class ThirdpartyApi extends DolibarrApi ...@@ -283,11 +384,9 @@ class ThirdpartyApi extends DolibarrApi
if( ! $result ) { if( ! $result ) {
throw new RestException(404, 'Thirdparty not found'); throw new RestException(404, 'Thirdparty not found');
} }
if( ! DolibarrApi::_checkAccessToResource('societe',$this->company->id)) { if( ! DolibarrApi::_checkAccessToResource('societe',$this->company->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);
} }
return $this->company->delete($id); return $this->company->delete($id);
} }
......
<?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\RestException;
//require_once DOL_DOCUMENT_ROOT . '/contact/class/contact.class.php';
/**
* API class for user object
*
* @smart-auto-routing false
* @access protected
* @class DolibarrApiAccess {@requires user,external}
*
*/
class UserApi extends DolibarrApi
{
/**
*
* @var array $FIELDS Mandatory fields, checked when create and update object
*/
static $FIELDS = array(
'login'
);
/**
* @var User $user {@type User}
*/
public $useraccount;
/**
* Constructor
*
* @url user/
*
*/
function __construct() {
global $db, $conf;
$this->db = $db;
$this->useraccount = new User($this->db);
}
/**
* Get properties of an user object
*
* Return an array with user informations
*
* @param int $id ID of user
* @return array|mixed data without useless information
*
* @url GET user/{id}
* @throws RestException
*/
function get($id) {
//if (!DolibarrApiAccess::$user->rights->user->user->lire) {
//throw new RestException(401);
//}
$result = $this->useraccount->fetch($id);
if (!$result)
{
throw new RestException(404, 'User not found');
}
if (!DolibarrApi::_checkAccessToResource('user', $this->useraccount->id, 'user'))
{
throw new RestException(401, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
}
return $this->_cleanObjectDatas($this->useraccount);
}
/**
* Create useraccount object from contact
*
* @param int $contactid Id of contact
* @param array $request_data Request datas
* @return int ID of user
*
* @url POST /contact/{contactid}/createUser
*/
function createFromContact($contactid, $request_data = NULL) {
//if (!DolibarrApiAccess::$user->rights->user->user->creer) {
//throw new RestException(401);
//}
if (!isset($request_data["login"]))
throw new RestException(400, "login field missing");
if (!isset($request_data["password"]))
throw new RestException(400, "password field missing");
if (!DolibarrApiAccess::$user->rights->societe->contact->lire) {
throw new RestException(401);
}
$contact = new Contact($this->db);
$contact->fetch($contactid);
if ($contact->id <= 0) {
throw new RestException(404, 'Contact not found');
}
if (!DolibarrApi::_checkAccessToResource('contact', $contact->id, 'socpeople&societe')) {
throw new RestException(401, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
}
// Check mandatory fields
$login = $request_data["login"];
$password = $request_data["password"];
$result = $this->useraccount->create_from_contact($contact,$login,$password);
if ($result <= 0) {
throw new RestException(500, "User not created");
}
// password parameter not used in create_from_contact
$this->useraccount->setPassword($this->useraccount,$password);
return $result;
}
/**
* Update account
*
* @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) {
//throw new RestException(401);
//}
$result = $this->useraccount->fetch($id);
if (!$result)
{
throw new RestException(404, 'Account not found');
}
if (!DolibarrApi::_checkAccessToResource('user', $this->useraccount->id, 'user'))
{
throw new RestException(401, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
}
foreach ($request_data as $field => $value)
{
$this->useraccount->$field = $value;
}
if ($this->useraccount->update($id, DolibarrApiAccess::$user, 1, '', '', 'update'))
return $this->get($id);
return false;
}
/**
* Delete account
*
* @param int $id Account ID
* @return array
*
* @url DELETE user/{id}
*/
function delete($id) {
//if (!DolibarrApiAccess::$user->rights->user->user->supprimer) {
//throw new RestException(401);
//}
$result = $this->useraccount->fetch($id);
if (!$result)
{
throw new RestException(404, 'User not found');
}
if (!DolibarrApi::_checkAccessToResource('user', $this->useraccount->id, 'user'))
{
throw new RestException(401, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
}
return $this->useraccount->delete($id);
}
/**
* Validate fields before create or update object
*
* @param array $data Data to validate
* @return array
* @throws RestException
*/
function _validate($data) {
$account = array();
foreach (UserApi::$FIELDS as $field)
{
if (!isset($data[$field]))
throw new RestException(400, "$field field missing");
$account[$field] = $data[$field];
}
return $account;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment