api_users.class.php 25 KB

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