Passed
Push — master ( 8cedf0...28b0e8 )
by Julius
18:26 queued 12s
created

PersonalInfo::getEmails()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 15
nc 1
nop 1
dl 0
loc 24
rs 9.7666
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright Copyright (c) 2017 Arthur Schiwon <[email protected]>
7
 *
8
 * @author Arthur Schiwon <[email protected]>
9
 * @author Christoph Wurst <[email protected]>
10
 * @author Christopher Ng <[email protected]>
11
 * @author Georg Ehrke <[email protected]>
12
 * @author Joas Schilling <[email protected]>
13
 * @author John Molakvoæ <[email protected]>
14
 * @author Morris Jobke <[email protected]>
15
 * @author Robin Appelman <[email protected]>
16
 * @author Roeland Jago Douma <[email protected]>
17
 * @author Thomas Citharel <[email protected]>
18
 * @author Vincent Petry <[email protected]>
19
 *
20
 * @license GNU AGPL version 3 or any later version
21
 *
22
 * This program is free software: you can redistribute it and/or modify
23
 * it under the terms of the GNU Affero General Public License as
24
 * published by the Free Software Foundation, either version 3 of the
25
 * License, or (at your option) any later version.
26
 *
27
 * This program is distributed in the hope that it will be useful,
28
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30
 * GNU Affero General Public License for more details.
31
 *
32
 * You should have received a copy of the GNU Affero General Public License
33
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
34
 *
35
 */
