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.

PersonalInfo.php 8.7KB

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