Completed
Push — master ( b40bae...a2c518 )
by Morris
16:08
created

PersonalInfo::getLanguages()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 32
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 19
nc 5
nop 1
dl 0
loc 32
rs 8.5806
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
 *
8
 * @license GNU AGPL version 3 or any later version
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License as
12
 * published by the Free Software Foundation, either version 3 of the
13
 * License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 */
24
25
namespace OC\Settings\Personal;
26
27
use OC\Accounts\AccountManager;
28
use OCA\FederatedFileSharing\AppInfo\Application;
29
use OCP\App\IAppManager;
30
use OCP\AppFramework\Http\TemplateResponse;
31
use OCP\Files\FileInfo;
32
use OCP\IConfig;
33
use OCP\IGroup;
34
use OCP\IGroupManager;
35
use OCP\IL10N;
36
use OCP\IUser;
37
use OCP\IUserManager;
38
use OCP\L10N\IFactory;
39
use OCP\Settings\ISettings;
40
41
class PersonalInfo implements ISettings {
42
43
	/** @var IConfig */
44
	private $config;
45
	/** @var IUserManager */
46
	private $userManager;
47
	/** @var AccountManager */
48
	private $accountManager;
49
	/** @var IGroupManager */
50
	private $groupManager;
51
	/** @var IAppManager */
52
	private $appManager;
53
	/** @var IFactory */
54
	private $l10nFactory;
55
	/** @var IL10N */
56
	private $l;
57
58
	/**
59
	 * @param IConfig $config
60
	 * @param IUserManager $userManager
61
	 * @param IGroupManager $groupManager
62
	 * @param AccountManager $accountManager
63
	 * @param IFactory $l10nFactory
64
	 * @param IL10N $l
65
	 */
66
	public function __construct(
67
		IConfig $config,
68
		IUserManager $userManager,
69
		IGroupManager $groupManager,
70
		AccountManager $accountManager,
71
		IAppManager $appManager,
72
		IFactory $l10nFactory,
73
		IL10N $l
74
	) {
75
		$this->config = $config;
76
		$this->userManager = $userManager;
77
		$this->accountManager = $accountManager;
78
		$this->groupManager = $groupManager;
79
		$this->appManager = $appManager;
80
		$this->l10nFactory = $l10nFactory;
81
		$this->l = $l;
82
	}
83
84
	/**
85
	 * @return TemplateResponse returns the instance with all parameters set, ready to be rendered
86
	 * @since 9.1
87
	 */
88
	public function getForm() {
89
		$federatedFileSharingEnabled = $this->appManager->isEnabledForUser('federatedfilesharing');
90
		$lookupServerUploadEnabled = false;
91
		if($federatedFileSharingEnabled) {
92
			$federatedFileSharing = new Application();
93
			$shareProvider = $federatedFileSharing->getFederatedShareProvider();
94
			$lookupServerUploadEnabled = $shareProvider->isLookupServerUploadEnabled();
95
		}
96
97
		$uid = \OC_User::getUser();
98
		$user = $this->userManager->get($uid);
99
		$userData = $this->accountManager->getUser($user);
0 ignored issues
show
Bug introduced by
It seems like $user defined by $this->userManager->get($uid) on line 98 can be null; however, OC\Accounts\AccountManager::getUser() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
100
101
		$storageInfo = \OC_Helper::getStorageInfo('/');
102 View Code Duplication
		if ($storageInfo['quota'] === FileInfo::SPACE_UNLIMITED) {
103
			$totalSpace = $this->l->t('Unlimited');
104
		} else {
105
			$totalSpace = \OC_Helper::humanFileSize($storageInfo['total']);
106
		}
107
108
		$languageParameters = $this->getLanguages($user);
0 ignored issues
show
Bug introduced by
It seems like $user defined by $this->userManager->get($uid) on line 98 can be null; however, OC\Settings\Personal\PersonalInfo::getLanguages() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
109
		$messageParameters = $this->getMessageParameters($userData);
110
111
		$parameters = [
112
			'total_space' => $totalSpace,
113
			'usage' => \OC_Helper::humanFileSize($storageInfo['used']),
114
			'usage_relative' => round($storageInfo['relative']),
115
			'quota' => $storageInfo['quota'],
116
			'avatarChangeSupported' => $user->canChangeAvatar(),
117
			'lookupServerUploadEnabled' => $lookupServerUploadEnabled,
118
			'avatarScope' => $userData[AccountManager::PROPERTY_AVATAR]['scope'],
119
			'displayNameChangeSupported' => $user->canChangeDisplayName(),
120
			'displayName' => $userData[AccountManager::PROPERTY_DISPLAYNAME]['value'],
121
			'displayNameScope' => $userData[AccountManager::PROPERTY_DISPLAYNAME]['scope'],
122
			'email' => $userData[AccountManager::PROPERTY_EMAIL]['value'],
123
			'emailScope' => $userData[AccountManager::PROPERTY_EMAIL]['scope'],
124
			'emailVerification' => $userData[AccountManager::PROPERTY_EMAIL]['verified'],
125
			'phone' => $userData[AccountManager::PROPERTY_PHONE]['value'],
126
			'phoneScope' => $userData[AccountManager::PROPERTY_PHONE]['scope'],
127
			'address' => $userData[AccountManager::PROPERTY_ADDRESS]['value'],
128
			'addressScope' => $userData[AccountManager::PROPERTY_ADDRESS]['scope'],
129
			'website' =>  $userData[AccountManager::PROPERTY_WEBSITE]['value'],
130
			'websiteScope' =>  $userData[AccountManager::PROPERTY_WEBSITE]['scope'],
131
			'websiteVerification' => $userData[AccountManager::PROPERTY_WEBSITE]['verified'],
132
			'twitter' => $userData[AccountManager::PROPERTY_TWITTER]['value'],
133
			'twitterScope' => $userData[AccountManager::PROPERTY_TWITTER]['scope'],
134
			'twitterVerification' => $userData[AccountManager::PROPERTY_TWITTER]['verified'],
135
			'groups' => $this->getGroups($user),
0 ignored issues
show
Bug introduced by
It seems like $user defined by $this->userManager->get($uid) on line 98 can be null; however, OC\Settings\Personal\PersonalInfo::getGroups() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
136
			'passwordChangeSupported' => $user->canChangePassword(),
137
		] + $messageParameters + $languageParameters;
138
139
140
		return new TemplateResponse('settings', 'settings/personal/personal.info', $parameters, '');
141
	}
142
143
	/**
144
	 * @return string the section ID, e.g. 'sharing'
145
	 * @since 9.1
146
	 */
147
	public function getSection() {
148
		return 'personal-info';
149
	}
150
151
	/**
152
	 * @return int whether the form should be rather on the top or bottom of
153
	 * the admin section. The forms are arranged in ascending order of the
154
	 * priority values. It is required to return a value between 0 and 100.
155
	 *
156
	 * E.g.: 70
157
	 * @since 9.1
158
	 */
159
	public function getPriority() {
160
		return 10;
161
	}
162
163
	/**
164
	 * returns a sorted list of the user's group GIDs
165
	 *
166
	 * @param IUser $user
167
	 * @return array
168
	 */
169
	private function getGroups(IUser $user) {
170
		$groups = array_map(
171
			function(IGroup $group) {
172
				return $group->getDisplayName();
173
			},
174
			$this->groupManager->getUserGroups($user)
175
		);
176
		sort($groups);
177
178
		return $groups;
179
	}
180
181
	/**
182
	 * returns the user language, common language and other languages in an
183
	 * associative array
184
	 *
185
	 * @param IUser $user
186
	 * @return array
187
	 */
188
	private function getLanguages(IUser $user) {
189
		$forceLanguage = $this->config->getSystemValue('force_language', false);
190
		if($forceLanguage !== false) {
191
			return [];
192
		}
193
194
		$uid = $user->getUID();
195
196
		$userConfLang = $this->config->getUserValue($uid, 'core', 'lang', $this->l10nFactory->findLanguage());
197
		$languages = $this->l10nFactory->getLanguages();
198
199
		// associate the user language with the proper array
200
		$userLangIndex = array_search($userConfLang, array_column($languages['commonlanguages'], 'code'));
201
		$userLang = $languages['commonlanguages'][$userLangIndex];
202
		// search in the other languages
203
		if ($userLangIndex === false) {
204
			$userLangIndex = array_search($userConfLang, array_column($languages['languages'], 'code'));		
205
			$userLang = $languages['languages'][$userLangIndex];
206
		}
207
		// if user language is not available but set somehow: show the actual code as name
208
		if (!is_array($userLang)) {
209
			$userLang = [
210
				'code' => $userConfLang,
211
				'name' => $userConfLang,
212
			];
213
		}
214
215
		return array_merge(
216
			array('activelanguage' => $userLang),
217
			$languages
218
		);
219
	}
220
221
	/**
222
	 * @param array $userData
223
	 * @return array
224
	 */
225
	private function getMessageParameters(array $userData) {
226
		$needVerifyMessage = [AccountManager::PROPERTY_EMAIL, AccountManager::PROPERTY_WEBSITE, AccountManager::PROPERTY_TWITTER];
227
		$messageParameters = [];
228
		foreach ($needVerifyMessage as $property) {
229
			switch ($userData[$property]['verified']) {
230
				case AccountManager::VERIFIED:
231
					$message = $this->l->t('Verifying');
232
					break;
233
				case AccountManager::VERIFICATION_IN_PROGRESS:
234
					$message = $this->l->t('Verifying …');
235
					break;
236
				default:
237
					$message = $this->l->t('Verify');
238
			}
239
			$messageParameters[$property . 'Message'] = $message;
240
		}
241
		return $messageParameters;
242
	}
243
244
}
245