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 if ($field ==
'array_options' && is_array($value)) {
410 foreach ($value as $index => $val) {
411 $this->useraccount->array_options[$index] = $this->
_checkValForAPI($field, $val, $this->useraccount);
415 $this->useraccount->$field = $value;
421 if ($this->useraccount->update(DolibarrApiAccess::$user) >= 0) {
422 return $this->
get($id);
424 throw new RestException(500, $this->useraccount->error);
442 if (empty(DolibarrApiAccess::$user->rights->user->user->lire) && empty(DolibarrApiAccess::$user->admin)) {
443 throw new RestException(403);
448 $user =
new User($this->db);
449 $result = $user->fetch($id);
451 throw new RestException(404,
'user not found');
455 $groups = $usergroup->listGroupsForUser($id,
false);
457 foreach ($groups as $group) {
482 if (!DolibarrApiAccess::$user->hasRight(
'user',
'user',
'creer') && empty(DolibarrApiAccess::$user->admin)) {
483 throw new RestException(401);
486 $result = $this->useraccount->fetch($id);
488 throw new RestException(404,
'User not found');
492 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
495 if (isModEnabled(
'multicompany') && !empty($conf->global->MULTICOMPANY_TRANSVERSE_MODE) && !empty(DolibarrApiAccess::$user->admin) && empty(DolibarrApiAccess::$user->entity)) {
496 $entity = (!empty($entity) ? $entity : $conf->entity);
500 $entity = (DolibarrApiAccess::$user->entity > 0 ? DolibarrApiAccess::$user->entity : $conf->entity);
503 $result = $this->useraccount->SetInGroup($group, $entity);
504 if (!($result > 0)) {
505 throw new RestException(500, $this->useraccount->error);
529 public function listGroups($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $group_ids = 0, $sqlfilters =
'')
535 if ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && empty(DolibarrApiAccess::$user->rights->user->user->lire) && empty(DolibarrApiAccess::$user->admin)) ||
536 !empty($conf->global->MAIN_USE_ADVANCED_PERMS) && empty(DolibarrApiAccess::$user->rights->user->group_advance->read) && empty(DolibarrApiAccess::$user->admin)) {
537 throw new RestException(401,
"You are not allowed to read groups");
543 $sql =
"SELECT t.rowid";
544 $sql .=
" FROM ".MAIN_DB_PREFIX.
"usergroup AS t LEFT JOIN ".MAIN_DB_PREFIX.
"usergroup_extrafields AS ef ON (ef.fk_object = t.rowid)";
545 $sql .=
' WHERE t.entity IN ('.getEntity(
'user').
')';
547 $sql .=
" AND t.rowid IN (".$this->db->sanitize($group_ids).
")";
554 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
558 $sql .= $this->db->order($sortfield, $sortorder);
563 $offset = $limit * $page;
565 $sql .= $this->db->plimit($limit + 1, $offset);
568 $result = $this->db->query($sql);
572 $num = $this->db->num_rows($result);
573 $min = min($num, ($limit <= 0 ? $num : $limit));
575 $obj = $this->db->fetch_object($result);
576 $group_static =
new UserGroup($this->db);
577 if ($group_static->fetch($obj->rowid)) {
583 throw new RestException(503,
'Error when retrieve Group list : '.$this->db->lasterror());
585 if (!count($obj_ret)) {
586 throw new RestException(404,
'No Group found');
609 if ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && empty(DolibarrApiAccess::$user->rights->user->user->lire) && empty(DolibarrApiAccess::$user->admin)) ||
610 !empty($conf->global->MAIN_USE_ADVANCED_PERMS) && empty(DolibarrApiAccess::$user->rights->user->group_advance->read) && empty(DolibarrApiAccess::$user->admin)) {
611 throw new RestException(401,
"You are not allowed to read groups");
614 $group_static =
new UserGroup($this->db);
615 $result = $group_static->fetch($group,
'', $load_members);
618 throw new RestException(404,
'Group not found');
633 public function delete($id)
635 if (empty(DolibarrApiAccess::$user->rights->user->user->supprimer) && empty(DolibarrApiAccess::$user->admin)) {
636 throw new RestException(401,
'Not allowed');
638 $result = $this->useraccount->fetch($id);
640 throw new RestException(404,
'User not found');
644 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
646 $this->useraccount->oldcopy = clone $this->useraccount;
648 if (!$this->useraccount->delete(DolibarrApiAccess::$user)) {
649 throw new RestException(500);
655 'message' =>
'Ticket deleted'
672 $object = parent::_cleanObjectDatas($object);
674 unset($object->default_values);
675 unset($object->lastsearch_values);
676 unset($object->lastsearch_values_tmp);
678 unset($object->total_ht);
679 unset($object->total_tva);
680 unset($object->total_localtax1);
681 unset($object->total_localtax2);
682 unset($object->total_ttc);
684 unset($object->label_incoterms);
685 unset($object->location_incoterms);
687 unset($object->fk_delivery_address);
688 unset($object->fk_incoterms);
689 unset($object->all_permissions_are_loaded);
690 unset($object->shipping_method_id);
691 unset($object->nb_rights);
692 unset($object->search_sid);
693 unset($object->ldap_sid);
694 unset($object->clicktodial_loaded);
697 unset($object->pass);
698 unset($object->pass_indatabase);
699 unset($object->pass_indatabase_crypted);
700 unset($object->pass_temp);
701 unset($object->api_key);
702 unset($object->clicktodial_password);
703 unset($object->openid);
705 unset($object->lines);
706 unset($object->model_pdf);
708 $canreadsalary = ((isModEnabled(
'salaries') && !empty(DolibarrApiAccess::$user->rights->salaries->read)) || (empty($conf->salaries->enabled)));
710 if (!$canreadsalary) {
711 unset($object->salary);
712 unset($object->salaryextra);
728 $cleanObjectList = array();
730 foreach ($objectList as $object) {
731 $cleanObject = parent::_cleanObjectDatas($object);
733 unset($cleanObject->default_values);
734 unset($cleanObject->lastsearch_values);
735 unset($cleanObject->lastsearch_values_tmp);
737 unset($cleanObject->total_ht);
738 unset($cleanObject->total_tva);
739 unset($cleanObject->total_localtax1);
740 unset($cleanObject->total_localtax2);
741 unset($cleanObject->total_ttc);
743 unset($cleanObject->libelle_incoterms);
744 unset($cleanObject->location_incoterms);
746 unset($cleanObject->fk_delivery_address);
747 unset($cleanObject->fk_incoterms);
748 unset($cleanObject->all_permissions_are_loaded);
749 unset($cleanObject->shipping_method_id);
750 unset($cleanObject->nb_rights);
751 unset($cleanObject->search_sid);
752 unset($cleanObject->ldap_sid);
753 unset($cleanObject->clicktodial_loaded);
755 unset($cleanObject->datec);
756 unset($cleanObject->tms);
757 unset($cleanObject->members);
758 unset($cleanObject->note);
759 unset($cleanObject->note_private);
761 $cleanObjectList[] = $cleanObject;
764 return $cleanObjectList;
777 foreach (Users::$FIELDS as $field) {
778 if (!isset($data[$field])) {
779 throw new RestException(400,
"$field field missing");
781 $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.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
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