Completed
Pull Request — master (#8255)
by Joas
44:07 queued 26:54
created
lib/private/Settings/Personal/PersonalInfo.php 1 patch
Indentation   +216 added lines, -216 removed lines patch added patch discarded remove patch
@@ -39,246 +39,246 @@
 block discarded – undo
39 39
 use OCP\Settings\ISettings;
40 40
 
41 41
 class PersonalInfo implements ISettings {
42
-	/** @var IConfig */
43
-	private $config;
44
-	/** @var IUserManager */
45
-	private $userManager;
46
-	/** @var AccountManager */
47
-	private $accountManager;
48
-	/** @var IGroupManager */
49
-	private $groupManager;
50
-	/** @var IAppManager */
51
-	private $appManager;
52
-	/** @var IFactory */
53
-	private $l10nFactory;
42
+    /** @var IConfig */
43
+    private $config;
44
+    /** @var IUserManager */
45
+    private $userManager;
46
+    /** @var AccountManager */
47
+    private $accountManager;
48
+    /** @var IGroupManager */
49
+    private $groupManager;
50
+    /** @var IAppManager */
51
+    private $appManager;
52
+    /** @var IFactory */
53
+    private $l10nFactory;
54 54
 
55
-	const COMMON_LANGUAGE_CODES = [
56
-		'en', 'es', 'fr', 'de', 'de_DE', 'ja', 'ar', 'ru', 'nl', 'it',
57
-		'pt_BR', 'pt_PT', 'da', 'fi_FI', 'nb_NO', 'sv', 'tr', 'zh_CN', 'ko'
58
-	];
55
+    const COMMON_LANGUAGE_CODES = [
56
+        'en', 'es', 'fr', 'de', 'de_DE', 'ja', 'ar', 'ru', 'nl', 'it',
57
+        'pt_BR', 'pt_PT', 'da', 'fi_FI', 'nb_NO', 'sv', 'tr', 'zh_CN', 'ko'
58
+    ];
59 59
 
60
-	/** @var IL10N */
61
-	private $l;
60
+    /** @var IL10N */
61
+    private $l;
62 62
 
63
-	/**
64
-	 * @param IConfig $config
65
-	 * @param IUserManager $userManager
66
-	 * @param IGroupManager $groupManager
67
-	 * @param AccountManager $accountManager
68
-	 * @param IFactory $l10nFactory
69
-	 * @param IL10N $l
70
-	 */
71
-	public function __construct(
72
-		IConfig $config,
73
-		IUserManager $userManager,
74
-		IGroupManager $groupManager,
75
-		AccountManager $accountManager,
76
-		IAppManager $appManager,
77
-		IFactory $l10nFactory,
78
-		IL10N $l
79
-	) {
80
-		$this->config = $config;
81
-		$this->userManager = $userManager;
82
-		$this->accountManager = $accountManager;
83
-		$this->groupManager = $groupManager;
84
-		$this->appManager = $appManager;
85
-		$this->l10nFactory = $l10nFactory;
86
-		$this->l = $l;
87
-	}
63
+    /**
64
+     * @param IConfig $config
65
+     * @param IUserManager $userManager
66
+     * @param IGroupManager $groupManager
67
+     * @param AccountManager $accountManager
68
+     * @param IFactory $l10nFactory
69
+     * @param IL10N $l
70
+     */
71
+    public function __construct(
72
+        IConfig $config,
73
+        IUserManager $userManager,
74
+        IGroupManager $groupManager,
75
+        AccountManager $accountManager,
76
+        IAppManager $appManager,
77
+        IFactory $l10nFactory,
78
+        IL10N $l
79
+    ) {
80
+        $this->config = $config;
81
+        $this->userManager = $userManager;
82
+        $this->accountManager = $accountManager;
83
+        $this->groupManager = $groupManager;
84
+        $this->appManager = $appManager;
85
+        $this->l10nFactory = $l10nFactory;
86
+        $this->l = $l;
87
+    }
88 88
 
89
-	/**
90
-	 * @return TemplateResponse returns the instance with all parameters set, ready to be rendered
91
-	 * @since 9.1
92
-	 */
93
-	public function getForm() {
94
-		$federatedFileSharingEnabled = $this->appManager->isEnabledForUser('federatedfilesharing');
95
-		$lookupServerUploadEnabled = false;
96
-		if($federatedFileSharingEnabled) {
97
-			$federatedFileSharing = new Application();
98
-			$shareProvider = $federatedFileSharing->getFederatedShareProvider();
99
-			$lookupServerUploadEnabled = $shareProvider->isLookupServerUploadEnabled();
100
-		}
89
+    /**
90
+     * @return TemplateResponse returns the instance with all parameters set, ready to be rendered
91
+     * @since 9.1
92
+     */
93
+    public function getForm() {
94
+        $federatedFileSharingEnabled = $this->appManager->isEnabledForUser('federatedfilesharing');
95
+        $lookupServerUploadEnabled = false;
96
+        if($federatedFileSharingEnabled) {
97
+            $federatedFileSharing = new Application();
98
+            $shareProvider = $federatedFileSharing->getFederatedShareProvider();
99
+            $lookupServerUploadEnabled = $shareProvider->isLookupServerUploadEnabled();
100
+        }
101 101
 
102
-		$uid = \OC_User::getUser();
103
-		$user = $this->userManager->get($uid);
104
-		$userData = $this->accountManager->getUser($user);
102
+        $uid = \OC_User::getUser();
103
+        $user = $this->userManager->get($uid);
104
+        $userData = $this->accountManager->getUser($user);
105 105
 
106
-		$storageInfo = \OC_Helper::getStorageInfo('/');
107
-		if ($storageInfo['quota'] === FileInfo::SPACE_UNLIMITED) {
108
-			$totalSpace = $this->l->t('Unlimited');
109
-		} else {
110
-			$totalSpace = \OC_Helper::humanFileSize($storageInfo['total']);
111
-		}
106
+        $storageInfo = \OC_Helper::getStorageInfo('/');
107
+        if ($storageInfo['quota'] === FileInfo::SPACE_UNLIMITED) {
108
+            $totalSpace = $this->l->t('Unlimited');
109
+        } else {
110
+            $totalSpace = \OC_Helper::humanFileSize($storageInfo['total']);
111
+        }
112 112
 
113
-		$languageParameters = $this->getLanguages($user);
114
-		$messageParameters = $this->getMessageParameters($userData);
113
+        $languageParameters = $this->getLanguages($user);
114
+        $messageParameters = $this->getMessageParameters($userData);
115 115
 
116
-		$parameters = [
117
-			'total_space' => $totalSpace,
118
-			'usage' => \OC_Helper::humanFileSize($storageInfo['used']),
119
-			'usage_relative' => round($storageInfo['relative']),
120
-			'quota' => $storageInfo['quota'],
121
-			'avatarChangeSupported' => $user->canChangeAvatar(),
122
-			'lookupServerUploadEnabled' => $lookupServerUploadEnabled,
123
-			'avatarScope' => $userData[AccountManager::PROPERTY_AVATAR]['scope'],
124
-			'displayNameChangeSupported' => $user->canChangeDisplayName(),
125
-			'displayName' => $userData[AccountManager::PROPERTY_DISPLAYNAME]['value'],
126
-			'displayNameScope' => $userData[AccountManager::PROPERTY_DISPLAYNAME]['scope'],
127
-			'email' => $userData[AccountManager::PROPERTY_EMAIL]['value'],
128
-			'emailScope' => $userData[AccountManager::PROPERTY_EMAIL]['scope'],
129
-			'emailVerification' => $userData[AccountManager::PROPERTY_EMAIL]['verified'],
130
-			'phone' => $userData[AccountManager::PROPERTY_PHONE]['value'],
131
-			'phoneScope' => $userData[AccountManager::PROPERTY_PHONE]['scope'],
132
-			'address' => $userData[AccountManager::PROPERTY_ADDRESS]['value'],
133
-			'addressScope' => $userData[AccountManager::PROPERTY_ADDRESS]['scope'],
134
-			'website' =>  $userData[AccountManager::PROPERTY_WEBSITE]['value'],
135
-			'websiteScope' =>  $userData[AccountManager::PROPERTY_WEBSITE]['scope'],
136
-			'websiteVerification' => $userData[AccountManager::PROPERTY_WEBSITE]['verified'],
137
-			'twitter' => $userData[AccountManager::PROPERTY_TWITTER]['value'],
138
-			'twitterScope' => $userData[AccountManager::PROPERTY_TWITTER]['scope'],
139
-			'twitterVerification' => $userData[AccountManager::PROPERTY_TWITTER]['verified'],
140
-			'groups' => $this->getGroups($user),
141
-			'passwordChangeSupported' => $user->canChangePassword(),
142
-		] + $messageParameters + $languageParameters;
116
+        $parameters = [
117
+            'total_space' => $totalSpace,
118
+            'usage' => \OC_Helper::humanFileSize($storageInfo['used']),
119
+            'usage_relative' => round($storageInfo['relative']),
120
+            'quota' => $storageInfo['quota'],
121
+            'avatarChangeSupported' => $user->canChangeAvatar(),
122
+            'lookupServerUploadEnabled' => $lookupServerUploadEnabled,
123
+            'avatarScope' => $userData[AccountManager::PROPERTY_AVATAR]['scope'],
124
+            'displayNameChangeSupported' => $user->canChangeDisplayName(),
125
+            'displayName' => $userData[AccountManager::PROPERTY_DISPLAYNAME]['value'],
126
+            'displayNameScope' => $userData[AccountManager::PROPERTY_DISPLAYNAME]['scope'],
127
+            'email' => $userData[AccountManager::PROPERTY_EMAIL]['value'],
128
+            'emailScope' => $userData[AccountManager::PROPERTY_EMAIL]['scope'],
129
+            'emailVerification' => $userData[AccountManager::PROPERTY_EMAIL]['verified'],
130
+            'phone' => $userData[AccountManager::PROPERTY_PHONE]['value'],
131
+            'phoneScope' => $userData[AccountManager::PROPERTY_PHONE]['scope'],
132
+            'address' => $userData[AccountManager::PROPERTY_ADDRESS]['value'],
133
+            'addressScope' => $userData[AccountManager::PROPERTY_ADDRESS]['scope'],
134
+            'website' =>  $userData[AccountManager::PROPERTY_WEBSITE]['value'],
135
+            'websiteScope' =>  $userData[AccountManager::PROPERTY_WEBSITE]['scope'],
136
+            'websiteVerification' => $userData[AccountManager::PROPERTY_WEBSITE]['verified'],
137
+            'twitter' => $userData[AccountManager::PROPERTY_TWITTER]['value'],
138
+            'twitterScope' => $userData[AccountManager::PROPERTY_TWITTER]['scope'],
139
+            'twitterVerification' => $userData[AccountManager::PROPERTY_TWITTER]['verified'],
140
+            'groups' => $this->getGroups($user),
141
+            'passwordChangeSupported' => $user->canChangePassword(),
142
+        ] + $messageParameters + $languageParameters;
143 143
 
144 144
 
145
-		return new TemplateResponse('settings', 'settings/personal/personal.info', $parameters, '');
146
-	}
145
+        return new TemplateResponse('settings', 'settings/personal/personal.info', $parameters, '');
146
+    }
147 147
 
148
-	/**
149
-	 * @return string the section ID, e.g. 'sharing'
150
-	 * @since 9.1
151
-	 */
152
-	public function getSection() {
153
-		return 'personal-info';
154
-	}
148
+    /**
149
+     * @return string the section ID, e.g. 'sharing'
150
+     * @since 9.1
151
+     */
152
+    public function getSection() {
153
+        return 'personal-info';
154
+    }
155 155
 
156
-	/**
157
-	 * @return int whether the form should be rather on the top or bottom of
158
-	 * the admin section. The forms are arranged in ascending order of the
159
-	 * priority values. It is required to return a value between 0 and 100.
160
-	 *
161
-	 * E.g.: 70
162
-	 * @since 9.1
163
-	 */
164
-	public function getPriority() {
165
-		return 10;
166
-	}
156
+    /**
157
+     * @return int whether the form should be rather on the top or bottom of
158
+     * the admin section. The forms are arranged in ascending order of the
159
+     * priority values. It is required to return a value between 0 and 100.
160
+     *
161
+     * E.g.: 70
162
+     * @since 9.1
163
+     */
164
+    public function getPriority() {
165
+        return 10;
166
+    }
167 167
 
168
-	/**
169
-	 * returns a sorted list of the user's group GIDs
170
-	 *
171
-	 * @param IUser $user
172
-	 * @return array
173
-	 */
174
-	private function getGroups(IUser $user) {
175
-		$groups = array_map(
176
-			function(IGroup $group) {
177
-				return $group->getDisplayName();
178
-			},
179
-			$this->groupManager->getUserGroups($user)
180
-		);
181
-		sort($groups);
168
+    /**
169
+     * returns a sorted list of the user's group GIDs
170
+     *
171
+     * @param IUser $user
172
+     * @return array
173
+     */
174
+    private function getGroups(IUser $user) {
175
+        $groups = array_map(
176
+            function(IGroup $group) {
177
+                return $group->getDisplayName();
178
+            },
179
+            $this->groupManager->getUserGroups($user)
180
+        );
181
+        sort($groups);
182 182
 
183
-		return $groups;
184
-	}
183
+        return $groups;
184
+    }
185 185
 
186
-	/**
187
-	 * returns the user language, common language and other languages in an
188
-	 * associative array
189
-	 *
190
-	 * @param IUser $user
191
-	 * @return array
192
-	 */
193
-	private function getLanguages(IUser $user) {
194
-		$forceLanguage = $this->config->getSystemValue('force_language', false);
195
-		if($forceLanguage !== false) {
196
-			return [];
197
-		}
186
+    /**
187
+     * returns the user language, common language and other languages in an
188
+     * associative array
189
+     *
190
+     * @param IUser $user
191
+     * @return array
192
+     */
193
+    private function getLanguages(IUser $user) {
194
+        $forceLanguage = $this->config->getSystemValue('force_language', false);
195
+        if($forceLanguage !== false) {
196
+            return [];
197
+        }
198 198
 
199
-		$uid = $user->getUID();
199
+        $uid = $user->getUID();
200 200
 
201
-		$userLang = $this->config->getUserValue($uid, 'core', 'lang', $this->l10nFactory->findLanguage());
202
-		$languageCodes = $this->l10nFactory->findAvailableLanguages();
201
+        $userLang = $this->config->getUserValue($uid, 'core', 'lang', $this->l10nFactory->findLanguage());
202
+        $languageCodes = $this->l10nFactory->findAvailableLanguages();
203 203
 
204
-		$commonLanguages = [];
205
-		$languages = [];
204
+        $commonLanguages = [];
205
+        $languages = [];
206 206
 
207
-		foreach($languageCodes as $lang) {
208
-			$l = \OC::$server->getL10N('lib', $lang);
209
-			// TRANSLATORS this is the language name for the language switcher in the personal settings and should be the localized version
210
-			$potentialName = (string) $l->t('__language_name__');
211
-			if($l->getLanguageCode() === $lang && $potentialName[0] !== '_') {//first check if the language name is in the translation file
212
-				$ln = array('code' => $lang, 'name' => $potentialName);
213
-			} elseif ($lang === 'en') {
214
-				$ln = ['code' => $lang, 'name' => 'English (US)'];
215
-			}else{//fallback to language code
216
-				$ln=array('code'=>$lang, 'name'=>$lang);
217
-			}
207
+        foreach($languageCodes as $lang) {
208
+            $l = \OC::$server->getL10N('lib', $lang);
209
+            // TRANSLATORS this is the language name for the language switcher in the personal settings and should be the localized version
210
+            $potentialName = (string) $l->t('__language_name__');
211
+            if($l->getLanguageCode() === $lang && $potentialName[0] !== '_') {//first check if the language name is in the translation file
212
+                $ln = array('code' => $lang, 'name' => $potentialName);
213
+            } elseif ($lang === 'en') {
214
+                $ln = ['code' => $lang, 'name' => 'English (US)'];
215
+            }else{//fallback to language code
216
+                $ln=array('code'=>$lang, 'name'=>$lang);
217
+            }
218 218
 
219
-			// put appropriate languages into appropriate arrays, to print them sorted
220
-			// used language -> common languages -> divider -> other languages
221
-			if ($lang === $userLang) {
222
-				$userLang = $ln;
223
-			} elseif (in_array($lang, self::COMMON_LANGUAGE_CODES)) {
224
-				$commonLanguages[array_search($lang, self::COMMON_LANGUAGE_CODES)]=$ln;
225
-			} else {
226
-				$languages[]=$ln;
227
-			}
228
-		}
219
+            // put appropriate languages into appropriate arrays, to print them sorted
220
+            // used language -> common languages -> divider -> other languages
221
+            if ($lang === $userLang) {
222
+                $userLang = $ln;
223
+            } elseif (in_array($lang, self::COMMON_LANGUAGE_CODES)) {
224
+                $commonLanguages[array_search($lang, self::COMMON_LANGUAGE_CODES)]=$ln;
225
+            } else {
226
+                $languages[]=$ln;
227
+            }
228
+        }
229 229
 
230
-		// if user language is not available but set somehow: show the actual code as name
231
-		if (!is_array($userLang)) {
232
-			$userLang = [
233
-				'code' => $userLang,
234
-				'name' => $userLang,
235
-			];
236
-		}
230
+        // if user language is not available but set somehow: show the actual code as name
231
+        if (!is_array($userLang)) {
232
+            $userLang = [
233
+                'code' => $userLang,
234
+                'name' => $userLang,
235
+            ];
236
+        }
237 237
 
238
-		ksort($commonLanguages);
238
+        ksort($commonLanguages);
239 239
 
240
-		// sort now by displayed language not the iso-code
241
-		usort( $languages, function ($a, $b) {
242
-			if ($a['code'] === $a['name'] && $b['code'] !== $b['name']) {
243
-				// If a doesn't have a name, but b does, list b before a
244
-				return 1;
245
-			}
246
-			if ($a['code'] !== $a['name'] && $b['code'] === $b['name']) {
247
-				// If a does have a name, but b doesn't, list a before b
248
-				return -1;
249
-			}
250
-			// Otherwise compare the names
251
-			return strcmp($a['name'], $b['name']);
252
-		});
240
+        // sort now by displayed language not the iso-code
241
+        usort( $languages, function ($a, $b) {
242
+            if ($a['code'] === $a['name'] && $b['code'] !== $b['name']) {
243
+                // If a doesn't have a name, but b does, list b before a
244
+                return 1;
245
+            }
246
+            if ($a['code'] !== $a['name'] && $b['code'] === $b['name']) {
247
+                // If a does have a name, but b doesn't, list a before b
248
+                return -1;
249
+            }
250
+            // Otherwise compare the names
251
+            return strcmp($a['name'], $b['name']);
252
+        });
253 253
 
254
-		return [
255
-			'activelanguage' => $userLang,
256
-			'commonlanguages' => $commonLanguages,
257
-			'languages' => $languages
258
-		];
259
-	}
254
+        return [
255
+            'activelanguage' => $userLang,
256
+            'commonlanguages' => $commonLanguages,
257
+            'languages' => $languages
258
+        ];
259
+    }
260 260
 
261
-	/**
262
-	 * @param array $userData
263
-	 * @return array
264
-	 */
265
-	private function getMessageParameters(array $userData) {
266
-		$needVerifyMessage = [AccountManager::PROPERTY_EMAIL, AccountManager::PROPERTY_WEBSITE, AccountManager::PROPERTY_TWITTER];
267
-		$messageParameters = [];
268
-		foreach ($needVerifyMessage as $property) {
269
-			switch ($userData[$property]['verified']) {
270
-				case AccountManager::VERIFIED:
271
-					$message = $this->l->t('Verifying');
272
-					break;
273
-				case AccountManager::VERIFICATION_IN_PROGRESS:
274
-					$message = $this->l->t('Verifying …');
275
-					break;
276
-				default:
277
-					$message = $this->l->t('Verify');
278
-			}
279
-			$messageParameters[$property . 'Message'] = $message;
280
-		}
281
-		return $messageParameters;
282
-	}
261
+    /**
262
+     * @param array $userData
263
+     * @return array
264
+     */
265
+    private function getMessageParameters(array $userData) {
266
+        $needVerifyMessage = [AccountManager::PROPERTY_EMAIL, AccountManager::PROPERTY_WEBSITE, AccountManager::PROPERTY_TWITTER];
267
+        $messageParameters = [];
268
+        foreach ($needVerifyMessage as $property) {
269
+            switch ($userData[$property]['verified']) {
270
+                case AccountManager::VERIFIED:
271
+                    $message = $this->l->t('Verifying');
272
+                    break;
273
+                case AccountManager::VERIFICATION_IN_PROGRESS:
274
+                    $message = $this->l->t('Verifying …');
275
+                    break;
276
+                default:
277
+                    $message = $this->l->t('Verify');
278
+            }
279
+            $messageParameters[$property . 'Message'] = $message;
280
+        }
281
+        return $messageParameters;
282
+    }
283 283
 
284 284
 }
Please login to merge, or discard this patch.
settings/Controller/UsersController.php 1 patch
Indentation   +968 added lines, -968 removed lines patch added patch discarded remove patch
@@ -68,973 +68,973 @@
 block discarded – undo
68 68
  * @package OC\Settings\Controller
69 69
  */
70 70
 class UsersController extends Controller {
71
-	/** @var IL10N */
72
-	private $l10n;
73
-	/** @var IUserSession */
74
-	private $userSession;
75
-	/** @var bool */
76
-	private $isAdmin;
77
-	/** @var IUserManager */
78
-	private $userManager;
79
-	/** @var IGroupManager */
80
-	private $groupManager;
81
-	/** @var IConfig */
82
-	private $config;
83
-	/** @var ILogger */
84
-	private $log;
85
-	/** @var IMailer */
86
-	private $mailer;
87
-	/** @var bool contains the state of the encryption app */
88
-	private $isEncryptionAppEnabled;
89
-	/** @var bool contains the state of the admin recovery setting */
90
-	private $isRestoreEnabled = false;
91
-	/** @var IAppManager */
92
-	private $appManager;
93
-	/** @var IAvatarManager */
94
-	private $avatarManager;
95
-	/** @var AccountManager */
96
-	private $accountManager;
97
-	/** @var ISecureRandom */
98
-	private $secureRandom;
99
-	/** @var NewUserMailHelper */
100
-	private $newUserMailHelper;
101
-	/** @var Manager */
102
-	private $keyManager;
103
-	/** @var IJobList */
104
-	private $jobList;
105
-
106
-	/** @var IUserMountCache */
107
-	private $userMountCache;
108
-
109
-	/** @var IManager */
110
-	private $encryptionManager;
111
-
112
-
113
-	/**
114
-	 * @param string $appName
115
-	 * @param IRequest $request
116
-	 * @param IUserManager $userManager
117
-	 * @param IGroupManager $groupManager
118
-	 * @param IUserSession $userSession
119
-	 * @param IConfig $config
120
-	 * @param bool $isAdmin
121
-	 * @param IL10N $l10n
122
-	 * @param ILogger $log
123
-	 * @param IMailer $mailer
124
-	 * @param IURLGenerator $urlGenerator
125
-	 * @param IAppManager $appManager
126
-	 * @param IAvatarManager $avatarManager
127
-	 * @param AccountManager $accountManager
128
-	 * @param ISecureRandom $secureRandom
129
-	 * @param NewUserMailHelper $newUserMailHelper
130
-	 * @param Manager $keyManager
131
-	 * @param IJobList $jobList
132
-	 * @param IUserMountCache $userMountCache
133
-	 * @param IManager $encryptionManager
134
-	 */
135
-	public function __construct($appName,
136
-								IRequest $request,
137
-								IUserManager $userManager,
138
-								IGroupManager $groupManager,
139
-								IUserSession $userSession,
140
-								IConfig $config,
141
-								$isAdmin,
142
-								IL10N $l10n,
143
-								ILogger $log,
144
-								IMailer $mailer,
145
-								IURLGenerator $urlGenerator,
146
-								IAppManager $appManager,
147
-								IAvatarManager $avatarManager,
148
-								AccountManager $accountManager,
149
-								ISecureRandom $secureRandom,
150
-								NewUserMailHelper $newUserMailHelper,
151
-								Manager $keyManager,
152
-								IJobList $jobList,
153
-								IUserMountCache $userMountCache,
154
-								IManager $encryptionManager) {
155
-		parent::__construct($appName, $request);
156
-		$this->userManager = $userManager;
157
-		$this->groupManager = $groupManager;
158
-		$this->userSession = $userSession;
159
-		$this->config = $config;
160
-		$this->isAdmin = $isAdmin;
161
-		$this->l10n = $l10n;
162
-		$this->log = $log;
163
-		$this->mailer = $mailer;
164
-		$this->appManager = $appManager;
165
-		$this->avatarManager = $avatarManager;
166
-		$this->accountManager = $accountManager;
167
-		$this->secureRandom = $secureRandom;
168
-		$this->newUserMailHelper = $newUserMailHelper;
169
-		$this->keyManager = $keyManager;
170
-		$this->jobList = $jobList;
171
-		$this->userMountCache = $userMountCache;
172
-		$this->encryptionManager = $encryptionManager;
173
-
174
-		// check for encryption state - TODO see formatUserForIndex
175
-		$this->isEncryptionAppEnabled = $appManager->isEnabledForUser('encryption');
176
-		if ($this->isEncryptionAppEnabled) {
177
-			// putting this directly in empty is possible in PHP 5.5+
178
-			$result = $config->getAppValue('encryption', 'recoveryAdminEnabled', '0');
179
-			$this->isRestoreEnabled = !empty($result);
180
-		}
181
-	}
182
-
183
-	/**
184
-	 * @param IUser $user
185
-	 * @param array|null $userGroups
186
-	 * @return array
187
-	 */
188
-	private function formatUserForIndex(IUser $user, array $userGroups = null) {
189
-
190
-		// TODO: eliminate this encryption specific code below and somehow
191
-		// hook in additional user info from other apps
192
-
193
-		// recovery isn't possible if admin or user has it disabled and encryption
194
-		// is enabled - so we eliminate the else paths in the conditional tree
195
-		// below
196
-		$restorePossible = false;
197
-
198
-		if ($this->isEncryptionAppEnabled) {
199
-			if ($this->isRestoreEnabled) {
200
-				// check for the users recovery setting
201
-				$recoveryMode = $this->config->getUserValue($user->getUID(), 'encryption', 'recoveryEnabled', '0');
202
-				// method call inside empty is possible with PHP 5.5+
203
-				$recoveryModeEnabled = !empty($recoveryMode);
204
-				if ($recoveryModeEnabled) {
205
-					// user also has recovery mode enabled
206
-					$restorePossible = true;
207
-				}
208
-			} else {
209
-				$modules = $this->encryptionManager->getEncryptionModules();
210
-				$restorePossible = true;
211
-				foreach ($modules as $id => $module) {
212
-					/* @var IEncryptionModule $instance */
213
-					$instance = call_user_func($module['callback']);
214
-					if ($instance->needDetailedAccessList()) {
215
-						$restorePossible = false;
216
-						break;
217
-					}
218
-				}
219
-			}
220
-		} else {
221
-			// recovery is possible if encryption is disabled (plain files are
222
-			// available)
223
-			$restorePossible = true;
224
-		}
225
-
226
-		$subAdminGroups = $this->groupManager->getSubAdmin()->getSubAdminsGroupsName($user);
227
-
228
-		$displayName = $user->getEMailAddress();
229
-		if (is_null($displayName)) {
230
-			$displayName = '';
231
-		}
232
-
233
-		$avatarAvailable = false;
234
-		try {
235
-			$avatarAvailable = $this->avatarManager->getAvatar($user->getUID())->exists();
236
-		} catch (\Exception $e) {
237
-			//No avatar yet
238
-		}
239
-
240
-		return [
241
-			'name' => $user->getUID(),
242
-			'displayname' => $user->getDisplayName(),
243
-			'groups' => empty($userGroups) ? $this->groupManager->getUserGroupNames($user) : $userGroups,
244
-			'subadmin' => $subAdminGroups,
245
-			'quota' => $user->getQuota(),
246
-			'quota_bytes' => Util::computerFileSize($user->getQuota()),
247
-			'storageLocation' => $user->getHome(),
248
-			'lastLogin' => $user->getLastLogin() * 1000,
249
-			'backend' => $user->getBackendClassName(),
250
-			'email' => $displayName,
251
-			'isRestoreDisabled' => !$restorePossible,
252
-			'isAvatarAvailable' => $avatarAvailable,
253
-			'isEnabled' => $user->isEnabled(),
254
-		];
255
-	}
256
-
257
-	/**
258
-	 * @param array $userIDs Array with schema [$uid => $displayName]
259
-	 * @return IUser[]
260
-	 */
261
-	private function getUsersForUID(array $userIDs) {
262
-		$users = [];
263
-		foreach ($userIDs as $uid => $displayName) {
264
-			$users[$uid] = $this->userManager->get($uid);
265
-		}
266
-		return $users;
267
-	}
268
-
269
-	/**
270
-	 * @NoAdminRequired
271
-	 *
272
-	 * @param int $offset
273
-	 * @param int $limit
274
-	 * @param string $gid GID to filter for
275
-	 * @param string $pattern Pattern to search for in the username
276
-	 * @param string $backend Backend to filter for (class-name)
277
-	 * @return DataResponse
278
-	 *
279
-	 * TODO: Tidy up and write unit tests - code is mainly static method calls
280
-	 */
281
-	public function index($offset = 0, $limit = 10, $gid = '', $pattern = '', $backend = '') {
282
-		// Remove backends
283
-		if (!empty($backend)) {
284
-			$activeBackends = $this->userManager->getBackends();
285
-			$this->userManager->clearBackends();
286
-			foreach ($activeBackends as $singleActiveBackend) {
287
-				if ($backend === get_class($singleActiveBackend)) {
288
-					$this->userManager->registerBackend($singleActiveBackend);
289
-					break;
290
-				}
291
-			}
292
-		}
293
-
294
-		$userObjects = [];
295
-		$users = [];
296
-		if ($this->isAdmin) {
297
-			if ($gid !== '' && $gid !== '_disabledUsers' && $gid !== '_everyone') {
298
-				$batch = $this->getUsersForUID($this->groupManager->displayNamesInGroup($gid, $pattern, $limit, $offset));
299
-			} else {
300
-				$batch = $this->userManager->search($pattern, $limit, $offset);
301
-			}
302
-
303
-			foreach ($batch as $user) {
304
-				if (($gid !== '_disabledUsers' && $user->isEnabled()) ||
305
-					($gid === '_disabledUsers' && !$user->isEnabled())
306
-				) {
307
-					$userObjects[] = $user;
308
-					$users[] = $this->formatUserForIndex($user);
309
-				}
310
-			}
311
-
312
-		} else {
313
-			$subAdminOfGroups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($this->userSession->getUser());
314
-			// New class returns IGroup[] so convert back
315
-			$gids = [];
316
-			foreach ($subAdminOfGroups as $group) {
317
-				$gids[] = $group->getGID();
318
-			}
319
-			$subAdminOfGroups = $gids;
320
-
321
-			// Set the $gid parameter to an empty value if the subadmin has no rights to access a specific group
322
-			if ($gid !== '' && $gid !== '_disabledUsers' && !in_array($gid, $subAdminOfGroups)) {
323
-				$gid = '';
324
-			}
325
-
326
-			// Batch all groups the user is subadmin of when a group is specified
327
-			$batch = [];
328
-			if ($gid !== '' && $gid !== '_disabledUsers' && $gid !== '_everyone') {
329
-				$batch = $this->groupManager->displayNamesInGroup($gid, $pattern, $limit, $offset);
330
-			} else {
331
-				foreach ($subAdminOfGroups as $group) {
332
-					$groupUsers = $this->groupManager->displayNamesInGroup($group, $pattern, $limit, $offset);
333
-
334
-					foreach ($groupUsers as $uid => $displayName) {
335
-						$batch[$uid] = $displayName;
336
-					}
337
-				}
338
-			}
339
-			$batch = $this->getUsersForUID($batch);
340
-
341
-			foreach ($batch as $user) {
342
-				// Only add the groups, this user is a subadmin of
343
-				$userGroups = array_values(array_intersect(
344
-					$this->groupManager->getUserGroupIds($user),
345
-					$subAdminOfGroups
346
-				));
347
-				if (($gid !== '_disabledUsers' && $user->isEnabled()) ||
348
-					($gid === '_disabledUsers' && !$user->isEnabled())
349
-				) {
350
-					$userObjects[] = $user;
351
-					$users[] = $this->formatUserForIndex($user, $userGroups);
352
-				}
353
-			}
354
-		}
355
-
356
-		$usedSpace = $this->userMountCache->getUsedSpaceForUsers($userObjects);
357
-
358
-		foreach ($users as &$userData) {
359
-			$userData['size'] = isset($usedSpace[$userData['name']]) ? $usedSpace[$userData['name']] : 0;
360
-		}
361
-
362
-		return new DataResponse($users);
363
-	}
364
-
365
-	/**
366
-	 * @NoAdminRequired
367
-	 * @PasswordConfirmationRequired
368
-	 *
369
-	 * @param string $username
370
-	 * @param string $password
371
-	 * @param array $groups
372
-	 * @param string $email
373
-	 * @return DataResponse
374
-	 */
375
-	public function create($username, $password, array $groups = [], $email = '') {
376
-		if ($email !== '' && !$this->mailer->validateMailAddress($email)) {
377
-			return new DataResponse(
378
-				[
379
-					'message' => (string)$this->l10n->t('Invalid mail address')
380
-				],
381
-				Http::STATUS_UNPROCESSABLE_ENTITY
382
-			);
383
-		}
384
-
385
-		$currentUser = $this->userSession->getUser();
386
-
387
-		if (!$this->isAdmin) {
388
-			if (!empty($groups)) {
389
-				foreach ($groups as $key => $group) {
390
-					$groupObject = $this->groupManager->get($group);
391
-					if ($groupObject === null) {
392
-						unset($groups[$key]);
393
-						continue;
394
-					}
395
-
396
-					if (!$this->groupManager->getSubAdmin()->isSubAdminofGroup($currentUser, $groupObject)) {
397
-						unset($groups[$key]);
398
-					}
399
-				}
400
-			}
401
-
402
-			if (empty($groups)) {
403
-				return new DataResponse(
404
-					[
405
-						'message' => $this->l10n->t('No valid group selected'),
406
-					],
407
-					Http::STATUS_FORBIDDEN
408
-				);
409
-			}
410
-		}
411
-
412
-		if ($this->userManager->userExists($username)) {
413
-			return new DataResponse(
414
-				[
415
-					'message' => (string)$this->l10n->t('A user with that name already exists.')
416
-				],
417
-				Http::STATUS_CONFLICT
418
-			);
419
-		}
420
-
421
-		$generatePasswordResetToken = false;
422
-		if ($password === '') {
423
-			if ($email === '') {
424
-				return new DataResponse(
425
-					[
426
-						'message' => (string)$this->l10n->t('To send a password link to the user an email address is required.')
427
-					],
428
-					Http::STATUS_UNPROCESSABLE_ENTITY
429
-				);
430
-			}
431
-
432
-			$password = $this->secureRandom->generate(30);
433
-			// Make sure we pass the password_policy
434
-			$password .= $this->secureRandom->generate(2, '$!.,;:-~+*[]{}()');
435
-			$generatePasswordResetToken = true;
436
-		}
437
-
438
-		try {
439
-			$user = $this->userManager->createUser($username, $password);
440
-		} catch (\Exception $exception) {
441
-			$message = $exception->getMessage();
442
-			if ($exception instanceof HintException && $exception->getHint()) {
443
-				$message = $exception->getHint();
444
-			}
445
-			if (!$message) {
446
-				$message = $this->l10n->t('Unable to create user.');
447
-			}
448
-			return new DataResponse(
449
-				[
450
-					'message' => (string)$message,
451
-				],
452
-				Http::STATUS_FORBIDDEN
453
-			);
454
-		}
455
-
456
-		if ($user instanceof IUser) {
457
-			if ($groups !== null) {
458
-				foreach ($groups as $groupName) {
459
-					$group = $this->groupManager->get($groupName);
460
-
461
-					if (empty($group)) {
462
-						$group = $this->groupManager->createGroup($groupName);
463
-					}
464
-					$group->addUser($user);
465
-				}
466
-			}
467
-			/**
468
-			 * Send new user mail only if a mail is set
469
-			 */
470
-			if ($email !== '') {
471
-				$user->setEMailAddress($email);
472
-				try {
473
-					$emailTemplate = $this->newUserMailHelper->generateTemplate($user, $generatePasswordResetToken);
474
-					$this->newUserMailHelper->sendMail($user, $emailTemplate);
475
-				} catch (\Exception $e) {
476
-					$this->log->logException($e, [
477
-						'message' => "Can't send new user mail to $email",
478
-						'level' => \OCP\Util::ERROR,
479
-						'app' => 'settings',
480
-					]);
481
-				}
482
-			}
483
-			// fetch users groups
484
-			$userGroups = $this->groupManager->getUserGroupNames($user);
485
-
486
-			return new DataResponse(
487
-				$this->formatUserForIndex($user, $userGroups),
488
-				Http::STATUS_CREATED
489
-			);
490
-		}
491
-
492
-		return new DataResponse(
493
-			[
494
-				'message' => (string)$this->l10n->t('Unable to create user.')
495
-			],
496
-			Http::STATUS_FORBIDDEN
497
-		);
498
-
499
-	}
500
-
501
-	/**
502
-	 * @NoAdminRequired
503
-	 * @PasswordConfirmationRequired
504
-	 *
505
-	 * @param string $id
506
-	 * @return DataResponse
507
-	 */
508
-	public function destroy($id) {
509
-		$userId = $this->userSession->getUser()->getUID();
510
-		$user = $this->userManager->get($id);
511
-
512
-		if ($userId === $id) {
513
-			return new DataResponse(
514
-				[
515
-					'status' => 'error',
516
-					'data' => [
517
-						'message' => (string)$this->l10n->t('Unable to delete user.')
518
-					]
519
-				],
520
-				Http::STATUS_FORBIDDEN
521
-			);
522
-		}
523
-
524
-		if (!$this->isAdmin && !$this->groupManager->getSubAdmin()->isUserAccessible($this->userSession->getUser(), $user)) {
525
-			return new DataResponse(
526
-				[
527
-					'status' => 'error',
528
-					'data' => [
529
-						'message' => (string)$this->l10n->t('Authentication error')
530
-					]
531
-				],
532
-				Http::STATUS_FORBIDDEN
533
-			);
534
-		}
535
-
536
-		if ($user) {
537
-			if ($user->delete()) {
538
-				return new DataResponse(
539
-					[
540
-						'status' => 'success',
541
-						'data' => [
542
-							'username' => $id
543
-						]
544
-					],
545
-					Http::STATUS_NO_CONTENT
546
-				);
547
-			}
548
-		}
549
-
550
-		return new DataResponse(
551
-			[
552
-				'status' => 'error',
553
-				'data' => [
554
-					'message' => (string)$this->l10n->t('Unable to delete user.')
555
-				]
556
-			],
557
-			Http::STATUS_FORBIDDEN
558
-		);
559
-	}
560
-
561
-	/**
562
-	 * @NoAdminRequired
563
-	 *
564
-	 * @param string $id
565
-	 * @param int $enabled
566
-	 * @return DataResponse
567
-	 */
568
-	public function setEnabled($id, $enabled) {
569
-		$enabled = (bool)$enabled;
570
-		if ($enabled) {
571
-			$errorMsgGeneral = (string)$this->l10n->t('Error while enabling user.');
572
-		} else {
573
-			$errorMsgGeneral = (string)$this->l10n->t('Error while disabling user.');
574
-		}
575
-
576
-		$userId = $this->userSession->getUser()->getUID();
577
-		$user = $this->userManager->get($id);
578
-
579
-		if ($userId === $id) {
580
-			return new DataResponse(
581
-				[
582
-					'status' => 'error',
583
-					'data' => [
584
-						'message' => $errorMsgGeneral
585
-					]
586
-				], Http::STATUS_FORBIDDEN
587
-			);
588
-		}
589
-
590
-		if ($user) {
591
-			if (!$this->isAdmin && !$this->groupManager->getSubAdmin()->isUserAccessible($this->userSession->getUser(), $user)) {
592
-				return new DataResponse(
593
-					[
594
-						'status' => 'error',
595
-						'data' => [
596
-							'message' => (string)$this->l10n->t('Authentication error')
597
-						]
598
-					],
599
-					Http::STATUS_FORBIDDEN
600
-				);
601
-			}
602
-
603
-			$user->setEnabled($enabled);
604
-			return new DataResponse(
605
-				[
606
-					'status' => 'success',
607
-					'data' => [
608
-						'username' => $id,
609
-						'enabled' => $enabled
610
-					]
611
-				]
612
-			);
613
-		} else {
614
-			return new DataResponse(
615
-				[
616
-					'status' => 'error',
617
-					'data' => [
618
-						'message' => $errorMsgGeneral
619
-					]
620
-				],
621
-				Http::STATUS_FORBIDDEN
622
-			);
623
-		}
624
-
625
-	}
626
-
627
-	/**
628
-	 * Set the mail address of a user
629
-	 *
630
-	 * @NoAdminRequired
631
-	 * @NoSubadminRequired
632
-	 * @PasswordConfirmationRequired
633
-	 *
634
-	 * @param string $account
635
-	 * @param bool $onlyVerificationCode only return verification code without updating the data
636
-	 * @return DataResponse
637
-	 */
638
-	public function getVerificationCode($account, $onlyVerificationCode) {
639
-
640
-		$user = $this->userSession->getUser();
641
-
642
-		if ($user === null) {
643
-			return new DataResponse([], Http::STATUS_BAD_REQUEST);
644
-		}
645
-
646
-		$accountData = $this->accountManager->getUser($user);
647
-		$cloudId = $user->getCloudId();
648
-		$message = "Use my Federated Cloud ID to share with me: " . $cloudId;
649
-		$signature = $this->signMessage($user, $message);
650
-
651
-		$code = $message . ' ' . $signature;
652
-		$codeMd5 = $message . ' ' . md5($signature);
653
-
654
-		switch ($account) {
655
-			case 'verify-twitter':
656
-				$accountData[AccountManager::PROPERTY_TWITTER]['verified'] = AccountManager::VERIFICATION_IN_PROGRESS;
657
-				$msg = $this->l10n->t('In order to verify your Twitter account, post the following tweet on Twitter (please make sure to post it without any line breaks):');
658
-				$code = $codeMd5;
659
-				$type = AccountManager::PROPERTY_TWITTER;
660
-				$data = $accountData[AccountManager::PROPERTY_TWITTER]['value'];
661
-				$accountData[AccountManager::PROPERTY_TWITTER]['signature'] = $signature;
662
-				break;
663
-			case 'verify-website':
664
-				$accountData[AccountManager::PROPERTY_WEBSITE]['verified'] = AccountManager::VERIFICATION_IN_PROGRESS;
665
-				$msg = $this->l10n->t('In order to verify your Website, store the following content in your web-root at \'.well-known/CloudIdVerificationCode.txt\' (please make sure that the complete text is in one line):');
666
-				$type = AccountManager::PROPERTY_WEBSITE;
667
-				$data = $accountData[AccountManager::PROPERTY_WEBSITE]['value'];
668
-				$accountData[AccountManager::PROPERTY_WEBSITE]['signature'] = $signature;
669
-				break;
670
-			default:
671
-				return new DataResponse([], Http::STATUS_BAD_REQUEST);
672
-		}
673
-
674
-		if ($onlyVerificationCode === false) {
675
-			$this->accountManager->updateUser($user, $accountData);
676
-
677
-			$this->jobList->add(VerifyUserData::class,
678
-				[
679
-					'verificationCode' => $code,
680
-					'data' => $data,
681
-					'type' => $type,
682
-					'uid' => $user->getUID(),
683
-					'try' => 0,
684
-					'lastRun' => $this->getCurrentTime()
685
-				]
686
-			);
687
-		}
688
-
689
-		return new DataResponse(['msg' => $msg, 'code' => $code]);
690
-	}
691
-
692
-	/**
693
-	 * get current timestamp
694
-	 *
695
-	 * @return int
696
-	 */
697
-	protected function getCurrentTime() {
698
-		return time();
699
-	}
700
-
701
-	/**
702
-	 * sign message with users private key
703
-	 *
704
-	 * @param IUser $user
705
-	 * @param string $message
706
-	 *
707
-	 * @return string base64 encoded signature
708
-	 */
709
-	protected function signMessage(IUser $user, $message) {
710
-		$privateKey = $this->keyManager->getKey($user)->getPrivate();
711
-		openssl_sign(json_encode($message), $signature, $privateKey, OPENSSL_ALGO_SHA512);
712
-		return base64_encode($signature);
713
-	}
714
-
715
-	/**
716
-	 * @NoAdminRequired
717
-	 * @NoSubadminRequired
718
-	 * @PasswordConfirmationRequired
719
-	 *
720
-	 * @param string $avatarScope
721
-	 * @param string $displayname
722
-	 * @param string $displaynameScope
723
-	 * @param string $phone
724
-	 * @param string $phoneScope
725
-	 * @param string $email
726
-	 * @param string $emailScope
727
-	 * @param string $website
728
-	 * @param string $websiteScope
729
-	 * @param string $address
730
-	 * @param string $addressScope
731
-	 * @param string $twitter
732
-	 * @param string $twitterScope
733
-	 * @return DataResponse
734
-	 */
735
-	public function setUserSettings($avatarScope,
736
-									$displayname,
737
-									$displaynameScope,
738
-									$phone,
739
-									$phoneScope,
740
-									$email,
741
-									$emailScope,
742
-									$website,
743
-									$websiteScope,
744
-									$address,
745
-									$addressScope,
746
-									$twitter,
747
-									$twitterScope
748
-	) {
749
-
750
-		if (!empty($email) && !$this->mailer->validateMailAddress($email)) {
751
-			return new DataResponse(
752
-				[
753
-					'status' => 'error',
754
-					'data' => [
755
-						'message' => (string)$this->l10n->t('Invalid mail address')
756
-					]
757
-				],
758
-				Http::STATUS_UNPROCESSABLE_ENTITY
759
-			);
760
-		}
761
-
762
-		$user = $this->userSession->getUser();
763
-
764
-		$data = $this->accountManager->getUser($user);
765
-
766
-		$data[AccountManager::PROPERTY_AVATAR] = ['scope' => $avatarScope];
767
-		if ($this->config->getSystemValue('allow_user_to_change_display_name', true) !== false) {
768
-			$data[AccountManager::PROPERTY_DISPLAYNAME] = ['value' => $displayname, 'scope' => $displaynameScope];
769
-			$data[AccountManager::PROPERTY_EMAIL] = ['value' => $email, 'scope' => $emailScope];
770
-		}
771
-
772
-		if ($this->appManager->isEnabledForUser('federatedfilesharing')) {
773
-			$federatedFileSharing = new \OCA\FederatedFileSharing\AppInfo\Application();
774
-			$shareProvider = $federatedFileSharing->getFederatedShareProvider();
775
-			if ($shareProvider->isLookupServerUploadEnabled()) {
776
-				$data[AccountManager::PROPERTY_WEBSITE] = ['value' => $website, 'scope' => $websiteScope];
777
-				$data[AccountManager::PROPERTY_ADDRESS] = ['value' => $address, 'scope' => $addressScope];
778
-				$data[AccountManager::PROPERTY_PHONE] = ['value' => $phone, 'scope' => $phoneScope];
779
-				$data[AccountManager::PROPERTY_TWITTER] = ['value' => $twitter, 'scope' => $twitterScope];
780
-			}
781
-		}
782
-
783
-		try {
784
-			$this->saveUserSettings($user, $data);
785
-			return new DataResponse(
786
-				[
787
-					'status' => 'success',
788
-					'data' => [
789
-						'userId' => $user->getUID(),
790
-						'avatarScope' => $data[AccountManager::PROPERTY_AVATAR]['scope'],
791
-						'displayname' => $data[AccountManager::PROPERTY_DISPLAYNAME]['value'],
792
-						'displaynameScope' => $data[AccountManager::PROPERTY_DISPLAYNAME]['scope'],
793
-						'email' => $data[AccountManager::PROPERTY_EMAIL]['value'],
794
-						'emailScope' => $data[AccountManager::PROPERTY_EMAIL]['scope'],
795
-						'website' => $data[AccountManager::PROPERTY_WEBSITE]['value'],
796
-						'websiteScope' => $data[AccountManager::PROPERTY_WEBSITE]['scope'],
797
-						'address' => $data[AccountManager::PROPERTY_ADDRESS]['value'],
798
-						'addressScope' => $data[AccountManager::PROPERTY_ADDRESS]['scope'],
799
-						'message' => (string)$this->l10n->t('Settings saved')
800
-					]
801
-				],
802
-				Http::STATUS_OK
803
-			);
804
-		} catch (ForbiddenException $e) {
805
-			return new DataResponse([
806
-				'status' => 'error',
807
-				'data' => [
808
-					'message' => $e->getMessage()
809
-				],
810
-			]);
811
-		}
812
-
813
-	}
814
-
815
-
816
-	/**
817
-	 * update account manager with new user data
818
-	 *
819
-	 * @param IUser $user
820
-	 * @param array $data
821
-	 * @throws ForbiddenException
822
-	 */
823
-	protected function saveUserSettings(IUser $user, $data) {
824
-
825
-		// keep the user back-end up-to-date with the latest display name and email
826
-		// address
827
-		$oldDisplayName = $user->getDisplayName();
828
-		$oldDisplayName = is_null($oldDisplayName) ? '' : $oldDisplayName;
829
-		if (isset($data[AccountManager::PROPERTY_DISPLAYNAME]['value'])
830
-			&& $oldDisplayName !== $data[AccountManager::PROPERTY_DISPLAYNAME]['value']
831
-		) {
832
-			$result = $user->setDisplayName($data[AccountManager::PROPERTY_DISPLAYNAME]['value']);
833
-			if ($result === false) {
834
-				throw new ForbiddenException($this->l10n->t('Unable to change full name'));
835
-			}
836
-		}
837
-
838
-		$oldEmailAddress = $user->getEMailAddress();
839
-		$oldEmailAddress = is_null($oldEmailAddress) ? '' : $oldEmailAddress;
840
-		if (isset($data[AccountManager::PROPERTY_EMAIL]['value'])
841
-			&& $oldEmailAddress !== $data[AccountManager::PROPERTY_EMAIL]['value']
842
-		) {
843
-			// this is the only permission a backend provides and is also used
844
-			// for the permission of setting a email address
845
-			if (!$user->canChangeDisplayName()) {
846
-				throw new ForbiddenException($this->l10n->t('Unable to change email address'));
847
-			}
848
-			$user->setEMailAddress($data[AccountManager::PROPERTY_EMAIL]['value']);
849
-		}
850
-
851
-		$this->accountManager->updateUser($user, $data);
852
-	}
853
-
854
-	/**
855
-	 * Count all unique users visible for the current admin/subadmin.
856
-	 *
857
-	 * @NoAdminRequired
858
-	 *
859
-	 * @return DataResponse
860
-	 */
861
-	public function stats() {
862
-		$userCount = 0;
863
-		if ($this->isAdmin) {
864
-			$countByBackend = $this->userManager->countUsers();
865
-
866
-			if (!empty($countByBackend)) {
867
-				foreach ($countByBackend as $count) {
868
-					$userCount += $count;
869
-				}
870
-			}
871
-		} else {
872
-			$groups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($this->userSession->getUser());
873
-
874
-			$uniqueUsers = [];
875
-			foreach ($groups as $group) {
876
-				foreach ($group->getUsers() as $uid => $displayName) {
877
-					$uniqueUsers[$uid] = true;
878
-				}
879
-			}
880
-
881
-			$userCount = count($uniqueUsers);
882
-		}
883
-
884
-		return new DataResponse(
885
-			[
886
-				'totalUsers' => $userCount
887
-			]
888
-		);
889
-	}
890
-
891
-
892
-	/**
893
-	 * Set the displayName of a user
894
-	 *
895
-	 * @NoAdminRequired
896
-	 * @NoSubadminRequired
897
-	 * @PasswordConfirmationRequired
898
-	 * @todo merge into saveUserSettings
899
-	 *
900
-	 * @param string $username
901
-	 * @param string $displayName
902
-	 * @return DataResponse
903
-	 */
904
-	public function setDisplayName($username, $displayName) {
905
-		$currentUser = $this->userSession->getUser();
906
-		$user = $this->userManager->get($username);
907
-
908
-		if ($user === null ||
909
-			!$user->canChangeDisplayName() ||
910
-			(
911
-				!$this->groupManager->isAdmin($currentUser->getUID()) &&
912
-				!$this->groupManager->getSubAdmin()->isUserAccessible($currentUser, $user) &&
913
-				$currentUser->getUID() !== $username
914
-
915
-			)
916
-		) {
917
-			return new DataResponse([
918
-				'status' => 'error',
919
-				'data' => [
920
-					'message' => $this->l10n->t('Authentication error'),
921
-				],
922
-			]);
923
-		}
924
-
925
-		$userData = $this->accountManager->getUser($user);
926
-		$userData[AccountManager::PROPERTY_DISPLAYNAME]['value'] = $displayName;
927
-
928
-
929
-		try {
930
-			$this->saveUserSettings($user, $userData);
931
-			return new DataResponse([
932
-				'status' => 'success',
933
-				'data' => [
934
-					'message' => $this->l10n->t('Your full name has been changed.'),
935
-					'username' => $username,
936
-					'displayName' => $displayName,
937
-				],
938
-			]);
939
-		} catch (ForbiddenException $e) {
940
-			return new DataResponse([
941
-				'status' => 'error',
942
-				'data' => [
943
-					'message' => $e->getMessage(),
944
-					'displayName' => $user->getDisplayName(),
945
-				],
946
-			]);
947
-		}
948
-	}
949
-
950
-	/**
951
-	 * Set the mail address of a user
952
-	 *
953
-	 * @NoAdminRequired
954
-	 * @NoSubadminRequired
955
-	 * @PasswordConfirmationRequired
956
-	 *
957
-	 * @param string $id
958
-	 * @param string $mailAddress
959
-	 * @return DataResponse
960
-	 */
961
-	public function setEMailAddress($id, $mailAddress) {
962
-		$user = $this->userManager->get($id);
963
-		if (!$this->isAdmin
964
-			&& !$this->groupManager->getSubAdmin()->isUserAccessible($this->userSession->getUser(), $user)
965
-		) {
966
-			return new DataResponse(
967
-				[
968
-					'status' => 'error',
969
-					'data' => [
970
-						'message' => (string)$this->l10n->t('Forbidden')
971
-					]
972
-				],
973
-				Http::STATUS_FORBIDDEN
974
-			);
975
-		}
976
-
977
-		if ($mailAddress !== '' && !$this->mailer->validateMailAddress($mailAddress)) {
978
-			return new DataResponse(
979
-				[
980
-					'status' => 'error',
981
-					'data' => [
982
-						'message' => (string)$this->l10n->t('Invalid mail address')
983
-					]
984
-				],
985
-				Http::STATUS_UNPROCESSABLE_ENTITY
986
-			);
987
-		}
988
-
989
-		if (!$user) {
990
-			return new DataResponse(
991
-				[
992
-					'status' => 'error',
993
-					'data' => [
994
-						'message' => (string)$this->l10n->t('Invalid user')
995
-					]
996
-				],
997
-				Http::STATUS_UNPROCESSABLE_ENTITY
998
-			);
999
-		}
1000
-		// this is the only permission a backend provides and is also used
1001
-		// for the permission of setting a email address
1002
-		if (!$user->canChangeDisplayName()) {
1003
-			return new DataResponse(
1004
-				[
1005
-					'status' => 'error',
1006
-					'data' => [
1007
-						'message' => (string)$this->l10n->t('Unable to change mail address')
1008
-					]
1009
-				],
1010
-				Http::STATUS_FORBIDDEN
1011
-			);
1012
-		}
1013
-
1014
-		$userData = $this->accountManager->getUser($user);
1015
-		$userData[AccountManager::PROPERTY_EMAIL]['value'] = $mailAddress;
1016
-
1017
-		try {
1018
-			$this->saveUserSettings($user, $userData);
1019
-			return new DataResponse(
1020
-				[
1021
-					'status' => 'success',
1022
-					'data' => [
1023
-						'username' => $id,
1024
-						'mailAddress' => $mailAddress,
1025
-						'message' => (string)$this->l10n->t('Email saved')
1026
-					]
1027
-				],
1028
-				Http::STATUS_OK
1029
-			);
1030
-		} catch (ForbiddenException $e) {
1031
-			return new DataResponse([
1032
-				'status' => 'error',
1033
-				'data' => [
1034
-					'message' => $e->getMessage()
1035
-				],
1036
-			]);
1037
-		}
1038
-	}
71
+    /** @var IL10N */
72
+    private $l10n;
73
+    /** @var IUserSession */
74
+    private $userSession;
75
+    /** @var bool */
76
+    private $isAdmin;
77
+    /** @var IUserManager */
78
+    private $userManager;
79
+    /** @var IGroupManager */
80
+    private $groupManager;
81
+    /** @var IConfig */
82
+    private $config;
83
+    /** @var ILogger */
84
+    private $log;
85
+    /** @var IMailer */
86
+    private $mailer;
87
+    /** @var bool contains the state of the encryption app */
88
+    private $isEncryptionAppEnabled;
89
+    /** @var bool contains the state of the admin recovery setting */
90
+    private $isRestoreEnabled = false;
91
+    /** @var IAppManager */
92
+    private $appManager;
93
+    /** @var IAvatarManager */
94
+    private $avatarManager;
95
+    /** @var AccountManager */
96
+    private $accountManager;
97
+    /** @var ISecureRandom */
98
+    private $secureRandom;
99
+    /** @var NewUserMailHelper */
100
+    private $newUserMailHelper;
101
+    /** @var Manager */
102
+    private $keyManager;
103
+    /** @var IJobList */
104
+    private $jobList;
105
+
106
+    /** @var IUserMountCache */
107
+    private $userMountCache;
108
+
109
+    /** @var IManager */
110
+    private $encryptionManager;
111
+
112
+
113
+    /**
114
+     * @param string $appName
115
+     * @param IRequest $request
116
+     * @param IUserManager $userManager
117
+     * @param IGroupManager $groupManager
118
+     * @param IUserSession $userSession
119
+     * @param IConfig $config
120
+     * @param bool $isAdmin
121
+     * @param IL10N $l10n
122
+     * @param ILogger $log
123
+     * @param IMailer $mailer
124
+     * @param IURLGenerator $urlGenerator
125
+     * @param IAppManager $appManager
126
+     * @param IAvatarManager $avatarManager
127
+     * @param AccountManager $accountManager
128
+     * @param ISecureRandom $secureRandom
129
+     * @param NewUserMailHelper $newUserMailHelper
130
+     * @param Manager $keyManager
131
+     * @param IJobList $jobList
132
+     * @param IUserMountCache $userMountCache
133
+     * @param IManager $encryptionManager
134
+     */
135
+    public function __construct($appName,
136
+                                IRequest $request,
137
+                                IUserManager $userManager,
138
+                                IGroupManager $groupManager,
139
+                                IUserSession $userSession,
140
+                                IConfig $config,
141
+                                $isAdmin,
142
+                                IL10N $l10n,
143
+                                ILogger $log,
144
+                                IMailer $mailer,
145
+                                IURLGenerator $urlGenerator,
146
+                                IAppManager $appManager,
147
+                                IAvatarManager $avatarManager,
148
+                                AccountManager $accountManager,
149
+                                ISecureRandom $secureRandom,
150
+                                NewUserMailHelper $newUserMailHelper,
151
+                                Manager $keyManager,
152
+                                IJobList $jobList,
153
+                                IUserMountCache $userMountCache,
154
+                                IManager $encryptionManager) {
155
+        parent::__construct($appName, $request);
156
+        $this->userManager = $userManager;
157
+        $this->groupManager = $groupManager;
158
+        $this->userSession = $userSession;
159
+        $this->config = $config;
160
+        $this->isAdmin = $isAdmin;
161
+        $this->l10n = $l10n;
162
+        $this->log = $log;
163
+        $this->mailer = $mailer;
164
+        $this->appManager = $appManager;
165
+        $this->avatarManager = $avatarManager;
166
+        $this->accountManager = $accountManager;
167
+        $this->secureRandom = $secureRandom;
168
+        $this->newUserMailHelper = $newUserMailHelper;
169
+        $this->keyManager = $keyManager;
170
+        $this->jobList = $jobList;
171
+        $this->userMountCache = $userMountCache;
172
+        $this->encryptionManager = $encryptionManager;
173
+
174
+        // check for encryption state - TODO see formatUserForIndex
175
+        $this->isEncryptionAppEnabled = $appManager->isEnabledForUser('encryption');
176
+        if ($this->isEncryptionAppEnabled) {
177
+            // putting this directly in empty is possible in PHP 5.5+
178
+            $result = $config->getAppValue('encryption', 'recoveryAdminEnabled', '0');
179
+            $this->isRestoreEnabled = !empty($result);
180
+        }
181
+    }
182
+
183
+    /**
184
+     * @param IUser $user
185
+     * @param array|null $userGroups
186
+     * @return array
187
+     */
188
+    private function formatUserForIndex(IUser $user, array $userGroups = null) {
189
+
190
+        // TODO: eliminate this encryption specific code below and somehow
191
+        // hook in additional user info from other apps
192
+
193
+        // recovery isn't possible if admin or user has it disabled and encryption
194
+        // is enabled - so we eliminate the else paths in the conditional tree
195
+        // below
196
+        $restorePossible = false;
197
+
198
+        if ($this->isEncryptionAppEnabled) {
199
+            if ($this->isRestoreEnabled) {
200
+                // check for the users recovery setting
201
+                $recoveryMode = $this->config->getUserValue($user->getUID(), 'encryption', 'recoveryEnabled', '0');
202
+                // method call inside empty is possible with PHP 5.5+
203
+                $recoveryModeEnabled = !empty($recoveryMode);
204
+                if ($recoveryModeEnabled) {
205
+                    // user also has recovery mode enabled
206
+                    $restorePossible = true;
207
+                }
208
+            } else {
209
+                $modules = $this->encryptionManager->getEncryptionModules();
210
+                $restorePossible = true;
211
+                foreach ($modules as $id => $module) {
212
+                    /* @var IEncryptionModule $instance */
213
+                    $instance = call_user_func($module['callback']);
214
+                    if ($instance->needDetailedAccessList()) {
215
+                        $restorePossible = false;
216
+                        break;
217
+                    }
218
+                }
219
+            }
220
+        } else {
221
+            // recovery is possible if encryption is disabled (plain files are
222
+            // available)
223
+            $restorePossible = true;
224
+        }
225
+
226
+        $subAdminGroups = $this->groupManager->getSubAdmin()->getSubAdminsGroupsName($user);
227
+
228
+        $displayName = $user->getEMailAddress();
229
+        if (is_null($displayName)) {
230
+            $displayName = '';
231
+        }
232
+
233
+        $avatarAvailable = false;
234
+        try {
235
+            $avatarAvailable = $this->avatarManager->getAvatar($user->getUID())->exists();
236
+        } catch (\Exception $e) {
237
+            //No avatar yet
238
+        }
239
+
240
+        return [
241
+            'name' => $user->getUID(),
242
+            'displayname' => $user->getDisplayName(),
243
+            'groups' => empty($userGroups) ? $this->groupManager->getUserGroupNames($user) : $userGroups,
244
+            'subadmin' => $subAdminGroups,
245
+            'quota' => $user->getQuota(),
246
+            'quota_bytes' => Util::computerFileSize($user->getQuota()),
247
+            'storageLocation' => $user->getHome(),
248
+            'lastLogin' => $user->getLastLogin() * 1000,
249
+            'backend' => $user->getBackendClassName(),
250
+            'email' => $displayName,
251
+            'isRestoreDisabled' => !$restorePossible,
252
+            'isAvatarAvailable' => $avatarAvailable,
253
+            'isEnabled' => $user->isEnabled(),
254
+        ];
255
+    }
256
+
257
+    /**
258
+     * @param array $userIDs Array with schema [$uid => $displayName]
259
+     * @return IUser[]
260
+     */
261
+    private function getUsersForUID(array $userIDs) {
262
+        $users = [];
263
+        foreach ($userIDs as $uid => $displayName) {
264
+            $users[$uid] = $this->userManager->get($uid);
265
+        }
266
+        return $users;
267
+    }
268
+
269
+    /**
270
+     * @NoAdminRequired
271
+     *
272
+     * @param int $offset
273
+     * @param int $limit
274
+     * @param string $gid GID to filter for
275
+     * @param string $pattern Pattern to search for in the username
276
+     * @param string $backend Backend to filter for (class-name)
277
+     * @return DataResponse
278
+     *
279
+     * TODO: Tidy up and write unit tests - code is mainly static method calls
280
+     */
281
+    public function index($offset = 0, $limit = 10, $gid = '', $pattern = '', $backend = '') {
282
+        // Remove backends
283
+        if (!empty($backend)) {
284
+            $activeBackends = $this->userManager->getBackends();
285
+            $this->userManager->clearBackends();
286
+            foreach ($activeBackends as $singleActiveBackend) {
287
+                if ($backend === get_class($singleActiveBackend)) {
288
+                    $this->userManager->registerBackend($singleActiveBackend);
289
+                    break;
290
+                }
291
+            }
292
+        }
293
+
294
+        $userObjects = [];
295
+        $users = [];
296
+        if ($this->isAdmin) {
297
+            if ($gid !== '' && $gid !== '_disabledUsers' && $gid !== '_everyone') {
298
+                $batch = $this->getUsersForUID($this->groupManager->displayNamesInGroup($gid, $pattern, $limit, $offset));
299
+            } else {
300
+                $batch = $this->userManager->search($pattern, $limit, $offset);
301
+            }
302
+
303
+            foreach ($batch as $user) {
304
+                if (($gid !== '_disabledUsers' && $user->isEnabled()) ||
305
+                    ($gid === '_disabledUsers' && !$user->isEnabled())
306
+                ) {
307
+                    $userObjects[] = $user;
308
+                    $users[] = $this->formatUserForIndex($user);
309
+                }
310
+            }
311
+
312
+        } else {
313
+            $subAdminOfGroups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($this->userSession->getUser());
314
+            // New class returns IGroup[] so convert back
315
+            $gids = [];
316
+            foreach ($subAdminOfGroups as $group) {
317
+                $gids[] = $group->getGID();
318
+            }
319
+            $subAdminOfGroups = $gids;
320
+
321
+            // Set the $gid parameter to an empty value if the subadmin has no rights to access a specific group
322
+            if ($gid !== '' && $gid !== '_disabledUsers' && !in_array($gid, $subAdminOfGroups)) {
323
+                $gid = '';
324
+            }
325
+
326
+            // Batch all groups the user is subadmin of when a group is specified
327
+            $batch = [];
328
+            if ($gid !== '' && $gid !== '_disabledUsers' && $gid !== '_everyone') {
329
+                $batch = $this->groupManager->displayNamesInGroup($gid, $pattern, $limit, $offset);
330
+            } else {
331
+                foreach ($subAdminOfGroups as $group) {
332
+                    $groupUsers = $this->groupManager->displayNamesInGroup($group, $pattern, $limit, $offset);
333
+
334
+                    foreach ($groupUsers as $uid => $displayName) {
335
+                        $batch[$uid] = $displayName;
336
+                    }
337
+                }
338
+            }
339
+            $batch = $this->getUsersForUID($batch);
340
+
341
+            foreach ($batch as $user) {
342
+                // Only add the groups, this user is a subadmin of
343
+                $userGroups = array_values(array_intersect(
344
+                    $this->groupManager->getUserGroupIds($user),
345
+                    $subAdminOfGroups
346
+                ));
347
+                if (($gid !== '_disabledUsers' && $user->isEnabled()) ||
348
+                    ($gid === '_disabledUsers' && !$user->isEnabled())
349
+                ) {
350
+                    $userObjects[] = $user;
351
+                    $users[] = $this->formatUserForIndex($user, $userGroups);
352
+                }
353
+            }
354
+        }
355
+
356
+        $usedSpace = $this->userMountCache->getUsedSpaceForUsers($userObjects);
357
+
358
+        foreach ($users as &$userData) {
359
+            $userData['size'] = isset($usedSpace[$userData['name']]) ? $usedSpace[$userData['name']] : 0;
360
+        }
361
+
362
+        return new DataResponse($users);
363
+    }
364
+
365
+    /**
366
+     * @NoAdminRequired
367
+     * @PasswordConfirmationRequired
368
+     *
369
+     * @param string $username
370
+     * @param string $password
371
+     * @param array $groups
372
+     * @param string $email
373
+     * @return DataResponse
374
+     */
375
+    public function create($username, $password, array $groups = [], $email = '') {
376
+        if ($email !== '' && !$this->mailer->validateMailAddress($email)) {
377
+            return new DataResponse(
378
+                [
379
+                    'message' => (string)$this->l10n->t('Invalid mail address')
380
+                ],
381
+                Http::STATUS_UNPROCESSABLE_ENTITY
382
+            );
383
+        }
384
+
385
+        $currentUser = $this->userSession->getUser();
386
+
387
+        if (!$this->isAdmin) {
388
+            if (!empty($groups)) {
389
+                foreach ($groups as $key => $group) {
390
+                    $groupObject = $this->groupManager->get($group);
391
+                    if ($groupObject === null) {
392
+                        unset($groups[$key]);
393
+                        continue;
394
+                    }
395
+
396
+                    if (!$this->groupManager->getSubAdmin()->isSubAdminofGroup($currentUser, $groupObject)) {
397
+                        unset($groups[$key]);
398
+                    }
399
+                }
400
+            }
401
+
402
+            if (empty($groups)) {
403
+                return new DataResponse(
404
+                    [
405
+                        'message' => $this->l10n->t('No valid group selected'),
406
+                    ],
407
+                    Http::STATUS_FORBIDDEN
408
+                );
409
+            }
410
+        }
411
+
412
+        if ($this->userManager->userExists($username)) {
413
+            return new DataResponse(
414
+                [
415
+                    'message' => (string)$this->l10n->t('A user with that name already exists.')
416
+                ],
417
+                Http::STATUS_CONFLICT
418
+            );
419
+        }
420
+
421
+        $generatePasswordResetToken = false;
422
+        if ($password === '') {
423
+            if ($email === '') {
424
+                return new DataResponse(
425
+                    [
426
+                        'message' => (string)$this->l10n->t('To send a password link to the user an email address is required.')
427
+                    ],
428
+                    Http::STATUS_UNPROCESSABLE_ENTITY
429
+                );
430
+            }
431
+
432
+            $password = $this->secureRandom->generate(30);
433
+            // Make sure we pass the password_policy
434
+            $password .= $this->secureRandom->generate(2, '$!.,;:-~+*[]{}()');
435
+            $generatePasswordResetToken = true;
436
+        }
437
+
438
+        try {
439
+            $user = $this->userManager->createUser($username, $password);
440
+        } catch (\Exception $exception) {
441
+            $message = $exception->getMessage();
442
+            if ($exception instanceof HintException && $exception->getHint()) {
443
+                $message = $exception->getHint();
444
+            }
445
+            if (!$message) {
446
+                $message = $this->l10n->t('Unable to create user.');
447
+            }
448
+            return new DataResponse(
449
+                [
450
+                    'message' => (string)$message,
451
+                ],
452
+                Http::STATUS_FORBIDDEN
453
+            );
454
+        }
455
+
456
+        if ($user instanceof IUser) {
457
+            if ($groups !== null) {
458
+                foreach ($groups as $groupName) {
459
+                    $group = $this->groupManager->get($groupName);
460
+
461
+                    if (empty($group)) {
462
+                        $group = $this->groupManager->createGroup($groupName);
463
+                    }
464
+                    $group->addUser($user);
465
+                }
466
+            }
467
+            /**
468
+             * Send new user mail only if a mail is set
469
+             */
470
+            if ($email !== '') {
471
+                $user->setEMailAddress($email);
472
+                try {
473
+                    $emailTemplate = $this->newUserMailHelper->generateTemplate($user, $generatePasswordResetToken);
474
+                    $this->newUserMailHelper->sendMail($user, $emailTemplate);
475
+                } catch (\Exception $e) {
476
+                    $this->log->logException($e, [
477
+                        'message' => "Can't send new user mail to $email",
478
+                        'level' => \OCP\Util::ERROR,
479
+                        'app' => 'settings',
480
+                    ]);
481
+                }
482
+            }
483
+            // fetch users groups
484
+            $userGroups = $this->groupManager->getUserGroupNames($user);
485
+
486
+            return new DataResponse(
487
+                $this->formatUserForIndex($user, $userGroups),
488
+                Http::STATUS_CREATED
489
+            );
490
+        }
491
+
492
+        return new DataResponse(
493
+            [
494
+                'message' => (string)$this->l10n->t('Unable to create user.')
495
+            ],
496
+            Http::STATUS_FORBIDDEN
497
+        );
498
+
499
+    }
500
+
501
+    /**
502
+     * @NoAdminRequired
503
+     * @PasswordConfirmationRequired
504
+     *
505
+     * @param string $id
506
+     * @return DataResponse
507
+     */
508
+    public function destroy($id) {
509
+        $userId = $this->userSession->getUser()->getUID();
510
+        $user = $this->userManager->get($id);
511
+
512
+        if ($userId === $id) {
513
+            return new DataResponse(
514
+                [
515
+                    'status' => 'error',
516
+                    'data' => [
517
+                        'message' => (string)$this->l10n->t('Unable to delete user.')
518
+                    ]
519
+                ],
520
+                Http::STATUS_FORBIDDEN
521
+            );
522
+        }
523
+
524
+        if (!$this->isAdmin && !$this->groupManager->getSubAdmin()->isUserAccessible($this->userSession->getUser(), $user)) {
525
+            return new DataResponse(
526
+                [
527
+                    'status' => 'error',
528
+                    'data' => [
529
+                        'message' => (string)$this->l10n->t('Authentication error')
530
+                    ]
531
+                ],
532
+                Http::STATUS_FORBIDDEN
533
+            );
534
+        }
535
+
536
+        if ($user) {
537
+            if ($user->delete()) {
538
+                return new DataResponse(
539
+                    [
540
+                        'status' => 'success',
541
+                        'data' => [
542
+                            'username' => $id
543
+                        ]
544
+                    ],
545
+                    Http::STATUS_NO_CONTENT
546
+                );
547
+            }
548
+        }
549
+
550
+        return new DataResponse(
551
+            [
552
+                'status' => 'error',
553
+                'data' => [
554
+                    'message' => (string)$this->l10n->t('Unable to delete user.')
555
+                ]
556
+            ],
557
+            Http::STATUS_FORBIDDEN
558
+        );
559
+    }
560
+
561
+    /**
562
+     * @NoAdminRequired
563
+     *
564
+     * @param string $id
565
+     * @param int $enabled
566
+     * @return DataResponse
567
+     */
568
+    public function setEnabled($id, $enabled) {
569
+        $enabled = (bool)$enabled;
570
+        if ($enabled) {
571
+            $errorMsgGeneral = (string)$this->l10n->t('Error while enabling user.');
572
+        } else {
573
+            $errorMsgGeneral = (string)$this->l10n->t('Error while disabling user.');
574
+        }
575
+
576
+        $userId = $this->userSession->getUser()->getUID();
577
+        $user = $this->userManager->get($id);
578
+
579
+        if ($userId === $id) {
580
+            return new DataResponse(
581
+                [
582
+                    'status' => 'error',
583
+                    'data' => [
584
+                        'message' => $errorMsgGeneral
585
+                    ]
586
+                ], Http::STATUS_FORBIDDEN
587
+            );
588
+        }
589
+
590
+        if ($user) {
591
+            if (!$this->isAdmin && !$this->groupManager->getSubAdmin()->isUserAccessible($this->userSession->getUser(), $user)) {
592
+                return new DataResponse(
593
+                    [
594
+                        'status' => 'error',
595
+                        'data' => [
596
+                            'message' => (string)$this->l10n->t('Authentication error')
597
+                        ]
598
+                    ],
599
+                    Http::STATUS_FORBIDDEN
600
+                );
601
+            }
602
+
603
+            $user->setEnabled($enabled);
604
+            return new DataResponse(
605
+                [
606
+                    'status' => 'success',
607
+                    'data' => [
608
+                        'username' => $id,
609
+                        'enabled' => $enabled
610
+                    ]
611
+                ]
612
+            );
613
+        } else {
614
+            return new DataResponse(
615
+                [
616
+                    'status' => 'error',
617
+                    'data' => [
618
+                        'message' => $errorMsgGeneral
619
+                    ]
620
+                ],
621
+                Http::STATUS_FORBIDDEN
622
+            );
623
+        }
624
+
625
+    }
626
+
627
+    /**
628
+     * Set the mail address of a user
629
+     *
630
+     * @NoAdminRequired
631
+     * @NoSubadminRequired
632
+     * @PasswordConfirmationRequired
633
+     *
634
+     * @param string $account
635
+     * @param bool $onlyVerificationCode only return verification code without updating the data
636
+     * @return DataResponse
637
+     */
638
+    public function getVerificationCode($account, $onlyVerificationCode) {
639
+
640
+        $user = $this->userSession->getUser();
641
+
642
+        if ($user === null) {
643
+            return new DataResponse([], Http::STATUS_BAD_REQUEST);
644
+        }
645
+
646
+        $accountData = $this->accountManager->getUser($user);
647
+        $cloudId = $user->getCloudId();
648
+        $message = "Use my Federated Cloud ID to share with me: " . $cloudId;
649
+        $signature = $this->signMessage($user, $message);
650
+
651
+        $code = $message . ' ' . $signature;
652
+        $codeMd5 = $message . ' ' . md5($signature);
653
+
654
+        switch ($account) {
655
+            case 'verify-twitter':
656
+                $accountData[AccountManager::PROPERTY_TWITTER]['verified'] = AccountManager::VERIFICATION_IN_PROGRESS;
657
+                $msg = $this->l10n->t('In order to verify your Twitter account, post the following tweet on Twitter (please make sure to post it without any line breaks):');
658
+                $code = $codeMd5;
659
+                $type = AccountManager::PROPERTY_TWITTER;
660
+                $data = $accountData[AccountManager::PROPERTY_TWITTER]['value'];
661
+                $accountData[AccountManager::PROPERTY_TWITTER]['signature'] = $signature;
662
+                break;
663
+            case 'verify-website':
664
+                $accountData[AccountManager::PROPERTY_WEBSITE]['verified'] = AccountManager::VERIFICATION_IN_PROGRESS;
665
+                $msg = $this->l10n->t('In order to verify your Website, store the following content in your web-root at \'.well-known/CloudIdVerificationCode.txt\' (please make sure that the complete text is in one line):');
666
+                $type = AccountManager::PROPERTY_WEBSITE;
667
+                $data = $accountData[AccountManager::PROPERTY_WEBSITE]['value'];
668
+                $accountData[AccountManager::PROPERTY_WEBSITE]['signature'] = $signature;
669
+                break;
670
+            default:
671
+                return new DataResponse([], Http::STATUS_BAD_REQUEST);
672
+        }
673
+
674
+        if ($onlyVerificationCode === false) {
675
+            $this->accountManager->updateUser($user, $accountData);
676
+
677
+            $this->jobList->add(VerifyUserData::class,
678
+                [
679
+                    'verificationCode' => $code,
680
+                    'data' => $data,
681
+                    'type' => $type,
682
+                    'uid' => $user->getUID(),
683
+                    'try' => 0,
684
+                    'lastRun' => $this->getCurrentTime()
685
+                ]
686
+            );
687
+        }
688
+
689
+        return new DataResponse(['msg' => $msg, 'code' => $code]);
690
+    }
691
+
692
+    /**
693
+     * get current timestamp
694
+     *
695
+     * @return int
696
+     */
697
+    protected function getCurrentTime() {
698
+        return time();
699
+    }
700
+
701
+    /**
702
+     * sign message with users private key
703
+     *
704
+     * @param IUser $user
705
+     * @param string $message
706
+     *
707
+     * @return string base64 encoded signature
708
+     */
709
+    protected function signMessage(IUser $user, $message) {
710
+        $privateKey = $this->keyManager->getKey($user)->getPrivate();
711
+        openssl_sign(json_encode($message), $signature, $privateKey, OPENSSL_ALGO_SHA512);
712
+        return base64_encode($signature);
713
+    }
714
+
715
+    /**
716
+     * @NoAdminRequired
717
+     * @NoSubadminRequired
718
+     * @PasswordConfirmationRequired
719
+     *
720
+     * @param string $avatarScope
721
+     * @param string $displayname
722
+     * @param string $displaynameScope
723
+     * @param string $phone
724
+     * @param string $phoneScope
725
+     * @param string $email
726
+     * @param string $emailScope
727
+     * @param string $website
728
+     * @param string $websiteScope
729
+     * @param string $address
730
+     * @param string $addressScope
731
+     * @param string $twitter
732
+     * @param string $twitterScope
733
+     * @return DataResponse
734
+     */
735
+    public function setUserSettings($avatarScope,
736
+                                    $displayname,
737
+                                    $displaynameScope,
738
+                                    $phone,
739
+                                    $phoneScope,
740
+                                    $email,
741
+                                    $emailScope,
742
+                                    $website,
743
+                                    $websiteScope,
744
+                                    $address,
745
+                                    $addressScope,
746
+                                    $twitter,
747
+                                    $twitterScope
748
+    ) {
749
+
750
+        if (!empty($email) && !$this->mailer->validateMailAddress($email)) {
751
+            return new DataResponse(
752
+                [
753
+                    'status' => 'error',
754
+                    'data' => [
755
+                        'message' => (string)$this->l10n->t('Invalid mail address')
756
+                    ]
757
+                ],
758
+                Http::STATUS_UNPROCESSABLE_ENTITY
759
+            );
760
+        }
761
+
762
+        $user = $this->userSession->getUser();
763
+
764
+        $data = $this->accountManager->getUser($user);
765
+
766
+        $data[AccountManager::PROPERTY_AVATAR] = ['scope' => $avatarScope];
767
+        if ($this->config->getSystemValue('allow_user_to_change_display_name', true) !== false) {
768
+            $data[AccountManager::PROPERTY_DISPLAYNAME] = ['value' => $displayname, 'scope' => $displaynameScope];
769
+            $data[AccountManager::PROPERTY_EMAIL] = ['value' => $email, 'scope' => $emailScope];
770
+        }
771
+
772
+        if ($this->appManager->isEnabledForUser('federatedfilesharing')) {
773
+            $federatedFileSharing = new \OCA\FederatedFileSharing\AppInfo\Application();
774
+            $shareProvider = $federatedFileSharing->getFederatedShareProvider();
775
+            if ($shareProvider->isLookupServerUploadEnabled()) {
776
+                $data[AccountManager::PROPERTY_WEBSITE] = ['value' => $website, 'scope' => $websiteScope];
777
+                $data[AccountManager::PROPERTY_ADDRESS] = ['value' => $address, 'scope' => $addressScope];
778
+                $data[AccountManager::PROPERTY_PHONE] = ['value' => $phone, 'scope' => $phoneScope];
779
+                $data[AccountManager::PROPERTY_TWITTER] = ['value' => $twitter, 'scope' => $twitterScope];
780
+            }
781
+        }
782
+
783
+        try {
784
+            $this->saveUserSettings($user, $data);
785
+            return new DataResponse(
786
+                [
787
+                    'status' => 'success',
788
+                    'data' => [
789
+                        'userId' => $user->getUID(),
790
+                        'avatarScope' => $data[AccountManager::PROPERTY_AVATAR]['scope'],
791
+                        'displayname' => $data[AccountManager::PROPERTY_DISPLAYNAME]['value'],
792
+                        'displaynameScope' => $data[AccountManager::PROPERTY_DISPLAYNAME]['scope'],
793
+                        'email' => $data[AccountManager::PROPERTY_EMAIL]['value'],
794
+                        'emailScope' => $data[AccountManager::PROPERTY_EMAIL]['scope'],
795
+                        'website' => $data[AccountManager::PROPERTY_WEBSITE]['value'],
796
+                        'websiteScope' => $data[AccountManager::PROPERTY_WEBSITE]['scope'],
797
+                        'address' => $data[AccountManager::PROPERTY_ADDRESS]['value'],
798
+                        'addressScope' => $data[AccountManager::PROPERTY_ADDRESS]['scope'],
799
+                        'message' => (string)$this->l10n->t('Settings saved')
800
+                    ]
801
+                ],
802
+                Http::STATUS_OK
803
+            );
804
+        } catch (ForbiddenException $e) {
805
+            return new DataResponse([
806
+                'status' => 'error',
807
+                'data' => [
808
+                    'message' => $e->getMessage()
809
+                ],
810
+            ]);
811
+        }
812
+
813
+    }
814
+
815
+
816
+    /**
817
+     * update account manager with new user data
818
+     *
819
+     * @param IUser $user
820
+     * @param array $data
821
+     * @throws ForbiddenException
822
+     */
823
+    protected function saveUserSettings(IUser $user, $data) {
824
+
825
+        // keep the user back-end up-to-date with the latest display name and email
826
+        // address
827
+        $oldDisplayName = $user->getDisplayName();
828
+        $oldDisplayName = is_null($oldDisplayName) ? '' : $oldDisplayName;
829
+        if (isset($data[AccountManager::PROPERTY_DISPLAYNAME]['value'])
830
+            && $oldDisplayName !== $data[AccountManager::PROPERTY_DISPLAYNAME]['value']
831
+        ) {
832
+            $result = $user->setDisplayName($data[AccountManager::PROPERTY_DISPLAYNAME]['value']);
833
+            if ($result === false) {
834
+                throw new ForbiddenException($this->l10n->t('Unable to change full name'));
835
+            }
836
+        }
837
+
838
+        $oldEmailAddress = $user->getEMailAddress();
839
+        $oldEmailAddress = is_null($oldEmailAddress) ? '' : $oldEmailAddress;
840
+        if (isset($data[AccountManager::PROPERTY_EMAIL]['value'])
841
+            && $oldEmailAddress !== $data[AccountManager::PROPERTY_EMAIL]['value']
842
+        ) {
843
+            // this is the only permission a backend provides and is also used
844
+            // for the permission of setting a email address
845
+            if (!$user->canChangeDisplayName()) {
846
+                throw new ForbiddenException($this->l10n->t('Unable to change email address'));
847
+            }
848
+            $user->setEMailAddress($data[AccountManager::PROPERTY_EMAIL]['value']);
849
+        }
850
+
851
+        $this->accountManager->updateUser($user, $data);
852
+    }
853
+
854
+    /**
855
+     * Count all unique users visible for the current admin/subadmin.
856
+     *
857
+     * @NoAdminRequired
858
+     *
859
+     * @return DataResponse
860
+     */
861
+    public function stats() {
862
+        $userCount = 0;
863
+        if ($this->isAdmin) {
864
+            $countByBackend = $this->userManager->countUsers();
865
+
866
+            if (!empty($countByBackend)) {
867
+                foreach ($countByBackend as $count) {
868
+                    $userCount += $count;
869
+                }
870
+            }
871
+        } else {
872
+            $groups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($this->userSession->getUser());
873
+
874
+            $uniqueUsers = [];
875
+            foreach ($groups as $group) {
876
+                foreach ($group->getUsers() as $uid => $displayName) {
877
+                    $uniqueUsers[$uid] = true;
878
+                }
879
+            }
880
+
881
+            $userCount = count($uniqueUsers);
882
+        }
883
+
884
+        return new DataResponse(
885
+            [
886
+                'totalUsers' => $userCount
887
+            ]
888
+        );
889
+    }
890
+
891
+
892
+    /**
893
+     * Set the displayName of a user
894
+     *
895
+     * @NoAdminRequired
896
+     * @NoSubadminRequired
897
+     * @PasswordConfirmationRequired
898
+     * @todo merge into saveUserSettings
899
+     *
900
+     * @param string $username
901
+     * @param string $displayName
902
+     * @return DataResponse
903
+     */
904
+    public function setDisplayName($username, $displayName) {
905
+        $currentUser = $this->userSession->getUser();
906
+        $user = $this->userManager->get($username);
907
+
908
+        if ($user === null ||
909
+            !$user->canChangeDisplayName() ||
910
+            (
911
+                !$this->groupManager->isAdmin($currentUser->getUID()) &&
912
+                !$this->groupManager->getSubAdmin()->isUserAccessible($currentUser, $user) &&
913
+                $currentUser->getUID() !== $username
914
+
915
+            )
916
+        ) {
917
+            return new DataResponse([
918
+                'status' => 'error',
919
+                'data' => [
920
+                    'message' => $this->l10n->t('Authentication error'),
921
+                ],
922
+            ]);
923
+        }
924
+
925
+        $userData = $this->accountManager->getUser($user);
926
+        $userData[AccountManager::PROPERTY_DISPLAYNAME]['value'] = $displayName;
927
+
928
+
929
+        try {
930
+            $this->saveUserSettings($user, $userData);
931
+            return new DataResponse([
932
+                'status' => 'success',
933
+                'data' => [
934
+                    'message' => $this->l10n->t('Your full name has been changed.'),
935
+                    'username' => $username,
936
+                    'displayName' => $displayName,
937
+                ],
938
+            ]);
939
+        } catch (ForbiddenException $e) {
940
+            return new DataResponse([
941
+                'status' => 'error',
942
+                'data' => [
943
+                    'message' => $e->getMessage(),
944
+                    'displayName' => $user->getDisplayName(),
945
+                ],
946
+            ]);
947
+        }
948
+    }
949
+
950
+    /**
951
+     * Set the mail address of a user
952
+     *
953
+     * @NoAdminRequired
954
+     * @NoSubadminRequired
955
+     * @PasswordConfirmationRequired
956
+     *
957
+     * @param string $id
958
+     * @param string $mailAddress
959
+     * @return DataResponse
960
+     */
961
+    public function setEMailAddress($id, $mailAddress) {
962
+        $user = $this->userManager->get($id);
963
+        if (!$this->isAdmin
964
+            && !$this->groupManager->getSubAdmin()->isUserAccessible($this->userSession->getUser(), $user)
965
+        ) {
966
+            return new DataResponse(
967
+                [
968
+                    'status' => 'error',
969
+                    'data' => [
970
+                        'message' => (string)$this->l10n->t('Forbidden')
971
+                    ]
972
+                ],
973
+                Http::STATUS_FORBIDDEN
974
+            );
975
+        }
976
+
977
+        if ($mailAddress !== '' && !$this->mailer->validateMailAddress($mailAddress)) {
978
+            return new DataResponse(
979
+                [
980
+                    'status' => 'error',
981
+                    'data' => [
982
+                        'message' => (string)$this->l10n->t('Invalid mail address')
983
+                    ]
984
+                ],
985
+                Http::STATUS_UNPROCESSABLE_ENTITY
986
+            );
987
+        }
988
+
989
+        if (!$user) {
990
+            return new DataResponse(
991
+                [
992
+                    'status' => 'error',
993
+                    'data' => [
994
+                        'message' => (string)$this->l10n->t('Invalid user')
995
+                    ]
996
+                ],
997
+                Http::STATUS_UNPROCESSABLE_ENTITY
998
+            );
999
+        }
1000
+        // this is the only permission a backend provides and is also used
1001
+        // for the permission of setting a email address
1002
+        if (!$user->canChangeDisplayName()) {
1003
+            return new DataResponse(
1004
+                [
1005
+                    'status' => 'error',
1006
+                    'data' => [
1007
+                        'message' => (string)$this->l10n->t('Unable to change mail address')
1008
+                    ]
1009
+                ],
1010
+                Http::STATUS_FORBIDDEN
1011
+            );
1012
+        }
1013
+
1014
+        $userData = $this->accountManager->getUser($user);
1015
+        $userData[AccountManager::PROPERTY_EMAIL]['value'] = $mailAddress;
1016
+
1017
+        try {
1018
+            $this->saveUserSettings($user, $userData);
1019
+            return new DataResponse(
1020
+                [
1021
+                    'status' => 'success',
1022
+                    'data' => [
1023
+                        'username' => $id,
1024
+                        'mailAddress' => $mailAddress,
1025
+                        'message' => (string)$this->l10n->t('Email saved')
1026
+                    ]
1027
+                ],
1028
+                Http::STATUS_OK
1029
+            );
1030
+        } catch (ForbiddenException $e) {
1031
+            return new DataResponse([
1032
+                'status' => 'error',
1033
+                'data' => [
1034
+                    'message' => $e->getMessage()
1035
+                ],
1036
+            ]);
1037
+        }
1038
+    }
1039 1039
 
1040 1040
 }
Please login to merge, or discard this patch.