You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

293 lines
9.1 KiB

  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 <tcit@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\Personal;
  32. use OC\Accounts\AccountManager;
  33. use OCA\FederatedFileSharing\AppInfo\Application;
  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. $federatedFileSharing = \OC::$server->query(Application::class);
  94. $shareProvider = $federatedFileSharing->getFederatedShareProvider();
  95. $lookupServerUploadEnabled = $shareProvider->isLookupServerUploadEnabled();
  96. }
  97. $uid = \OC_User::getUser();
  98. $user = $this->userManager->get($uid);
  99. $userData = $this->accountManager->getUser($user);
  100. $storageInfo = \OC_Helper::getStorageInfo('/');
  101. if ($storageInfo['quota'] === FileInfo::SPACE_UNLIMITED) {
  102. $totalSpace = $this->l->t('Unlimited');
  103. } else {
  104. $totalSpace = \OC_Helper::humanFileSize($storageInfo['total']);
  105. }
  106. $languageParameters = $this->getLanguages($user);
  107. $localeParameters = $this->getLocales($user);
  108. $messageParameters = $this->getMessageParameters($userData);
  109. $parameters = [
  110. 'total_space' => $totalSpace,
  111. 'usage' => \OC_Helper::humanFileSize($storageInfo['used']),
  112. 'usage_relative' => round($storageInfo['relative']),
  113. 'quota' => $storageInfo['quota'],
  114. 'avatarChangeSupported' => $user->canChangeAvatar(),
  115. 'lookupServerUploadEnabled' => $lookupServerUploadEnabled,
  116. 'avatarScope' => $userData[AccountManager::PROPERTY_AVATAR]['scope'],
  117. 'displayNameChangeSupported' => $user->canChangeDisplayName(),
  118. 'displayName' => $userData[AccountManager::PROPERTY_DISPLAYNAME]['value'],
  119. 'displayNameScope' => $userData[AccountManager::PROPERTY_DISPLAYNAME]['scope'],
  120. 'email' => $userData[AccountManager::PROPERTY_EMAIL]['value'],
  121. 'emailScope' => $userData[AccountManager::PROPERTY_EMAIL]['scope'],
  122. 'emailVerification' => $userData[AccountManager::PROPERTY_EMAIL]['verified'],
  123. 'phone' => $userData[AccountManager::PROPERTY_PHONE]['value'],
  124. 'phoneScope' => $userData[AccountManager::PROPERTY_PHONE]['scope'],
  125. 'address' => $userData[AccountManager::PROPERTY_ADDRESS]['value'],
  126. 'addressScope' => $userData[AccountManager::PROPERTY_ADDRESS]['scope'],
  127. 'website' => $userData[AccountManager::PROPERTY_WEBSITE]['value'],
  128. 'websiteScope' => $userData[AccountManager::PROPERTY_WEBSITE]['scope'],
  129. 'websiteVerification' => $userData[AccountManager::PROPERTY_WEBSITE]['verified'],
  130. 'twitter' => $userData[AccountManager::PROPERTY_TWITTER]['value'],
  131. 'twitterScope' => $userData[AccountManager::PROPERTY_TWITTER]['scope'],
  132. 'twitterVerification' => $userData[AccountManager::PROPERTY_TWITTER]['verified'],
  133. 'groups' => $this->getGroups($user),
  134. ] + $messageParameters + $languageParameters + $localeParameters;
  135. return new TemplateResponse('settings', 'settings/personal/personal.info', $parameters, '');
  136. }
  137. /**
  138. * @return string the section ID, e.g. 'sharing'
  139. * @since 9.1
  140. */
  141. public function getSection() {
  142. return 'personal-info';
  143. }
  144. /**
  145. * @return int whether the form should be rather on the top or bottom of
  146. * the admin section. The forms are arranged in ascending order of the
  147. * priority values. It is required to return a value between 0 and 100.
  148. *
  149. * E.g.: 70
  150. * @since 9.1
  151. */
  152. public function getPriority() {
  153. return 10;
  154. }
  155. /**
  156. * returns a sorted list of the user's group GIDs
  157. *
  158. * @param IUser $user
  159. * @return array
  160. */
  161. private function getGroups(IUser $user) {
  162. $groups = array_map(
  163. function(IGroup $group) {
  164. return $group->getDisplayName();
  165. },
  166. $this->groupManager->getUserGroups($user)
  167. );
  168. sort($groups);
  169. return $groups;
  170. }
  171. /**
  172. * returns the user language, common language and other languages in an
  173. * associative array
  174. *
  175. * @param IUser $user
  176. * @return array
  177. */
  178. private function getLanguages(IUser $user) {
  179. $forceLanguage = $this->config->getSystemValue('force_language', false);
  180. if($forceLanguage !== false) {
  181. return [];
  182. }
  183. $uid = $user->getUID();
  184. $userConfLang = $this->config->getUserValue($uid, 'core', 'lang', $this->l10nFactory->findLanguage());
  185. $languages = $this->l10nFactory->getLanguages();
  186. // associate the user language with the proper array
  187. $userLangIndex = array_search($userConfLang, array_column($languages['commonlanguages'], 'code'));
  188. $userLang = $languages['commonlanguages'][$userLangIndex];
  189. // search in the other languages
  190. if ($userLangIndex === false) {
  191. $userLangIndex = array_search($userConfLang, array_column($languages['languages'], 'code'));
  192. $userLang = $languages['languages'][$userLangIndex];
  193. }
  194. // if user language is not available but set somehow: show the actual code as name
  195. if (!is_array($userLang)) {
  196. $userLang = [
  197. 'code' => $userConfLang,
  198. 'name' => $userConfLang,
  199. ];
  200. }
  201. return array_merge(
  202. array('activelanguage' => $userLang),
  203. $languages
  204. );
  205. }
  206. private function getLocales(IUser $user) {
  207. $forceLanguage = $this->config->getSystemValue('force_locale', false);
  208. if($forceLanguage !== false) {
  209. return [];
  210. }
  211. $uid = $user->getUID();
  212. $userLocaleString = $this->config->getUserValue($uid, 'core', 'locale', $this->l10nFactory->findLocale());
  213. $userLang = $this->config->getUserValue($uid, 'core', 'lang', $this->l10nFactory->findLanguage());
  214. $localeCodes = $this->l10nFactory->findAvailableLocales();
  215. $userLocale = array_filter($localeCodes, function($value) use ($userLocaleString) {
  216. return $userLocaleString === $value['code'];
  217. });
  218. if (!empty($userLocale))
  219. {
  220. $userLocale = reset($userLocale);
  221. }
  222. $localesForLanguage = array_filter($localeCodes, function($localeCode) use ($userLang) {
  223. return 0 === strpos($localeCode['code'], $userLang);
  224. });
  225. if (!$userLocale) {
  226. $userLocale = [
  227. 'code' => 'en',
  228. 'name' => 'English'
  229. ];
  230. }
  231. return [
  232. 'activelocaleLang' => $userLocaleString,
  233. 'activelocale' => $userLocale,
  234. 'locales' => $localeCodes,
  235. 'localesForLanguage' => $localesForLanguage,
  236. ];
  237. }
  238. /**
  239. * @param array $userData
  240. * @return array
  241. */
  242. private function getMessageParameters(array $userData) {
  243. $needVerifyMessage = [AccountManager::PROPERTY_EMAIL, AccountManager::PROPERTY_WEBSITE, AccountManager::PROPERTY_TWITTER];
  244. $messageParameters = [];
  245. foreach ($needVerifyMessage as $property) {
  246. switch ($userData[$property]['verified']) {
  247. case AccountManager::VERIFIED:
  248. $message = $this->l->t('Verifying');
  249. break;
  250. case AccountManager::VERIFICATION_IN_PROGRESS:
  251. $message = $this->l->t('Verifying …');
  252. break;
  253. default:
  254. $message = $this->l->t('Verify');
  255. }
  256. $messageParameters[$property . 'Message'] = $message;
  257. }
  258. return $messageParameters;
  259. }
  260. }