Passed
Push — master ( 2e7eb3...34dc16 )
by Morris
11:12
created

PersonalInfo::getGroups()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Arthur Schiwon <[email protected]>
4
 *
5
 * @author Arthur Schiwon <[email protected]>
6
 * @author Morris Jobke <[email protected]>
7
 * @author Thomas Citharel <[email protected]>
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
26
namespace OC\Settings\Personal;
27
28
use OC\Accounts\AccountManager;
29
use OC\Settings\Theming\ServerInfo;
30
use OCA\FederatedFileSharing\AppInfo\Application;
31
use OCP\App\IAppManager;
32
use OCP\AppFramework\Http\TemplateResponse;
33
use OCP\Files\FileInfo;
34
use OCP\IConfig;
35
use OCP\IGroup;
36
use OCP\IGroupManager;
37
use OCP\IL10N;
38
use OCP\IUser;
39
use OCP\IUserManager;
40
use OCP\L10N\IFactory;
41
use OCP\Settings\ISettings;
42
use OCP\Encryption\IManager as EncryptionManager;
43
44
class PersonalInfo implements ISettings {
45
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
	/** @var EncryptionManager */
61
	private $encryptionManager;
62
63
	/**
64
	 * @param IConfig $config
65
	 * @param IUserManager $userManager
66
	 * @param IGroupManager $groupManager
67
	 * @param AccountManager $accountManager
68
	 * @param IAppManager $appManager
69
	 * @param IFactory $l10nFactory
70
	 * @param IL10N $l
71
	 * @param EncryptionManager $encryptionManager
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
		EncryptionManager $encryptionManager
82
	) {
83
		$this->config = $config;
84
		$this->userManager = $userManager;
85
		$this->accountManager = $accountManager;
86
		$this->groupManager = $groupManager;
87
		$this->appManager = $appManager;
88
		$this->l10nFactory = $l10nFactory;
89
		$this->l = $l;
90
		$this->encryptionManager = $encryptionManager;
91
	}
92
93
	/**
94
	 * @return TemplateResponse returns the instance with all parameters set, ready to be rendered
95
	 * @since 9.1
96
	 */
97
	public function getForm() {
98
		$federatedFileSharingEnabled = $this->appManager->isEnabledForUser('federatedfilesharing');
99
		$lookupServerUploadEnabled = false;
100
		if($federatedFileSharingEnabled) {
101
			$federatedFileSharing = new Application();
102
			$shareProvider = $federatedFileSharing->getFederatedShareProvider();
103
			$lookupServerUploadEnabled = $shareProvider->isLookupServerUploadEnabled();
104
		}
105
106
		$uid = \OC_User::getUser();
107
		$user = $this->userManager->get($uid);
108
		$userData = $this->accountManager->getUser($user);
0 ignored issues
show
Bug introduced by
It seems like $user can also be of type null; however, parameter $user of OC\Accounts\AccountManager::getUser() does only seem to accept OCP\IUser, maybe add an additional type check? ( Ignorable by Annotation )

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

108
		$userData = $this->accountManager->getUser(/** @scrutinizer ignore-type */ $user);
Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like $user can also be of type null; however, parameter $user of OC\Settings\Personal\PersonalInfo::getLanguages() does only seem to accept OCP\IUser, maybe add an additional type check? ( Ignorable by Annotation )

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

117
		$languageParameters = $this->getLanguages(/** @scrutinizer ignore-type */ $user);
Loading history...
118
		$localeParameters = $this->getLocales($user);
0 ignored issues
show
Bug introduced by
It seems like $user can also be of type null; however, parameter $user of OC\Settings\Personal\PersonalInfo::getLocales() does only seem to accept OCP\IUser, maybe add an additional type check? ( Ignorable by Annotation )

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

118
		$localeParameters = $this->getLocales(/** @scrutinizer ignore-type */ $user);
Loading history...
119
		$messageParameters = $this->getMessageParameters($userData);
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' => $userData[AccountManager::PROPERTY_AVATAR]['scope'],
129
			'displayNameChangeSupported' => $user->canChangeDisplayName(),
130
			'displayName' => $userData[AccountManager::PROPERTY_DISPLAYNAME]['value'],
131
			'displayNameScope' => $userData[AccountManager::PROPERTY_DISPLAYNAME]['scope'],
132
			'email' => $userData[AccountManager::PROPERTY_EMAIL]['value'],
133
			'emailScope' => $userData[AccountManager::PROPERTY_EMAIL]['scope'],
134
			'emailVerification' => $userData[AccountManager::PROPERTY_EMAIL]['verified'],
135
			'phone' => $userData[AccountManager::PROPERTY_PHONE]['value'],
136
			'phoneScope' => $userData[AccountManager::PROPERTY_PHONE]['scope'],
137
			'address' => $userData[AccountManager::PROPERTY_ADDRESS]['value'],
138
			'addressScope' => $userData[AccountManager::PROPERTY_ADDRESS]['scope'],
139
			'website' =>  $userData[AccountManager::PROPERTY_WEBSITE]['value'],
140
			'websiteScope' =>  $userData[AccountManager::PROPERTY_WEBSITE]['scope'],
141
			'websiteVerification' => $userData[AccountManager::PROPERTY_WEBSITE]['verified'],
142
			'twitter' => $userData[AccountManager::PROPERTY_TWITTER]['value'],
143
			'twitterScope' => $userData[AccountManager::PROPERTY_TWITTER]['scope'],
144
			'twitterVerification' => $userData[AccountManager::PROPERTY_TWITTER]['verified'],
145
			'groups' => $this->getGroups($user),
0 ignored issues
show
Bug introduced by
It seems like $user can also be of type null; however, parameter $user of OC\Settings\Personal\PersonalInfo::getGroups() does only seem to accept OCP\IUser, maybe add an additional type check? ( Ignorable by Annotation )

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

145
			'groups' => $this->getGroups(/** @scrutinizer ignore-type */ $user),
Loading history...
146
		] + $this->getWhereIsYourDataParams() + $messageParameters + $languageParameters + $localeParameters;
147
148
		return new TemplateResponse('settings', 'settings/personal/personal.info', $parameters, '');
149
	}
