PersonalInfo.php 11 KB

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