From 27d6071a73bcde203566a1bb7d8f977efd3cf0b5 Mon Sep 17 00:00:00 2001 From: ptibogxiv Date: Tue, 26 Nov 2019 21:33:42 +0100 Subject: [PATCH 1/4] NEW add list of group and object group API --- htdocs/user/class/api_users.class.php | 110 ++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) diff --git a/htdocs/user/class/api_users.class.php b/htdocs/user/class/api_users.class.php index 1fa9e971306..a155827c673 100644 --- a/htdocs/user/class/api_users.class.php +++ b/htdocs/user/class/api_users.class.php @@ -363,6 +363,116 @@ class Users extends DolibarrApi return 1; } + + /** + * List Groups + * + * Return an array with a list of Groups + * + * @url GET /groups + * + * @param string $sortfield Sort field + * @param string $sortorder Sort order + * @param int $limit Limit for list + * @param int $page Page number + * @param string $group_ids Groups ids filter field. Example: '1' or '1,2,3' {@pattern /^[0-9,]*$/i} + * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.ref:like:'SO-%') and (t.date_creation:<:'20160101')" + * @return array Array of User objects + */ + public function listGroups($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $group_ids = 0, $sqlfilters = '') + { + global $db, $conf; + + $obj_ret = array(); + + if (!DolibarrApiAccess::$user->rights->user->group_advance->read) { + throw new RestException(401, "You are not allowed to read list of groups"); + } + + // case of external user, $societe param is ignored and replaced by user's socid + //$socid = DolibarrApiAccess::$user->socid ? DolibarrApiAccess::$user->socid : $societe; + + $sql = "SELECT t.rowid"; + $sql .= " FROM ".MAIN_DB_PREFIX."usergroup as t"; + $sql .= ' WHERE t.entity IN ('.getEntity('user').')'; + if ($group_ids) $sql .= " AND t.rowid IN (".$group_ids.")"; + // Add sql filters + if ($sqlfilters) + { + if (!DolibarrApi::_checkFilters($sqlfilters)) + { + throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters); + } + $regexstring = '\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)'; + $sql .= " AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")"; + } + + $sql .= $db->order($sortfield, $sortorder); + if ($limit) { + if ($page < 0) + { + $page = 0; + } + $offset = $limit * $page; + + $sql .= $db->plimit($limit + 1, $offset); + } + + $result = $db->query($sql); + + if ($result) + { + $i = 0; + $num = $db->num_rows($result); + $min = min($num, ($limit <= 0 ? $num : $limit)); + while ($i < $min) + { + $obj = $db->fetch_object($result); + $group_static = new UserGroup($this->db); + if ($group_static->fetch($obj->rowid)) { + $obj_ret[] = $this->_cleanObjectDatas($group_static); + } + $i++; + } + } + else { + throw new RestException(503, 'Error when retrieve Group list : '.$db->lasterror()); + } + if (!count($obj_ret)) { + throw new RestException(404, 'No Group found'); + } + return $obj_ret; + } + + /** + * Get properties of an user object + * + * Return an array with group informations + * + * @url GET /groups/{id} + * + * @param int $id ID of group + * @param int $load_members Load members list or not {@min 0} {@max 1} + * @return array Array of User objects + */ + public function infoGroups($id, $load_members = 0) + { + global $db, $conf; + + if (!DolibarrApiAccess::$user->rights->user->group_advance->read) { + throw new RestException(401, "You are not allowed to read groups"); + } + + $group_static = new UserGroup($this->db); + $result = $group_static->fetch($id, '', $load_members); + + if (!$result) + { + throw new RestException(404, 'Group not found'); + } + + return $this->_cleanObjectDatas($group_static); + } /** * Delete account From bc1f9f5aaa00fb1f2a85a1f7b14af200ced070cd Mon Sep 17 00:00:00 2001 From: ptibogxiv Date: Tue, 26 Nov 2019 21:34:45 +0100 Subject: [PATCH 2/4] Update api_users.class.php --- htdocs/user/class/api_users.class.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/htdocs/user/class/api_users.class.php b/htdocs/user/class/api_users.class.php index a155827c673..ab85177c6f0 100644 --- a/htdocs/user/class/api_users.class.php +++ b/htdocs/user/class/api_users.class.php @@ -449,13 +449,13 @@ class Users extends DolibarrApi * * Return an array with group informations * - * @url GET /groups/{id} + * @url GET /groups/{group} * * @param int $id ID of group * @param int $load_members Load members list or not {@min 0} {@max 1} * @return array Array of User objects */ - public function infoGroups($id, $load_members = 0) + public function infoGroups($group, $load_members = 0) { global $db, $conf; @@ -464,7 +464,7 @@ class Users extends DolibarrApi } $group_static = new UserGroup($this->db); - $result = $group_static->fetch($id, '', $load_members); + $result = $group_static->fetch($group, '', $load_members); if (!$result) { From bba4ed02d285be2e4d577e8a1f47b92bdaff14b2 Mon Sep 17 00:00:00 2001 From: stickler-ci Date: Tue, 26 Nov 2019 20:36:30 +0000 Subject: [PATCH 3/4] Fixing style errors. --- htdocs/user/class/api_users.class.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/htdocs/user/class/api_users.class.php b/htdocs/user/class/api_users.class.php index ab85177c6f0..761638f007b 100644 --- a/htdocs/user/class/api_users.class.php +++ b/htdocs/user/class/api_users.class.php @@ -363,12 +363,12 @@ class Users extends DolibarrApi return 1; } - + /** * List Groups * * Return an array with a list of Groups - * + * * @url GET /groups * * @param string $sortfield Sort field @@ -443,12 +443,12 @@ class Users extends DolibarrApi } return $obj_ret; } - + /** * Get properties of an user object * * Return an array with group informations - * + * * @url GET /groups/{group} * * @param int $id ID of group @@ -464,13 +464,13 @@ class Users extends DolibarrApi } $group_static = new UserGroup($this->db); - $result = $group_static->fetch($group, '', $load_members); + $result = $group_static->fetch($group, '', $load_members); if (!$result) { throw new RestException(404, 'Group not found'); } - + return $this->_cleanObjectDatas($group_static); } From 825349e07e8815e3e0f6a3ec070fbcb6b22abf61 Mon Sep 17 00:00:00 2001 From: ptibogxiv Date: Wed, 27 Nov 2019 16:04:52 +0100 Subject: [PATCH 4/4] Update api_users.class.php --- htdocs/user/class/api_users.class.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/htdocs/user/class/api_users.class.php b/htdocs/user/class/api_users.class.php index 761638f007b..be768a3e15a 100644 --- a/htdocs/user/class/api_users.class.php +++ b/htdocs/user/class/api_users.class.php @@ -445,13 +445,13 @@ class Users extends DolibarrApi } /** - * Get properties of an user object + * Get properties of an group object * * Return an array with group informations * * @url GET /groups/{group} * - * @param int $id ID of group + * @param int $group ID of group * @param int $load_members Load members list or not {@min 0} {@max 1} * @return array Array of User objects */