Passed
Push — master ( 4765f5...78c7e6 )
by Roeland
11:41 queued 14s
created

PersonalInfo::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 16
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 7
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Arthur Schiwon <[email protected]>
4
 *
5
 * @author Arthur Schiwon <[email protected]>
6
 * @author Christoph Wurst <[email protected]>
7
 * @author Georg Ehrke <[email protected]>
8
 * @author Joas Schilling <[email protected]>
9
 * @author John Molakvoæ (skjnldsv) <[email protected]>
10
 * @author Morris Jobke <[email protected]>
11
 * @author Robin Appelman <[email protected]>
12
 * @author Roeland Jago Douma <[email protected]>
13
 * @author Thomas Citharel <[email protected]>
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
32
namespace OCA\Settings\Settings\Personal;
33
34
use OC\Accounts\AccountManager;
35
use OCA\FederatedFileSharing\AppInfo\Application;
36
use OCP\App\IAppManager;
37
use OCP\AppFramework\Http\TemplateResponse;
38
use OCP\Files\FileInfo;
39
use OCP\IConfig;
40
use OCP\IGroup;
41
use OCP\IGroupManager;
42
use OCP\IL10N;
43
use OCP\IUser;
44
use OCP\IUserManager;
45
use OCP\L10N\IFactory;
46
use OCP\Settings\ISettings;
47
48
class PersonalInfo implements ISettings {
49
50
	/** @var IConfig */
51
	private $config;
52
	/** @var IUserManager */
53
	private $userManager;
54
	/** @var AccountManager */
55
	private $accountManager;
56
	/** @var IGroupManager */
57
	private $groupManager;
58
	/** @var IAppManager */
59
	private $appManager;
60
	/** @var IFactory */
61
	private $l10nFactory;
62
	/** @var IL10N */
63
	private $l;
64
65
	/**
66
	 * @param IConfig $config
67
	 * @param IUserManager $userManager
68
	 * @param IGroupManager $groupManager
69
	 * @param AccountManager $accountManager
70
	 * @param IFactory $l10nFactory
71
	 * @param IL10N $l
72
	 */
73
	public function __construct(
74
		IConfig $config,
75
		IUserManager $userManager,
76
		IGroupManager $groupManager,
77
		AccountManager $accountManager,
78
		IAppManager $appManager,
79
		IFactory $l10nFactory,
80
		IL10N $l
81
	) {
82
		$this->config = $config;
83
		$this->userManager = $userManager;
84
		$this->accountManager = $accountManager;
85
		$this->groupManager = $groupManager;
86
		$this->appManager = $appManager;
87
		$this->l10nFactory = $l10nFactory;
88
		$this->l = $l;
89
	}
90
91
	/**
92
	 * @return TemplateResponse returns the instance with all parameters set, ready to be rendered
93
	 * @since 9.1
94
	 */
95
	public function getForm() {
96
		$federatedFileSharingEnabled = $this->appManager->isEnabledForUser('federatedfilesharing');
97
		$lookupServerUploadEnabled = false;
98
		if($federatedFileSharingEnabled) {
99
			$federatedFileSharing = \OC::$server->query(Application::class);
100
			$shareProvider = $federatedFileSharing->getFederatedShareProvider();
101
			$lookupServerUploadEnabled = $shareProvider->isLookupServerUploadEnabled();
102
		}
103
104
		$uid = \OC_User::getUser();
105
		$user = $this->userManager->get($uid);
106
		$userData = $this->accountManager->getUser($user);
107
108
		$storageInfo = \OC_Helper::getStorageInfo('/');
109
		if ($storageInfo['quota'] === FileInfo::SPACE_UNLIMITED) {
110
			$totalSpace = $this->l->t('Unlimited');
111
		} else {
112
			$totalSpace = \OC_Helper::humanFileSize($storageInfo['total']);
113
		}
114
115
		$languageParameters = $this->getLanguages($user);
116
		$localeParameters = $this->getLocales($user);
117
		$messageParameters = $this->getMessageParameters($userData);
118
119
		$parameters = [
120
			'total_space' => $totalSpace,
121
			'usage' => \OC_Helper::humanFileSize($storageInfo['used']),
122
			'usage_relative' => round($storageInfo['relative']),
123
			'quota' => $storageInfo['quota'],
124
			'avatarChangeSupported' => $user->canChangeAvatar(),
125
			'lookupServerUploadEnabled' => $lookupServerUploadEnabled,
126
			'avatarScope' => $userData[AccountManager::PROPERTY_AVATAR]['scope'],
127
			'displayNameChangeSupported' => $user->canChangeDisplayName(),
128
			'displayName' => $userData[AccountManager::PROPERTY_DISPLAYNAME]['value'],
129
			'displayNameScope' => $userData[AccountManager::PROPERTY_DISPLAYNAME]['scope'],
130
			'email' => $userData[AccountManager::PROPERTY_EMAIL]['value'],
131
			'emailScope' => $userData[AccountManager::PROPERTY_EMAIL]['scope'],
132
			'emailVerification' => $userData[AccountManager::PROPERTY_EMAIL]['verified'],
133
			'phone' => $userData[AccountManager::PROPERTY_PHONE]['value'],
134
			'phoneScope' => $userData[AccountManager::PROPERTY_PHONE]['scope'],
135
			'address' => $userData[AccountManager::PROPERTY_ADDRESS]['value'],
136
			'addressScope' => $userData[AccountManager::PROPERTY_ADDRESS]['scope'],
137
			'website' =>  $userData[AccountManager::PROPERTY_WEBSITE]['value'],
138
			'websiteScope' =>  $userData[AccountManager::PROPERTY_WEBSITE]['scope'],
139
			'websiteVerification' => $userData[AccountManager::PROPERTY_WEBSITE]['verified'],
140
			'twitter' => $userData[AccountManager::PROPERTY_TWITTER]['value'],
141
			'twitterScope' => $userData[AccountManager::PROPERTY_TWITTER]['scope'],
142
			'twitterVerification' => $userData[AccountManager::PROPERTY_TWITTER]['verified'],
143
			'groups' => $this->getGroups($user),
144
		] + $messageParameters + $languageParameters + $localeParameters;
145
146
147
		return new TemplateResponse('settings', 'settings/personal/personal.info', $parameters, '');
148
	}
149
150
	/**
151
	 * @return string the section ID, e.g. 'sharing'
152
	 * @since 9.1
153
	 */
154
	public function getSection() {
155
		return 'personal-info';
156
	}
157
158
	/**
159
	 * @return int whether the form should be rather on the top or bottom of
160
	 * the admin section. The forms are arranged in ascending order of the
161
	 * priority values. It is required to return a value between 0 and 100.
162
	 *
163
	 * E.g.: 70
164
	 * @since 9.1
165
	 */
166
	public function getPriority() {
167
		return 10;
168
	}
169
170
	/**
171
	 * returns a sorted list of the user's group GIDs
172
	 *
173
	 * @param IUser $user
174
	 * @return array
175
	 */
176
	private function getGroups(IUser $user) {
177
		$groups = array_map(
178
			function(IGroup $group) {
179
				return $group->getDisplayName();
180
			},
181
			$this->groupManager->getUserGroups($user)
182
		);
183
		sort($groups);
184
185
		return $groups;
186
	}
187
188
	/**
189
	 * returns the user language, common language and other languages in an
190
	 * associative array
191
	 *
192
	 * @param IUser $user
193
	 * @return array
194
	 */
195
	private function getLanguages(IUser $user) {
196
		$forceLanguage = $this->config->getSystemValue('force_language', false);
197
		if($forceLanguage !== false) {
198
			return [];
199
		}
200
201
		$uid = $user->getUID();
202
203
		$userConfLang = $this->config->getUserValue($uid, 'core', 'lang', $this->l10nFactory->findLanguage());
204
		$languages = $this->l10nFactory->getLanguages();
0 ignored issues
show
Bug introduced by
The method getLanguages() does not exist on OCP\L10N\IFactory. Did you maybe mean getLanguageIterator()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

204
		/** @scrutinizer ignore-call */ 
205
  $languages = $this->l10nFactory->getLanguages();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
205
206
		// associate the user language with the proper array
207
		$userLangIndex = array_search($userConfLang, array_column($languages['commonlanguages'], 'code'));
208
		$userLang = $languages['commonlanguages'][$userLangIndex];
209
		// search in the other languages
210
		if ($userLangIndex === false) {
211
			$userLangIndex = array_search($userConfLang, array_column($languages['languages'], 'code'));
212
			$userLang = $languages['languages'][$userLangIndex];
213
		}
214
		// if user language is not available but set somehow: show the actual code as name
215
		if (!is_array($userLang)) {
216
			$userLang = [
217
				'code' => $userConfLang,
218
				'name' => $userConfLang,
219
			];
220
		}
221
222
		return array_merge(
223
			array('activelanguage' => $userLang),
224
			$languages
225
		);
226
	}
227
228
	private function getLocales(IUser $user) {
229
		$forceLanguage = $this->config->getSystemValue('force_locale', false);
230
		if($forceLanguage !== false) {
231
			return [];
232
		}
233
234
		$uid = $user->getUID();
235
236
		$userLocaleString = $this->config->getUserValue($uid, 'core', 'locale', $this->l10nFactory->findLocale());
237
238
		$userLang = $this->config->getUserValue($uid, 'core', 'lang', $this->l10nFactory->findLanguage());
239
240
		$localeCodes = $this->l10nFactory->findAvailableLocales();
241
242
		$userLocale = array_filter($localeCodes, function($value) use ($userLocaleString) {
243
			return $userLocaleString === $value['code'];
244
		});
245
246
		if (!empty($userLocale))
247
		{
248
			$userLocale = reset($userLocale);
249
		}
250
251
		$localesForLanguage = array_filter($localeCodes, function($localeCode) use ($userLang) {
252
			return 0 === strpos($localeCode['code'], $userLang);
253
		});
254
255
		if (!$userLocale) {
256
			$userLocale = [
257
				'code' => 'en',
258
				'name' => 'English'
259
			];
260
		}
261
262
		return [
263
			'activelocaleLang' => $userLocaleString,
264
			'activelocale' => $userLocale,
265
			'locales' => $localeCodes,
266
			'localesForLanguage' => $localesForLanguage,
267
		];
268
	}
269
270
	/**
271
	 * @param array $userData
272
	 * @return array
273
	 */
274
	private function getMessageParameters(array $userData) {
275
		$needVerifyMessage = [AccountManager::PROPERTY_EMAIL, AccountManager::PROPERTY_WEBSITE, AccountManager::PROPERTY_TWITTER];
276
		$messageParameters = [];
277
		foreach ($needVerifyMessage as $property) {
278
			switch ($userData[$property]['verified']) {
279
				case AccountManager::VERIFIED:
280
					$message = $this->l->t('Verifying');
281
					break;
282
				case AccountManager::VERIFICATION_IN_PROGRESS:
283
					$message = $this->l->t('Verifying …');
284
					break;
285
				default:
286
					$message = $this->l->t('Verify');
287
			}
288
			$messageParameters[$property . 'Message'] = $message;
289
		}
290
		return $messageParameters;
291
	}
292
293
}
294