1
0

PersonalInfo.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Arthur Schiwon <blizzz@arthur-schiwon.de>
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Georg Ehrke <oc.list@georgehrke.com>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. * @author Robin Appelman <robin@icewind.nl>
  12. * @author Roeland Jago Douma <roeland@famdouma.nl>
  13. * @author Thomas Citharel <nextcloud@tcit.fr>
  14. *
  15. * @license GNU AGPL version 3 or any later version
  16. *
  17. * This program is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU Affero General Public License as
  19. * published by the Free Software Foundation, either version 3 of the
  20. * License, or (at your option) any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU Affero General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU Affero General Public License
  28. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  29. *
  30. */
  31. namespace OCA\Settings\Settings\Personal;
  32. use OC\Accounts\AccountManager;
  33. use OCA\FederatedFileSharing\FederatedShareProvider;
  34. use OCP\App\IAppManager;
  35. use OCP\AppFramework\Http\TemplateResponse;
  36. use OCP\Files\FileInfo;
  37. use OCP\IConfig;
  38. use OCP\IGroup;
  39. use OCP\IGroupManager;
  40. use OCP\IL10N;
  41. use OCP\IUser;
  42. use OCP\IUserManager;
  43. use OCP\L10N\IFactory;
  44. use OCP\Settings\ISettings;
  45. class PersonalInfo implements ISettings {
  46. /** @var IConfig */
  47. private $config;
  48. /** @var IUserManager */
  49. private $userManager;
  50. /** @var AccountManager */
  51. private $accountManager;
  52. /** @var IGroupManager */
  53. private $groupManager;
  54. /** @var IAppManager */
  55. private $appManager;
  56. /** @var IFactory */
  57. private $l10nFactory;
  58. /** @var IL10N */
  59. private $l;
  60. /**
  61. * @param IConfig $config
  62. * @param IUserManager $userManager
  63. * @param IGroupManager $groupManager
  64. * @param AccountManager $accountManager
  65. * @param IFactory $l10nFactory
  66. * @param IL10N $l
  67. */
  68. public function __construct(
  69. IConfig $config,
  70. IUserManager $userManager,
  71. IGroupManager $groupManager,
  72. AccountManager $accountManager,
  73. IAppManager $appManager,
  74. IFactory $l10nFactory,
  75. IL10N $l
  76. ) {
  77. $this->config = $config;
  78. $this->userManager = $userManager;
  79. $this->accountManager = $accountManager;
  80. $this->groupManager = $groupManager;
  81. $this->appManager = $appManager;
  82. $this->l10nFactory = $l10nFactory;
  83. $this->l = $l;
  84. }
  85. /**
  86. * @return TemplateResponse returns the instance with all parameters set, ready to be rendered
  87. * @since 9.1
  88. */
  89. public function getForm() {
  90. $federatedFileSharingEnabled = $this->appManager->isEnabledForUser('federatedfilesharing');
  91. $lookupServerUploadEnabled = false;
  92. if ($federatedFileSharingEnabled) {
  93. /** @var FederatedShareProvider $shareProvider */
  94. $shareProvider = \OC::$server->query(FederatedShareProvider::class);
  95. $lookupServerUploadEnabled = $shareProvider->isLookupServerUploadEnabled();
  96. }
  97. $uid = \OC_User::getUser();
  98. $user = $this->userManager->get($uid);
  99. $userData = $this->accountManager->getUser($user);
  100. // make sure FS is setup before querying storage related stuff...
  101. \OC_Util::setupFS($user->getUID());
  102. $storageInfo = \OC_Helper::getStorageInfo('/');
  103. if ($storageInfo['quota'] === FileInfo::SPACE_UNLIMITED) {
  104. $totalSpace = $this->l->t('Unlimited');
  105. } else {
  106. $totalSpace = \OC_Helper::humanFileSize($storageInfo['total']);
  107. }
  108. $languageParameters = $this->getLanguages($user);
  109. $localeParameters = $this->getLocales($user);
  110. $messageParameters = $this->getMessageParameters($userData);
  111. $parameters = [
  112. 'total_space' => $totalSpace,
  113. 'usage' => \OC_Helper::humanFileSize($storageInfo['used']),
  114. 'usage_relative' => round($storageInfo['relative']),
  115. 'quota' => $storageInfo['quota'],
  116. 'avatarChangeSupported' => $user->canChangeAvatar(),
  117. 'lookupServerUploadEnabled' => $lookupServerUploadEnabled,
  118. 'avatarScope' => $userData[AccountManager::PROPERTY_AVATAR]['scope'],
  119. 'displayNameChangeSupported' => $user->canChangeDisplayName(),
  120. 'displayName' => $userData[AccountManager::PROPERTY_DISPLAYNAME]['value'],
  121. 'displayNameScope' => $userData[AccountManager::PROPERTY_DISPLAYNAME]['scope'],
  122. 'email' => $userData[AccountManager::PROPERTY_EMAIL]['value'],
  123. 'emailScope' => $userData[AccountManager::PROPERTY_EMAIL]['scope'],
  124. 'emailVerification' => $userData[AccountManager::PROPERTY_EMAIL]['verified'],
  125. 'phone' => $userData[AccountManager::PROPERTY_PHONE]['value'],
  126. 'phoneScope' => $userData[AccountManager::PROPERTY_PHONE]['scope'],
  127. 'address' => $userData[AccountManager::PROPERTY_ADDRESS]['value'],
  128. 'addressScope' => $userData[AccountManager::PROPERTY_ADDRESS]['scope'],
  129. 'website' => $userData[AccountManager::PROPERTY_WEBSITE]['value'],
  130. 'websiteScope' => $userData[AccountManager::PROPERTY_WEBSITE]['scope'],
  131. 'websiteVerification' => $userData[AccountManager::PROPERTY_WEBSITE]['verified'],
  132. 'twitter' => $userData[AccountManager::PROPERTY_TWITTER]['value'],
  133. 'twitterScope' => $userData[AccountManager::PROPERTY_TWITTER]['scope'],
  134. 'twitterVerification' => $userData[AccountManager::PROPERTY_TWITTER]['verified'],
  135. 'groups' => $this->getGroups($user),
  136. ] + $messageParameters + $languageParameters + $localeParameters;
  137. return new TemplateResponse('settings', 'settings/personal/personal.info', $parameters, '');
  138. }
  139. /**
  140. * @return string the section ID, e.g. 'sharing'
  141. * @since 9.1
  142. */
  143. public function getSection() {
  144. return 'personal-info';
  145. }
  146. /**
  147. * @return int whether the form should be rather on the top or bottom of
  148. * the admin section. The forms are arranged in ascending order of the
  149. * priority values. It is required to return a value between 0 and 100.
  150. *
  151. * E.g.: 70
  152. * @since 9.1
  153. */
  154. public function getPriority() {
  155. return 10;
  156. }
  157. /**
  158. * returns a sorted list of the user's group GIDs
  159. *
  160. * @param IUser $user
  161. * @return array
  162. */
  163. private function getGroups(IUser $user) {
  164. $groups = array_map(
  165. function (IGroup $group) {
  166. return $group->getDisplayName();
  167. },
  168. $this->groupManager->getUserGroups($user)
  169. );
  170. sort($groups);
  171. return $groups;
  172. }
  173. /**
  174. * returns the user language, common language and other languages in an
  175. * associative array
  176. *
  177. * @param IUser $user
  178. * @return array
  179. */
  180. private function getLanguages(IUser $user) {
  181. $forceLanguage = $this->config->getSystemValue('force_language', false);
  182. if ($forceLanguage !== false) {
  183. return [];
  184. }
  185. $uid = $user->getUID();
  186. $userConfLang = $this->config->getUserValue($uid, 'core', 'lang', $this->l10nFactory->findLanguage());
  187. $languages = $this->l10nFactory->getLanguages();
  188. // associate the user language with the proper array
  189. $userLangIndex = array_search($userConfLang, array_column($languages['commonlanguages'], 'code'));
  190. $userLang = $languages['commonlanguages'][$userLangIndex];
  191. // search in the other languages
  192. if ($userLangIndex === false) {
  193. $userLangIndex = array_search($userConfLang, array_column($languages['languages'], 'code'));
  194. $userLang = $languages['languages'][$userLangIndex];
  195. }
  196. // if user language is not available but set somehow: show the actual code as name
  197. if (!is_array($userLang)) {
  198. $userLang = [
  199. 'code' => $userConfLang,
  200. 'name' => $userConfLang,
  201. ];
  202. }
  203. return array_merge(
  204. ['activelanguage' => $userLang],
  205. $languages
  206. );
  207. }
  208. private function getLocales(IUser $user) {
  209. $forceLanguage = $this->config->getSystemValue('force_locale', false);
  210. if ($forceLanguage !== false) {
  211. return [];
  212. }
  213. $uid = $user->getUID();
  214. $userLocaleString = $this->config->getUserValue($uid, 'core', 'locale', $this->l10nFactory->findLocale());
  215. $userLang = $this->config->getUserValue($uid, 'core', 'lang', $this->l10nFactory->findLanguage());
  216. $localeCodes = $this->l10nFactory->findAvailableLocales();
  217. $userLocale = array_filter($localeCodes, function ($value) use ($userLocaleString) {
  218. return $userLocaleString === $value['code'];
  219. });
  220. if (!empty($userLocale)) {
  221. $userLocale = reset($userLocale);
  222. }
  223. $localesForLanguage = array_filter($localeCodes, function ($localeCode) use ($userLang) {
  224. return 0 === strpos($localeCode['code'], $userLang);
  225. });
  226. if (!$userLocale) {
  227. $userLocale = [
  228. 'code' => 'en',
  229. 'name' => 'English'
  230. ];
  231. }
  232. return [
  233. 'activelocaleLang' => $userLocaleString,
  234. 'activelocale' => $userLocale,
  235. 'locales' => $localeCodes,
  236. 'localesForLanguage' => $localesForLanguage,
  237. ];
  238. }
  239. /**
  240. * @param array $userData
  241. * @return array
  242. */
  243. private function getMessageParameters(array $userData) {
  244. $needVerifyMessage = [AccountManager::PROPERTY_EMAIL, AccountManager::PROPERTY_WEBSITE, AccountManager::PROPERTY_TWITTER];
  245. $messageParameters = [];
  246. foreach ($needVerifyMessage as $property) {
  247. switch ($userData[$property]['verified']) {
  248. case AccountManager::VERIFIED:
  249. $message = $this->l->t('Verifying');
  250. break;
  251. case AccountManager::VERIFICATION_IN_PROGRESS:
  252. $message = $this->l->t('Verifying …');
  253. break;
  254. default:
  255. $message = $this->l->t('Verify');
  256. }
  257. $messageParameters[$property . 'Message'] = $message;
  258. }
  259. return $messageParameters;
  260. }
  261. }