1
0

PersonalInfo.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. 'pronouns' => $this->getProperty($account, IAccountManager::PROPERTY_PRONOUNS),
  124. ];
  125. $accountParameters = [
  126. 'avatarChangeSupported' => $user->canChangeAvatar(),
  127. 'displayNameChangeSupported' => $user->canChangeDisplayName(),
  128. 'federationEnabled' => $federationEnabled,
  129. 'lookupServerUploadEnabled' => $lookupServerUploadEnabled,
  130. ];
  131. $profileParameters = [
  132. 'profileConfig' => $this->profileManager->getProfileConfigWithMetadata($user, $user),
  133. ];
  134. $this->initialStateService->provideInitialState('profileEnabledGlobally', $this->profileManager->isProfileEnabled());
  135. $this->initialStateService->provideInitialState('personalInfoParameters', $personalInfoParameters);
  136. $this->initialStateService->provideInitialState('accountParameters', $accountParameters);
  137. $this->initialStateService->provideInitialState('profileParameters', $profileParameters);
  138. return new TemplateResponse('settings', 'settings/personal/personal.info', $parameters, '');
  139. }
  140. /**
  141. * Check if is fair use of free push service
  142. * @return boolean
  143. */
  144. private function isFairUseOfFreePushService(): bool {
  145. return $this->manager->isFairUseOfFreePushService();
  146. }
  147. /**
  148. * returns the property data in an
  149. * associative array
  150. */
  151. private function getProperty(IAccount $account, string $property): array {
  152. $property = [
  153. 'name' => $account->getProperty($property)->getName(),
  154. 'value' => $account->getProperty($property)->getValue(),
  155. 'scope' => $account->getProperty($property)->getScope(),
  156. 'verified' => $account->getProperty($property)->getVerified(),
  157. ];
  158. return $property;
  159. }
  160. /**
  161. * returns the section ID string, e.g. 'sharing'
  162. * @since 9.1
  163. */
  164. public function getSection(): string {
  165. return 'personal-info';
  166. }
  167. /**
  168. * @return int whether the form should be rather on the top or bottom of
  169. * the admin section. The forms are arranged in ascending order of the
  170. * priority values. It is required to return a value between 0 and 100.
  171. *
  172. * E.g.: 70
  173. * @since 9.1
  174. */
  175. public function getPriority(): int {
  176. return 10;
  177. }
  178. /**
  179. * returns a sorted list of the user's group GIDs
  180. */
  181. private function getGroups(IUser $user): array {
  182. $groups = array_map(
  183. static function (IGroup $group) {
  184. return $group->getDisplayName();
  185. },
  186. $this->groupManager->getUserGroups($user)
  187. );
  188. sort($groups);
  189. return $groups;
  190. }
  191. /**
  192. * returns the primary email and additional emails in an
  193. * associative array
  194. */
  195. private function getEmailMap(IAccount $account): array {
  196. $systemEmail = [
  197. 'name' => $account->getProperty(IAccountManager::PROPERTY_EMAIL)->getName(),
  198. 'value' => $account->getProperty(IAccountManager::PROPERTY_EMAIL)->getValue(),
  199. 'scope' => $account->getProperty(IAccountManager::PROPERTY_EMAIL)->getScope(),
  200. 'verified' => $account->getProperty(IAccountManager::PROPERTY_EMAIL)->getVerified(),
  201. ];
  202. $additionalEmails = array_map(
  203. function (IAccountProperty $property) {
  204. return [
  205. 'name' => $property->getName(),
  206. 'value' => $property->getValue(),
  207. 'scope' => $property->getScope(),
  208. 'verified' => $property->getVerified(),
  209. 'locallyVerified' => $property->getLocallyVerified(),
  210. ];
  211. },
  212. $account->getPropertyCollection(IAccountManager::COLLECTION_EMAIL)->getProperties(),
  213. );
  214. $emailMap = [
  215. 'primaryEmail' => $systemEmail,
  216. 'additionalEmails' => $additionalEmails,
  217. 'notificationEmail' => (string)$account->getUser()->getPrimaryEMailAddress(),
  218. ];
  219. return $emailMap;
  220. }
  221. /**
  222. * returns the user's active language, common languages, and other languages in an
  223. * associative array
  224. */
  225. private function getLanguageMap(IUser $user): array {
  226. $forceLanguage = $this->config->getSystemValue('force_language', false);
  227. if ($forceLanguage !== false) {
  228. return [];
  229. }
  230. $uid = $user->getUID();
  231. $userConfLang = $this->config->getUserValue($uid, 'core', 'lang', $this->l10nFactory->findLanguage());
  232. $languages = $this->l10nFactory->getLanguages();
  233. // associate the user language with the proper array
  234. $userLangIndex = array_search($userConfLang, array_column($languages['commonLanguages'], 'code'));
  235. $userLang = $languages['commonLanguages'][$userLangIndex];
  236. // search in the other languages
  237. if ($userLangIndex === false) {
  238. $userLangIndex = array_search($userConfLang, array_column($languages['otherLanguages'], 'code'));
  239. $userLang = $languages['otherLanguages'][$userLangIndex];
  240. }
  241. // if user language is not available but set somehow: show the actual code as name
  242. if (!is_array($userLang)) {
  243. $userLang = [
  244. 'code' => $userConfLang,
  245. 'name' => $userConfLang,
  246. ];
  247. }
  248. return array_merge(
  249. ['activeLanguage' => $userLang],
  250. $languages
  251. );
  252. }
  253. private function getLocaleMap(IUser $user): array {
  254. $forceLanguage = $this->config->getSystemValue('force_locale', false);
  255. if ($forceLanguage !== false) {
  256. return [];
  257. }
  258. $uid = $user->getUID();
  259. $userLocaleString = $this->config->getUserValue($uid, 'core', 'locale', $this->l10nFactory->findLocale());
  260. $userLang = $this->config->getUserValue($uid, 'core', 'lang', $this->l10nFactory->findLanguage());
  261. $localeCodes = $this->l10nFactory->findAvailableLocales();
  262. $userLocale = array_filter($localeCodes, fn ($value) => $userLocaleString === $value['code']);
  263. if (!empty($userLocale)) {
  264. $userLocale = reset($userLocale);
  265. }
  266. $localesForLanguage = array_values(array_filter($localeCodes, fn ($localeCode) => str_starts_with($localeCode['code'], $userLang)));
  267. $otherLocales = array_values(array_filter($localeCodes, fn ($localeCode) => !str_starts_with($localeCode['code'], $userLang)));
  268. if (!$userLocale) {
  269. $userLocale = [
  270. 'code' => 'en',
  271. 'name' => 'English'
  272. ];
  273. }
  274. return [
  275. 'activeLocaleLang' => $userLocaleString,
  276. 'activeLocale' => $userLocale,
  277. 'localesForLanguage' => $localesForLanguage,
  278. 'otherLocales' => $otherLocales,
  279. ];
  280. }
  281. /**
  282. * returns the message parameters
  283. */
  284. private function getMessageParameters(IAccount $account): array {
  285. $needVerifyMessage = [IAccountManager::PROPERTY_EMAIL, IAccountManager::PROPERTY_WEBSITE, IAccountManager::PROPERTY_TWITTER];
  286. $messageParameters = [];
  287. foreach ($needVerifyMessage as $property) {
  288. switch ($account->getProperty($property)->getVerified()) {
  289. case IAccountManager::VERIFIED:
  290. $message = $this->l->t('Verifying');
  291. break;
  292. case IAccountManager::VERIFICATION_IN_PROGRESS:
  293. $message = $this->l->t('Verifying …');
  294. break;
  295. default:
  296. $message = $this->l->t('Verify');
  297. }
  298. $messageParameters[$property . 'Message'] = $message;
  299. }
  300. return $messageParameters;
  301. }
  302. }