api_users.class.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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 users
  21. *
  22. * @access protected
  23. * @class DolibarrApiAccess {@requires user,external}
  24. */
  25. class Users extends DolibarrApi
  26. {
  27. /**
  28. *
  29. * @var array $FIELDS Mandatory fields, checked when create and update object
  30. */
  31. static $FIELDS = array(
  32. 'login'
  33. );
  34. /**
  35. * @var User $user {@type User}
  36. */
  37. public $useraccount;
  38. /**
  39. * Constructor
  40. */
  41. function __construct() {
  42. global $db, $conf;
  43. $this->db = $db;
  44. $this->useraccount = new User($this->db);
  45. }
  46. /**
  47. * List Users
  48. *
  49. * Get a list of Users
  50. *
  51. * @param string $sortfield Sort field
  52. * @param string $sortorder Sort order
  53. * @param int $limit Limit for list
  54. * @param int $page Page number
  55. * @param string $user_ids User ids filter field. Example: '1' or '1,2,3' {@pattern /^[0-9,]*$/i}
  56. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.ref:like:'SO-%') and (t.date_creation:<:'20160101')"
  57. * @return array Array of User objects
  58. */
  59. function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 0, $page = 0, $user_ids = 0, $sqlfilters = '') {
  60. global $db, $conf;
  61. $obj_ret = array();
  62. if(! DolibarrApiAccess::$user->rights->user->user->lire) {
  63. throw new RestException(401, "You are not allowed to read list of users");
  64. }
  65. // case of external user, $societe param is ignored and replaced by user's socid
  66. //$socid = DolibarrApiAccess::$user->societe_id ? DolibarrApiAccess::$user->societe_id : $societe;
  67. $sql = "SELECT t.rowid";
  68. $sql.= " FROM ".MAIN_DB_PREFIX."user as t";
  69. $sql.= ' WHERE t.entity IN ('.getEntity('user', 1).')';
  70. if ($user_ids) $sql.=" AND t.rowid IN (".$user_ids.")";
  71. // Add sql filters
  72. if ($sqlfilters)
  73. {
  74. if (! DolibarrApi::_checkFilters($sqlfilters))
  75. {
  76. throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters);
  77. }
  78. $regexstring='\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
  79. $sql.=" AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
  80. }
  81. $sql.= $db->order($sortfield, $sortorder);
  82. if ($limit) {
  83. if ($page < 0)
  84. {
  85. $page = 0;
  86. }
  87. $offset = $limit * $page;
  88. $sql.= $db->plimit($limit + 1, $offset);
  89. }
  90. $result = $db->query($sql);
  91. if ($result)
  92. {
  93. $num = $db->num_rows($result);
  94. $min = min($num, ($limit <= 0 ? $num : $limit));
  95. while ($i < $min)
  96. {
  97. $obj = $db->fetch_object($result);
  98. $user_static = new User($db);
  99. if($user_static->fetch($obj->rowid)) {
  100. $obj_ret[] = $this->_cleanObjectDatas($user_static);
  101. }
  102. $i++;
  103. }
  104. }
  105. else {
  106. throw new RestException(503, 'Error when retrieve User list : '.$db->lasterror());
  107. }
  108. if( ! count($obj_ret)) {
  109. throw new RestException(404, 'No User found');
  110. }
  111. return $obj_ret;
  112. }
  113. /**
  114. * Get properties of an user object
  115. *
  116. * Return an array with user informations
  117. *
  118. * @param int $id ID of user
  119. * @return array|mixed data without useless information
  120. *
  121. * @throws RestException
  122. */
  123. function get($id) {
  124. //if (!DolibarrApiAccess::$user->rights->user->user->lire) {
  125. //throw new RestException(401);
  126. //}
  127. $result = $this->useraccount->fetch($id);
  128. if (!$result)
  129. {
  130. throw new RestException(404, 'User not found');
  131. }
  132. if (!DolibarrApi::_checkAccessToResource('user', $this->useraccount->id, 'user'))
  133. {
  134. throw new RestException(401, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
  135. }
  136. return $this->_cleanObjectDatas($this->useraccount);
  137. }
  138. /**
  139. * Create user account
  140. *
  141. * @param array $request_data New user data
  142. * @return int
  143. */
  144. function post($request_data = NULL) {
  145. // check user authorization
  146. //if(! DolibarrApiAccess::$user->rights->user->creer) {
  147. // throw new RestException(401, "User creation not allowed");
  148. //}
  149. // check mandatory fields
  150. /*if (!isset($request_data["login"]))
  151. throw new RestException(400, "login field missing");
  152. if (!isset($request_data["password"]))
  153. throw new RestException(400, "password field missing");
  154. if (!isset($request_data["lastname"]))
  155. throw new RestException(400, "lastname field missing");*/
  156. //assign field values
  157. foreach ($request_data as $field => $value)
  158. {
  159. $this->useraccount->$field = $value;
  160. }
  161. if ($this->useraccount->create(DolibarrApiAccess::$user) < 0) {
  162. throw new RestException(500, 'Error creating', array_merge(array($this->useraccount->error), $this->useraccount->errors));
  163. }
  164. return $this->useraccount->id;
  165. }
  166. /**
  167. * Update account
  168. *
  169. * @param int $id Id of account to update
  170. * @param array $request_data Datas
  171. * @return int
  172. */
  173. function put($id, $request_data = NULL) {
  174. //if (!DolibarrApiAccess::$user->rights->user->user->creer) {
  175. //throw new RestException(401);
  176. //}
  177. $result = $this->useraccount->fetch($id);
  178. if (!$result)
  179. {
  180. throw new RestException(404, 'Account not found');
  181. }
  182. if (!DolibarrApi::_checkAccessToResource('user', $this->useraccount->id, 'user'))
  183. {
  184. throw new RestException(401, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
  185. }
  186. foreach ($request_data as $field => $value)
  187. {
  188. if ($field == 'id') continue;
  189. $this->useraccount->$field = $value;
  190. }
  191. if ($this->useraccount->update(DolibarrApiAccess::$user, 1))
  192. return $this->get($id);
  193. return false;
  194. }
  195. /**
  196. * add user to group
  197. *
  198. * @param int $id User ID
  199. * @param int $group Group ID
  200. * @return int 1 if success
  201. *
  202. * @url GET {id}/setGroup/{group}
  203. */
  204. function setGroup($id, $group) {
  205. //if (!DolibarrApiAccess::$user->rights->user->user->supprimer) {
  206. //throw new RestException(401);
  207. //}
  208. $result = $this->useraccount->fetch($id);
  209. if (!$result)
  210. {
  211. throw new RestException(404, 'User not found');
  212. }
  213. if (!DolibarrApi::_checkAccessToResource('user', $this->useraccount->id, 'user'))
  214. {
  215. throw new RestException(401, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
  216. }
  217. $result = $this->useraccount->SetInGroup($group,1);
  218. if (! ($result > 0))
  219. {
  220. throw new RestException(500, $this->useraccount->error);
  221. }
  222. return 1;
  223. }
  224. /**
  225. * Delete account
  226. *
  227. * @param int $id Account ID
  228. * @return array
  229. */
  230. function delete($id) {
  231. //if (!DolibarrApiAccess::$user->rights->user->user->supprimer) {
  232. //throw new RestException(401);
  233. //}
  234. $result = $this->useraccount->fetch($id);
  235. if (!$result)
  236. {
  237. throw new RestException(404, 'User not found');
  238. }
  239. if (!DolibarrApi::_checkAccessToResource('user', $this->useraccount->id, 'user'))
  240. {
  241. throw new RestException(401, 'Access not allowed for login ' . DolibarrApiAccess::$user->login);
  242. }
  243. return $this->useraccount->delete($id);
  244. }
  245. /**
  246. * Clean sensible object datas
  247. *
  248. * @param object $object Object to clean
  249. * @return array Array of cleaned object properties
  250. */
  251. function _cleanObjectDatas($object) {
  252. $object = parent::_cleanObjectDatas($object);
  253. unset($object->default_values);
  254. unset($object->lastsearch_values);
  255. unset($object->lastsearch_values_tmp);
  256. unset($object->total_ht);
  257. unset($object->total_tva);
  258. unset($object->total_localtax1);
  259. unset($object->total_localtax2);
  260. unset($object->total_ttc);
  261. return $object;
  262. }
  263. /**
  264. * Validate fields before create or update object
  265. *
  266. * @param array|null $data Data to validate
  267. * @return array
  268. * @throws RestException
  269. */
  270. function _validate($data) {
  271. $account = array();
  272. foreach (Users::$FIELDS as $field)
  273. {
  274. if (!isset($data[$field]))
  275. throw new RestException(400, "$field field missing");
  276. $account[$field] = $data[$field];
  277. }
  278. return $account;
  279. }
  280. }