1
0

AUserData.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2018 John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
  5. *
  6. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Georg Ehrke <oc.list@georgehrke.com>
  9. * @author Joas Schilling <coding@schilljs.com>
  10. * @author John Molakvoæ <skjnldsv@protonmail.com>
  11. * @author Roeland Jago Douma <roeland@famdouma.nl>
  12. * @author Vincent Petry <vincent@nextcloud.com>
  13. *
  14. * @license GNU AGPL version 3 or any later version
  15. *
  16. * This program is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License as
  18. * published by the Free Software Foundation, either version 3 of the
  19. * License, or (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Affero General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public License
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. *
  29. */
  30. namespace OCA\Provisioning_API\Controller;
  31. use OC\Group\Manager;
  32. use OC\User\Backend;
  33. use OC\User\NoUserException;
  34. use OC_Helper;
  35. use OCP\Accounts\IAccountManager;
  36. use OCP\Accounts\PropertyDoesNotExistException;
  37. use OCP\AppFramework\Http;
  38. use OCP\AppFramework\OCS\OCSException;
  39. use OCP\AppFramework\OCS\OCSNotFoundException;
  40. use OCP\AppFramework\OCSController;
  41. use OCP\Files\NotFoundException;
  42. use OCP\IConfig;
  43. use OCP\IGroupManager;
  44. use OCP\IRequest;
  45. use OCP\IUserManager;
  46. use OCP\IUserSession;
  47. use OCP\L10N\IFactory;
  48. use OCP\User\Backend\ISetDisplayNameBackend;
  49. use OCP\User\Backend\ISetPasswordBackend;
  50. abstract class AUserData extends OCSController {
  51. public const SCOPE_SUFFIX = 'Scope';
  52. public const USER_FIELD_DISPLAYNAME = 'display';
  53. public const USER_FIELD_LANGUAGE = 'language';
  54. public const USER_FIELD_LOCALE = 'locale';
  55. public const USER_FIELD_PASSWORD = 'password';
  56. public const USER_FIELD_QUOTA = 'quota';
  57. public const USER_FIELD_NOTIFICATION_EMAIL = 'notify_email';
  58. /** @var IUserManager */
  59. protected $userManager;
  60. /** @var IConfig */
  61. protected $config;
  62. /** @var IGroupManager|Manager */ // FIXME Requires a method that is not on the interface
  63. protected $groupManager;
  64. /** @var IUserSession */
  65. protected $userSession;
  66. /** @var IAccountManager */
  67. protected $accountManager;
  68. /** @var IFactory */
  69. protected $l10nFactory;
  70. public function __construct(string $appName,
  71. IRequest $request,
  72. IUserManager $userManager,
  73. IConfig $config,
  74. IGroupManager $groupManager,
  75. IUserSession $userSession,
  76. IAccountManager $accountManager,
  77. IFactory $l10nFactory) {
  78. parent::__construct($appName, $request);
  79. $this->userManager = $userManager;
  80. $this->config = $config;
  81. $this->groupManager = $groupManager;
  82. $this->userSession = $userSession;
  83. $this->accountManager = $accountManager;
  84. $this->l10nFactory = $l10nFactory;
  85. }
  86. /**
  87. * creates a array with all user data
  88. *
  89. * @param string $userId
  90. * @param bool $includeScopes
  91. * @return array
  92. * @throws NotFoundException
  93. * @throws OCSException
  94. * @throws OCSNotFoundException
  95. */
  96. protected function getUserData(string $userId, bool $includeScopes = false): array {
  97. $currentLoggedInUser = $this->userSession->getUser();
  98. assert($currentLoggedInUser !== null, 'No user logged in');
  99. $data = [];
  100. // Check if the target user exists
  101. $targetUserObject = $this->userManager->get($userId);
  102. if ($targetUserObject === null) {
  103. throw new OCSNotFoundException('User does not exist');
  104. }
  105. $isAdmin = $this->groupManager->isAdmin($currentLoggedInUser->getUID());
  106. if ($isAdmin
  107. || $this->groupManager->getSubAdmin()->isUserAccessible($currentLoggedInUser, $targetUserObject)) {
  108. $data['enabled'] = $this->config->getUserValue($targetUserObject->getUID(), 'core', 'enabled', 'true') === 'true';
  109. } else {
  110. // Check they are looking up themselves
  111. if ($currentLoggedInUser->getUID() !== $targetUserObject->getUID()) {
  112. return $data;
  113. }
  114. }
  115. // Get groups data
  116. $userAccount = $this->accountManager->getAccount($targetUserObject);
  117. $groups = $this->groupManager->getUserGroups($targetUserObject);
  118. $gids = [];
  119. foreach ($groups as $group) {
  120. $gids[] = $group->getGID();
  121. }
  122. if ($isAdmin) {
  123. try {
  124. # might be thrown by LDAP due to handling of users disappears
  125. # from the external source (reasons unknown to us)
  126. # cf. https://github.com/nextcloud/server/issues/12991
  127. $data['storageLocation'] = $targetUserObject->getHome();
  128. } catch (NoUserException $e) {
  129. throw new OCSNotFoundException($e->getMessage(), $e);
  130. }
  131. }
  132. // Find the data
  133. $data['id'] = $targetUserObject->getUID();
  134. $data['lastLogin'] = $targetUserObject->getLastLogin() * 1000;
  135. $data['backend'] = $targetUserObject->getBackendClassName();
  136. $data['subadmin'] = $this->getUserSubAdminGroupsData($targetUserObject->getUID());
  137. $data[self::USER_FIELD_QUOTA] = $this->fillStorageInfo($targetUserObject->getUID());
  138. try {
  139. if ($includeScopes) {
  140. $data[IAccountManager::PROPERTY_AVATAR . self::SCOPE_SUFFIX] = $userAccount->getProperty(IAccountManager::PROPERTY_AVATAR)->getScope();
  141. }
  142. $data[IAccountManager::PROPERTY_EMAIL] = $targetUserObject->getSystemEMailAddress();
  143. if ($includeScopes) {
  144. $data[IAccountManager::PROPERTY_EMAIL . self::SCOPE_SUFFIX] = $userAccount->getProperty(IAccountManager::PROPERTY_EMAIL)->getScope();
  145. }
  146. $additionalEmails = $additionalEmailScopes = [];
  147. $emailCollection = $userAccount->getPropertyCollection(IAccountManager::COLLECTION_EMAIL);
  148. foreach ($emailCollection->getProperties() as $property) {
  149. $additionalEmails[] = $property->getValue();
  150. if ($includeScopes) {
  151. $additionalEmailScopes[] = $property->getScope();
  152. }
  153. }
  154. $data[IAccountManager::COLLECTION_EMAIL] = $additionalEmails;
  155. if ($includeScopes) {
  156. $data[IAccountManager::COLLECTION_EMAIL . self::SCOPE_SUFFIX] = $additionalEmailScopes;
  157. }
  158. $data[IAccountManager::PROPERTY_DISPLAYNAME] = $targetUserObject->getDisplayName();
  159. if ($includeScopes) {
  160. $data[IAccountManager::PROPERTY_DISPLAYNAME . self::SCOPE_SUFFIX] = $userAccount->getProperty(IAccountManager::PROPERTY_DISPLAYNAME)->getScope();
  161. }
  162. foreach ([
  163. IAccountManager::PROPERTY_PHONE,
  164. IAccountManager::PROPERTY_ADDRESS,
  165. IAccountManager::PROPERTY_WEBSITE,
  166. IAccountManager::PROPERTY_TWITTER,
  167. IAccountManager::PROPERTY_FEDIVERSE,
  168. IAccountManager::PROPERTY_ORGANISATION,
  169. IAccountManager::PROPERTY_ROLE,
  170. IAccountManager::PROPERTY_HEADLINE,
  171. IAccountManager::PROPERTY_BIOGRAPHY,
  172. IAccountManager::PROPERTY_PROFILE_ENABLED,
  173. ] as $propertyName) {
  174. $property = $userAccount->getProperty($propertyName);
  175. $data[$propertyName] = $property->getValue();
  176. if ($includeScopes) {
  177. $data[$propertyName . self::SCOPE_SUFFIX] = $property->getScope();
  178. }
  179. }
  180. } catch (PropertyDoesNotExistException $e) {
  181. // hard coded properties should exist
  182. throw new OCSException($e->getMessage(), Http::STATUS_INTERNAL_SERVER_ERROR, $e);
  183. }
  184. $data['groups'] = $gids;
  185. $data[self::USER_FIELD_LANGUAGE] = $this->l10nFactory->getUserLanguage($targetUserObject);
  186. $data[self::USER_FIELD_LOCALE] = $this->config->getUserValue($targetUserObject->getUID(), 'core', 'locale');
  187. $data[self::USER_FIELD_NOTIFICATION_EMAIL] = $targetUserObject->getPrimaryEMailAddress();
  188. $backend = $targetUserObject->getBackend();
  189. $data['backendCapabilities'] = [
  190. 'setDisplayName' => $backend instanceof ISetDisplayNameBackend || $backend->implementsActions(Backend::SET_DISPLAYNAME),
  191. 'setPassword' => $backend instanceof ISetPasswordBackend || $backend->implementsActions(Backend::SET_PASSWORD),
  192. ];
  193. return $data;
  194. }
  195. /**
  196. * Get the groups a user is a subadmin of
  197. *
  198. * @param string $userId
  199. * @return array
  200. * @throws OCSException
  201. */
  202. protected function getUserSubAdminGroupsData(string $userId): array {
  203. $user = $this->userManager->get($userId);
  204. // Check if the user exists
  205. if ($user === null) {
  206. throw new OCSNotFoundException('User does not exist');
  207. }
  208. // Get the subadmin groups
  209. $subAdminGroups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($user);
  210. $groups = [];
  211. foreach ($subAdminGroups as $key => $group) {
  212. $groups[] = $group->getGID();
  213. }
  214. return $groups;
  215. }
  216. /**
  217. * @param string $userId
  218. * @return array
  219. * @throws OCSException
  220. */
  221. protected function fillStorageInfo(string $userId): array {
  222. try {
  223. \OC_Util::tearDownFS();
  224. \OC_Util::setupFS($userId);
  225. $storage = OC_Helper::getStorageInfo('/', null, true, false);
  226. $data = [
  227. 'free' => $storage['free'],
  228. 'used' => $storage['used'],
  229. 'total' => $storage['total'],
  230. 'relative' => $storage['relative'],
  231. self::USER_FIELD_QUOTA => $storage['quota'],
  232. ];
  233. } catch (NotFoundException $ex) {
  234. // User fs is not setup yet
  235. $user = $this->userManager->get($userId);
  236. if ($user === null) {
  237. throw new OCSException('User does not exist', 101);
  238. }
  239. $quota = $user->getQuota();
  240. if ($quota !== 'none') {
  241. $quota = OC_Helper::computerFileSize($quota);
  242. }
  243. $data = [
  244. self::USER_FIELD_QUOTA => $quota !== false ? $quota : 'none',
  245. 'used' => 0
  246. ];
  247. } catch (\Exception $e) {
  248. \OC::$server->get(\Psr\Log\LoggerInterface::class)->error(
  249. "Could not load storage info for {user}",
  250. [
  251. 'app' => 'provisioning_api',
  252. 'user' => $userId,
  253. 'exception' => $e,
  254. ]
  255. );
  256. /* In case the Exception left things in a bad state */
  257. \OC_Util::tearDownFS();
  258. return [];
  259. }
  260. return $data;
  261. }
  262. }