PersonalInfo.php 12 KB

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