api_users.class.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  1. <?php
  2. /* Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
  3. * Copyright (C) 2020 Thibault FOUCART <support@ptibogxiv.net>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use Luracast\Restler\RestException;
  19. require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
  20. require_once DOL_DOCUMENT_ROOT.'/user/class/usergroup.class.php';
  21. /**
  22. * API class for users
  23. *
  24. * @access protected
  25. * @class DolibarrApiAccess {@requires user,external}
  26. */
  27. class Users extends DolibarrApi
  28. {
  29. /**
  30. * @var array $FIELDS Mandatory fields, checked when create and update object
  31. */
  32. static $FIELDS = array(
  33. 'login',
  34. );
  35. /**
  36. * @var User $user {@type User}
  37. */
  38. public $useraccount;
  39. /**
  40. * Constructor
  41. */
  42. public function __construct()
  43. {
  44. global $db, $conf;
  45. $this->db = $db;
  46. $this->useraccount = new User($this->db);
  47. }
  48. /**
  49. * List Users
  50. *
  51. * Get a list of Users
  52. *
  53. * @param string $sortfield Sort field
  54. * @param string $sortorder Sort order
  55. * @param int $limit Limit for list
  56. * @param int $page Page number
  57. * @param string $user_ids User ids filter field. Example: '1' or '1,2,3' {@pattern /^[0-9,]*$/i}
  58. * @param int $category Use this param to filter list by category
  59. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.ref:like:'SO-%') and (t.date_creation:<:'20160101')"
  60. * @param string $properties Restrict the data returned to theses properties. Ignored if empty. Comma separated list of properties names
  61. * @return array Array of User objects
  62. */
  63. public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $user_ids = 0, $category = 0, $sqlfilters = '', $properties = '')
  64. {
  65. global $conf;
  66. if (empty(DolibarrApiAccess::$user->rights->user->user->lire) && empty(DolibarrApiAccess::$user->admin)) {
  67. throw new RestException(401, "You are not allowed to read list of users");
  68. }
  69. $obj_ret = array();
  70. // case of external user, $societe param is ignored and replaced by user's socid
  71. //$socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $societe;
  72. $sql = "SELECT t.rowid";
  73. $sql .= " FROM ".MAIN_DB_PREFIX."user AS t LEFT JOIN ".MAIN_DB_PREFIX."user_extrafields AS ef ON (ef.fk_object = t.rowid)"; // Modification VMR Global Solutions to include extrafields as search parameters in the API GET call, so we will be able to filter on extrafields
  74. if ($category > 0) {
  75. $sql .= ", ".$this->db->prefix()."categorie_user as c";
  76. }
  77. $sql .= ' WHERE t.entity IN ('.getEntity('user').')';
  78. if ($user_ids) {
  79. $sql .= " AND t.rowid IN (".$this->db->sanitize($user_ids).")";
  80. }
  81. // Select products of given category
  82. if ($category > 0) {
  83. $sql .= " AND c.fk_categorie = ".((int) $category);
  84. $sql .= " AND c.fk_user = t.rowid";
  85. }
  86. // Add sql filters
  87. if ($sqlfilters) {
  88. $errormessage = '';
  89. $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
  90. if ($errormessage) {
  91. throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
  92. }
  93. }
  94. $sql .= $this->db->order($sortfield, $sortorder);
  95. if ($limit) {
  96. if ($page < 0) {
  97. $page = 0;
  98. }
  99. $offset = $limit * $page;
  100. $sql .= $this->db->plimit($limit + 1, $offset);
  101. }
  102. $result = $this->db->query($sql);
  103. if ($result) {
  104. $i = 0;
  105. $num = $this->db->num_rows($result);
  106. $min = min($num, ($limit <= 0 ? $num : $limit));
  107. while ($i < $min) {
  108. $obj = $this->db->fetch_object($result);
  109. $user_static = new User($this->db);
  110. if ($user_static->fetch($obj->rowid)) {
  111. $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($user_static), $properties);
  112. }
  113. $i++;
  114. }
  115. } else {
  116. throw new RestException(503, 'Error when retrieve User list : '.$this->db->lasterror());
  117. }
  118. if (!count($obj_ret)) {
  119. throw new RestException(404, 'No User found');
  120. }
  121. return $obj_ret;
  122. }
  123. /**
  124. * Get properties of an user object
  125. *
  126. * @param int $id ID of user
  127. * @param int $includepermissions Set this to 1 to have the array of permissions loaded (not done by default for performance purpose)
  128. * @return array|mixed data without useless information
  129. *
  130. * @throws RestException 401 Insufficient rights
  131. * @throws RestException 404 User or group not found
  132. */
  133. public function get($id, $includepermissions = 0)
  134. {
  135. if (empty(DolibarrApiAccess::$user->rights->user->user->lire) && empty(DolibarrApiAccess::$user->admin) && $id != 0 && DolibarrApiAccess::$user->id != $id) {
  136. throw new RestException(401, 'Not allowed');
  137. }
  138. if ($id == 0) {
  139. $result = $this->useraccount->initAsSpecimen();
  140. } else {
  141. $result = $this->useraccount->fetch($id);
  142. }
  143. if (!$result) {
  144. throw new RestException(404, 'User not found');
  145. }
  146. if ($id > 0 && !DolibarrApi::_checkAccessToResource('user', $this->useraccount->id, 'user')) {
  147. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  148. }
  149. if ($includepermissions) {
  150. $this->useraccount->getRights();
  151. }
  152. return $this->_cleanObjectDatas($this->useraccount);
  153. }
  154. /**
  155. * Get properties of an user object by login
  156. *
  157. * @param string $login Login of user
  158. * @param int $includepermissions Set this to 1 to have the array of permissions loaded (not done by default for performance purpose)
  159. * @return array|mixed Data without useless information
  160. *
  161. * @url GET login/{login}
  162. *
  163. * @throws RestException 400 Bad request
  164. * @throws RestException 401 Insufficient rights
  165. * @throws RestException 404 User or group not found
  166. */
  167. public function getByLogin($login, $includepermissions = 0)
  168. {
  169. if (empty($login)) {
  170. throw new RestException(400, 'Bad parameters');
  171. }
  172. if (empty(DolibarrApiAccess::$user->rights->user->user->lire) && empty(DolibarrApiAccess::$user->admin) && DolibarrApiAccess::$user->login != $login) {
  173. throw new RestException(401, 'Not allowed');
  174. }
  175. $result = $this->useraccount->fetch('', $login);
  176. if (!$result) {
  177. throw new RestException(404, 'User not found');
  178. }
  179. if (!DolibarrApi::_checkAccessToResource('user', $this->useraccount->id, 'user')) {
  180. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  181. }
  182. if ($includepermissions) {
  183. $this->useraccount->getRights();
  184. }
  185. return $this->_cleanObjectDatas($this->useraccount);
  186. }
  187. /**
  188. * Get properties of an user object by Email
  189. *
  190. * @param string $email Email of user
  191. * @param int $includepermissions Set this to 1 to have the array of permissions loaded (not done by default for performance purpose)
  192. * @return array|mixed Data without useless information
  193. *
  194. * @url GET email/{email}
  195. *
  196. * @throws RestException 400 Bad request
  197. * @throws RestException 401 Insufficient rights
  198. * @throws RestException 404 User or group not found
  199. */
  200. public function getByEmail($email, $includepermissions = 0)
  201. {
  202. if (empty($email)) {
  203. throw new RestException(400, 'Bad parameters');
  204. }
  205. if (empty(DolibarrApiAccess::$user->rights->user->user->lire) && empty(DolibarrApiAccess::$user->admin) && DolibarrApiAccess::$user->email != $email) {
  206. throw new RestException(401, 'Not allowed');
  207. }
  208. $result = $this->useraccount->fetch('', '', '', 0, -1, $email);
  209. if (!$result) {
  210. throw new RestException(404, 'User not found');
  211. }
  212. if (!DolibarrApi::_checkAccessToResource('user', $this->useraccount->id, 'user')) {
  213. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  214. }
  215. if ($includepermissions) {
  216. $this->useraccount->getRights();
  217. }
  218. return $this->_cleanObjectDatas($this->useraccount);
  219. }
  220. /**
  221. * Get more properties of a user
  222. *
  223. * @url GET /info
  224. *
  225. * @param int $includepermissions Set this to 1 to have the array of permissions loaded (not done by default for performance purpose)
  226. * @return array|mixed Data without useless information
  227. *
  228. * @throws RestException 401 Insufficient rights
  229. * @throws RestException 404 User or group not found
  230. */
  231. public function getInfo($includepermissions = 0)
  232. {
  233. if (empty(DolibarrApiAccess::$user->rights->user->self->creer) && empty(DolibarrApiAccess::$user->rights->user->user->lire) && empty(DolibarrApiAccess::$user->admin)) {
  234. throw new RestException(401, 'Not allowed');
  235. }
  236. $apiUser = DolibarrApiAccess::$user;
  237. $result = $this->useraccount->fetch($apiUser->id);
  238. if (!$result) {
  239. throw new RestException(404, 'User not found');
  240. }
  241. if (!DolibarrApi::_checkAccessToResource('user', $this->useraccount->id, 'user')) {
  242. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  243. }
  244. if ($includepermissions) {
  245. $this->useraccount->getRights();
  246. }
  247. $usergroup = new UserGroup($this->db);
  248. $userGroupList = $usergroup->listGroupsForUser($apiUser->id, false);
  249. if (!is_array($userGroupList)) {
  250. throw new RestException(404, 'User group not found');
  251. }
  252. $this->useraccount->user_group_list = $this->_cleanUserGroupListDatas($userGroupList);
  253. return $this->_cleanObjectDatas($this->useraccount);
  254. }
  255. /**
  256. * Create user account
  257. *
  258. * @param array $request_data New user data
  259. * @return int
  260. *
  261. * @throws RestException 401 Not allowed
  262. */
  263. public function post($request_data = null)
  264. {
  265. // Check user authorization
  266. if (empty(DolibarrApiAccess::$user->rights->user->creer) && empty(DolibarrApiAccess::$user->admin)) {
  267. throw new RestException(401, "User creation not allowed for login ".DolibarrApiAccess::$user->login);
  268. }
  269. // check mandatory fields
  270. /*if (!isset($request_data["login"]))
  271. throw new RestException(400, "login field missing");
  272. if (!isset($request_data["password"]))
  273. throw new RestException(400, "password field missing");
  274. if (!isset($request_data["lastname"]))
  275. throw new RestException(400, "lastname field missing");*/
  276. //assign field values
  277. foreach ($request_data as $field => $value) {
  278. if (in_array($field, array('pass_crypted', 'pass_indatabase', 'pass_indatabase_crypted', 'pass_temp', 'api_key'))) {
  279. // This properties can't be set/modified with API
  280. throw new RestException(401, 'The property '.$field." can't be set/modified using the APIs");
  281. }
  282. /*if ($field == 'pass') {
  283. if (empty(DolibarrApiAccess::$user->rights->user->user->password)) {
  284. throw new RestException(401, 'You are not allowed to modify/set password of other users');
  285. continue;
  286. }
  287. }
  288. */
  289. $this->useraccount->$field = $value;
  290. }
  291. if ($this->useraccount->create(DolibarrApiAccess::$user) < 0) {
  292. throw new RestException(500, 'Error creating', array_merge(array($this->useraccount->error), $this->useraccount->errors));
  293. }
  294. return $this->useraccount->id;
  295. }
  296. /**
  297. * Update user account
  298. *
  299. * @param int $id Id of account to update
  300. * @param array $request_data Datas
  301. * @return array|mixed Record after update
  302. *
  303. * @throws RestException 401 Not allowed
  304. * @throws RestException 404 Not found
  305. * @throws RestException 500 System error
  306. */
  307. public function put($id, $request_data = null)
  308. {
  309. // Check user authorization
  310. if (!DolibarrApiAccess::$user->hasRight('user', 'user', 'creer') && empty(DolibarrApiAccess::$user->admin)) {
  311. throw new RestException(401, "User update not allowed");
  312. }
  313. $result = $this->useraccount->fetch($id);
  314. if (!$result) {
  315. throw new RestException(404, 'Account not found');
  316. }
  317. if (!DolibarrApi::_checkAccessToResource('user', $this->useraccount->id, 'user')) {
  318. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  319. }
  320. foreach ($request_data as $field => $value) {
  321. if ($field == 'id') {
  322. continue;
  323. }
  324. if (in_array($field, array('pass_crypted', 'pass_indatabase', 'pass_indatabase_crypted', 'pass_temp', 'api_key'))) {
  325. // This properties can't be set/modified with API
  326. throw new RestException(401, 'The property '.$field." can't be set/modified using the APIs");
  327. }
  328. if ($field == 'pass') {
  329. if ($this->useraccount->id != DolibarrApiAccess::$user->id && empty(DolibarrApiAccess::$user->rights->user->user->password)) {
  330. throw new RestException(401, 'You are not allowed to modify password of other users');
  331. }
  332. if ($this->useraccount->id == DolibarrApiAccess::$user->id && empty(DolibarrApiAccess::$user->rights->user->self->password)) {
  333. throw new RestException(401, 'You are not allowed to modify your own password');
  334. }
  335. }
  336. if (DolibarrApiAccess::$user->admin) { // If user for API is admin
  337. if ($field == 'admin' && $value != $this->useraccount->admin && empty($value)) {
  338. throw new RestException(401, 'Reseting the admin status of a user is not possible using the API');
  339. }
  340. } else {
  341. if ($field == 'admin' && $value != $this->useraccount->admin) {
  342. throw new RestException(401, 'Only an admin user can modify the admin status of another user');
  343. }
  344. }
  345. if ($field == 'entity' && $value != $this->useraccount->entity) {
  346. throw new RestException(401, 'Changing entity of a user using the APIs is not possible');
  347. }
  348. // The status must be updated using setstatus() because it
  349. // is not handled by the update() method.
  350. if ($field == 'statut') {
  351. $result = $this->useraccount->setstatus($value);
  352. if ($result < 0) {
  353. throw new RestException(500, 'Error when updating status of user: '.$this->useraccount->error);
  354. }
  355. } else {
  356. $this->useraccount->$field = $value;
  357. }
  358. }
  359. // If there is no error, update() returns the number of affected
  360. // rows so if the update is a no op, the return value is zezo.
  361. if ($this->useraccount->update(DolibarrApiAccess::$user) >= 0) {
  362. return $this->get($id);
  363. } else {
  364. throw new RestException(500, $this->useraccount->error);
  365. }
  366. }
  367. /**
  368. * List the groups of a user
  369. *
  370. * @param int $id Id of user
  371. * @return array Array of group objects
  372. *
  373. * @throws RestException 403 Not allowed
  374. * @throws RestException 404 Not found
  375. *
  376. * @url GET {id}/groups
  377. */
  378. public function getGroups($id)
  379. {
  380. if (empty(DolibarrApiAccess::$user->rights->user->user->lire) && empty(DolibarrApiAccess::$user->admin)) {
  381. throw new RestException(403);
  382. }
  383. $obj_ret = array();
  384. $user = new User($this->db);
  385. $result = $user->fetch($id);
  386. if (!$result) {
  387. throw new RestException(404, 'user not found');
  388. }
  389. $usergroup = new UserGroup($this->db);
  390. $groups = $usergroup->listGroupsForUser($id, false);
  391. $obj_ret = array();
  392. foreach ($groups as $group) {
  393. $obj_ret[] = $this->_cleanObjectDatas($group);
  394. }
  395. return $obj_ret;
  396. }
  397. /**
  398. * Add a user into a group
  399. *
  400. * @param int $id User ID
  401. * @param int $group Group ID
  402. * @param int $entity Entity ID (valid only for superadmin in multicompany transverse mode)
  403. * @return int 1 if success
  404. *
  405. * @throws RestException 401 Not allowed
  406. * @throws RestException 404 User not found
  407. * @throws RestException 500 System error
  408. *
  409. * @url GET {id}/setGroup/{group}
  410. */
  411. public function setGroup($id, $group, $entity = 1)
  412. {
  413. global $conf;
  414. if (!DolibarrApiAccess::$user->hasRight('user', 'user', 'creer') && empty(DolibarrApiAccess::$user->admin)) {
  415. throw new RestException(401);
  416. }
  417. $result = $this->useraccount->fetch($id);
  418. if (!$result) {
  419. throw new RestException(404, 'User not found');
  420. }
  421. if (!DolibarrApi::_checkAccessToResource('user', $this->useraccount->id, 'user')) {
  422. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  423. }
  424. if (isModEnabled('multicompany') && getDolGlobalString('MULTICOMPANY_TRANSVERSE_MODE') && !empty(DolibarrApiAccess::$user->admin) && empty(DolibarrApiAccess::$user->entity)) {
  425. $entity = (!empty($entity) ? $entity : $conf->entity);
  426. } else {
  427. // When using API, action is done on entity of logged user because a user of entity X with permission to create user should not be able to
  428. // hack the security by giving himself permissions on another entity.
  429. $entity = (DolibarrApiAccess::$user->entity > 0 ? DolibarrApiAccess::$user->entity : $conf->entity);
  430. }
  431. $result = $this->useraccount->SetInGroup($group, $entity);
  432. if (!($result > 0)) {
  433. throw new RestException(500, $this->useraccount->error);
  434. }
  435. return 1;
  436. }
  437. /**
  438. * List Groups
  439. *
  440. * Return an array with a list of Groups
  441. *
  442. * @url GET /groups
  443. *
  444. * @param string $sortfield Sort field
  445. * @param string $sortorder Sort order
  446. * @param int $limit Limit for list
  447. * @param int $page Page number
  448. * @param string $group_ids Groups ids filter field. Example: '1' or '1,2,3' {@pattern /^[0-9,]*$/i}
  449. * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.ref:like:'SO-%') and (t.date_creation:<:'20160101')"
  450. * @param string $properties Restrict the data returned to theses properties. Ignored if empty. Comma separated list of properties names
  451. * @return array Array of User objects
  452. *
  453. * @throws RestException 404 User not found
  454. * @throws RestException 503 Error
  455. */
  456. public function listGroups($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $group_ids = 0, $sqlfilters = '', $properties = '')
  457. {
  458. global $conf;
  459. $obj_ret = array();
  460. if ((!getDolGlobalString('MAIN_USE_ADVANCED_PERMS') && empty(DolibarrApiAccess::$user->rights->user->user->lire) && empty(DolibarrApiAccess::$user->admin)) ||
  461. getDolGlobalString('MAIN_USE_ADVANCED_PERMS') && empty(DolibarrApiAccess::$user->rights->user->group_advance->read) && empty(DolibarrApiAccess::$user->admin)) {
  462. throw new RestException(401, "You are not allowed to read groups");
  463. }
  464. // case of external user, $societe param is ignored and replaced by user's socid
  465. //$socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $societe;
  466. $sql = "SELECT t.rowid";
  467. $sql .= " FROM ".MAIN_DB_PREFIX."usergroup AS t LEFT JOIN ".MAIN_DB_PREFIX."usergroup_extrafields AS ef ON (ef.fk_object = t.rowid)"; // Modification VMR Global Solutions to include extrafields as search parameters in the API GET call, so we will be able to filter on extrafields
  468. $sql .= ' WHERE t.entity IN ('.getEntity('user').')';
  469. if ($group_ids) {
  470. $sql .= " AND t.rowid IN (".$this->db->sanitize($group_ids).")";
  471. }
  472. // Add sql filters
  473. if ($sqlfilters) {
  474. $errormessage = '';
  475. $sql .= forgeSQLFromUniversalSearchCriteria($sqlfilters, $errormessage);
  476. if ($errormessage) {
  477. throw new RestException(400, 'Error when validating parameter sqlfilters -> '.$errormessage);
  478. }
  479. }
  480. $sql .= $this->db->order($sortfield, $sortorder);
  481. if ($limit) {
  482. if ($page < 0) {
  483. $page = 0;
  484. }
  485. $offset = $limit * $page;
  486. $sql .= $this->db->plimit($limit + 1, $offset);
  487. }
  488. $result = $this->db->query($sql);
  489. if ($result) {
  490. $i = 0;
  491. $num = $this->db->num_rows($result);
  492. $min = min($num, ($limit <= 0 ? $num : $limit));
  493. while ($i < $min) {
  494. $obj = $this->db->fetch_object($result);
  495. $group_static = new UserGroup($this->db);
  496. if ($group_static->fetch($obj->rowid)) {
  497. $obj_ret[] = $this->_filterObjectProperties($this->_cleanObjectDatas($group_static), $properties);
  498. }
  499. $i++;
  500. }
  501. } else {
  502. throw new RestException(503, 'Error when retrieve Group list : '.$this->db->lasterror());
  503. }
  504. if (!count($obj_ret)) {
  505. throw new RestException(404, 'No Group found');
  506. }
  507. return $obj_ret;
  508. }
  509. /**
  510. * Get properties of an group object
  511. *
  512. * Return an array with group informations
  513. *
  514. * @url GET /groups/{group}
  515. *
  516. * @param int $group ID of group
  517. * @param int $load_members Load members list or not {@min 0} {@max 1}
  518. * @return object object of User objects
  519. *
  520. * @throws RestException 401 Not allowed
  521. * @throws RestException 404 User not found
  522. */
  523. public function infoGroups($group, $load_members = 0)
  524. {
  525. global $db, $conf;
  526. if ((!getDolGlobalString('MAIN_USE_ADVANCED_PERMS') && empty(DolibarrApiAccess::$user->rights->user->user->lire) && empty(DolibarrApiAccess::$user->admin)) ||
  527. getDolGlobalString('MAIN_USE_ADVANCED_PERMS') && empty(DolibarrApiAccess::$user->rights->user->group_advance->read) && empty(DolibarrApiAccess::$user->admin)) {
  528. throw new RestException(401, "You are not allowed to read groups");
  529. }
  530. $group_static = new UserGroup($this->db);
  531. $result = $group_static->fetch($group, '', $load_members);
  532. if (!$result) {
  533. throw new RestException(404, 'Group not found');
  534. }
  535. return $this->_cleanObjectDatas($group_static);
  536. }
  537. /**
  538. * Delete account/user
  539. *
  540. * @param int $id Account ID
  541. * @return array
  542. *
  543. * @throws RestException 401 Not allowed
  544. * @throws RestException 404 User not found
  545. */
  546. public function delete($id)
  547. {
  548. if (empty(DolibarrApiAccess::$user->rights->user->user->supprimer) && empty(DolibarrApiAccess::$user->admin)) {
  549. throw new RestException(401, 'Not allowed');
  550. }
  551. $result = $this->useraccount->fetch($id);
  552. if (!$result) {
  553. throw new RestException(404, 'User not found');
  554. }
  555. if (!DolibarrApi::_checkAccessToResource('user', $this->useraccount->id, 'user')) {
  556. throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
  557. }
  558. $this->useraccount->oldcopy = clone $this->useraccount;
  559. if (!$this->useraccount->delete(DolibarrApiAccess::$user)) {
  560. throw new RestException(500);
  561. }
  562. return array(
  563. 'success' => array(
  564. 'code' => 200,
  565. 'message' => 'Ticket deleted'
  566. )
  567. );
  568. }
  569. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.PublicUnderscore
  570. /**
  571. * Clean sensible object datas
  572. *
  573. * @param Object $object Object to clean
  574. * @return Object Object with cleaned properties
  575. */
  576. protected function _cleanObjectDatas($object)
  577. {
  578. // phpcs:enable
  579. global $conf;
  580. $object = parent::_cleanObjectDatas($object);
  581. unset($object->default_values);
  582. unset($object->lastsearch_values);
  583. unset($object->lastsearch_values_tmp);
  584. unset($object->total_ht);
  585. unset($object->total_tva);
  586. unset($object->total_localtax1);
  587. unset($object->total_localtax2);
  588. unset($object->total_ttc);
  589. unset($object->label_incoterms);
  590. unset($object->location_incoterms);
  591. unset($object->fk_delivery_address);
  592. unset($object->fk_incoterms);
  593. unset($object->all_permissions_are_loaded);
  594. unset($object->shipping_method_id);
  595. unset($object->nb_rights);
  596. unset($object->search_sid);
  597. unset($object->ldap_sid);
  598. unset($object->clicktodial_loaded);
  599. // List of properties never returned by API, whatever are permissions
  600. unset($object->pass);
  601. unset($object->pass_indatabase);
  602. unset($object->pass_indatabase_crypted);
  603. unset($object->pass_temp);
  604. unset($object->api_key);
  605. unset($object->clicktodial_password);
  606. unset($object->openid);
  607. unset($object->lines);
  608. unset($object->model_pdf);
  609. $canreadsalary = ((isModEnabled('salaries') && !empty(DolibarrApiAccess::$user->rights->salaries->read)) || (empty($conf->salaries->enabled)));
  610. if (!$canreadsalary) {
  611. unset($object->salary);
  612. unset($object->salaryextra);
  613. unset($object->thm);
  614. unset($object->tjm);
  615. }
  616. return $object;
  617. }
  618. /**
  619. * Clean sensible user group list datas
  620. *
  621. * @param array $objectList Array of object to clean
  622. * @return array Array of cleaned object properties
  623. */
  624. private function _cleanUserGroupListDatas($objectList)
  625. {
  626. $cleanObjectList = array();
  627. foreach ($objectList as $object) {
  628. $cleanObject = parent::_cleanObjectDatas($object);
  629. unset($cleanObject->default_values);
  630. unset($cleanObject->lastsearch_values);
  631. unset($cleanObject->lastsearch_values_tmp);
  632. unset($cleanObject->total_ht);
  633. unset($cleanObject->total_tva);
  634. unset($cleanObject->total_localtax1);
  635. unset($cleanObject->total_localtax2);
  636. unset($cleanObject->total_ttc);
  637. unset($cleanObject->libelle_incoterms);
  638. unset($cleanObject->location_incoterms);
  639. unset($cleanObject->fk_delivery_address);
  640. unset($cleanObject->fk_incoterms);
  641. unset($cleanObject->all_permissions_are_loaded);
  642. unset($cleanObject->shipping_method_id);
  643. unset($cleanObject->nb_rights);
  644. unset($cleanObject->search_sid);
  645. unset($cleanObject->ldap_sid);
  646. unset($cleanObject->clicktodial_loaded);
  647. unset($cleanObject->datec);
  648. unset($cleanObject->tms);
  649. unset($cleanObject->members);
  650. unset($cleanObject->note);
  651. unset($cleanObject->note_private);
  652. $cleanObjectList[] = $cleanObject;
  653. }
  654. return $cleanObjectList;
  655. }
  656. /**
  657. * Validate fields before create or update object
  658. *
  659. * @param array|null $data Data to validate
  660. * @return array
  661. * @throws RestException
  662. */
  663. private function _validate($data)
  664. {
  665. $account = array();
  666. foreach (Users::$FIELDS as $field) {
  667. if (!isset($data[$field])) {
  668. throw new RestException(400, "$field field missing");
  669. }
  670. $account[$field] = $data[$field];
  671. }
  672. return $account;
  673. }
  674. }