PersonalInfo.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\Settings\Settings\Personal;
  8. use OC\Profile\ProfileManager;
  9. use OCA\FederatedFileSharing\FederatedShareProvider;
  10. use OCP\Accounts\IAccount;
  11. use OCP\Accounts\IAccountManager;
  12. use OCP\Accounts\IAccountProperty;
  13. use OCP\App\IAppManager;
  14. use OCP\AppFramework\Http\TemplateResponse;
  15. use OCP\AppFramework\Services\IInitialState;
  16. use OCP\Files\FileInfo;
  17. use OCP\IConfig;
  18. use OCP\IGroup;
  19. use OCP\IGroupManager;
  20. use OCP\IL10N;
  21. use OCP\IUser;
  22. use OCP\IUserManager;
  23. use OCP\L10N\IFactory;
  24. use OCP\Notification\IManager;
  25. use OCP\Settings\ISettings;
  26. class PersonalInfo implements ISettings {
  27. /** @var IConfig */
  28. private $config;
  29. /** @var IUserManager */
  30. private $userManager;
  31. /** @var IAccountManager */
  32. private $accountManager;
  33. /** @var ProfileManager */
  34. private $profileManager;
  35. /** @var IGroupManager */
  36. private $groupManager;
  37. /** @var IAppManager */
  38. private $appManager;
  39. /** @var IFactory */
  40. private $l10nFactory;
  41. /** @var IL10N */
  42. private $l;
  43. /** @var IInitialState */
  44. private $initialStateService;
  45. /** @var IManager */
  46. private $manager;
  47. public function __construct(
  48. IConfig $config,
  49. IUserManager $userManager,
  50. IGroupManager $groupManager,
  51. IAccountManager $accountManager,
  52. ProfileManager $profileManager,
  53. IAppManager $appManager,
  54. IFactory $l10nFactory,
  55. IL10N $l,
  56. IInitialState $initialStateService,
  57. IManager $manager
  58. ) {
  59. $this->config = $config;
  60. $this->userManager = $userManager;
  61. $this->accountManager = $accountManager;
  62. $this->profileManager = $profileManager;
  63. $this->groupManager = $groupManager;
  64. $this->appManager = $appManager;
  65. $this->l10nFactory = $l10nFactory;
  66. $this->l = $l;
  67. $this->initialStateService = $initialStateService;
  68. $this->manager = $manager;
  69. }
  70. public function getForm(): TemplateResponse {
  71. $federationEnabled = $this->appManager->isEnabledForUser('federation');
  72. $federatedFileSharingEnabled = $this->appManager->isEnabledForUser('federatedfilesharing');
  73. $lookupServerUploadEnabled = false;
  74. if ($federatedFileSharingEnabled) {
  75. /** @var FederatedShareProvider $shareProvider */
  76. $shareProvider = \OC::$server->query(FederatedShareProvider::class);
  77. $lookupServerUploadEnabled = $shareProvider->isLookupServerUploadEnabled();
  78. }
  79. $uid = \OC_User::getUser();
  80. $user = $this->userManager->get($uid);
  81. $account = $this->accountManager->getAccount($user);
  82. // make sure FS is setup before querying storage related stuff...
  83. \OC_Util::setupFS($user->getUID());
  84. $storageInfo = \OC_Helper::getStorageInfo('/');
  85. if ($storageInfo['quota'] === FileInfo::SPACE_UNLIMITED) {
  86. $totalSpace = $this->l->t('Unlimited');
  87. } else {
  88. $totalSpace = \OC_Helper::humanFileSize($storageInfo['total']);
  89. }
  90. $messageParameters = $this->getMessageParameters($account);
  91. $parameters = [
  92. 'lookupServerUploadEnabled' => $lookupServerUploadEnabled,
  93. 'isFairUseOfFreePushService' => $this->isFairUseOfFreePushService(),
  94. 'profileEnabledGlobally' => $this->profileManager->isProfileEnabled(),
  95. ] + $messageParameters;
  96. $personalInfoParameters = [
  97. 'userId' => $uid,
  98. 'avatar' => $this->getProperty($account, IAccountManager::PROPERTY_AVATAR),
  99. 'groups' => $this->getGroups($user),
  100. 'quota' => $storageInfo['quota'],
  101. 'totalSpace' => $totalSpace,
  102. 'usage' => \OC_Helper::humanFileSize($storageInfo['used']),
  103. 'usageRelative' => round($storageInfo['relative']),
  104. 'displayName' => $this->getProperty($account, IAccountManager::PROPERTY_DISPLAYNAME),
  105. 'emailMap' => $this->getEmailMap($account),
  106. 'phone' => $this->getProperty($account, IAccountManager::PROPERTY_PHONE),
  107. 'defaultPhoneRegion' => $this->config->getSystemValueString('default_phone_region'),
  108. 'location' => $this->getProperty($account, IAccountManager::PROPERTY_ADDRESS),
  109. 'website' => $this->getProperty($account, IAccountManager::PROPERTY_WEBSITE),
  110. 'twitter' => $this->getProperty($account, IAccountManager::PROPERTY_TWITTER),
  111. 'fediverse' => $this->getProperty($account, IAccountManager::PROPERTY_FEDIVERSE),
  112. 'languageMap' => $this->getLanguageMap($user),
  113. 'localeMap' => $this->getLocaleMap($user),
  114. 'profileEnabledGlobally' => $this->profileManager->isProfileEnabled(),
  115. 'profileEnabled' => $this->profileManager->isProfileEnabled($user),
  116. 'organisation' => $this->getProperty($account, IAccountManager::PROPERTY_ORGANISATION),
  117. 'role' => $this->getProperty($account, IAccountManager::PROPERTY_ROLE),
  118. 'headline' => $this->getProperty($account, IAccountManager::PROPERTY_HEADLINE),
  119. 'biography' => $this->getProperty($account, IAccountManager::PROPERTY_BIOGRAPHY),
  120. 'birthdate' => $this->getProperty($account, IAccountManager::PROPERTY_BIRTHDATE),
  121. ];
  122. $accountParameters = [
  123. 'avatarChangeSupported' => $user->canChangeAvatar(),
  124. 'displayNameChangeSupported' => $user->canChangeDisplayName(),
  125. 'federationEnabled' => $federationEnabled,
  126. 'lookupServerUploadEnabled' => $lookupServerUploadEnabled,
  127. ];
  128. $profileParameters = [
  129. 'profileConfig' => $this->profileManager->getProfileConfigWithMetadata($user, $user),
  130. ];
  131. $this->initialStateService->provideInitialState('profileEnabledGlobally', $this->profileManager->isProfileEnabled());
  132. $this->initialStateService->provideInitialState('personalInfoParameters', $personalInfoParameters);
  133. $this->initialStateService->provideInitialState('accountParameters', $accountParameters);
  134. $this->initialStateService->provideInitialState('profileParameters', $profileParameters);
  135. return new TemplateResponse('settings', 'settings/personal/personal.info', $parameters, '');
  136. }
  137. /**
  138. * Check if is fair use of free push service
  139. * @return boolean
  140. */
  141. private function isFairUseOfFreePushService(): bool {
  142. return $this->manager->isFairUseOfFreePushService();
  143. }
  144. /**
  145. * returns the property data in an
  146. * associative array
  147. */
  148. private function getProperty(IAccount $account, string $property): array {
  149. $property = [
  150. 'name' => $account->getProperty($property)->getName(),
  151. 'value' => $account->getProperty($property)->getValue(),
  152. 'scope' => $account->getProperty($property)->getScope(),
  153. 'verified' => $account->getProperty($property)->getVerified(),
  154. ];
  155. return $property;
  156. }
  157. /**
  158. * returns the section ID string, e.g. 'sharing'
  159. * @since 9.1
  160. */
  161. public function getSection(): string {
  162. return 'personal-info';
  163. }
  164. /**
  165. * @return int whether the form should be rather on the top or bottom of
  166. * the admin section. The forms are arranged in ascending order of the
  167. * priority values. It is required to return a value between 0 and 100.
  168. *
  169. * E.g.: 70
  170. * @since 9.1
  171. */
  172. public function getPriority(): int {
  173. return 10;
  174. }
  175. /**
  176. * returns a sorted list of the user's group GIDs
  177. */
  178. private function getGroups(IUser $user): array {
  179. $groups = array_map(
  180. static function (IGroup $group) {
  181. return $group->getDisplayName();
  182. },
  183. $this->groupManager->getUserGroups($user)
  184. );
  185. sort($groups);
  186. return $groups;
  187. }
  188. /**
  189. * returns the primary email and additional emails in an
  190. * associative array
  191. */
  192. private function getEmailMap(IAccount $account): array {
  193. $systemEmail = [
  194. 'name' => $account->getProperty(IAccountManager::PROPERTY_EMAIL)->getName(),
  195. 'value' => $account->getProperty(IAccountManager::PROPERTY_EMAIL)->getValue(),
  196. 'scope' => $account->getProperty(IAccountManager::PROPERTY_EMAIL)->getScope(),
  197. 'verified' => $account->getProperty(IAccountManager::PROPERTY_EMAIL)->getVerified(),
  198. ];
  199. $additionalEmails = array_map(
  200. function (IAccountProperty $property) {
  201. return [
  202. 'name' => $property->getName(),
  203. 'value' => $property->getValue(),
  204. 'scope' => $property->getScope(),
  205. 'verified' => $property->getVerified(),
  206. 'locallyVerified' => $property->getLocallyVerified(),
  207. ];
  208. },
  209. $account->getPropertyCollection(IAccountManager::COLLECTION_EMAIL)->getProperties(),
  210. );
  211. $emailMap = [
  212. 'primaryEmail' => $systemEmail,
  213. 'additionalEmails' => $additionalEmails,
  214. 'notificationEmail' => (string)$account->getUser()->getPrimaryEMailAddress(),
  215. ];
  216. return $emailMap;
  217. }
  218. /**
  219. * returns the user's active language, common languages, and other languages in an
  220. * associative array
  221. */
  222. private function getLanguageMap(IUser $user): array {
  223. $forceLanguage = $this->config->getSystemValue('force_language', false);
  224. if ($forceLanguage !== false) {
  225. return [];
  226. }
  227. $uid = $user->getUID();
  228. $userConfLang = $this->config->getUserValue($uid, 'core', 'lang', $this->l10nFactory->findLanguage());
  229. $languages = $this->l10nFactory->getLanguages();
  230. // associate the user language with the proper array
  231. $userLangIndex = array_search($userConfLang, array_column($languages['commonLanguages'], 'code'));
  232. $userLang = $languages['commonLanguages'][$userLangIndex];
  233. // search in the other languages
  234. if ($userLangIndex === false) {
  235. $userLangIndex = array_search($userConfLang, array_column($languages['otherLanguages'], 'code'));
  236. $userLang = $languages['otherLanguages'][$userLangIndex];
  237. }
  238. // if user language is not available but set somehow: show the actual code as name
  239. if (!is_array($userLang)) {
  240. $userLang = [
  241. 'code' => $userConfLang,
  242. 'name' => $userConfLang,
  243. ];
  244. }
  245. return array_merge(
  246. ['activeLanguage' => $userLang],
  247. $languages
  248. );
  249. }
  250. private function getLocaleMap(IUser $user): array {
  251. $forceLanguage = $this->config->getSystemValue('force_locale', false);
  252. if ($forceLanguage !== false) {
  253. return [];
  254. }
  255. $uid = $user->getUID();
  256. $userLocaleString = $this->config->getUserValue($uid, 'core', 'locale', $this->l10nFactory->findLocale());
  257. $userLang = $this->config->getUserValue($uid, 'core', 'lang', $this->l10nFactory->findLanguage());
  258. $localeCodes = $this->l10nFactory->findAvailableLocales();
  259. $userLocale = array_filter($localeCodes, fn ($value) => $userLocaleString === $value['code']);
  260. if (!empty($userLocale)) {
  261. $userLocale = reset($userLocale);
  262. }
  263. $localesForLanguage = array_values(array_filter($localeCodes, fn ($localeCode) => str_starts_with($localeCode['code'], $userLang)));
  264. $otherLocales = array_values(array_filter($localeCodes, fn ($localeCode) => !str_starts_with($localeCode['code'], $userLang)));
  265. if (!$userLocale) {
  266. $userLocale = [
  267. 'code' => 'en',
  268. 'name' => 'English'
  269. ];
  270. }
  271. return [
  272. 'activeLocaleLang' => $userLocaleString,
  273. 'activeLocale' => $userLocale,
  274. 'localesForLanguage' => $localesForLanguage,
  275. 'otherLocales' => $otherLocales,
  276. ];
  277. }
  278. /**
  279. * returns the message parameters
  280. */
  281. private function getMessageParameters(IAccount $account): array {
  282. $needVerifyMessage = [IAccountManager::PROPERTY_EMAIL, IAccountManager::PROPERTY_WEBSITE, IAccountManager::PROPERTY_TWITTER];
  283. $messageParameters = [];
  284. foreach ($needVerifyMessage as $property) {
  285. switch ($account->getProperty($property)->getVerified()) {
  286. case IAccountManager::VERIFIED:
  287. $message = $this->l->t('Verifying');
  288. break;
  289. case IAccountManager::VERIFICATION_IN_PROGRESS:
  290. $message = $this->l->t('Verifying …');
  291. break;
  292. default:
  293. $message = $this->l->t('Verify');
  294. }
  295. $messageParameters[$property . 'Message'] = $message;
  296. }
  297. return $messageParameters;
  298. }
  299. }