24use Luracast\Restler\RestException;
26require_once DOL_DOCUMENT_ROOT.
'/user/class/user.class.php';
27require_once DOL_DOCUMENT_ROOT.
'/user/class/usergroup.class.php';
28require_once DOL_DOCUMENT_ROOT.
'/core/class/notify.class.php';
44 public static $FIELDS = array(
61 $this->useraccount =
new User($this->db);
86 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $user_ids =
'0', $category = 0, $sqlfilters =
'', $properties =
'')
88 if (!DolibarrApiAccess::$user->hasRight(
'user',
'user',
'lire') && empty(DolibarrApiAccess::$user->admin)) {
89 throw new RestException(403,
"You are not allowed to read list of users");
97 $sql =
"SELECT t.rowid";
98 $sql .=
" FROM ".MAIN_DB_PREFIX.
"user AS t LEFT JOIN ".MAIN_DB_PREFIX.
"user_extrafields AS ef ON (ef.fk_object = t.rowid)";
100 $sql .=
", ".$this->db->prefix().
"categorie_user as c";
102 $sql .=
' WHERE t.entity IN ('.getEntity(
'user').
')';
104 $sql .=
" AND t.rowid IN (".$this->db->sanitize($user_ids).
")";
109 $sql .=
" AND c.fk_categorie = ".((int) $category);
110 $sql .=
" AND c.fk_user = t.rowid";
116 $forbiddenfilterfields = array(
123 $canreadsalary = ((
isModEnabled(
'salaries') && DolibarrApiAccess::$user->hasRight(
'salaries',
'read')) || !
isModEnabled(
'salaries'));
124 if (!$canreadsalary) {
125 $forbiddenfilterfields[] =
'salary';
126 $forbiddenfilterfields[] =
'salaryextra';
127 $forbiddenfilterfields[] =
'thm';
128 $forbiddenfilterfields[] =
'tjm';
134 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
138 $sql .= $this->db->order($sortfield, $sortorder);
143 $offset = $limit * $page;
145 $sql .= $this->db->plimit($limit + 1, $offset);
148 $result = $this->db->query($sql);
152 $num = $this->db->num_rows($result);
153 $min = min($num, ($limit <= 0 ? $num : $limit));
155 $obj = $this->db->fetch_object($result);
156 $user_static =
new User($this->db);
157 if ($user_static->fetch($obj->rowid)) {
163 throw new RestException(503,
'Error when retrieve User list : '.$this->db->lasterror());
184 public function get(
$id, $includepermissions = 0)
187 throw new RestException(400,
'No user with id=0 can exist');
190 if (!DolibarrApiAccess::$user->hasRight(
'user',
'user',
'lire') && empty(DolibarrApiAccess::$user->admin) && DolibarrApiAccess::$user->
id !=
$id) {
191 throw new RestException(403,
'Not allowed');
195 $result = $this->useraccount->initAsSpecimen();
197 $result = $this->useraccount->fetch(
$id);
200 throw new RestException(404,
'User not found');
204 throw new RestException(403,
'Access on this object not allowed for login '.DolibarrApiAccess::$user->login);
207 if ($includepermissions) {
208 $this->useraccount->loadRights();
234 throw new RestException(400,
'Bad parameters');
237 if (!DolibarrApiAccess::$user->hasRight(
'user',
'user',
'lire') && empty(DolibarrApiAccess::$user->admin) && DolibarrApiAccess::$user->login != $login) {
238 throw new RestException(403,
'Not allowed');
241 $result = $this->useraccount->fetch(0, $login);
243 throw new RestException(404,
'User not found');
247 throw new RestException(403,
'Access on this object not allowed for login '.DolibarrApiAccess::$user->login);
250 if ($includepermissions) {
251 $this->useraccount->loadRights();
277 throw new RestException(400,
'Bad parameters');
280 if (!DolibarrApiAccess::$user->hasRight(
'user',
'user',
'lire') && empty(DolibarrApiAccess::$user->admin) && DolibarrApiAccess::$user->
email != $email) {
281 throw new RestException(403,
'Not allowed');
284 $result = $this->useraccount->fetch(0,
'',
'', 0, -1, $email);
286 throw new RestException(404,
'User not found');
290 throw new RestException(403,
'Access on this object not allowed for login '.DolibarrApiAccess::$user->login);
293 if ($includepermissions) {
294 $this->useraccount->loadRights();
315 public function getInfo($includepermissions = 0)
317 if (!DolibarrApiAccess::$user->hasRight(
'user',
'self',
'creer') && !DolibarrApiAccess::$user->hasRight(
'user',
'user',
'lire') && empty(DolibarrApiAccess::$user->admin)) {
318 throw new RestException(403,
'Not allowed');
321 $apiUser = DolibarrApiAccess::$user;
323 $result = $this->useraccount->fetch($apiUser->id);
325 throw new RestException(404,
'User not found');
329 throw new RestException(403,
'Access on this object not allowed to current logged user');
332 if ($includepermissions) {
333 $this->useraccount->loadRights();
337 $userGroupList = $usergroup->listGroupsForUser($apiUser->id,
false);
338 if (!is_array($userGroupList)) {
339 throw new RestException(404,
'User group not found');
359 public function post($request_data =
null)
362 if (!DolibarrApiAccess::$user->hasRight(
'user',
'user',
'creer') && empty(DolibarrApiAccess::$user->admin)) {
363 throw new RestException(403,
"User creation not allowed for login ".DolibarrApiAccess::$user->login);
367 if (!isset($request_data[
"login"]))
368 throw new RestException(500,
"login field missing");
375 foreach ($request_data as $field => $value) {
376 if (in_array($field, array(
'pass_crypted',
'pass_indatabase',
'pass_indatabase_crypted',
'pass_temp',
'api_key',
'openid'))) {
378 throw new RestException(405,
'The property '.$field.
" can't be set/modified using the APIs");
380 if ($field ==
'pass') {
381 if (!DolibarrApiAccess::$user->hasRight(
'user',
'user',
'password')) {
382 throw new RestException(403,
'You are not allowed to modify/set password of other users');
384 if (!DolibarrApiAccess::$user->admin) {
385 throw new RestException(403,
'As a non admin user, you are not allowed to set a password from this API. Use the /setPassword endpoint for this.');
389 $canreadsalary = ((
isModEnabled(
'salaries') && DolibarrApiAccess::$user->hasRight(
'salaries',
'read')) || !
isModEnabled(
'salaries'));
390 if (!$canreadsalary) {
391 if (in_array($field, array(
'salary',
'salaryextra',
'thm',
'tjm'))) {
393 throw new RestException(405,
'The property '.$field.
" can't be set/modified using the APIs with permission on salaries");
397 if ($field ===
'caller') {
399 $this->useraccount->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
403 if (DolibarrApiAccess::$user->admin) {
404 if ($field ==
'admin' && $value != $this->useraccount->admin && empty($value)) {
405 throw new RestException(403,
'Reseting the admin status of a user is not possible using the API');
408 if ($field ==
'admin' && $value != $this->useraccount->admin) {
409 throw new RestException(403,
'Only an admin user can modify the admin status of another user');
413 $this->useraccount->$field = $this->
_checkValForAPI($field, $value, $this->useraccount);
416 if ($this->useraccount->create(DolibarrApiAccess::$user) < 0) {
417 throw new RestException(500,
'Error creating', array_merge(array($this->useraccount->error), $this->useraccount->errors));
419 return $this->useraccount->id;
438 public function put(
$id, $request_data =
null)
440 $isSelfUpdate = ((int)
$id === (
int) DolibarrApiAccess::$user->id);
444 !DolibarrApiAccess::$user->hasRight(
'user',
'user',
'creer')
445 && !DolibarrApiAccess::$user->hasRight(
'user',
'user',
'write')
449 DolibarrApiAccess::$user->hasRight(
'user',
'self',
'creer')
450 || DolibarrApiAccess::$user->hasRight(
'user',
'self',
'write')
453 && empty(DolibarrApiAccess::$user->admin)
455 throw new RestException(403,
"User update not allowed");
458 $result = $this->useraccount->fetch(
$id);
460 throw new RestException(404,
'Account not found');
464 throw new RestException(403,
'Access on this object not allowed for login '.DolibarrApiAccess::$user->login);
467 foreach ($request_data as $field => $value) {
468 if (in_array($field, array(
'pass_crypted',
'pass_indatabase',
'pass_indatabase_crypted',
'pass_temp',
'api_key',
'openid'))) {
470 throw new RestException(405,
'The property '.$field.
" can't be set/modified using the APIs");
473 $canreadsalary = ((
isModEnabled(
'salaries') && DolibarrApiAccess::$user->hasRight(
'salaries',
'read')) || !
isModEnabled(
'salaries'));
474 if (!$canreadsalary) {
475 if (in_array($field, array(
'salary',
'salaryextra',
'thm',
'tjm'))) {
477 throw new RestException(405,
'The property '.$field.
" can't be set/modified using the APIs with permission on salaries");
481 if ($field ==
'id') {
484 if ($field ==
'pass') {
485 if ($this->useraccount->id != DolibarrApiAccess::$user->id && !DolibarrApiAccess::$user->hasRight(
'user',
'user',
'password')) {
486 throw new RestException(403,
'You are not allowed to modify password of other users');
488 if ($this->useraccount->id == DolibarrApiAccess::$user->id && !DolibarrApiAccess::$user->hasRight(
'user',
'self',
'password')) {
489 throw new RestException(403,
'You are not allowed to modify your own password');
491 if (!DolibarrApiAccess::$user->admin) {
492 throw new RestException(403,
'As a non admin user, you are not allowed to set a password from this API. Use the /setPassword endpoint for this.');
495 if ($field ===
'caller') {
497 $this->useraccount->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
500 if ($field ==
'array_options' && is_array($value)) {
501 foreach ($value as $index => $val) {
507 if (DolibarrApiAccess::$user->admin) {
508 if ($field ==
'admin' && $value != $this->useraccount->admin && empty($value)) {
509 throw new RestException(403,
'Reseting the admin status of a user is not possible using the API');
512 if ($field ==
'admin' && $value != $this->useraccount->admin) {
513 throw new RestException(403,
'Only an admin user can modify the admin status of another user');
516 if ($field ==
'entity' && $value != $this->useraccount->entity) {
517 throw new RestException(403,
'Changing entity of a user using the APIs is not possible');
522 if ($field ==
'statut' || $field ==
'status') {
523 $result = $this->useraccount->setstatus($value);
525 throw new RestException(500,
'Error when updating status of user: '.$this->useraccount->errorsToString());
528 $this->useraccount->$field = $this->
_checkValForAPI($field, $value, $this->useraccount);
534 if ($this->useraccount->update(DolibarrApiAccess::$user) >= 0) {
535 return $this->
get(
$id);
537 throw new RestException(500, $this->useraccount->errorsToString());
559 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.");
563 throw new RestException(403,
"Error: password reset APIs are disabled by default. To allow this, the option API_ALLOW_PASSWORD_RESET must be set.");
566 if (
$id != DolibarrApiAccess::$user->
id && !DolibarrApiAccess::$user->hasRight(
'user',
'user',
'password')) {
567 throw new RestException(403,
'You are not allowed to modify password of other users');
569 if (
$id == DolibarrApiAccess::$user->
id && !DolibarrApiAccess::$user->hasRight(
'user',
'self',
'password')) {
570 throw new RestException(403,
'You are not allowed to modify your own password');
573 $result = $this->useraccount->fetch(
$id);
575 throw new RestException(404,
'User not found, no password changed');
579 throw new RestException(403,
'Access on this object not allowed for login '.DolibarrApiAccess::$user->login);
582 $newpassword = $this->useraccount->setPassword($this->useraccount,
'');
583 if (is_int($newpassword) && $newpassword < 0) {
584 throw new RestException(500,
'ErrorFailedToSetNewPassword'.$this->useraccount->errorsToString());
587 if ($send_password) {
588 if ($this->useraccount->send_password($this->useraccount, $newpassword) > 0) {
591 throw new RestException(500,
'ErrorFailedSendingNewPassword - '.$this->useraccount->errorsToString());
618 throw new RestException(400,
'No user with id=0 can exist');
621 if (!DolibarrApiAccess::$user->hasRight(
'user',
'user',
'lire') && empty(DolibarrApiAccess::$user->admin)) {
622 throw new RestException(403);
625 $user =
new User($this->db);
626 $result = $user->fetch(
$id);
628 throw new RestException(404,
'User not found');
631 throw new RestException(403,
'Access on this object not allowed for login '.DolibarrApiAccess::$user->login);
635 $groups = $usergroup->listGroupsForUser(
$id,
false);
637 foreach ($groups as $group) {
664 if (!DolibarrApiAccess::$user->hasRight(
'user',
'user',
'creer') && empty(DolibarrApiAccess::$user->admin)) {
665 throw new RestException(403,
'setGroup on users not allowed for login '.DolibarrApiAccess::$user->login);
668 $result = $this->useraccount->fetch(
$id);
670 throw new RestException(404,
'User not found');
674 throw new RestException(403,
'Access on this object not allowed for login '.DolibarrApiAccess::$user->login);
677 if (
isModEnabled(
'multicompany') &&
getDolGlobalString(
'MULTICOMPANY_TRANSVERSE_MODE') && !empty(DolibarrApiAccess::$user->admin) && empty(DolibarrApiAccess::$user->entity)) {
678 $entity = (!empty($entity) ? (int) $entity :
$conf->entity);
682 $entity = (((int) DolibarrApiAccess::$user->entity) > 0 ? (int) DolibarrApiAccess::$user->entity :
$conf->entity);
685 $result = $this->useraccount->setInGroup($group, $entity);
686 if (!($result > 0)) {
687 throw new RestException(500, $this->useraccount->errorsToString());
709 if (!DolibarrApiAccess::$user->hasRight(
'user',
'group_advance',
'write') && empty(DolibarrApiAccess::$user->admin)) {
710 throw new RestException(403,
"Usergroup creation not allowed for login ".DolibarrApiAccess::$user->login);
713 foreach ($request_data as $field => $value) {
714 if ($field ===
'caller') {
716 $usergroup->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
719 if ($field ==
'id') {
720 throw new RestException(400,
'Creating with id field is forbidden');
723 $usergroup->$field = $this->
_checkValForAPI($field, $value, $usergroup);
726 if ($usergroup->create(1) < 0) {
727 throw new RestException(500,
'Error creating', array_merge(array($usergroup->error), $usergroup->errors));
729 return $usergroup->id;
752 if (!DolibarrApiAccess::$user->hasRight(
'user',
'group_advance',
'write') && empty(DolibarrApiAccess::$user->admin)) {
753 throw new RestException(403,
"Usergroup update not allowed");
758 $result = $usergroup->fetch($group);
760 throw new RestException(404,
'Usergroup not found');
764 throw new RestException(403,
'Access on this object not allowed for login '.DolibarrApiAccess::$user->login);
767 foreach ($request_data as $field => $value) {
768 if ($field ==
'id') {
769 throw new RestException(400,
'Updating with id field is forbidden');
771 if ($field ===
'caller') {
773 $usergroup->context[
'caller'] =
sanitizeVal($request_data[
'caller'],
'aZ09');
777 if ($field ==
'entity' && $value != $usergroup->entity) {
778 throw new RestException(403,
'Changing entity of a user using the APIs is not possible');
781 $usergroup->$field = $this->
_checkValForAPI($field, $value, $usergroup);
786 if ($usergroup->update() >= 0) {
789 throw new RestException(500, $usergroup->errorsToString());
812 if (!DolibarrApiAccess::$user->admin) {
813 throw new RestException(403,
'Only admin can remove users from groups');
816 $result = $this->useraccount->fetch(
$id);
818 throw new RestException(404,
'User not found');
824 $entity =
$conf->entity;
826 $entity = (((int) DolibarrApiAccess::$user->entity) > 0 ? (int) DolibarrApiAccess::$user->entity :
$conf->entity);
829 $result = $this->useraccount->removeFromGroup($group, $entity);
830 if (!($result > 0)) {
831 throw new RestException(500, $this->useraccount->error);
836 'message' =>
"User $id removed from group $group"
864 public function listGroups($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $group_ids =
'0', $sqlfilters =
'', $properties =
'')
868 if ((!
getDolGlobalString(
'MAIN_USE_ADVANCED_PERMS') && !DolibarrApiAccess::$user->hasRight(
'user',
'user',
'lire') && empty(DolibarrApiAccess::$user->admin)) ||
869 getDolGlobalString(
'MAIN_USE_ADVANCED_PERMS') && !DolibarrApiAccess::$user->hasRight(
'user',
'group_advance',
'read') && empty(DolibarrApiAccess::$user->admin)) {
870 throw new RestException(403,
"You are not allowed to read groups");
876 $sql =
"SELECT t.rowid";
877 $sql .=
" FROM ".MAIN_DB_PREFIX.
"usergroup AS t LEFT JOIN ".MAIN_DB_PREFIX.
"usergroup_extrafields AS ef ON (ef.fk_object = t.rowid)";
878 $sql .=
' WHERE t.entity IN ('.getEntity(
'user').
')';
880 $sql .=
" AND t.rowid IN (".$this->db->sanitize($group_ids).
")";
887 throw new RestException(400,
'Error when validating parameter sqlfilters -> '.$errormessage);
891 $sql .= $this->db->order($sortfield, $sortorder);
896 $offset = $limit * $page;
898 $sql .= $this->db->plimit($limit + 1, $offset);
901 $result = $this->db->query($sql);
905 $num = $this->db->num_rows($result);
906 $min = min($num, ($limit <= 0 ? $num : $limit));
908 $obj = $this->db->fetch_object($result);
909 $group_static =
new UserGroup($this->db);
910 if ($group_static->fetch($obj->rowid)) {
916 throw new RestException(503,
'Error when retrieve Group list : '.$this->db->lasterror());
940 public function infoGroups($group, $load_members = 0, $includepermissions = 0)
943 throw new RestException(400,
'No usergroup with id=0 can exist');
946 if ((!
getDolGlobalString(
'MAIN_USE_ADVANCED_PERMS') && !DolibarrApiAccess::$user->hasRight(
'user',
'user',
'lire') && empty(DolibarrApiAccess::$user->admin)) ||
947 getDolGlobalString(
'MAIN_USE_ADVANCED_PERMS') && !DolibarrApiAccess::$user->hasRight(
'user',
'group_advance',
'read') && empty(DolibarrApiAccess::$user->admin)) {
948 throw new RestException(403,
"You are not allowed to read groups");
951 $group_static =
new UserGroup($this->db);
952 $result = $group_static->fetch($group,
'', (
bool) $load_members);
955 throw new RestException(404,
'Usergroup not found');
959 throw new RestException(403,
'Access on this object not allowed for login '.DolibarrApiAccess::$user->login);
962 if ($includepermissions) {
963 $group_static->loadRights();
966 if ($load_members > 0 && is_array($group_static->members) && count($group_static->members) > 0) {
967 foreach ($group_static->members as &$member) {
988 public function delete(
$id)
990 if (!DolibarrApiAccess::$user->hasRight(
'user',
'user',
'supprimer') && empty(DolibarrApiAccess::$user->admin)) {
991 throw new RestException(403,
'Not allowed');
993 $result = $this->useraccount->fetch(
$id);
995 throw new RestException(404,
'User not found');
999 throw new RestException(403,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
1002 if ($this->useraccount->admin && empty(DolibarrApiAccess::$user->admin)) {
1003 throw new RestException(403,
'Only admin users can delete admin users');
1005 if ($this->useraccount->admin && empty($this->useraccount->entity) && !empty(DolibarrApiAccess::$user->entity)) {
1006 throw new RestException(403,
'Only superadmin users can delete superadmin users');
1009 $this->useraccount->oldcopy = clone $this->useraccount;
1011 if (!$this->useraccount->delete(DolibarrApiAccess::$user)) {
1012 throw new RestException(500);
1018 'message' =>
'User deleted'
1040 if (!DolibarrApiAccess::$user->hasRight(
'user',
'group_advance',
'delete') && empty(DolibarrApiAccess::$user->admin)) {
1041 throw new RestException(403,
'Not allowed');
1046 $result = $usergroup->fetch($group);
1048 throw new RestException(404,
'Usergroup not found');
1052 throw new RestException(403,
'Access on this object not allowed for login '.DolibarrApiAccess::$user->login);
1055 if (!$usergroup->delete(DolibarrApiAccess::$user)) {
1056 throw new RestException(500);
1062 'message' =>
'Usergroup deleted'
1087 throw new RestException(400,
'No user with id=0 can exist');
1089 if (!DolibarrApiAccess::$user->hasRight(
'user',
'user',
'lire') && empty(DolibarrApiAccess::$user->admin)) {
1090 throw new RestException(403);
1093 throw new RestException(403,
'Access on this object not allowed for login '.DolibarrApiAccess::$user->login);
1100 $sql =
"SELECT rowid as id, fk_action as event, fk_user, type, datec, tms";
1101 $sql .=
" FROM ".MAIN_DB_PREFIX.
"notify_def";
1102 $sql .=
" WHERE fk_user = ".((int)
$id);
1104 $result = $this->db->query($sql);
1105 if ($this->db->num_rows($result) == 0) {
1106 throw new RestException(404,
'Notification not found');
1111 $notifications = array();
1114 $num = $this->db->num_rows($result);
1118 $obj = $this->db->fetch_object($result);
1119 $notifications[] = $obj;
1123 throw new RestException(404,
'No notifications found');
1126 $fields = array(
'id',
'fk_user',
'event',
'datec',
'tms',
'type');
1128 $returnNotifications = array();
1130 foreach ($notifications as $notification) {
1132 foreach ($notification as $key => $value) {
1133 if (in_array($key, $fields)) {
1137 $returnNotifications[] =
$object;
1141 return $returnNotifications;
1162 if (!DolibarrApiAccess::$user->hasRight(
'user',
'user',
'creer')) {
1163 throw new RestException(403,
"User has no right to update users");
1165 if ($this->useraccount->fetch(
$id) <= 0) {
1166 throw new RestException(404,
'Error creating User Notification, User doesn\'t exists');
1168 $notification =
new Notify($this->db);
1170 $notification->fk_user =
$id;
1172 foreach ($request_data as $field => $value) {
1173 $notification->$field = $this->
_checkValForAPI($field, $value, $notification);
1176 $event = $notification->event;
1178 throw new RestException(500,
'Error creating User Notification, request_data missing event');
1180 $fk_user = $notification->fk_user;
1182 $exists_sql =
"SELECT rowid, fk_action as event, fk_user, type, datec, tms as datem";
1183 $exists_sql .=
" FROM ".MAIN_DB_PREFIX.
"notify_def";
1184 $exists_sql .=
" WHERE fk_action = '".$this->db->escape((
string) $event).
"'";
1185 $exists_sql .=
" AND fk_user = '".$this->db->escape((
string) $fk_user).
"'";
1187 $exists_result = $this->db->query($exists_sql);
1188 if ($this->db->num_rows($exists_result) > 0) {
1189 throw new RestException(403,
'Notification already exists');
1192 if ($notification->create(DolibarrApiAccess::$user) < 0) {
1193 throw new RestException(500,
'Error creating User Notification');
1196 if ($notification->update(DolibarrApiAccess::$user) < 0) {
1197 throw new RestException(500,
'Error updating values');
1223 if (!DolibarrApiAccess::$user->hasRight(
'user',
'user',
'creer')) {
1224 throw new RestException(403,
"User has no right to update users");
1226 if ($this->useraccount->fetch(
$id) <= 0) {
1227 throw new RestException(404,
'Error creating User Notification, User doesn\'t exists');
1229 $notification =
new Notify($this->db);
1230 $notification->fk_user =
$id;
1232 $sql =
"SELECT t.rowid as id FROM ".MAIN_DB_PREFIX.
"c_action_trigger as t";
1233 $sql .=
" WHERE t.code = '".$this->db->escape($code).
"'";
1235 $result = $this->db->query($sql);
1236 if ($this->db->num_rows($result) == 0) {
1237 throw new RestException(404,
'Action Trigger code not found');
1240 $notification->event = $this->db->fetch_row($result)[0];
1241 foreach ($request_data as $field => $value) {
1242 if ($field ===
'event') {
1243 throw new RestException(500,
'Error creating User Notification, request_data contains event key');
1245 if ($field ===
'fk_action') {
1246 throw new RestException(500,
'Error creating User Notification, request_data contains fk_action key');
1248 $notification->$field = $this->
_checkValForAPI($field, $value, $notification);
1251 $event = $notification->event;
1252 $fk_user = $notification->fk_user;
1254 $exists_sql =
"SELECT rowid, fk_action as event, fk_user, type, datec, tms as datem";
1255 $exists_sql .=
" FROM ".MAIN_DB_PREFIX.
"notify_def";
1256 $exists_sql .=
" WHERE fk_action = '".$this->db->escape((
string) $event).
"'";
1257 $exists_sql .=
" AND fk_user = '".$this->db->escape((
string) $fk_user).
"'";
1259 $exists_result = $this->db->query($exists_sql);
1260 if ($this->db->num_rows($exists_result) > 0) {
1261 throw new RestException(403,
'Notification already exists');
1264 if ($notification->create(DolibarrApiAccess::$user) < 0) {
1265 throw new RestException(500,
'Error creating User Notification, are request_data well formed?');
1268 if ($notification->update(DolibarrApiAccess::$user) < 0) {
1269 throw new RestException(500,
'Error updating values');
1291 if (!DolibarrApiAccess::$user->hasRight(
'user',
'user',
'creer')) {
1292 throw new RestException(403,
"User has no right to update users");
1295 $notification =
new Notify($this->db);
1297 $notification->fetch($notification_id);
1299 $fk_user = (int) $notification->fk_user;
1301 if ($fk_user ==
$id) {
1302 return $notification->delete(DolibarrApiAccess::$user);
1304 throw new RestException(403,
"Not allowed due to bad consistency of input data");
1327 if (!DolibarrApiAccess::$user->hasRight(
'user',
'user',
'creer')) {
1328 throw new RestException(403,
"User has no right to update users");
1330 if ($this->useraccount->fetch(
$id) <= 0) {
1331 throw new RestException(404,
'Error creating Notification, User doesn\'t exists');
1333 $notification =
new Notify($this->db);
1336 $notification->fetch($notification_id,
$id);
1338 if ($notification->fk_user !=
$id) {
1339 throw new RestException(403,
"Not allowed due to bad consistency of input data");
1342 foreach ($request_data as $field => $value) {
1343 $notification->$field = $this->
_checkValForAPI($field, $value, $notification);
1346 if ($notification->update(DolibarrApiAccess::$user) < 0) {
1347 throw new RestException(500,
'Error updating values');
1368 unset(
$object->default_values);
1369 unset(
$object->lastsearch_values);
1370 unset(
$object->lastsearch_values_tmp);
1374 unset(
$object->total_localtax1);
1375 unset(
$object->total_localtax2);
1378 unset(
$object->label_incoterms);
1379 unset(
$object->location_incoterms);
1381 unset(
$object->fk_delivery_address);
1383 unset(
$object->all_permissions_are_loaded);
1384 unset(
$object->shipping_method_id);
1388 unset(
$object->clicktodial_loaded);
1395 unset(
$object->pass_indatabase);
1396 unset(
$object->pass_indatabase_crypted);
1399 unset(
$object->clicktodial_password);
1402 $canreadsalary = ((
isModEnabled(
'salaries') && DolibarrApiAccess::$user->hasRight(
'salaries',
'read')) || !
isModEnabled(
'salaries'));
1403 if (!$canreadsalary) {
1425 unset(
$object->actiontypecode);
1426 unset(
$object->all_permissions_are_loaded);
1427 unset(
$object->barcode_type_coder);
1430 unset(
$object->civility_code);
1432 unset(
$object->clicktodial_loaded);
1433 unset(
$object->cond_reglement_id);
1434 unset(
$object->cond_reglement_supplier_id);
1436 unset(
$object->contacts_ids_internal);
1441 unset(
$object->date_creation);
1442 unset(
$object->date_modification);
1443 unset(
$object->date_validation);
1444 unset(
$object->default_values);
1445 unset(
$object->demand_reason_id);
1446 unset(
$object->deposit_percent);
1450 unset(
$object->fk_delivery_address);
1452 unset(
$object->fk_multicurrency);
1454 unset(
$object->fk_user_creat);
1455 unset(
$object->fk_user_modif);
1458 unset(
$object->last_main_doc);
1460 unset(
$object->lastsearch_values_tmp);
1461 unset(
$object->lastsearch_values);
1463 unset(
$object->libelle_incoterms);
1465 unset(
$object->linkedObjectsIds);
1466 unset(
$object->location_incoterms);
1468 unset(
$object->mode_reglement_id);
1470 unset(
$object->multicurrency_code);
1471 unset(
$object->multicurrency_total_ht);
1472 unset(
$object->multicurrency_total_localtax1);
1473 unset(
$object->multicurrency_total_localtax2);
1474 unset(
$object->multicurrency_total_ttc);
1475 unset(
$object->multicurrency_total_tva);
1476 unset(
$object->multicurrency_tx);
1486 unset(
$object->retained_warranty_fk_cond_reglement);
1489 unset(
$object->shipping_method_id);
1490 unset(
$object->shipping_method);
1496 unset(
$object->total_localtax1);
1497 unset(
$object->total_localtax2);
1500 unset(
$object->totalpaid_multicurrency);
1502 unset(
$object->transport_mode_id);
1503 unset(
$object->TRIGGER_PREFIX);
1504 unset(
$object->user_closing_id);
1505 unset(
$object->user_creation_id);
1506 unset(
$object->user_modification_id);
1507 unset(
$object->user_validation_id);
1509 unset(
$object->usergroup_entity);
1523 $cleanObjectList = array();
1525 foreach ($objectList as
$object) {
1526 $cleanObject = parent::_cleanObjectDatas(
$object);
1528 unset($cleanObject->default_values);
1529 unset($cleanObject->lastsearch_values);
1530 unset($cleanObject->lastsearch_values_tmp);
1532 unset($cleanObject->total_ht);
1533 unset($cleanObject->total_tva);
1534 unset($cleanObject->total_localtax1);
1535 unset($cleanObject->total_localtax2);
1536 unset($cleanObject->total_ttc);
1538 unset($cleanObject->libelle_incoterms);
1539 unset($cleanObject->location_incoterms);
1541 unset($cleanObject->fk_delivery_address);
1542 unset($cleanObject->fk_incoterms);
1543 unset($cleanObject->all_permissions_are_loaded);
1544 unset($cleanObject->shipping_method_id);
1545 unset($cleanObject->nb_rights);
1546 unset($cleanObject->search_sid);
1547 unset($cleanObject->ldap_sid);
1548 unset($cleanObject->clicktodial_loaded);
1550 unset($cleanObject->datec);
1551 unset($cleanObject->tms);
1552 unset($cleanObject->members);
1553 unset($cleanObject->note);
1554 unset($cleanObject->note_private);
1556 $cleanObjectList[] = $cleanObject;
1559 return $cleanObjectList;
1572 foreach (Users::$FIELDS as $field) {
1573 if (!isset($data[$field])) {
1574 throw new RestException(400,
"$field field missing");
1576 $account[$field] = $data[$field];
$id
Support class for third parties, contacts, members, users or resources.
if(! $sortfield) if(! $sortorder) $object
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
_checkValExtrafieldsForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
_filterObjectProperties($object, $properties)
Filter properties that will be returned on object.
_checkValForAPI($field, $value, $object)
Check and convert a string depending on its type/name.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $parenttableforentity='')
Check access by user to a given resource.
Class to manage the table of subscription to notifications.
Class to manage user groups.
Class to manage Dolibarr users.
put($id, $request_data=null)
Update a user.
listGroups($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $group_ids='0', $sqlfilters='', $properties='')
List groups of the current user (so user of API token)
index($sortfield="t.rowid", $sortorder='ASC', $limit=100, $page=0, $user_ids='0', $category=0, $sqlfilters='', $properties='')
List users.
deleteGroups($group)
Delete a usergroup.
_cleanObjectDatas($object)
Clean sensible object datas @phpstan-template T.
getInfo($includepermissions=0)
Get more properties of the current user (so user of API token).
_cleanUserGroupListDatas($objectList)
Clean sensible user group list datas.
updateUserNotification($id, $notification_id, $request_data=null)
Update a notification for a user.
setGroup($id, $group, $entity=1)
Add a user to a group.
setPassword($id, $send_password=false)
Update a user password.
postGroups($request_data=null)
Create user group.
deleteUserNotification($id, $notification_id)
Delete a notification attached to a user.
_validate($data)
Validate fields before create or update object.
infoGroups($group, $load_members=0, $includepermissions=0)
Get properties of a user group.
getByEmail($email, $includepermissions=0)
Get a user by email.
getGroups($id)
List the groups of a user.
putGroups($group, $request_data=null)
Update user group.
createUserNotificationByCode($id, $code, $request_data=null)
Create a notification for a user using action trigger code.
post($request_data=null)
Create a user.
createUserNotification($id, $request_data=null)
Create a notification for a user.
getByLogin($login, $includepermissions=0)
Get a user by login.
removeUserFromGroup($id, $group)
Remove user from group (only admin)
getUserNotification($id)
Get notifications for a user.
__construct()
Constructor.
_cleanUserGroup($object)
Clean sensible usergroup object datas.
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").
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
forgeSQLFromUniversalSearchCriteria($filter, &$errorstr='', $noand=0, $nopar=0, $noerror=0, $forbiddenfields=array())
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.
isModEnabled($module)
Is Dolibarr module enabled.