36
namespace OCA\Settings\Settings\Personal;
37
38
use OCA\FederatedFileSharing\FederatedShareProvider;
39
use OCP\Accounts\IAccount;
40
use OCP\Accounts\IAccountManager;
41
use OCP\App\IAppManager;
42
use OCP\AppFramework\Http\TemplateResponse;
43
use OCP\Files\FileInfo;
44
use OCP\IConfig;
45
use OCP\IGroup;
46
use OCP\IGroupManager;
47
use OCP\IL10N;
48
use OCP\IUser;
49
use OCP\IUserManager;
50
use OCP\L10N\IFactory;
51
use OCP\Settings\ISettings;
52
use OCP\Accounts\IAccountProperty;
53
use OCP\AppFramework\Services\IInitialState;
54
55
class PersonalInfo implements ISettings {
56
57
	/** @var IConfig */
58
	private $config;
59
	/** @var IUserManager */
60
	private $userManager;
61
	/** @var IAccountManager */
62
	private $accountManager;
63
	/** @var IGroupManager */
64
	private $groupManager;
65
	/** @var IAppManager */
66
	private $appManager;
67
	/** @var IFactory */
68
	private $l10nFactory;
69
	/** @var IL10N */
70
	private $l;
71
	/** @var IInitialState */
72
	private $initialStateService;
73
74
	public function __construct(
75
		IConfig $config,
76
		IUserManager $userManager,
77
		IGroupManager $groupManager,
78
		IAccountManager $accountManager,
79
		IAppManager $appManager,
80
		IFactory $l10nFactory,
81
		IL10N $l,
82
		IInitialState $initialStateService
83
	) {
84
		$this->config = $config;
85
		$this->userManager = $userManager;
86
		$this->accountManager = $accountManager;
87
		$this->groupManager = $groupManager;
88
		$this->appManager = $appManager;
89
		$this->l10nFactory = $l10nFactory;
90
		$this->l = $l;
91
		$this->initialStateService = $initialStateService;
92
	}
93
94
	public function getForm(): TemplateResponse {
95
		$federatedFileSharingEnabled = $this->appManager->isEnabledForUser('federatedfilesharing');
96
		$lookupServerUploadEnabled = false;
97
		if ($federatedFileSharingEnabled) {
98
			/** @var FederatedShareProvider $shareProvider */
99
			$shareProvider = \OC::$server->query(FederatedShareProvider::class);
100
			$lookupServerUploadEnabled = $shareProvider->isLookupServerUploadEnabled();
101
		}
102
103
		$uid = \OC_User::getUser();
104
		$user = $this->userManager->get($uid);
105
		$account = $this->accountManager->getAccount($user);
106
107
		// make sure FS is setup before querying storage related stuff...
108
		\OC_Util::setupFS($user->getUID());
109
110
		$storageInfo = \OC_Helper::getStorageInfo('/');
111
		if ($storageInfo['quota'] === FileInfo::SPACE_UNLIMITED) {
112
			$totalSpace = $this->l->t('Unlimited');
113
		} else {
114
			$totalSpace = \OC_Helper::humanFileSize($storageInfo['total']);
115
		}
116
117
		$languageParameters = $this->getLanguages($user);
118
		$localeParameters = $this->getLocales($user);
119
		$messageParameters = $this->getMessageParameters($account);
120
121
		$parameters = [
122
			'total_space' => $totalSpace,
123
			'usage' => \OC_Helper::humanFileSize($storageInfo['used']),
124
			'usage_relative' => round($storageInfo['relative']),
125
			'quota' => $storageInfo['quota'],
126
			'avatarChangeSupported' => $user->canChangeAvatar(),
127
			'lookupServerUploadEnabled' => $lookupServerUploadEnabled,
128
			'avatarScope' => $account->getProperty(IAccountManager::PROPERTY_AVATAR)->getScope(),
129
			'displayNameChangeSupported' => $user->canChangeDisplayName(),
130
			'displayName' => $account->getProperty(IAccountManager::PROPERTY_DISPLAYNAME)->getValue(),
131
			'displayNameScope' => $account->getProperty(IAccountManager::PROPERTY_DISPLAYNAME)->getScope(),
132
			'email' => $account->getProperty(IAccountManager::PROPERTY_EMAIL)->getValue(),
133
			'emailScope' => $account->getProperty(IAccountManager::PROPERTY_EMAIL)->getScope(),
134
			'emailVerification' => $account->getProperty(IAccountManager::PROPERTY_EMAIL)->getVerified(),
135
			'phone' => $account->getProperty(IAccountManager::PROPERTY_PHONE)->getValue(),
136
			'phoneScope' => $account->getProperty(IAccountManager::PROPERTY_PHONE)->getScope(),
137
			'address' => $account->getProperty(IAccountManager::PROPERTY_ADDRESS)->getValue(),
138
			'addressScope' => $account->getProperty(IAccountManager::PROPERTY_ADDRESS)->getScope(),
139
			'website' => $account->getProperty(IAccountManager::PROPERTY_WEBSITE)->getValue(),
140
			'websiteScope' => $account->getProperty(IAccountManager::PROPERTY_WEBSITE)->getScope(),
141
			'websiteVerification' => $account->getProperty(IAccountManager::PROPERTY_WEBSITE)->getVerified(),
142
			'twitter' => $account->getProperty(IAccountManager::PROPERTY_TWITTER)->getValue(),
143
			'twitterScope' => $account->getProperty(IAccountManager::PROPERTY_TWITTER)->getScope(),
144
			'twitterVerification' => $account->getProperty(IAccountManager::PROPERTY_TWITTER)->getVerified(),
145
			'groups' => $this->getGroups($user),
146
		] + $messageParameters + $languageParameters + $localeParameters;
147
148
		$emails = $this->getEmails($account);
149
150
		$accountParameters = [
151
			'displayNameChangeSupported' => $user->canChangeDisplayName(),
152
		];
153
154
		$this->initialStateService->provideInitialState('emails', $emails);
155
		$this->initialStateService->provideInitialState('accountParameters', $accountParameters);
156
157
		return new TemplateResponse('settings', 'settings/personal/personal.info', $parameters, '');
158
	}
159
160
	/**
161
	 * @return string the section ID, e.g. 'sharing'
162
	 * @since 9.1
163
	 */
164
	public function getSection(): string {
165
		return 'personal-info';
166
	}
167
168
	/**
169
	 * @return int whether the form should be rather on the top or bottom of
170
	 * the admin section. The forms are arranged in ascending order of the
171
	 * priority values. It is required to return a value between 0 and 100.
172
	 *
173
	 * E.g.: 70
174
	 * @since 9.1
175
	 */
176
	public function getPriority(): int {
177
		return 10;
178
	}
179
180
	/**
181
	 * returns a sorted list of the user's group GIDs
182
	 *
183
	 * @param IUser $user
184
	 * @return array
185
	 */
186
	private function getGroups(IUser $user): array {
187
		$groups = array_map(
188
			static function (IGroup $group) {
189
				return $group->getDisplayName();
190
			},
191
			$this->groupManager->getUserGroups($user)
192
		);
193
		sort($groups);
194
195
		return $groups;
196
	}
197
198
	/**
199
	 * returns the primary email and additional emails in an
200
	 * associative array
201
	 *
202
	 * @param IAccount $account
203
	 * @return array
204
	 */
205
	private function getEmails(IAccount $account): array {
206
		$primaryEmail = [
207
			'value' => $account->getProperty(IAccountManager::PROPERTY_EMAIL)->getValue(),
208
			'scope' => $account->getProperty(IAccountManager::PROPERTY_EMAIL)->getScope(),
209
			'verified' => $account->getProperty(IAccountManager::PROPERTY_EMAIL)->getVerified(),
210
		];
211
212
		$additionalEmails = array_map(
213
			function (IAccountProperty $property) {
214
				return [
215
					'value' => $property->getValue(),
216
					'scope' => $property->getScope(),
217
					'verified' => $property->getVerified(),
218
				];
219
			},
220
			$account->getPropertyCollection(IAccountManager::COLLECTION_EMAIL)->getProperties()
221
		);
222
223
		$emails = [
224
			'primaryEmail' => $primaryEmail,
225
			'additionalEmails' => $additionalEmails,
226
		];
227
228
		return $emails;
229
	}
230
231
	/**
232
	 * returns the user language, common language and other languages in an
233
	 * associative array
234
	 *
235
	 * @param IUser $user
236
	 * @return array
237
	 */
238
	private function getLanguages(IUser $user): array {
239
		$forceLanguage = $this->config->getSystemValue('force_language', false);
240
		if ($forceLanguage !== false) {
241
			return [];
242
		}
243
244
		$uid = $user->getUID();
245
246
		$userConfLang = $this->config->getUserValue($uid, 'core', 'lang', $this->l10nFactory->findLanguage());
247
		$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

247
		/** @scrutinizer ignore-call */ 
248
  $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...
248
249
		// associate the user language with the proper array
250
		$userLangIndex = array_search($userConfLang, array_column($languages['commonlanguages'], 'code'));
251
		$userLang = $languages['commonlanguages'][$userLangIndex];
252
		// search in the other languages
253
		if ($userLangIndex === false) {
254
			$userLangIndex = array_search($userConfLang, array_column($languages['languages'], 'code'));
255
			$userLang = $languages['languages'][$userLangIndex];
256
		}
257
		// if user language is not available but set somehow: show the actual code as name
258
		if (!is_array($userLang)) {
259
			$userLang = [
260
				'code' => $userConfLang,
261
				'name' => $userConfLang,
262
			];
263
		}
264
265
		return array_merge(
266
			['activelanguage' => $userLang],
267
			$languages
268
		);
269
	}
270
271
	private function getLocales(IUser $user): array {
272
		$forceLanguage = $this->config->getSystemValue('force_locale', false);
273
		if ($forceLanguage !== false) {
274
			return [];
275
		}
276
277
		$uid = $user->getUID();
278
279
		$userLocaleString = $this->config->getUserValue($uid, 'core', 'locale', $this->l10nFactory->findLocale());
280
281
		$userLang = $this->config->getUserValue($uid, 'core', 'lang', $this->l10nFactory->findLanguage());
282
283
		$localeCodes = $this->l10nFactory->findAvailableLocales();
284
285
		$userLocale = array_filter($localeCodes, function ($value) use ($userLocaleString) {
286
			return $userLocaleString === $value['code'];
287
		});
288
289
		if (!empty($userLocale)) {
290
			$userLocale = reset($userLocale);
291
		}
292
293
		$localesForLanguage = array_filter($localeCodes, function ($localeCode) use ($userLang) {
294
			return 0 === strpos($localeCode['code'], $userLang);
295
		});
296
297
		if (!$userLocale) {
298
			$userLocale = [
299
				'code' => 'en',
300
				'name' => 'English'
301
			];
302
		}
303
304
		return [
305
			'activelocaleLang' => $userLocaleString,
306
			'activelocale' => $userLocale,
307
			'locales' => $localeCodes,
308
			'localesForLanguage' => $localesForLanguage,
309
		];
310
	}
311
312
	/**
313
	 * @param IAccount $account
314
	 * @return array
315
	 */
316
	private function getMessageParameters(IAccount $account): array {
317
		$needVerifyMessage = [IAccountManager::PROPERTY_EMAIL, IAccountManager::PROPERTY_WEBSITE, IAccountManager::PROPERTY_TWITTER];
318
		$messageParameters = [];
319
		foreach ($needVerifyMessage as $property) {
320
			switch ($account->getProperty($property)->getVerified()) {
321
				case IAccountManager::VERIFIED:
322
					$message = $this->l->t('Verifying');
323
					break;
324
				case IAccountManager::VERIFICATION_IN_PROGRESS:
325
					$message = $this->l->t('Verifying …');
326
					break;
327
				default:
328
					$message = $this->l->t('Verify');
329
			}
330
			$messageParameters[$property . 'Message'] = $message;
331
		}
332
		return $messageParameters;
333
	}
334
}
335