PersonalInfo.php 11 KB

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