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(
52 $this->useraccount =
new User($this->
db);
70 public function index($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $user_ids = 0, $category = 0, $sqlfilters =
'')
76 if (!DolibarrApiAccess::$user->rights->user->user->lire) {
77 throw new RestException(401,
"You are not allowed to read list of users");
83 $sql =
"SELECT t.rowid";
84 $sql .=
" FROM ".MAIN_DB_PREFIX.
"user as t";
86 $sql .=
", ".MAIN_DB_PREFIX.
"categorie_user as c";
88 $sql .=
' WHERE t.entity IN ('.getEntity(
'user').
')';
90 $sql .=
" AND t.rowid IN (".$user_ids.
")";
95 $sql .=
" AND c.fk_categorie = ".$this->db->escape($category);
96 $sql .=
" AND c.fk_user = t.rowid ";
102 throw new RestException(503,
'Error when validating parameter sqlfilters '.$sqlfilters);
104 $regexstring =
'\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
105 $sql .=
" AND (".preg_replace_callback(
'/'.$regexstring.
'/',
'DolibarrApi::_forge_criteria_callback', $sqlfilters).
")";
108 $sql .= $this->
db->order($sortfield, $sortorder);
113 $offset = $limit * $page;
115 $sql .= $this->
db->plimit($limit + 1, $offset);
118 $result = $this->
db->query($sql);
122 $num = $this->
db->num_rows($result);
123 $min = min($num, ($limit <= 0 ? $num : $limit));
125 $obj = $this->
db->fetch_object($result);
126 $user_static =
new User($this->
db);
127 if ($user_static->fetch($obj->rowid)) {
133 throw new RestException(503,
'Error when retrieve User list : '.$this->
db->lasterror());
135 if (!count($obj_ret)) {
136 throw new RestException(404,
'No User found');
151 public function get($id, $includepermissions = 0)
157 $result = $this->useraccount->initAsSpecimen();
159 $result = $this->useraccount->fetch($id);
162 throw new RestException(404,
'User not found');
166 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
169 if ($includepermissions) {
170 $this->useraccount->getRights();
194 $result = $this->useraccount->fetch(
'', $login);
196 throw new RestException(404,
'User not found');
200 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
203 if ($includepermissions) {
204 $this->useraccount->getRights();
228 $result = $this->useraccount->fetch(
'',
'',
'', 0, -1, $email);
230 throw new RestException(404,
'User not found');
234 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
237 if ($includepermissions) {
238 $this->useraccount->getRights();
255 public function getInfo($includepermissions = 0)
257 $apiUser = DolibarrApiAccess::$user;
259 $result = $this->useraccount->fetch($apiUser->id);
261 throw new RestException(404,
'User not found');
265 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
268 if ($includepermissions) {
269 $this->useraccount->getRights();
273 $userGroupList = $usergroup->listGroupsForUser($apiUser->id,
false);
274 if (!is_array($userGroupList)) {
275 throw new RestException(404,
'User group not found');
289 public function post($request_data = null)
303 foreach ($request_data as $field => $value) {
304 $this->useraccount->$field = $value;
307 if ($this->useraccount->create(DolibarrApiAccess::$user) < 0) {
308 throw new RestException(500,
'Error creating', array_merge(array($this->useraccount->error), $this->useraccount->errors));
310 return $this->useraccount->id;
325 public function put($id, $request_data = null)
331 $result = $this->useraccount->fetch($id);
333 throw new RestException(404,
'Account not found');
337 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
340 foreach ($request_data as $field => $value) {
341 if ($field ==
'id') {
346 if ($field ==
'statut') {
347 $result = $this->useraccount->setstatus($value);
349 throw new RestException(500,
'Error when updating status of user: '.$this->useraccount->error);
352 $this->useraccount->$field = $value;
358 if ($this->useraccount->update(DolibarrApiAccess::$user) >= 0) {
359 return $this->
get($id);
361 throw new RestException(500, $this->useraccount->error);
381 if (!DolibarrApiAccess::$user->rights->user->user->lire) {
382 throw new RestException(403);
385 $user =
new User($this->
db);
386 $result = $user->fetch($id);
388 throw new RestException(404,
'user not found');
392 $groups = $usergroup->listGroupsForUser($id,
false);
394 foreach ($groups as $group) {
419 $result = $this->useraccount->fetch($id);
421 throw new RestException(404,
'User not found');
425 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
428 if (!empty($conf->multicompany->enabled) && !empty($conf->global->MULTICOMPANY_TRANSVERSE_MODE) && !empty(DolibarrApiAccess::$user->admin) && empty(DolibarrApiAccess::$user->entity)) {
429 $entity = (!empty($entity) ? $entity : $conf->entity);
433 $entity = (DolibarrApiAccess::$user->entity > 0 ? DolibarrApiAccess::$user->entity : $conf->entity);
436 $result = $this->useraccount->SetInGroup($group, $entity);
437 if (!($result > 0)) {
438 throw new RestException(500, $this->useraccount->error);
459 public function listGroups($sortfield =
"t.rowid", $sortorder =
'ASC', $limit = 100, $page = 0, $group_ids = 0, $sqlfilters =
'')
465 if (!DolibarrApiAccess::$user->rights->user->group_advance->read) {
466 throw new RestException(401,
"You are not allowed to read list of groups");
472 $sql =
"SELECT t.rowid";
473 $sql .=
" FROM ".MAIN_DB_PREFIX.
"usergroup as t";
474 $sql .=
' WHERE t.entity IN ('.getEntity(
'user').
')';
476 $sql .=
" AND t.rowid IN (".$group_ids.
")";
481 throw new RestException(503,
'Error when validating parameter sqlfilters '.$sqlfilters);
483 $regexstring =
'\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
484 $sql .=
" AND (".preg_replace_callback(
'/'.$regexstring.
'/',
'DolibarrApi::_forge_criteria_callback', $sqlfilters).
")";
487 $sql .= $this->
db->order($sortfield, $sortorder);
492 $offset = $limit * $page;
494 $sql .= $this->
db->plimit($limit + 1, $offset);
497 $result = $this->
db->query($sql);
501 $num = $this->
db->num_rows($result);
502 $min = min($num, ($limit <= 0 ? $num : $limit));
504 $obj = $this->
db->fetch_object($result);
506 if ($group_static->fetch($obj->rowid)) {
512 throw new RestException(503,
'Error when retrieve Group list : '.$this->
db->lasterror());
514 if (!count($obj_ret)) {
515 throw new RestException(404,
'No Group found');
535 if (!DolibarrApiAccess::$user->rights->user->group_advance->read) {
536 throw new RestException(401,
"You are not allowed to read groups");
540 $result = $group_static->fetch($group,
'', $load_members);
543 throw new RestException(404,
'Group not found');
555 public function delete($id)
560 $result = $this->useraccount->fetch($id);
562 throw new RestException(404,
'User not found');
566 throw new RestException(401,
'Access not allowed for login '.DolibarrApiAccess::$user->login);
568 $this->useraccount->oldcopy = clone $this->useraccount;
569 return $this->useraccount->delete(DolibarrApiAccess::$user);
584 $object = parent::_cleanObjectDatas($object);
586 unset($object->default_values);
587 unset($object->lastsearch_values);
588 unset($object->lastsearch_values_tmp);
590 unset($object->total_ht);
591 unset($object->total_tva);
592 unset($object->total_localtax1);
593 unset($object->total_localtax2);
594 unset($object->total_ttc);
596 unset($object->label_incoterms);
597 unset($object->location_incoterms);
599 unset($object->fk_delivery_address);
600 unset($object->fk_incoterms);
601 unset($object->all_permissions_are_loaded);
602 unset($object->shipping_method_id);
603 unset($object->nb_rights);
604 unset($object->search_sid);
605 unset($object->ldap_sid);
606 unset($object->clicktodial_loaded);
609 unset($object->pass);
610 unset($object->pass_indatabase);
611 unset($object->pass_indatabase_crypted);
612 unset($object->pass_temp);
613 unset($object->api_key);
614 unset($object->clicktodial_password);
615 unset($object->openid);
617 unset($object->lines);
618 unset($object->model_pdf);
619 unset($object->skype);
620 unset($object->twitter);
621 unset($object->facebook);
622 unset($object->linkedin);
624 $canreadsalary = ((!empty($conf->salaries->enabled) && !empty(DolibarrApiAccess::$user->rights->salaries->read))
625 || (!empty($conf->hrm->enabled) && !empty(DolibarrApiAccess::$user->rights->hrm->employee->read)));
627 if (!$canreadsalary) {
628 unset($object->salary);
629 unset($object->salaryextra);
645 $cleanObjectList = array();
647 foreach ($objectList as $object) {
648 $cleanObject = parent::_cleanObjectDatas($object);
650 unset($cleanObject->default_values);
651 unset($cleanObject->lastsearch_values);
652 unset($cleanObject->lastsearch_values_tmp);
654 unset($cleanObject->total_ht);
655 unset($cleanObject->total_tva);
656 unset($cleanObject->total_localtax1);
657 unset($cleanObject->total_localtax2);
658 unset($cleanObject->total_ttc);
660 unset($cleanObject->libelle_incoterms);
661 unset($cleanObject->location_incoterms);
663 unset($cleanObject->fk_delivery_address);
664 unset($cleanObject->fk_incoterms);
665 unset($cleanObject->all_permissions_are_loaded);
666 unset($cleanObject->shipping_method_id);
667 unset($cleanObject->nb_rights);
668 unset($cleanObject->search_sid);
669 unset($cleanObject->ldap_sid);
670 unset($cleanObject->clicktodial_loaded);
672 unset($cleanObject->datec);
673 unset($cleanObject->datem);
674 unset($cleanObject->members);
675 unset($cleanObject->note);
676 unset($cleanObject->note_private);
678 $cleanObjectList[] = $cleanObject;
681 return $cleanObjectList;
694 foreach (Users::$FIELDS as $field) {
695 if (!isset($data[$field])) {
696 throw new RestException(400,
"$field field missing");
698 $account[$field] = $data[$field];
index($sortfield="t.rowid", $sortorder= 'ASC', $limit=100, $page=0, $user_ids=0, $category=0, $sqlfilters= '')
List Users.
_cleanObjectDatas($object)
Clean sensible object datas.
__construct()
Constructor.
setGroup($id, $group, $entity=1)
Add a user into a group.
Class to manage Dolibarr users.
_validate($data)
Validate fields before create or update object.
infoGroups($group, $load_members=0)
Get properties of an group object.
_cleanUserGroupListDatas($objectList)
Clean sensible user group list datas.
Class to manage user groups.
listGroups($sortfield="t.rowid", $sortorder= 'ASC', $limit=100, $page=0, $group_ids=0, $sqlfilters= '')
List Groups.
$conf db
API class for accounts.
_checkFilters($sqlfilters)
Return if a $sqlfilters parameter is valid.
getByLogin($login, $includepermissions=0)
Get properties of an user object by login.
getInfo($includepermissions=0)
Get properties of user connected.
getByEmail($email, $includepermissions=0)
Get properties of an user object by Email.
put($id, $request_data=null)
Update account.
static _checkAccessToResource($resource, $resource_id=0, $dbtablename= '', $feature2= '', $dbt_keyfield= 'fk_soc', $dbt_select= 'rowid')
Check user access to a resource.
post($request_data=null)
Create user account.
getGroups($id)
List the groups of a user.