You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AUserData.php 6.7KB

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