150
151
	/**
152
	 * Returns the "where is your data" template params.
153
	 *
154
	 * @return array
155
	 */
156
	private function getWhereIsYourDataParams(): array {
157
158
		$adminContactConfigId = $this->config->getSystemValue(ServerInfo::SETTING_PROVIDER_ADMIN_CONTACT);
159
		$adminContact = $this->userManager->get($adminContactConfigId);
160
161
		$params = [
162
			'dataLocation' => $this->config->getSystemValue(ServerInfo::SETTING_LOCATION),
163
			'provider' => $this->config->getSystemValue(ServerInfo::SETTING_PROVIDER),
164
			'providerLink' => $this->config->getSystemValue(ServerInfo::SETTING_PROVIDER_WEBSITE),
165
			'providerPrivacyLink' => $this->config->getSystemValue(ServerInfo::SETTING_PROVIDER_PRIVACY_LINK),
166
			'encryptionEnabled' => $this->encryptionManager->isEnabled(),
167
			'adminName' => $adminContact !== null ? $adminContact->getDisplayName() : '',
168
			'adminMail' => $adminContact !== null ? $adminContact->getEMailAddress() : ''
169
		];
170
171
		$params['show_where_is_your_data_section'] = empty($params['dataLocation']) === false
172
			|| empty($params['provider']) === false
173
			|| $params['encryptionEnabled'] === true
174
			|| empty($params['adminName']) === false;
175
176
		return $params;
177
178
	}
179
180
	/**
181
	 * @return string the section ID, e.g. 'sharing'
182
	 * @since 9.1
183
	 */
184
	public function getSection() {
185
		return 'personal-info';
186
	}
187
188
	/**
189
	 * @return int whether the form should be rather on the top or bottom of
190
	 * the admin section. The forms are arranged in ascending order of the
191
	 * priority values. It is required to return a value between 0 and 100.
192
	 *
193
	 * E.g.: 70
194
	 * @since 9.1
195
	 */
196
	public function getPriority() {
197
		return 10;
198
	}
199
200
	/**
201
	 * returns a sorted list of the user's group GIDs
202
	 *
203
	 * @param IUser $user
204
	 * @return array
205
	 */
206
	private function getGroups(IUser $user) {
207
		$groups = array_map(
208
			function(IGroup $group) {
209
				return $group->getDisplayName();
210
			},
211
			$this->groupManager->getUserGroups($user)
212
		);
213
		sort($groups);
214
215
		return $groups;
216
	}
217
218
	/**
219
	 * returns the user language, common language and other languages in an
220
	 * associative array
221
	 *
222
	 * @param IUser $user
223
	 * @return array
224
	 */
225
	private function getLanguages(IUser $user) {
226
		$forceLanguage = $this->config->getSystemValue('force_language', false);
227
		if($forceLanguage !== false) {
228
			return [];
229
		}
230
231
		$uid = $user->getUID();
232
233
		$userConfLang = $this->config->getUserValue($uid, 'core', 'lang', $this->l10nFactory->findLanguage());
234
		$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

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