AUserData.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 OCP\AppFramework\OCS\OCSException;
  25. use OCP\AppFramework\OCS\OCSNotFoundException;
  26. use OCP\AppFramework\OCSController;
  27. use OCP\Files\NotFoundException;
  28. use OC_Helper;
  29. use OCP\IConfig;
  30. use OCP\IGroupManager;
  31. use OCP\IRequest;
  32. use OCP\IUserManager;
  33. use OCP\IUserSession;
  34. use OCP\User\Backend\ISetDisplayNameBackend;
  35. use OCP\User\Backend\ISetPasswordBackend;
  36. abstract class AUserData extends OCSController {
  37. /** @var IUserManager */
  38. protected $userManager;
  39. /** @var IConfig */
  40. protected $config;
  41. /** @var IGroupManager|\OC\Group\Manager */ // FIXME Requires a method that is not on the interface
  42. protected $groupManager;
  43. /** @var IUserSession */
  44. protected $userSession;
  45. /** @var AccountManager */
  46. protected $accountManager;
  47. /**
  48. * @param string $appName
  49. * @param IRequest $request
  50. * @param IUserManager $userManager
  51. * @param IConfig $config
  52. * @param IGroupManager $groupManager
  53. * @param IUserSession $userSession
  54. * @param AccountManager $accountManager
  55. */
  56. public function __construct(string $appName,
  57. IRequest $request,
  58. IUserManager $userManager,
  59. IConfig $config,
  60. IGroupManager $groupManager,
  61. IUserSession $userSession,
  62. AccountManager $accountManager) {
  63. parent::__construct($appName, $request);
  64. $this->userManager = $userManager;
  65. $this->config = $config;
  66. $this->groupManager = $groupManager;
  67. $this->userSession = $userSession;
  68. $this->accountManager = $accountManager;
  69. }
  70. /**
  71. * creates a array with all user data
  72. *
  73. * @param string $userId
  74. * @return array
  75. * @throws OCSException
  76. */
  77. protected function getUserData(string $userId): array {
  78. $currentLoggedInUser = $this->userSession->getUser();
  79. $data = [];
  80. // Check if the target user exists
  81. $targetUserObject = $this->userManager->get($userId);
  82. if($targetUserObject === null) {
  83. throw new OCSNotFoundException('User does not exist');
  84. }
  85. // Should be at least Admin Or SubAdmin!
  86. if ($this->groupManager->isAdmin($currentLoggedInUser->getUID())
  87. || $this->groupManager->getSubAdmin()->isUserAccessible($currentLoggedInUser, $targetUserObject)) {
  88. $data['enabled'] = $this->config->getUserValue($targetUserObject->getUID(), 'core', 'enabled', 'true') === 'true';
  89. } else {
  90. // Check they are looking up themselves
  91. if ($currentLoggedInUser->getUID() !== $targetUserObject->getUID()) {
  92. return $data;
  93. }
  94. }
  95. // Get groups data
  96. $userAccount = $this->accountManager->getUser($targetUserObject);
  97. $groups = $this->groupManager->getUserGroups($targetUserObject);
  98. $gids = [];
  99. foreach ($groups as $group) {
  100. $gids[] = $group->getGID();
  101. }
  102. // Find the data
  103. $data['id'] = $targetUserObject->getUID();
  104. $data['storageLocation'] = $targetUserObject->getHome();
  105. $data['lastLogin'] = $targetUserObject->getLastLogin() * 1000;
  106. $data['backend'] = $targetUserObject->getBackendClassName();
  107. $data['subadmin'] = $this->getUserSubAdminGroupsData($targetUserObject->getUID());
  108. $data['quota'] = $this->fillStorageInfo($targetUserObject->getUID());
  109. $data[AccountManager::PROPERTY_EMAIL] = $targetUserObject->getEMailAddress();
  110. $data[AccountManager::PROPERTY_DISPLAYNAME] = $targetUserObject->getDisplayName();
  111. $data[AccountManager::PROPERTY_PHONE] = $userAccount[AccountManager::PROPERTY_PHONE]['value'];
  112. $data[AccountManager::PROPERTY_ADDRESS] = $userAccount[AccountManager::PROPERTY_ADDRESS]['value'];
  113. $data[AccountManager::PROPERTY_WEBSITE] = $userAccount[AccountManager::PROPERTY_WEBSITE]['value'];
  114. $data[AccountManager::PROPERTY_TWITTER] = $userAccount[AccountManager::PROPERTY_TWITTER]['value'];
  115. $data['groups'] = $gids;
  116. $data['language'] = $this->config->getUserValue($targetUserObject->getUID(), 'core', 'lang');
  117. $data['locale'] = $this->config->getUserValue($targetUserObject->getUID(), 'core', 'locale');
  118. $backend = $targetUserObject->getBackend();
  119. $data['backendCapabilities'] = [
  120. 'setDisplayName' => $backend instanceof ISetDisplayNameBackend || $backend->implementsActions(Backend::SET_DISPLAYNAME),
  121. 'setPassword' => $backend instanceof ISetPasswordBackend || $backend->implementsActions(Backend::SET_PASSWORD),
  122. ];
  123. return $data;
  124. }
  125. /**
  126. * Get the groups a user is a subadmin of
  127. *
  128. * @param string $userId
  129. * @return array
  130. * @throws OCSException
  131. */
  132. protected function getUserSubAdminGroupsData(string $userId): array {
  133. $user = $this->userManager->get($userId);
  134. // Check if the user exists
  135. if($user === null) {
  136. throw new OCSNotFoundException('User does not exist');
  137. }
  138. // Get the subadmin groups
  139. $subAdminGroups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($user);
  140. $groups = [];
  141. foreach ($subAdminGroups as $key => $group) {
  142. $groups[] = $group->getGID();
  143. }
  144. return $groups;
  145. }
  146. /**
  147. * @param string $userId
  148. * @return array
  149. * @throws \OCP\Files\NotFoundException
  150. */
  151. protected function fillStorageInfo(string $userId): array {
  152. try {
  153. \OC_Util::tearDownFS();
  154. \OC_Util::setupFS($userId);
  155. $storage = OC_Helper::getStorageInfo('/');
  156. $data = [
  157. 'free' => $storage['free'],
  158. 'used' => $storage['used'],
  159. 'total' => $storage['total'],
  160. 'relative' => $storage['relative'],
  161. 'quota' => $storage['quota'],
  162. ];
  163. } catch (NotFoundException $ex) {
  164. // User fs is not setup yet
  165. $user = $this->userManager->get($userId);
  166. if ($user === null) {
  167. throw new OCSException('User does not exist', 101);
  168. }
  169. $quota = $user->getQuota();
  170. if ($quota !== 'none') {
  171. $quota = OC_Helper::computerFileSize($quota);
  172. }
  173. $data = [
  174. 'quota' => $quota !== false ? $quota : 'none',
  175. 'used' => 0
  176. ];
  177. }
  178. return $data;
  179. }
  180. }