api_deprecated_user.class.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. /* Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. use Luracast\Restler\RestException;
  18. //require_once DOL_DOCUMENT_ROOT . '/contact/class/contact.class.php';
  19. /**
  20. * API class for user object
  21. *
  22. * @smart-auto-routing false
  23. * @access protected
  24. * @class DolibarrApiAccess {@requires user,external}
  25. * @deprecated Use Users instead (defined in api_users.class.php)
  26. */
  27. class UserApi extends DolibarrApi
  28. {
  29. /**
  30. *
  31. * @var array $FIELDS Mandatory fields, checked when create and update object
  32. */
  33. static $FIELDS = array(
  34. 'login'
  35. );
  36. /**
  37. * @var User $user {@type User}
  38. */
  39. public $useraccount;
  40. /**
  41. * Constructor <b>Warning: Deprecated</b>
  42. *
  43. * @url user/
  44. *
  45. */
  46. function __construct() {
  47. global $db, $conf;
  48. $this->db = $db;
  49. $this->useraccount = new User($this->db);
  50. }
  51. /**
  52. * Get properties of an user object <b>Warning: Deprecated</b>
  53. *
  54. * Return an array with user informations
  55. *
  56. * @param int $id ID of user
  57. * @return array|mixed data without useless information
  58. *
  59. * @url GET user/{id}
  60. * @throws RestException
  61. */
  62. function get($id) {
  63. //if (!DolibarrApiAccess::$user->rights->user->user->lire) {
  64. //throw new RestException(401);
  65. //}
  66. $result = $this->useraccount->fetch($id);
  67. if (!$result)
  68. {
  69. throw new RestException(404, 'User not found');
  70. }
  71. if (!DolibarrApi::_checkAccessToResource('user', $this->useraccount->id, 'user'))
  72. {
  73. throw new RestException(401, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
  74. }
  75. return $this->_cleanObjectDatas($this->useraccount);
  76. }
  77. /**
  78. * Create useraccount object from contact <b>Warning: Deprecated</b>
  79. *
  80. * @param int $contactid Id of contact
  81. * @param array $request_data Request datas
  82. * @return int ID of user
  83. *
  84. * @url POST /contact/{contactid}/createUser
  85. */
  86. function createFromContact($contactid, $request_data = NULL) {
  87. //if (!DolibarrApiAccess::$user->rights->user->user->creer) {
  88. //throw new RestException(401);
  89. //}
  90. if (!isset($request_data["login"]))
  91. throw new RestException(400, "login field missing");
  92. if (!isset($request_data["password"]))
  93. throw new RestException(400, "password field missing");
  94. if (!DolibarrApiAccess::$user->rights->societe->contact->lire) {
  95. throw new RestException(401);
  96. }
  97. $contact = new Contact($this->db);
  98. $contact->fetch($contactid);
  99. if ($contact->id <= 0) {
  100. throw new RestException(404, 'Contact not found');
  101. }
  102. if (!DolibarrApi::_checkAccessToResource('contact', $contact->id, 'socpeople&societe')) {
  103. throw new RestException(401, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
  104. }
  105. // Check mandatory fields
  106. $login = $request_data["login"];
  107. $password = $request_data["password"];
  108. $result = $this->useraccount->create_from_contact($contact,$login,$password);
  109. if ($result <= 0) {
  110. throw new RestException(500, "User not created");
  111. }
  112. // password parameter not used in create_from_contact
  113. $this->useraccount->setPassword($this->useraccount,$password);
  114. return $result;
  115. }
  116. /**
  117. * Create user account <b>Warning: Deprecated</b>
  118. *
  119. * @param array $request_data New user data
  120. * @return int
  121. *
  122. * @url POST user/
  123. */
  124. function post($request_data = NULL) {
  125. // check user authorization
  126. //if(! DolibarrApiAccess::$user->rights->user->creer) {
  127. // throw new RestException(401, "User creation not allowed");
  128. //}
  129. // check mandatory fields
  130. /*if (!isset($request_data["login"]))
  131. throw new RestException(400, "login field missing");
  132. if (!isset($request_data["password"]))
  133. throw new RestException(400, "password field missing");
  134. if (!isset($request_data["lastname"]))
  135. throw new RestException(400, "lastname field missing");*/
  136. //assign field values
  137. $xxx=var_export($request_data, true);
  138. dol_syslog("xxx=".$xxx);
  139. foreach ($request_data as $field => $value)
  140. {
  141. $this->useraccount->$field = $value;
  142. }
  143. $result = $this->useraccount->create(DolibarrApiAccess::$user);
  144. if ($result <=0) {
  145. throw new RestException(500, "User not created : ".$this->useraccount->error);
  146. }
  147. return array('id'=>$result);
  148. }
  149. /**
  150. * Update account <b>Warning: Deprecated</b>
  151. *
  152. * @param int $id Id of account to update
  153. * @param array $request_data Datas
  154. * @return int
  155. *
  156. * @url PUT user/{id}
  157. */
  158. function put($id, $request_data = NULL) {
  159. //if (!DolibarrApiAccess::$user->rights->user->user->creer) {
  160. //throw new RestException(401);
  161. //}
  162. $result = $this->useraccount->fetch($id);
  163. if (!$result)
  164. {
  165. throw new RestException(404, 'Account not found');
  166. }
  167. if (!DolibarrApi::_checkAccessToResource('user', $this->useraccount->id, 'user'))
  168. {
  169. throw new RestException(401, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
  170. }
  171. foreach ($request_data as $field => $value)
  172. {
  173. if ($field == 'id') continue;
  174. $this->useraccount->$field = $value;
  175. }
  176. if ($this->useraccount->update($id, DolibarrApiAccess::$user, 1, '', '', 'update'))
  177. return $this->get($id);
  178. return false;
  179. }
  180. /**
  181. * add user to group <b>Warning: Deprecated</b>
  182. *
  183. * @param int $id User ID
  184. * @param int $group Group ID
  185. * @return int
  186. *
  187. * @url GET user/{id}/setGroup/{group}
  188. */
  189. function setGroup($id,$group) {
  190. //if (!DolibarrApiAccess::$user->rights->user->user->supprimer) {
  191. //throw new RestException(401);
  192. //}
  193. $result = $this->useraccount->fetch($id);
  194. if (!$result)
  195. {
  196. throw new RestException(404, 'User not found');
  197. }
  198. if (!DolibarrApi::_checkAccessToResource('user', $this->useraccount->id, 'user'))
  199. {
  200. throw new RestException(401, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
  201. }
  202. return $this->useraccount->SetInGroup($group,1);
  203. }
  204. /**
  205. * Delete account <b>Warning: Deprecated</b>
  206. *
  207. * @param int $id Account ID
  208. * @return array
  209. *
  210. * @url DELETE user/{id}
  211. */
  212. function delete($id) {
  213. //if (!DolibarrApiAccess::$user->rights->user->user->supprimer) {
  214. //throw new RestException(401);
  215. //}
  216. $result = $this->useraccount->fetch($id);
  217. if (!$result)
  218. {
  219. throw new RestException(404, 'User not found');
  220. }
  221. if (!DolibarrApi::_checkAccessToResource('user', $this->useraccount->id, 'user'))
  222. {
  223. throw new RestException(401, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
  224. }
  225. return $this->useraccount->delete($id);
  226. }
  227. /**
  228. * Validate fields before create or update object
  229. *
  230. * @param array|null $data Data to validate
  231. * @return array
  232. * @throws RestException
  233. */
  234. function _validate($data) {
  235. $account = array();
  236. foreach (UserApi::$FIELDS as $field)
  237. {
  238. if (!isset($data[$field]))
  239. throw new RestException(400, "$field field missing");
  240. $account[$field] = $data[$field];
  241. }
  242. return $account;
  243. }
  244. }