PersonalInfo.php 12 KB

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