21use Luracast\Restler\RestException;
23require_once DOL_DOCUMENT_ROOT.
'/user/class/user.class.php';
24require_once DOL_DOCUMENT_ROOT.
'/user/class/usergroup.class.php';
38 public static $FIELDS = array(
55 $this->useraccount =
new User($this->db);
76 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $user_ids =
'0', $category = 0, $sqlfilters =
'', $properties =
'')
78 if (!DolibarrApiAccess::$user->hasRight(
'user',
'user',
'lire') && empty(DolibarrApiAccess::$user->admin)) {
79 throw new RestException(403,
"You are not allowed to read list of users");
87 $sql =
"SELECT t.rowid";
88 $sql .=
" FROM ".MAIN_DB_PREFIX.
"user AS t LEFT JOIN ".MAIN_DB_PREFIX.
"user_extrafields AS ef ON (ef.fk_object = t.rowid)";
90 $sql .=
", ".$this->db->prefix().
"categorie_user as c";
92 $sql .=
' WHERE t.entity IN ('.getEntity(
'user').
')';
94 $sql .=
" AND t.rowid IN (".$this->db->sanitize($user_ids).
")";
99 $sql .=
" AND c.fk_categorie = ".((int) $category);
100 $sql .=
" AND c.fk_user = t.rowid";
108 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
112 $sql .= $this->db->order($sortfield, $sortorder);
117 $offset = $limit * $page;
119 $sql .= $this->db->plimit($limit + 1, $offset);
122 $result = $this->db->query($sql);
126 $num = $this->db->num_rows($result);
127 $min = min($num, ($limit <= 0 ? $num : $limit));
129 $obj = $this->db->fetch_object($result);
130 $user_static =
new User($this->db);
131 if ($user_static->fetch($obj->rowid)) {
137 throw new RestException(503,
'Error when retrieve User list : '.$this->db->lasterror());
155 public function get(
$id, $includepermissions = 0)
157 if (!DolibarrApiAccess::$user->hasRight(
'user',
'user',
'lire') && empty(DolibarrApiAccess::$user->admin) &&
$id != 0 && DolibarrApiAccess::$user->id !=
$id) {
158 throw new RestException(403,
'Not allowed');
162 $result = $this->useraccount->initAsSpecimen();
164 $result = $this->useraccount->fetch(
$id);
167 throw new RestException(404,
'User not found');
171 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
174 if ($includepermissions) {
175 $this->useraccount->loadRights();
199 throw new RestException(400,
'Bad parameters');
202 if (!DolibarrApiAccess::$user->hasRight(
'user',
'user',
'lire') && empty(DolibarrApiAccess::$user->admin) && DolibarrApiAccess::$user->login != $login) {
203 throw new RestException(403,
'Not allowed');
206 $result = $this->useraccount->fetch(0, $login);
208 throw new RestException(404,
'User not found');
212 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
215 if ($includepermissions) {
216 $this->useraccount->loadRights();
240 throw new RestException(400,
'Bad parameters');
243 if (!DolibarrApiAccess::$user->hasRight(
'user',
'user',
'lire') && empty(DolibarrApiAccess::$user->admin) && DolibarrApiAccess::$user->
email != $email) {
244 throw new RestException(403,
'Not allowed');
247 $result = $this->useraccount->fetch(0,
'',
'', 0, -1, $email);
249 throw new RestException(404,
'User not found');
253 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
256 if ($includepermissions) {
257 $this->useraccount->loadRights();
274 public function getInfo($includepermissions = 0)
276 if (!DolibarrApiAccess::$user->hasRight(
'user',
'self',
'creer') && !DolibarrApiAccess::$user->hasRight(
'user',
'user',
'lire') && empty(DolibarrApiAccess::$user->admin)) {
277 throw new RestException(403,
'Not allowed');
280 $apiUser = DolibarrApiAccess::$user;
282 $result = $this->useraccount->fetch($apiUser->id);
284 throw new RestException(404,
'User not found');
288 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
291 if ($includepermissions) {
292 $this->useraccount->loadRights();
296 $userGroupList = $usergroup->listGroupsForUser($apiUser->id,
false);
297 if (!is_array($userGroupList)) {
298 throw new RestException(404,
'User group not found');
316 public function post($request_data =
null)
319 if (!DolibarrApiAccess::$user->hasRight(
'user',
'creer') && empty(DolibarrApiAccess::$user->admin)) {
320 throw new RestException(403,
"User creation not allowed for login ".DolibarrApiAccess::$user->login);
332 foreach ($request_data as $field => $value) {
333 if (in_array($field, array(
'pass_crypted',
'pass_indatabase',
'pass_indatabase_crypted',
'pass_temp',
'api_key'))) {
335 throw new RestException(405,
'The property '.$field.
" can't be set/modified using the APIs");
337 if ($field ===
'caller') {
339 $this->useraccount->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
350 $this->useraccount->$field = $this->
_checkValForAPI($field, $value, $this->useraccount);
353 if ($this->useraccount->create(DolibarrApiAccess::$user) < 0) {
354 throw new RestException(500,
'Error creating', array_merge(array($this->useraccount->error), $this->useraccount->errors));
356 return $this->useraccount->id;
373 public function put(
$id, $request_data =
null)
376 if (!DolibarrApiAccess::$user->hasRight(
'user',
'user',
'creer') && empty(DolibarrApiAccess::$user->admin)) {
377 throw new RestException(403,
"User update not allowed");
380 $result = $this->useraccount->fetch(
$id);
382 throw new RestException(404,
'Account not found');
386 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
389 foreach ($request_data as $field => $value) {
390 if (in_array($field, array(
'pass_crypted',
'pass_indatabase',
'pass_indatabase_crypted',
'pass_temp',
'api_key'))) {
392 throw new RestException(405,
'The property '.$field.
" can't be set/modified using the APIs");
394 if ($field ==
'id') {
397 if ($field ==
'pass') {
398 if ($this->useraccount->id != DolibarrApiAccess::$user->id && !DolibarrApiAccess::$user->hasRight(
'user',
'user',
'password')) {
399 throw new RestException(403,
'You are not allowed to modify password of other users');
401 if ($this->useraccount->id == DolibarrApiAccess::$user->id && !DolibarrApiAccess::$user->hasRight(
'user',
'self',
'password')) {
402 throw new RestException(403,
'You are not allowed to modify your own password');
405 if ($field ===
'caller') {
407 $this->useraccount->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
411 if (DolibarrApiAccess::$user->admin) {
412 if ($field ==
'admin' && $value != $this->useraccount->admin && empty($value)) {
413 throw new RestException(403,
'Reseting the admin status of a user is not possible using the API');
416 if ($field ==
'admin' && $value != $this->useraccount->admin) {
417 throw new RestException(403,
'Only an admin user can modify the admin status of another user');
420 if ($field ==
'entity' && $value != $this->useraccount->entity) {
421 throw new RestException(403,
'Changing entity of a user using the APIs is not possible');
426 if ($field ==
'statut' || $field ==
'status') {
427 $result = $this->useraccount->setstatus($value);
429 throw new RestException(500,
'Error when updating status of user: '.$this->useraccount->error);
432 $this->useraccount->$field = $this->
_checkValForAPI($field, $value, $this->useraccount);
438 if ($this->useraccount->update(DolibarrApiAccess::$user) >= 0) {
439 return $this->
get(
$id);
441 throw new RestException(500, $this->useraccount->error);
462 throw new RestException(403,
"Error: login and password reset APIs are disabled. You can get access token from the backoffice to get access permission but permission and password manipulation from APIs are forbidden.");
467 throw new RestException(403,
"Error: password reset APIs are disabled by default. To allow this, the option API_ALLOW_PASSWORD_RESET must be set.");
470 if (!DolibarrApiAccess::$user->hasRight(
'user',
'user',
'creer') && empty(DolibarrApiAccess::$user->admin)) {
471 throw new RestException(403,
"setPassword on user not allowed for login ".DolibarrApiAccess::$user->login);
474 $result = $this->useraccount->fetch(
$id);
476 throw new RestException(404,
'User not found, no password changed');
480 throw new RestException(403,
'Access on this object not allowed for login '.DolibarrApiAccess::$user->login);
483 $newpassword = $this->useraccount->setPassword($this->useraccount,
'');
484 if (is_int($newpassword) && $newpassword < 0) {
485 throw new RestException(500,
'ErrorFailedToSetNewPassword'.$this->useraccount->error);
488 if ($send_password) {
489 if ($this->useraccount->send_password($this->useraccount, $newpassword) > 0) {
492 throw new RestException(500,
'ErrorFailedSendingNewPassword - '.$this->useraccount->error);
515 if (!DolibarrApiAccess::$user->hasRight(
'user',
'user',
'lire') && empty(DolibarrApiAccess::$user->admin)) {
516 throw new RestException(403);
519 $user =
new User($this->db);
520 $result = $user->fetch(
$id);
522 throw new RestException(404,
'user not found');
526 $groups = $usergroup->listGroupsForUser(
$id,
false);
528 foreach ($groups as $group) {
553 if (!DolibarrApiAccess::$user->hasRight(
'user',
'user',
'creer') && empty(DolibarrApiAccess::$user->admin)) {
554 throw new RestException(403,
'setGroup on users not allowed for login '.DolibarrApiAccess::$user->login);
557 $result = $this->useraccount->fetch(
$id);
559 throw new RestException(404,
'User not found');
563 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
566 if (isModEnabled(
'multicompany') &&
getDolGlobalString(
'MULTICOMPANY_TRANSVERSE_MODE') && !empty(DolibarrApiAccess::$user->admin) && empty(DolibarrApiAccess::$user->entity)) {
567 $entity = (!empty($entity) ? $entity : $conf->entity);
571 $entity = (DolibarrApiAccess::$user->entity > 0 ? DolibarrApiAccess::$user->entity : $conf->entity);
574 $result = $this->useraccount->SetInGroup($group, $entity);
575 if (!($result > 0)) {
576 throw new RestException(500, $this->useraccount->error);
604 public function listGroups($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $group_ids =
'0', $sqlfilters =
'', $properties =
'')
608 if ((!
getDolGlobalString(
'MAIN_USE_ADVANCED_PERMS') && !DolibarrApiAccess::$user->hasRight(
'user',
'user',
'lire') && empty(DolibarrApiAccess::$user->admin)) ||
609 getDolGlobalString(
'MAIN_USE_ADVANCED_PERMS') && !DolibarrApiAccess::$user->hasRight(
'user',
'group_advance',
'read') && empty(DolibarrApiAccess::$user->admin)) {
610 throw new RestException(403,
"You are not allowed to read groups");
616 $sql =
"SELECT t.rowid";
617 $sql .=
" FROM ".MAIN_DB_PREFIX.
"usergroup AS t LEFT JOIN ".MAIN_DB_PREFIX.
"usergroup_extrafields AS ef ON (ef.fk_object = t.rowid)";
618 $sql .=
' WHERE t.entity IN ('.getEntity(
'user').
')';
620 $sql .=
" AND t.rowid IN (".$this->db->sanitize($group_ids).
")";
627 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
631 $sql .= $this->db->order($sortfield, $sortorder);
636 $offset = $limit * $page;
638 $sql .= $this->db->plimit($limit + 1, $offset);
641 $result = $this->db->query($sql);
645 $num = $this->db->num_rows($result);
646 $min = min($num, ($limit <= 0 ? $num : $limit));
648 $obj = $this->db->fetch_object($result);
649 $group_static =
new UserGroup($this->db);
650 if ($group_static->fetch($obj->rowid)) {
656 throw new RestException(503,
'Error when retrieve Group list : '.$this->db->lasterror());
678 if ((!
getDolGlobalString(
'MAIN_USE_ADVANCED_PERMS') && !DolibarrApiAccess::$user->hasRight(
'user',
'user',
'lire') && empty(DolibarrApiAccess::$user->admin)) ||
679 getDolGlobalString(
'MAIN_USE_ADVANCED_PERMS') && !DolibarrApiAccess::$user->hasRight(
'user',
'group_advance',
'read') && empty(DolibarrApiAccess::$user->admin)) {
680 throw new RestException(403,
"You are not allowed to read groups");
683 $group_static =
new UserGroup($this->db);
684 $result = $group_static->fetch($group,
'', $load_members);
687 throw new RestException(404,
'Group not found');
704 public function delete(
$id)
706 if (!DolibarrApiAccess::$user->hasRight(
'user',
'user',
'supprimer') && empty(DolibarrApiAccess::$user->admin)) {
707 throw new RestException(403,
'Not allowed');
709 $result = $this->useraccount->fetch(
$id);
711 throw new RestException(404,
'User not found');
715 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
717 $this->useraccount->oldcopy = clone $this->useraccount;
719 if (!$this->useraccount->delete(DolibarrApiAccess::$user)) {
720 throw new RestException(500);
726 'message' =>
'Ticket deleted'
743 unset(
$object->default_values);
744 unset(
$object->lastsearch_values);
745 unset(
$object->lastsearch_values_tmp);
749 unset(
$object->total_localtax1);
750 unset(
$object->total_localtax2);
753 unset(
$object->label_incoterms);
754 unset(
$object->location_incoterms);
756 unset(
$object->fk_delivery_address);
758 unset(
$object->all_permissions_are_loaded);
759 unset(
$object->shipping_method_id);
763 unset(
$object->clicktodial_loaded);
767 unset(
$object->pass_indatabase);
768 unset(
$object->pass_indatabase_crypted);
771 unset(
$object->clicktodial_password);
777 $canreadsalary = ((isModEnabled(
'salaries') && DolibarrApiAccess::$user->hasRight(
'salaries',
'read')) || !isModEnabled(
'salaries'));
779 if (!$canreadsalary) {
797 $cleanObjectList = array();
799 foreach ($objectList as
$object) {
800 $cleanObject = parent::_cleanObjectDatas(
$object);
802 unset($cleanObject->default_values);
803 unset($cleanObject->lastsearch_values);
804 unset($cleanObject->lastsearch_values_tmp);
806 unset($cleanObject->total_ht);
807 unset($cleanObject->total_tva);
808 unset($cleanObject->total_localtax1);
809 unset($cleanObject->total_localtax2);
810 unset($cleanObject->total_ttc);
812 unset($cleanObject->libelle_incoterms);
813 unset($cleanObject->location_incoterms);
815 unset($cleanObject->fk_delivery_address);
816 unset($cleanObject->fk_incoterms);
817 unset($cleanObject->all_permissions_are_loaded);
818 unset($cleanObject->shipping_method_id);
819 unset($cleanObject->nb_rights);
820 unset($cleanObject->search_sid);
821 unset($cleanObject->ldap_sid);
822 unset($cleanObject->clicktodial_loaded);
824 unset($cleanObject->datec);
825 unset($cleanObject->tms);
826 unset($cleanObject->members);
827 unset($cleanObject->note);
828 unset($cleanObject->note_private);
830 $cleanObjectList[] = $cleanObject;
833 return $cleanObjectList;
846 foreach (Users::$FIELDS as $field) {
847 if (!isset($data[$field])) {
848 throw new RestException(400,
"$field field missing");
850 $account[$field] = $data[$field];
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
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.
listGroups($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $group_ids='0', $sqlfilters='', $properties='')
List Groups.
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $user_ids='0', $category=0, $sqlfilters='', $properties='')
List Users.
_cleanObjectDatas($object)
Clean sensible object datas.
getInfo($includepermissions=0)
Get more properties of a user.
_cleanUserGroupListDatas($objectList)
Clean sensible user group list datas.
setGroup($id, $group, $entity=1)
Add a user into a group.
setPassword($id, $send_password=false)
Update a user password.
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.
post($request_data=null)
Create user account.
getByLogin($login, $includepermissions=0)
Get properties of an user object by login.
__construct()
Constructor.
print $script_file $mode $langs defaultlang(is_numeric($duration_value) ? " delay=". $duration_value :"").(is_numeric($duration_value2) ? " after cd cd cd description as p label as s rowid as s nom as s email
Sender: Who sends the email ("Sender" has sent emails on behalf of "From").
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0)
forgeSQLFromUniversalSearchCriteria
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
sanitizeVal($out='', $check='alphanohtml', $filter=null, $options=null)
Return a sanitized or empty value after checking value against a rule.