19use Luracast\Restler\RestException;
21require_once DOL_DOCUMENT_ROOT.
'/user/class/user.class.php';
22require_once DOL_DOCUMENT_ROOT.
'/user/class/usergroup.class.php';
36 static $FIELDS = array(
53 $this->useraccount =
new User($this->db);
71 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $user_ids = 0, $category = 0, $sqlfilters =
'')
75 if (empty(DolibarrApiAccess::$user->rights->user->user->lire) && empty(DolibarrApiAccess::$user->admin)) {
76 throw new RestException(401,
"You are not allowed to read list of users");
84 $sql =
"SELECT t.rowid";
85 $sql .=
" FROM ".MAIN_DB_PREFIX.
"user AS t LEFT JOIN ".MAIN_DB_PREFIX.
"user_extrafields AS ef ON (ef.fk_object = t.rowid)";
87 $sql .=
", ".$this->db->prefix().
"categorie_user as c";
89 $sql .=
' WHERE t.entity IN ('.getEntity(
'user').
')';
91 $sql .=
" AND t.rowid IN (".$this->db->sanitize($user_ids).
")";
96 $sql .=
" AND c.fk_categorie = ".((int) $category);
97 $sql .=
" AND c.fk_user = t.rowid";
105 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
109 $sql .= $this->db->order($sortfield, $sortorder);
114 $offset = $limit * $page;
116 $sql .= $this->db->plimit($limit + 1, $offset);
119 $result = $this->db->query($sql);
123 $num = $this->db->num_rows($result);
124 $min = min($num, ($limit <= 0 ? $num : $limit));
126 $obj = $this->db->fetch_object($result);
127 $user_static =
new User($this->db);
128 if ($user_static->fetch($obj->rowid)) {
134 throw new RestException(503,
'Error when retrieve User list : '.$this->db->lasterror());
136 if (!count($obj_ret)) {
137 throw new RestException(404,
'No User found');
152 public function get($id, $includepermissions = 0)
154 if (empty(DolibarrApiAccess::$user->rights->user->user->lire) && empty(DolibarrApiAccess::$user->admin) && $id != 0 && DolibarrApiAccess::$user->id != $id) {
155 throw new RestException(401,
'Not allowed');
159 $result = $this->useraccount->initAsSpecimen();
161 $result = $this->useraccount->fetch($id);
164 throw new RestException(404,
'User not found');
168 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
171 if ($includepermissions) {
172 $this->useraccount->getRights();
194 throw new RestException(400,
'Bad parameters');
197 if (empty(DolibarrApiAccess::$user->rights->user->user->lire) && empty(DolibarrApiAccess::$user->admin) && DolibarrApiAccess::$user->login != $login) {
198 throw new RestException(401,
'Not allowed');
201 $result = $this->useraccount->fetch(
'', $login);
203 throw new RestException(404,
'User not found');
207 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
210 if ($includepermissions) {
211 $this->useraccount->getRights();
233 throw new RestException(400,
'Bad parameters');
236 if (empty(DolibarrApiAccess::$user->rights->user->user->lire) && empty(DolibarrApiAccess::$user->admin) && DolibarrApiAccess::$user->email != $email) {
237 throw new RestException(401,
'Not allowed');
240 $result = $this->useraccount->fetch(
'',
'',
'', 0, -1, $email);
242 throw new RestException(404,
'User not found');
246 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
249 if ($includepermissions) {
250 $this->useraccount->getRights();
267 public function getInfo($includepermissions = 0)
269 if (empty(DolibarrApiAccess::$user->rights->user->self->creer) && empty(DolibarrApiAccess::$user->rights->user->user->lire) && empty(DolibarrApiAccess::$user->admin)) {
270 throw new RestException(401,
'Not allowed');
273 $apiUser = DolibarrApiAccess::$user;
275 $result = $this->useraccount->fetch($apiUser->id);
277 throw new RestException(404,
'User not found');
281 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
284 if ($includepermissions) {
285 $this->useraccount->getRights();
289 $userGroupList = $usergroup->listGroupsForUser($apiUser->id,
false);
290 if (!is_array($userGroupList)) {
291 throw new RestException(404,
'User group not found');
307 public function post($request_data =
null)
310 if (empty(DolibarrApiAccess::$user->rights->user->creer) && empty(DolibarrApiAccess::$user->admin)) {
311 throw new RestException(401,
"User creation not allowed for login ".DolibarrApiAccess::$user->login);
323 foreach ($request_data as $field => $value) {
324 if (in_array($field, array(
'pass_crypted',
'pass_indatabase',
'pass_indatabase_crypted',
'pass_temp',
'api_key'))) {
326 throw new RestException(401,
'The property '.$field.
" can't be set/modified using the APIs");
336 $this->useraccount->$field = $value;
339 if ($this->useraccount->create(DolibarrApiAccess::$user) < 0) {
340 throw new RestException(500,
'Error creating', array_merge(array($this->useraccount->error), $this->useraccount->errors));
342 return $this->useraccount->id;
357 public function put($id, $request_data =
null)
360 if (!DolibarrApiAccess::$user->hasRight(
'user',
'user',
'creer') && empty(DolibarrApiAccess::$user->admin)) {
361 throw new RestException(401,
"User update not allowed");
364 $result = $this->useraccount->fetch($id);
366 throw new RestException(404,
'Account not found');
370 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
373 foreach ($request_data as $field => $value) {
374 if ($field ==
'id') {
377 if (in_array($field, array(
'pass_crypted',
'pass_indatabase',
'pass_indatabase_crypted',
'pass_temp',
'api_key'))) {
379 throw new RestException(401,
'The property '.$field.
" can't be set/modified using the APIs");
381 if ($field ==
'pass') {
382 if ($this->useraccount->id != DolibarrApiAccess::$user->id && empty(DolibarrApiAccess::$user->rights->user->user->password)) {
383 throw new RestException(401,
'You are not allowed to modify password of other users');
385 if ($this->useraccount->id == DolibarrApiAccess::$user->id && empty(DolibarrApiAccess::$user->rights->user->self->password)) {
386 throw new RestException(401,
'You are not allowed to modify your own password');
389 if (DolibarrApiAccess::$user->admin) {
390 if ($field ==
'admin' && $value != $this->useraccount->admin && empty($value)) {
391 throw new RestException(401,
'Reseting the admin status of a user is not possible using the API');
394 if ($field ==
'admin' && $value != $this->useraccount->admin) {
395 throw new RestException(401,
'Only an admin user can modify the admin status of another user');
398 if ($field ==
'entity' && $value != $this->useraccount->entity) {
399 throw new RestException(401,
'Changing entity of a user using the APIs is not possible');
403 if ($field ==
'statut') {
404 $result = $this->useraccount->setstatus($value);
406 throw new RestException(500,
'Error when updating status of user: '.$this->useraccount->error);
409 $this->useraccount->$field = $value;
415 if ($this->useraccount->update(DolibarrApiAccess::$user) >= 0) {
416 return $this->
get($id);
418 throw new RestException(500, $this->useraccount->error);
436 if (empty(DolibarrApiAccess::$user->rights->user->user->lire) && empty(DolibarrApiAccess::$user->admin)) {
437 throw new RestException(403);
442 $user =
new User($this->db);
443 $result = $user->fetch($id);
445 throw new RestException(404,
'user not found');
449 $groups = $usergroup->listGroupsForUser($id,
false);
451 foreach ($groups as $group) {
476 if (!DolibarrApiAccess::$user->hasRight(
'user',
'user',
'creer') && empty(DolibarrApiAccess::$user->admin)) {
477 throw new RestException(401);
480 $result = $this->useraccount->fetch($id);
482 throw new RestException(404,
'User not found');
486 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
489 if (isModEnabled(
'multicompany') && !empty($conf->global->MULTICOMPANY_TRANSVERSE_MODE) && !empty(DolibarrApiAccess::$user->admin) && empty(DolibarrApiAccess::$user->entity)) {
490 $entity = (!empty($entity) ? $entity : $conf->entity);
494 $entity = (DolibarrApiAccess::$user->entity > 0 ? DolibarrApiAccess::$user->entity : $conf->entity);
497 $result = $this->useraccount->SetInGroup($group, $entity);
498 if (!($result > 0)) {
499 throw new RestException(500, $this->useraccount->error);
523 public function listGroups($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $group_ids = 0, $sqlfilters =
'')
529 if ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && empty(DolibarrApiAccess::$user->rights->user->user->lire) && empty(DolibarrApiAccess::$user->admin)) ||
530 !empty($conf->global->MAIN_USE_ADVANCED_PERMS) && empty(DolibarrApiAccess::$user->rights->user->group_advance->read) && empty(DolibarrApiAccess::$user->admin)) {
531 throw new RestException(401,
"You are not allowed to read groups");
537 $sql =
"SELECT t.rowid";
538 $sql .=
" FROM ".MAIN_DB_PREFIX.
"usergroup AS t LEFT JOIN ".MAIN_DB_PREFIX.
"usergroup_extrafields AS ef ON (ef.fk_object = t.rowid)";
539 $sql .=
' WHERE t.entity IN ('.getEntity(
'user').
')';
541 $sql .=
" AND t.rowid IN (".$this->db->sanitize($group_ids).
")";
548 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
552 $sql .= $this->db->order($sortfield, $sortorder);
557 $offset = $limit * $page;
559 $sql .= $this->db->plimit($limit + 1, $offset);
562 $result = $this->db->query($sql);
566 $num = $this->db->num_rows($result);
567 $min = min($num, ($limit <= 0 ? $num : $limit));
569 $obj = $this->db->fetch_object($result);
570 $group_static =
new UserGroup($this->db);
571 if ($group_static->fetch($obj->rowid)) {
577 throw new RestException(503,
'Error when retrieve Group list : '.$this->db->lasterror());
579 if (!count($obj_ret)) {
580 throw new RestException(404,
'No Group found');
603 if ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && empty(DolibarrApiAccess::$user->rights->user->user->lire) && empty(DolibarrApiAccess::$user->admin)) ||
604 !empty($conf->global->MAIN_USE_ADVANCED_PERMS) && empty(DolibarrApiAccess::$user->rights->user->group_advance->read) && empty(DolibarrApiAccess::$user->admin)) {
605 throw new RestException(401,
"You are not allowed to read groups");
608 $group_static =
new UserGroup($this->db);
609 $result = $group_static->fetch($group,
'', $load_members);
612 throw new RestException(404,
'Group not found');
627 public function delete($id)
629 if (empty(DolibarrApiAccess::$user->rights->user->user->supprimer) && empty(DolibarrApiAccess::$user->admin)) {
630 throw new RestException(401,
'Not allowed');
632 $result = $this->useraccount->fetch($id);
634 throw new RestException(404,
'User not found');
638 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
640 $this->useraccount->oldcopy = clone $this->useraccount;
642 if (!$this->useraccount->delete(DolibarrApiAccess::$user)) {
643 throw new RestException(500);
649 'message' =>
'Ticket deleted'
666 $object = parent::_cleanObjectDatas($object);
668 unset($object->default_values);
669 unset($object->lastsearch_values);
670 unset($object->lastsearch_values_tmp);
672 unset($object->total_ht);
673 unset($object->total_tva);
674 unset($object->total_localtax1);
675 unset($object->total_localtax2);
676 unset($object->total_ttc);
678 unset($object->label_incoterms);
679 unset($object->location_incoterms);
681 unset($object->fk_delivery_address);
682 unset($object->fk_incoterms);
683 unset($object->all_permissions_are_loaded);
684 unset($object->shipping_method_id);
685 unset($object->nb_rights);
686 unset($object->search_sid);
687 unset($object->ldap_sid);
688 unset($object->clicktodial_loaded);
691 unset($object->pass);
692 unset($object->pass_indatabase);
693 unset($object->pass_indatabase_crypted);
694 unset($object->pass_temp);
695 unset($object->api_key);
696 unset($object->clicktodial_password);
697 unset($object->openid);
699 unset($object->lines);
700 unset($object->model_pdf);
702 $canreadsalary = ((isModEnabled(
'salaries') && !empty(DolibarrApiAccess::$user->rights->salaries->read)) || (empty($conf->salaries->enabled)));
704 if (!$canreadsalary) {
705 unset($object->salary);
706 unset($object->salaryextra);
722 $cleanObjectList = array();
724 foreach ($objectList as $object) {
725 $cleanObject = parent::_cleanObjectDatas($object);
727 unset($cleanObject->default_values);
728 unset($cleanObject->lastsearch_values);
729 unset($cleanObject->lastsearch_values_tmp);
731 unset($cleanObject->total_ht);
732 unset($cleanObject->total_tva);
733 unset($cleanObject->total_localtax1);
734 unset($cleanObject->total_localtax2);
735 unset($cleanObject->total_ttc);
737 unset($cleanObject->libelle_incoterms);
738 unset($cleanObject->location_incoterms);
740 unset($cleanObject->fk_delivery_address);
741 unset($cleanObject->fk_incoterms);
742 unset($cleanObject->all_permissions_are_loaded);
743 unset($cleanObject->shipping_method_id);
744 unset($cleanObject->nb_rights);
745 unset($cleanObject->search_sid);
746 unset($cleanObject->ldap_sid);
747 unset($cleanObject->clicktodial_loaded);
749 unset($cleanObject->datec);
750 unset($cleanObject->tms);
751 unset($cleanObject->members);
752 unset($cleanObject->note);
753 unset($cleanObject->note_private);
755 $cleanObjectList[] = $cleanObject;
758 return $cleanObjectList;
771 foreach (Users::$FIELDS as $field) {
772 if (!isset($data[$field])) {
773 throw new RestException(400,
"$field field missing");
775 $account[$field] = $data[$field];
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid')
Check access by user to a given resource.
Class to manage user groups.
Class to manage Dolibarr users.
put($id, $request_data=null)
Update user account.
_cleanObjectDatas($object)
Clean sensible object datas.
getInfo($includepermissions=0)
Get more properties of a user.
_cleanUserGroupListDatas($objectList)
Clean sensible user group list datas.
listGroups($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $group_ids=0, $sqlfilters='')
List Groups.
setGroup($id, $group, $entity=1)
Add a user into a group.
infoGroups($group, $load_members=0)
Get properties of an group object.
_validate($data)
Validate fields before create or update object.
getByEmail($email, $includepermissions=0)
Get properties of an user object by Email.
getGroups($id)
List the groups of a user.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $user_ids=0, $category=0, $sqlfilters='')
List Users.
post($request_data=null)
Create user account.
getByLogin($login, $includepermissions=0)
Get properties of an user object by login.
__construct()
Constructor.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria