diff --git a/htdocs/user/class/api_users.class.php b/htdocs/user/class/api_users.class.php index d477e37c160..40a02dc8af1 100644 --- a/htdocs/user/class/api_users.class.php +++ b/htdocs/user/class/api_users.class.php @@ -1,6 +1,6 @@ -/* Copyright (C) 2030 Thibault FOUCART +/* Copyright (C) 2020 Thibault FOUCART * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -143,13 +143,13 @@ class Users extends DolibarrApi /** * Get properties of an user object - * Return an array with user informations * * @param int $id ID of user * @param int $includepermissions Set this to 1 to have the array of permissions loaded (not done by default for performance purpose) * @return array|mixed data without useless information * - * @throws RestException + * @throws RestException 401 Insufficient rights + * @throws RestException 404 User or group not found */ public function get($id, $includepermissions = 0) { @@ -174,6 +174,78 @@ class Users extends DolibarrApi return $this->_cleanObjectDatas($this->useraccount); } + + /** + * Get properties of an user object by login + * + * @param string $login Login of user + * @param int $includepermissions Set this to 1 to have the array of permissions loaded (not done by default for performance purpose) + * @return array|mixed data without useless information + * + * @url GET login/{login} + * + * @throws RestException 401 Insufficient rights + * @throws RestException 404 User or group not found + */ + public function getByLogin($login, $includepermissions = 0) + { + //if (!DolibarrApiAccess::$user->rights->user->user->lire) { + //throw new RestException(401); + //} + + $result = $this->useraccount->fetch('', $login); + if (!$result) + { + throw new RestException(404, 'User not found'); + } + + if (!DolibarrApi::_checkAccessToResource('user', $this->useraccount->id, 'user')) + { + throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); + } + + if ($includepermissions) { + $this->useraccount->getRights(); + } + + return $this->_cleanObjectDatas($this->useraccount); + } + + /** + * Get properties of an user object by Email + * + * @param string $email Email of user + * @param int $includepermissions Set this to 1 to have the array of permissions loaded (not done by default for performance purpose) + * @return array|mixed data without useless information + * + * @url GET email/{email} + * + * @throws RestException 401 Insufficient rights + * @throws RestException 404 User or group not found + */ + public function getByEmail($email, $includepermissions = 0) + { + //if (!DolibarrApiAccess::$user->rights->user->user->lire) { + //throw new RestException(401); + //} + + $result = $this->useraccount->fetch('', '', '', 0, -1, $email); + if (!$result) + { + throw new RestException(404, 'User not found'); + } + + if (!DolibarrApi::_checkAccessToResource('user', $this->useraccount->id, 'user')) + { + throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); + } + + if ($includepermissions) { + $this->useraccount->getRights(); + } + + return $this->_cleanObjectDatas($this->useraccount); + } /** * Get properties of user connected