19 use Luracast\Restler\RestException;
21 require_once DOL_DOCUMENT_ROOT.
'/user/class/user.class.php';
22 require_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 ".$this->db->prefix().
"user as t";
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";
104 throw new RestException(503,
'Error when validating parameter sqlfilters -> '.$errormessage);
106 $regexstring =
'\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
107 $sql .=
" AND (".preg_replace_callback(
'/'.$regexstring.
'/',
'DolibarrApi::_forge_criteria_callback', $sqlfilters).
")";
110 $sql .= $this->
db->order($sortfield, $sortorder);
115 $offset = $limit * $page;
117 $sql .= $this->
db->plimit($limit + 1, $offset);
120 $result = $this->
db->query($sql);
124 $num = $this->
db->num_rows($result);
125 $min = min($num, ($limit <= 0 ? $num : $limit));
127 $obj = $this->
db->fetch_object($result);
128 $user_static =
new User($this->
db);
129 if ($user_static->fetch($obj->rowid)) {
135 throw new RestException(503,
'Error when retrieve User list : '.$this->
db->lasterror());
137 if (!count($obj_ret)) {
138 throw new RestException(404,
'No User found');
153 public function get($id, $includepermissions = 0)
155 if (empty(DolibarrApiAccess::$user->rights->user->user->lire) && empty(DolibarrApiAccess::$user->admin) && $id != 0 && DolibarrApiAccess::$user->id != $id) {
156 throw new RestException(401,
'Not allowed');
160 $result = $this->useraccount->initAsSpecimen();
162 $result = $this->useraccount->fetch($id);
165 throw new RestException(404,
'User not found');
169 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
172 if ($includepermissions) {
173 $this->useraccount->getRights();
195 throw new RestException(400,
'Bad parameters');
198 if (empty(DolibarrApiAccess::$user->rights->user->user->lire) && empty(DolibarrApiAccess::$user->admin) && DolibarrApiAccess::$user->login != $login) {
199 throw new RestException(401,
'Not allowed');
202 $result = $this->useraccount->fetch(
'', $login);
204 throw new RestException(404,
'User not found');
208 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
211 if ($includepermissions) {
212 $this->useraccount->getRights();
234 throw new RestException(400,
'Bad parameters');
237 if (empty(DolibarrApiAccess::$user->rights->user->user->lire) && empty(DolibarrApiAccess::$user->admin) && DolibarrApiAccess::$user->email != $email) {
238 throw new RestException(401,
'Not allowed');
241 $result = $this->useraccount->fetch(
'',
'',
'', 0, -1, $email);
243 throw new RestException(404,
'User not found');
247 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
250 if ($includepermissions) {
251 $this->useraccount->getRights();
268 public function getInfo($includepermissions = 0)
270 if (empty(DolibarrApiAccess::$user->rights->user->self->creer) && empty(DolibarrApiAccess::$user->rights->user->user->lire) && empty(DolibarrApiAccess::$user->admin)) {
271 throw new RestException(401,
'Not allowed');
274 $apiUser = DolibarrApiAccess::$user;
276 $result = $this->useraccount->fetch($apiUser->id);
278 throw new RestException(404,
'User not found');
282 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
285 if ($includepermissions) {
286 $this->useraccount->getRights();
290 $userGroupList = $usergroup->listGroupsForUser($apiUser->id,
false);
291 if (!is_array($userGroupList)) {
292 throw new RestException(404,
'User group not found');
308 public function post($request_data =
null)
311 if (empty(DolibarrApiAccess::$user->rights->user->creer) && empty(DolibarrApiAccess::$user->admin)) {
312 throw new RestException(401,
"User creation not allowed for login ".DolibarrApiAccess::$user->login);
324 foreach ($request_data as $field => $value) {
325 if (in_array($field, array(
'pass_crypted',
'pass_indatabase',
'pass_indatabase_crypted',
'pass_temp',
'api_key'))) {
327 throw new RestException(401,
'The property '.$field.
" can't be set/modified using the APIs");
337 $this->useraccount->$field = $value;
340 if ($this->useraccount->create(DolibarrApiAccess::$user) < 0) {
341 throw new RestException(500,
'Error creating', array_merge(array($this->useraccount->error), $this->useraccount->errors));
343 return $this->useraccount->id;
358 public function put($id, $request_data =
null)
361 if (empty(DolibarrApiAccess::$user->rights->user->user->creer) && empty(DolibarrApiAccess::$user->admin)) {
362 throw new RestException(401,
"User update not allowed");
365 $result = $this->useraccount->fetch($id);
367 throw new RestException(404,
'Account not found');
371 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
374 foreach ($request_data as $field => $value) {
375 if ($field ==
'id') {
378 if (in_array($field, array(
'pass_crypted',
'pass_indatabase',
'pass_indatabase_crypted',
'pass_temp',
'api_key'))) {
380 throw new RestException(401,
'The property '.$field.
" can't be set/modified using the APIs");
382 if ($field ==
'pass') {
383 if ($this->useraccount->id != DolibarrApiAccess::$user->id && empty(DolibarrApiAccess::$user->rights->user->user->password)) {
384 throw new RestException(401,
'You are not allowed to modify password of other users');
386 if ($this->useraccount->id == DolibarrApiAccess::$user->id && empty(DolibarrApiAccess::$user->rights->user->self->password)) {
387 throw new RestException(401,
'You are not allowed to modify your own password');
390 if (DolibarrApiAccess::$user->admin) {
391 if ($field ==
'admin' && $value != $this->useraccount->admin && empty($value)) {
392 throw new RestException(401,
'Reseting the admin status of a user is not possible using the API');
395 if ($field ==
'admin' && $value != $this->useraccount->admin) {
396 throw new RestException(401,
'Only an admin user can modify the admin status of another user');
399 if ($field ==
'entity' && $value != $this->useraccount->entity) {
400 throw new RestException(401,
'Changing entity of a user using the APIs is not possible');
404 if ($field ==
'statut') {
405 $result = $this->useraccount->setstatus($value);
407 throw new RestException(500,
'Error when updating status of user: '.$this->useraccount->error);
410 $this->useraccount->$field = $value;
416 if ($this->useraccount->update(DolibarrApiAccess::$user) >= 0) {
417 return $this->
get($id);
419 throw new RestException(500, $this->useraccount->error);
437 if (empty(DolibarrApiAccess::$user->rights->user->user->lire) && empty(DolibarrApiAccess::$user->admin)) {
438 throw new RestException(403);
443 $user =
new User($this->
db);
444 $result = $user->fetch($id);
446 throw new RestException(404,
'user not found');
450 $groups = $usergroup->listGroupsForUser($id,
false);
452 foreach ($groups as $group) {
477 if (empty(DolibarrApiAccess::$user->rights->user->user->creer) && empty(DolibarrApiAccess::$user->admin)) {
478 throw new RestException(401);
481 $result = $this->useraccount->fetch($id);
483 throw new RestException(404,
'User not found');
487 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
490 if (
isModEnabled(
'multicompany') && !empty($conf->global->MULTICOMPANY_TRANSVERSE_MODE) && !empty(DolibarrApiAccess::$user->admin) && empty(DolibarrApiAccess::$user->entity)) {
491 $entity = (!empty($entity) ? $entity : $conf->entity);
495 $entity = (DolibarrApiAccess::$user->entity > 0 ? DolibarrApiAccess::$user->entity : $conf->entity);
498 $result = $this->useraccount->SetInGroup($group, $entity);
499 if (!($result > 0)) {
500 throw new RestException(500, $this->useraccount->error);
524 public function listGroups($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $group_ids = 0, $sqlfilters =
'')
530 if ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && empty(DolibarrApiAccess::$user->rights->user->user->lire) && empty(DolibarrApiAccess::$user->admin)) ||
531 !empty($conf->global->MAIN_USE_ADVANCED_PERMS) && empty(DolibarrApiAccess::$user->rights->user->group_advance->read) && empty(DolibarrApiAccess::$user->admin)) {
532 throw new RestException(401,
"You are not allowed to read groups");
538 $sql =
"SELECT t.rowid";
539 $sql .=
" FROM ".$this->db->prefix().
"usergroup as t";
540 $sql .=
' WHERE t.entity IN ('.getEntity(
'user').
')';
542 $sql .=
" AND t.rowid IN (".$this->db->sanitize($group_ids).
")";
548 throw new RestException(503,
'Error when validating parameter sqlfilters -> '.$errormessage);
550 $regexstring =
'\(([^:\'\(\)]+:[^:\'\(\)]+:[^\(\)]+)\)';
551 $sql .=
" AND (".preg_replace_callback(
'/'.$regexstring.
'/',
'DolibarrApi::_forge_criteria_callback', $sqlfilters).
")";
554 $sql .= $this->
db->order($sortfield, $sortorder);
559 $offset = $limit * $page;
561 $sql .= $this->
db->plimit($limit + 1, $offset);
564 $result = $this->
db->query($sql);
568 $num = $this->
db->num_rows($result);
569 $min = min($num, ($limit <= 0 ? $num : $limit));
571 $obj = $this->
db->fetch_object($result);
573 if ($group_static->fetch($obj->rowid)) {
579 throw new RestException(503,
'Error when retrieve Group list : '.$this->
db->lasterror());
581 if (!count($obj_ret)) {
582 throw new RestException(404,
'No Group found');
605 if ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && empty(DolibarrApiAccess::$user->rights->user->user->lire) && empty(DolibarrApiAccess::$user->admin)) ||
606 !empty($conf->global->MAIN_USE_ADVANCED_PERMS) && empty(DolibarrApiAccess::$user->rights->user->group_advance->read) && empty(DolibarrApiAccess::$user->admin)) {
607 throw new RestException(401,
"You are not allowed to read groups");
611 $result = $group_static->fetch($group,
'', $load_members);
614 throw new RestException(404,
'Group not found');
629 public function delete($id)
631 if (empty(DolibarrApiAccess::$user->rights->user->user->supprimer) && empty(DolibarrApiAccess::$user->admin)) {
632 throw new RestException(401,
'Not allowed');
634 $result = $this->useraccount->fetch($id);
636 throw new RestException(404,
'User not found');
640 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
642 $this->useraccount->oldcopy = clone $this->useraccount;
644 if (!$this->useraccount->delete(DolibarrApiAccess::$user)) {
645 throw new RestException(500);
651 'message' =>
'Ticket deleted'
668 $object = parent::_cleanObjectDatas($object);
670 unset($object->default_values);
671 unset($object->lastsearch_values);
672 unset($object->lastsearch_values_tmp);
674 unset($object->total_ht);
675 unset($object->total_tva);
676 unset($object->total_localtax1);
677 unset($object->total_localtax2);
678 unset($object->total_ttc);
680 unset($object->label_incoterms);
681 unset($object->location_incoterms);
683 unset($object->fk_delivery_address);
684 unset($object->fk_incoterms);
685 unset($object->all_permissions_are_loaded);
686 unset($object->shipping_method_id);
687 unset($object->nb_rights);
688 unset($object->search_sid);
689 unset($object->ldap_sid);
690 unset($object->clicktodial_loaded);
693 unset($object->pass);
694 unset($object->pass_indatabase);
695 unset($object->pass_indatabase_crypted);
696 unset($object->pass_temp);
697 unset($object->api_key);
698 unset($object->clicktodial_password);
699 unset($object->openid);
701 unset($object->lines);
702 unset($object->model_pdf);
704 unset($object->skype);
705 unset($object->twitter);
706 unset($object->facebook);
707 unset($object->linkedin);
709 $canreadsalary = ((!empty($conf->salaries->enabled) && !empty(DolibarrApiAccess::$user->rights->salaries->read)) || (empty($conf->salaries->enabled)));
711 if (!$canreadsalary) {
712 unset($object->salary);
713 unset($object->salaryextra);
729 $cleanObjectList = array();
731 foreach ($objectList as $object) {
732 $cleanObject = parent::_cleanObjectDatas($object);
734 unset($cleanObject->default_values);
735 unset($cleanObject->lastsearch_values);
736 unset($cleanObject->lastsearch_values_tmp);
738 unset($cleanObject->total_ht);
739 unset($cleanObject->total_tva);
740 unset($cleanObject->total_localtax1);
741 unset($cleanObject->total_localtax2);
742 unset($cleanObject->total_ttc);
744 unset($cleanObject->libelle_incoterms);
745 unset($cleanObject->location_incoterms);
747 unset($cleanObject->fk_delivery_address);
748 unset($cleanObject->fk_incoterms);
749 unset($cleanObject->all_permissions_are_loaded);
750 unset($cleanObject->shipping_method_id);
751 unset($cleanObject->nb_rights);
752 unset($cleanObject->search_sid);
753 unset($cleanObject->ldap_sid);
754 unset($cleanObject->clicktodial_loaded);
756 unset($cleanObject->datec);
757 unset($cleanObject->datem);
758 unset($cleanObject->members);
759 unset($cleanObject->note);
760 unset($cleanObject->note_private);
762 $cleanObjectList[] = $cleanObject;
765 return $cleanObjectList;
778 foreach (Users::$FIELDS as $field) {
779 if (!isset($data[$field])) {
780 throw new RestException(400,
"$field field missing");
782 $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.
_checkFilters($sqlfilters, &$error='')
Return if a $sqlfilters parameter is valid.
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.
isModEnabled($module)
Is Dolibarr module enabled.
$conf db
API class for accounts.