diff --git a/htdocs/user/class/api_users.class.php b/htdocs/user/class/api_users.class.php index 3b7d7ee5b00..3dff0d02f15 100644 --- a/htdocs/user/class/api_users.class.php +++ b/htdocs/user/class/api_users.class.php @@ -17,7 +17,8 @@ use Luracast\Restler\RestException; -//require_once DOL_DOCUMENT_ROOT . '/contact/class/contact.class.php'; +require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php'; +require_once DOL_DOCUMENT_ROOT.'/user/class/usergroup.class.php'; /** * API class for users @@ -231,6 +232,41 @@ class Users extends DolibarrApi } } + + /** + * List the groups of a user + * + * @param int $id Id of user + * @return array Array of group objects + * + * @throws RestException + * + * @url GET {id}/groups + */ + function getGroups($id) + { + $obj_ret = array(); + + if (! DolibarrApiAccess::$user->rights->user->user->lire) { + throw new RestException(401); + } + + $user = new User($this->db); + $result = $user->fetch($id); + if( ! $result ) { + throw new RestException(404, 'user not found'); + } + + $usergroup = new UserGroup($this->db); + $groups = $usergroup->listGroupsForUser($id, false); + $obj_ret = array(); + foreach ($groups as $group) { + $obj_ret[] = $this->_cleanObjectDatas($group); + } + return $obj_ret; + } + + /** * Add a user into a group *