1
0

PersonalInfo.php 11 KB

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