From 6d673035016d138b7f9ede1a95ebc6a884f5a7dc Mon Sep 17 00:00:00 2001 From: Xebax Date: Fri, 17 Jun 2016 14:35:35 +0200 Subject: [PATCH] REST API: move the definition of /contacts/{id}/createUser from api_users.class.php to api_contacts.class.php. --- htdocs/societe/class/api_contacts.class.php | 44 +++++++++++++++++++++ htdocs/user/class/api_users.class.php | 44 --------------------- 2 files changed, 44 insertions(+), 44 deletions(-) diff --git a/htdocs/societe/class/api_contacts.class.php b/htdocs/societe/class/api_contacts.class.php index d49be1bd4f1..349fa8ecae2 100644 --- a/htdocs/societe/class/api_contacts.class.php +++ b/htdocs/societe/class/api_contacts.class.php @@ -256,6 +256,50 @@ class Contacts extends DolibarrApi return $this->contact->delete($id); } + /** + * Create useraccount object from contact + * + * @param int $id Id of contact + * @param array $request_data Request datas + * @return int ID of user + * + * @url POST {id}/createUser + */ + function createUser($id, $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($id); + 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"]; + $useraccount = new User($this->db); + $result = $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 + $useraccount->setPassword($useraccount,$password); + + return $result; + } + /** * Validate fields before create or update object * diff --git a/htdocs/user/class/api_users.class.php b/htdocs/user/class/api_users.class.php index 7b2396434de..a9eb7771d9d 100644 --- a/htdocs/user/class/api_users.class.php +++ b/htdocs/user/class/api_users.class.php @@ -77,50 +77,6 @@ class Users extends DolibarrApi return $this->_cleanObjectDatas($this->useraccount); } - - /** - * TODO move to the /contacts/ API - * 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; - } /**