diff --git a/htdocs/categories/class/api_category.class.php b/htdocs/categories/class/api_category.class.php index 364147d097d0a084614569af0bc45e152296c294..5e558ddc486a3fcdc00661e14c5e2cfb40bc91fd 100644 --- a/htdocs/categories/class/api_category.class.php +++ b/htdocs/categories/class/api_category.class.php @@ -18,6 +18,7 @@ use Luracast\Restler\RestException; 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 @@ -162,6 +163,86 @@ class CategoryApi extends DolibarrApi } 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 @@ -193,6 +274,56 @@ class CategoryApi extends DolibarrApi function getListCategoryCustomer($sortfield = "s.rowid", $sortorder = 'ASC', $limit = 0, $page = 0) { 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 diff --git a/htdocs/commande/class/api_commande.class.php b/htdocs/commande/class/api_commande.class.php index b7b6cb0c21cf17430bb7819900265a266f7b6000..04d78f30a1da5e8b0107c234f1b513580cc101b8 100644 --- a/htdocs/commande/class/api_commande.class.php +++ b/htdocs/commande/class/api_commande.class.php @@ -102,16 +102,17 @@ class CommandeApi extends DolibarrApi * @param string $sortorder Sort order * @param int $limit Limit for list * @param int $page Page number + * @param string $societe Societe filter field * * @url GET /order/list * @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; $obj_ret = array(); - - $socid = DolibarrApiAccess::$user->societe_id ? DolibarrApiAccess::$user->societe_id : ''; + // case of external user, $societe param is ignored and replaced by user's socid + $socid = DolibarrApiAccess::$user->societe_id ? DolibarrApiAccess::$user->societe_id : $societe; // 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; @@ -178,6 +179,22 @@ class CommandeApi extends DolibarrApi } 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 @@ -186,7 +203,7 @@ class CommandeApi extends DolibarrApi * * @url POST order/ * - * @return string ID of commande + * @return int ID of commande */ function post($request_data = NULL) { @@ -195,19 +212,203 @@ class CommandeApi extends DolibarrApi } // Check mandatory fields $result = $this->_validate($request_data); - + foreach($request_data as $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) ) { 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; + } + /** + * 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 + * Update order general fields (won't touch lines of order) * * @param int $id Id of commande to update * @param array $request_data Datas @@ -216,11 +417,10 @@ class CommandeApi extends DolibarrApi * * @return int */ - function put($id, $request_data = NULL) - { - if(! DolibarrApiAccess::$user->rights->commande->creer) { - throw new RestException(401); - } + function put($id, $request_data = NULL) { + if(! DolibarrApiAccess::$user->rights->commande->creer) { + throw new RestException(401); + } $result = $this->commande->fetch($id); if( ! $result ) { @@ -230,13 +430,12 @@ class CommandeApi extends DolibarrApi if( ! DolibarrApi::_checkAccessToResource('commande',$this->commande->id)) { throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); } - foreach($request_data as $field => $value) { $this->commande->$field = $value; } if($this->commande->update($id, DolibarrApiAccess::$user,1,'','','update')) - return $this->get ($id); + return $this->get($id); return false; } @@ -329,6 +528,7 @@ class CommandeApi extends DolibarrApi if (!isset($data[$field])) throw new RestException(400, "$field field missing"); $commande[$field] = $data[$field]; + } return $commande; } diff --git a/htdocs/societe/class/api_contact.class.php b/htdocs/societe/class/api_contact.class.php index f2c19f2e48421ad6a29e605fd520fda1c2f403f5..ba7c0d4a2119dfae640ac9111dcc33f2f06e4695 100644 --- a/htdocs/societe/class/api_contact.class.php +++ b/htdocs/societe/class/api_contact.class.php @@ -17,7 +17,7 @@ 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 @@ -100,6 +100,7 @@ class ContactApi extends DolibarrApi * @url GET /contact/list * @url GET /contact/list/{socid} * @url GET /thirdparty/{socid}/contacts + * @url GET /customer/{socid}/contacts * * @throws RestException */ diff --git a/htdocs/societe/class/api_thirdparty.class.php b/htdocs/societe/class/api_thirdparty.class.php index d56c269e08efdd4339e843c295c34d334b87a25e..9c65d08ac434292bc20378d10d52861c1f1a2fbb 100644 --- a/htdocs/societe/class/api_thirdparty.class.php +++ b/htdocs/societe/class/api_thirdparty.class.php @@ -40,6 +40,10 @@ class ThirdpartyApi extends DolibarrApi * @var Societe $company {@type Societe} */ public $company; + /** + * @var Customer $customer {@type Client} + */ + public $customer; /** * Constructor @@ -52,39 +56,87 @@ class ThirdpartyApi extends DolibarrApi global $db, $conf; $this->db = $db; $this->company = new Societe($this->db); + $this->customer = new Client($this->db); if (! empty($conf->global->SOCIETE_MAIL_REQUIRED)) { 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); + } + /** - * Get properties of a thirdparty object - * - * Return an array with thirdparty informations + * Search customer by email + * + * @param string $email email id * - * @param int $id ID of thirdparty - * @return array|mixed data without useless information - * - * @url GET thirdparty/{id} - * @throws RestException + * @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 + * + * Return an array with thirdparty informations + * + * @param int $id ID of thirdparty + * @return array|mixed data without useless information + * + * @url GET thirdparty/{id} + * @throws RestException + */ function get($id) { - if(! DolibarrApiAccess::$user->rights->societe->lire) { - throw new RestException(401); - } + if(! DolibarrApiAccess::$user->rights->societe->lire) { + throw new RestException(401); + } - $result = $this->company->fetch($id); - if( ! $result ) { - throw new RestException(404, 'Thirdparty not found'); - } + $result = $this->company->fetch($id); + if( ! $result ) { + throw new RestException(404, 'Thirdparty not found'); + } - if( ! DolibarrApi::_checkAccessToResource('societe',$this->company->id)) { - throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); - } + if( ! DolibarrApi::_checkAccessToResource('societe',$this->company->id)) { + throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); + } - return $this->_cleanObjectDatas($this->company); + return $this->_cleanObjectDatas($this->company); } /** @@ -95,6 +147,7 @@ class ThirdpartyApi extends DolibarrApi * @param int $mode Set to 1 to show only customers * Set to 2 to show only prospects * 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 $sortorder Sort order * @param int $limit Limit for list @@ -104,7 +157,7 @@ class ThirdpartyApi extends DolibarrApi * @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; $obj_ret = array(); @@ -126,6 +179,7 @@ class ThirdpartyApi extends DolibarrApi if ($mode == 3) $sql.= " AND s.client IN (0)"; $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 ($email != NULL) $sql.= " AND s.email = \"".$email."\""; 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 @@ -183,6 +237,7 @@ class ThirdpartyApi extends DolibarrApi * @return array List of customers * * @url GET /thirdparty/list/customers + * @url GET /customer/list */ function getListCustomers() { return $this->getList(1); @@ -196,7 +251,7 @@ class ThirdpartyApi extends DolibarrApi * @url GET /thirdparty/list/prospects */ function getListProspects() { - return $this->getList('',1); + return $this->getList(2); } /** @@ -207,7 +262,7 @@ class ThirdpartyApi extends DolibarrApi * @url GET /thirdparty/list/others */ function getListOthers() { - return $this->getList('','',1); + return $this->getList(3); } /** @@ -220,16 +275,31 @@ class ThirdpartyApi extends DolibarrApi */ function post($request_data = NULL) { - if(! DolibarrApiAccess::$user->rights->societe->creer) { - throw new RestException(401); - } - // Check mandatory fields - $result = $this->_validate($request_data); - - foreach($request_data as $field => $value) { - $this->company->$field = $value; - } - return $this->company->create(DolibarrApiAccess::$user); + if(! DolibarrApiAccess::$user->rights->societe->creer) { + throw new RestException(401); + } + // Check mandatory fields + $result = $this->_validate($request_data); + + foreach($request_data as $field => $value) { + $this->company->$field = $value; + } + 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; } /** @@ -265,30 +335,59 @@ class ThirdpartyApi extends DolibarrApi 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 * * @param int $id Thirparty ID - * @return integer + * @return type * * @url DELETE thirdparty/{id} + * @url DELETE customer/{id} */ function delete($id) { - if(! DolibarrApiAccess::$user->rights->societe->supprimer) { - throw new RestException(401); - } - $result = $this->company->fetch($id); - if( ! $result ) { - throw new RestException(404, 'Thirdparty not found'); - } - - if( ! DolibarrApi::_checkAccessToResource('societe',$this->company->id)) { - throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); - } - - return $this->company->delete($id); + if(! DolibarrApiAccess::$user->rights->societe->supprimer) { + throw new RestException(401); + } + $result = $this->company->fetch($id); + if( ! $result ) { + throw new RestException(404, 'Thirdparty not found'); + } + if( ! DolibarrApi::_checkAccessToResource('societe',$this->company->id)) { + throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); + } + return $this->company->delete($id); } /** diff --git a/htdocs/user/class/api_user.class.php b/htdocs/user/class/api_user.class.php new file mode 100644 index 0000000000000000000000000000000000000000..d09785d3ccb0e805c246f0f02ee06c44e1e826b0 --- /dev/null +++ b/htdocs/user/class/api_user.class.php @@ -0,0 +1,208 @@ +<?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; + } +}