AUserData.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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) <skjnldsv@protonmail.com>
  11. * @author Roeland Jago Douma <roeland@famdouma.nl>
  12. *
  13. * @license GNU AGPL version 3 or any later version
  14. *
  15. * This program is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License as
  17. * published by the Free Software Foundation, either version 3 of the
  18. * License, or (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  27. *
  28. */
  29. namespace OCA\Provisioning_API\Controller;
  30. use OC\Accounts\AccountManager;
  31. use OC\User\Backend;
  32. use OC\User\NoUserException;
  33. use OC_Helper;
  34. use OCP\Accounts\IAccountManager;
  35. use OCP\AppFramework\OCS\OCSException;
  36. use OCP\AppFramework\OCS\OCSNotFoundException;
  37. use OCP\AppFramework\OCSController;
  38. use OCP\Files\NotFoundException;
  39. use OCP\IConfig;
  40. use OCP\IGroupManager;
  41. use OCP\IRequest;
  42. use OCP\IUserManager;
  43. use OCP\IUserSession;
  44. use OCP\L10N\IFactory;
  45. use OCP\User\Backend\ISetDisplayNameBackend;
  46. use OCP\User\Backend\ISetPasswordBackend;
  47. abstract class AUserData extends OCSController {
  48. /** @var IUserManager */
  49. protected $userManager;
  50. /** @var IConfig */
  51. protected $config;
  52. /** @var IGroupManager|\OC\Group\Manager */ // FIXME Requires a method that is not on the interface
  53. protected $groupManager;
  54. /** @var IUserSession */
  55. protected $userSession;
  56. /** @var AccountManager */
  57. protected $accountManager;
  58. /** @var IFactory */
  59. protected $l10nFactory;
  60. public function __construct(string $appName,
  61. IRequest $request,
  62. IUserManager $userManager,
  63. IConfig $config,
  64. IGroupManager $groupManager,
  65. IUserSession $userSession,
  66. AccountManager $accountManager,
  67. IFactory $l10nFactory) {
  68. parent::__construct($appName, $request);
  69. $this->userManager = $userManager;
  70. $this->config = $config;
  71. $this->groupManager = $groupManager;
  72. $this->userSession = $userSession;
  73. $this->accountManager = $accountManager;
  74. $this->l10nFactory = $l10nFactory;
  75. }
  76. /**
  77. * creates a array with all user data
  78. *
  79. * @param string $userId
  80. * @return array
  81. * @throws NotFoundException
  82. * @throws OCSException
  83. * @throws OCSNotFoundException
  84. */
  85. protected function getUserData(string $userId): array {
  86. $currentLoggedInUser = $this->userSession->getUser();
  87. $data = [];
  88. // Check if the target user exists
  89. $targetUserObject = $this->userManager->get($userId);
  90. if ($targetUserObject === null) {
  91. throw new OCSNotFoundException('User does not exist');
  92. }
  93. // Should be at least Admin Or SubAdmin!
  94. if ($this->groupManager->isAdmin($currentLoggedInUser->getUID())
  95. || $this->groupManager->getSubAdmin()->isUserAccessible($currentLoggedInUser, $targetUserObject)) {
  96. $data['enabled'] = $this->config->getUserValue($targetUserObject->getUID(), 'core', 'enabled', 'true') === 'true';
  97. } else {
  98. // Check they are looking up themselves
  99. if ($currentLoggedInUser->getUID() !== $targetUserObject->getUID()) {
  100. return $data;
  101. }
  102. }
  103. // Get groups data
  104. $userAccount = $this->accountManager->getUser($targetUserObject);
  105. $groups = $this->groupManager->getUserGroups($targetUserObject);
  106. $gids = [];
  107. foreach ($groups as $group) {
  108. $gids[] = $group->getGID();
  109. }
  110. try {
  111. # might be thrown by LDAP due to handling of users disappears
  112. # from the external source (reasons unknown to us)
  113. # cf. https://github.com/nextcloud/server/issues/12991
  114. $data['storageLocation'] = $targetUserObject->getHome();
  115. } catch (NoUserException $e) {
  116. throw new OCSNotFoundException($e->getMessage(), $e);
  117. }
  118. // Find the data
  119. $data['id'] = $targetUserObject->getUID();
  120. $data['lastLogin'] = $targetUserObject->getLastLogin() * 1000;
  121. $data['backend'] = $targetUserObject->getBackendClassName();
  122. $data['subadmin'] = $this->getUserSubAdminGroupsData($targetUserObject->getUID());
  123. $data['quota'] = $this->fillStorageInfo($targetUserObject->getUID());
  124. $data[IAccountManager::PROPERTY_EMAIL] = $targetUserObject->getEMailAddress();
  125. $data[IAccountManager::PROPERTY_DISPLAYNAME] = $targetUserObject->getDisplayName();
  126. $data[IAccountManager::PROPERTY_PHONE] = $userAccount[IAccountManager::PROPERTY_PHONE]['value'];
  127. $data[IAccountManager::PROPERTY_ADDRESS] = $userAccount[IAccountManager::PROPERTY_ADDRESS]['value'];
  128. $data[IAccountManager::PROPERTY_WEBSITE] = $userAccount[IAccountManager::PROPERTY_WEBSITE]['value'];
  129. $data[IAccountManager::PROPERTY_TWITTER] = $userAccount[IAccountManager::PROPERTY_TWITTER]['value'];
  130. $data['groups'] = $gids;
  131. $data['language'] = $this->l10nFactory->getUserLanguage($targetUserObject);
  132. $data['locale'] = $this->config->getUserValue($targetUserObject->getUID(), 'core', 'locale');
  133. $backend = $targetUserObject->getBackend();
  134. $data['backendCapabilities'] = [
  135. 'setDisplayName' => $backend instanceof ISetDisplayNameBackend || $backend->implementsActions(Backend::SET_DISPLAYNAME),
  136. 'setPassword' => $backend instanceof ISetPasswordBackend || $backend->implementsActions(Backend::SET_PASSWORD),
  137. ];
  138. return $data;
  139. }
  140. /**
  141. * Get the groups a user is a subadmin of
  142. *
  143. * @param string $userId
  144. * @return array
  145. * @throws OCSException
  146. */
  147. protected function getUserSubAdminGroupsData(string $userId): array {
  148. $user = $this->userManager->get($userId);
  149. // Check if the user exists
  150. if ($user === null) {
  151. throw new OCSNotFoundException('User does not exist');
  152. }
  153. // Get the subadmin groups
  154. $subAdminGroups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($user);
  155. $groups = [];
  156. foreach ($subAdminGroups as $key => $group) {
  157. $groups[] = $group->getGID();
  158. }
  159. return $groups;
  160. }
  161. /**
  162. * @param string $userId
  163. * @return array
  164. * @throws \OCP\Files\NotFoundException
  165. */
  166. protected function fillStorageInfo(string $userId): array {
  167. try {
  168. \OC_Util::tearDownFS();
  169. \OC_Util::setupFS($userId);
  170. $storage = OC_Helper::getStorageInfo('/');
  171. $data = [
  172. 'free' => $storage['free'],
  173. 'used' => $storage['used'],
  174. 'total' => $storage['total'],
  175. 'relative' => $storage['relative'],
  176. 'quota' => $storage['quota'],
  177. ];
  178. } catch (NotFoundException $ex) {
  179. // User fs is not setup yet
  180. $user = $this->userManager->get($userId);
  181. if ($user === null) {
  182. throw new OCSException('User does not exist', 101);
  183. }
  184. $quota = $user->getQuota();
  185. if ($quota !== 'none') {
  186. $quota = OC_Helper::computerFileSize($quota);
  187. }
  188. $data = [
  189. 'quota' => $quota !== false ? $quota : 'none',
  190. 'used' => 0
  191. ];
  192. }
  193. return $data;
  194. }
  195. }