Completed
Push — master ( 3797b8...5aaa8a )
by Roeland
12:24
created

AccountManager::buildDefaultUserRecord()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 44
rs 9.216
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 * @copyright Copyright (c) 2016, Björn Schießle
5
 *
6
 * @author Bjoern Schiessle <[email protected]>
7
 * @author Björn Schießle <[email protected]>
8
 * @author Joas Schilling <[email protected]>
9
 *
10
 * @license AGPL-3.0
11
 *
12
 * This code is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License, version 3,
14
 * as published by the Free Software Foundation.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License, version 3,
22
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
23
 *
24
 */
25
26
27
namespace OC\Accounts;
28
29
use OCP\Accounts\IAccount;
30
use OCP\Accounts\IAccountManager;
31
use OCP\BackgroundJob\IJobList;
32
use OCP\IDBConnection;
33
use OCP\IUser;
34
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
35
use Symfony\Component\EventDispatcher\GenericEvent;
36
use OC\Settings\BackgroundJobs\VerifyUserData;
37
38
/**
39
 * Class AccountManager
40
 *
41
 * Manage system accounts table
42
 *
43
 * @group DB
44
 * @package OC\Accounts
45
 */
46
class AccountManager implements IAccountManager {
47
48
	/** @var  IDBConnection database connection */
49
	private $connection;
50
51
	/** @var string table name */
52
	private $table = 'accounts';
53
54
	/** @var EventDispatcherInterface */
55
	private $eventDispatcher;
56
57
	/** @var IJobList */
58
	private $jobList;
59
60
	/**
61
	 * AccountManager constructor.
62
	 *
63
	 * @param IDBConnection $connection
64
	 * @param EventDispatcherInterface $eventDispatcher
65
	 * @param IJobList $jobList
66
	 */
67
	public function __construct(IDBConnection $connection,
68
								EventDispatcherInterface $eventDispatcher,
69
								IJobList $jobList) {
70
		$this->connection = $connection;
71
		$this->eventDispatcher = $eventDispatcher;
72
		$this->jobList = $jobList;
73
	}
74
75
	/**
76
	 * update user record
77
	 *
78
	 * @param IUser $user
79
	 * @param $data
80
	 */
81
	public function updateUser(IUser $user, $data) {
82
		$userData = $this->getUser($user);
83
		$updated = true;
84
		if (empty($userData)) {
85
			$this->insertNewUser($user, $data);
86
		} elseif ($userData !== $data) {
87
			$data = $this->checkEmailVerification($userData, $data, $user);
88
			$data = $this->updateVerifyStatus($userData, $data);
89
			$this->updateExistingUser($user, $data);
90
		} else {
91
			// nothing needs to be done if new and old data set are the same
92
			$updated = false;
93
		}
94
95
		if ($updated) {
96
			$this->eventDispatcher->dispatch(
97
				'OC\AccountManager::userUpdated',
98
				new GenericEvent($user, $data)
99
			);
100
		}
101
	}
102
103
	/**
104
	 * delete user from accounts table
105
	 *
106
	 * @param IUser $user
107
	 */
108 View Code Duplication
	public function deleteUser(IUser $user) {
109
		$uid = $user->getUID();
110
		$query = $this->connection->getQueryBuilder();
111
		$query->delete($this->table)
112
			->where($query->expr()->eq('uid', $query->createNamedParameter($uid)))
113
			->execute();
114
	}
115
116
	/**
117
	 * get stored data from a given user
118
	 *
119
	 * @param IUser $user
120
	 * @return array
121
	 */
122
	public function getUser(IUser $user) {
123
		$uid = $user->getUID();
124
		$query = $this->connection->getQueryBuilder();
125
		$query->select('data')->from($this->table)
126
			->where($query->expr()->eq('uid', $query->createParameter('uid')))
127
			->setParameter('uid', $uid);
128
		$query->execute();
129
		$result = $query->execute()->fetchAll();
130
131
		if (empty($result)) {
132
			$userData = $this->buildDefaultUserRecord($user);
133
			$this->insertNewUser($user, $userData);
134
			return $userData;
135
		}
136
137
		$userDataArray = json_decode($result[0]['data'], true);
138
139
		$userDataArray = $this->addMissingDefaultValues($userDataArray);
140
141
		return $userDataArray;
142
	}
143
144
	/**
145
	 * check if we need to ask the server for email verification, if yes we create a cronjob
146
	 *
147
	 * @param $oldData
148
	 * @param $newData
149
	 * @param IUser $user
150
	 * @return array
151
	 */
152
	protected function checkEmailVerification($oldData, $newData, IUser $user) {
153
		if ($oldData[self::PROPERTY_EMAIL]['value'] !== $newData[self::PROPERTY_EMAIL]['value']) {
154
			$this->jobList->add(VerifyUserData::class,
155
				[
156
					'verificationCode' => '',
157
					'data' => $newData[self::PROPERTY_EMAIL]['value'],
158
					'type' => self::PROPERTY_EMAIL,
159
					'uid' => $user->getUID(),
160
					'try' => 0,
161
					'lastRun' => time()
162
				]
163
			);
164
			$newData[AccountManager::PROPERTY_EMAIL]['verified'] = AccountManager::VERIFICATION_IN_PROGRESS;
165
		}
166
167
		return $newData;
168
	}
169
170
	/**
171
	 * make sure that all expected data are set
172
	 *
173
	 * @param array $userData
174
	 * @return array
175
	 */
176
	protected function addMissingDefaultValues(array $userData) {
177
178
		foreach ($userData as $key => $value) {
179
			if (!isset($userData[$key]['verified'])) {
180
				$userData[$key]['verified'] = self::NOT_VERIFIED;
181
			}
182
		}
183
184
		return $userData;
185
	}
186
187
	/**
188
	 * reset verification status if personal data changed
189
	 *
190
	 * @param array $oldData
191
	 * @param array $newData
192
	 * @return array
193
	 */
194
	protected function updateVerifyStatus($oldData, $newData) {
195
196
		// which account was already verified successfully?
197
		$twitterVerified = isset($oldData[self::PROPERTY_TWITTER]['verified']) && $oldData[self::PROPERTY_TWITTER]['verified'] === self::VERIFIED;
198
		$websiteVerified = isset($oldData[self::PROPERTY_WEBSITE]['verified']) && $oldData[self::PROPERTY_WEBSITE]['verified'] === self::VERIFIED;
199
		$emailVerified = isset($oldData[self::PROPERTY_EMAIL]['verified']) && $oldData[self::PROPERTY_EMAIL]['verified'] === self::VERIFIED;
200
201
		// keep old verification status if we don't have a new one
202 View Code Duplication
		if(!isset($newData[self::PROPERTY_TWITTER]['verified'])) {
203
			// keep old verification status if value didn't changed and an old value exists
204
			$keepOldStatus = $newData[self::PROPERTY_TWITTER]['value'] === $oldData[self::PROPERTY_TWITTER]['value'] && isset($oldData[self::PROPERTY_TWITTER]['verified']);
205
			$newData[self::PROPERTY_TWITTER]['verified'] = $keepOldStatus ? $oldData[self::PROPERTY_TWITTER]['verified'] : self::NOT_VERIFIED;
206
		}
207
208 View Code Duplication
		if(!isset($newData[self::PROPERTY_WEBSITE]['verified'])) {
209
			// keep old verification status if value didn't changed and an old value exists
210
			$keepOldStatus = $newData[self::PROPERTY_WEBSITE]['value'] === $oldData[self::PROPERTY_WEBSITE]['value'] && isset($oldData[self::PROPERTY_WEBSITE]['verified']);
211
			$newData[self::PROPERTY_WEBSITE]['verified'] = $keepOldStatus ? $oldData[self::PROPERTY_WEBSITE]['verified'] : self::NOT_VERIFIED;
212
		}
213
214 View Code Duplication
		if(!isset($newData[self::PROPERTY_EMAIL]['verified'])) {
215
			// keep old verification status if value didn't changed and an old value exists
216
			$keepOldStatus = $newData[self::PROPERTY_EMAIL]['value'] === $oldData[self::PROPERTY_EMAIL]['value'] && isset($oldData[self::PROPERTY_EMAIL]['verified']);
217
			$newData[self::PROPERTY_EMAIL]['verified'] = $keepOldStatus ? $oldData[self::PROPERTY_EMAIL]['verified'] : self::VERIFICATION_IN_PROGRESS;
218
		}
219
220
		// reset verification status if a value from a previously verified data was changed
221 View Code Duplication
		if($twitterVerified &&
222
			$oldData[self::PROPERTY_TWITTER]['value'] !== $newData[self::PROPERTY_TWITTER]['value']
223
		) {
224
			$newData[self::PROPERTY_TWITTER]['verified'] = self::NOT_VERIFIED;
225
		}
226
227 View Code Duplication
		if($websiteVerified &&
228
			$oldData[self::PROPERTY_WEBSITE]['value'] !== $newData[self::PROPERTY_WEBSITE]['value']
229
		) {
230
			$newData[self::PROPERTY_WEBSITE]['verified'] = self::NOT_VERIFIED;
231
		}
232
233 View Code Duplication
		if($emailVerified &&
234
			$oldData[self::PROPERTY_EMAIL]['value'] !== $newData[self::PROPERTY_EMAIL]['value']
235
		) {
236
			$newData[self::PROPERTY_EMAIL]['verified'] = self::NOT_VERIFIED;
237
		}
238
239
		return $newData;
240
241
	}
242
243
	/**
244
	 * add new user to accounts table
245
	 *
246
	 * @param IUser $user
247
	 * @param array $data
248
	 */
249
	protected function insertNewUser(IUser $user, $data) {
250
		$uid = $user->getUID();
251
		$jsonEncodedData = json_encode($data);
252
		$query = $this->connection->getQueryBuilder();
253
		$query->insert($this->table)
254
			->values(
255
				[
256
					'uid' => $query->createNamedParameter($uid),
257
					'data' => $query->createNamedParameter($jsonEncodedData),
258
				]
259
			)
260
			->execute();
261
	}
262
263
	/**
264
	 * update existing user in accounts table
265
	 *
266
	 * @param IUser $user
267
	 * @param array $data
268
	 */
269 View Code Duplication
	protected function updateExistingUser(IUser $user, $data) {
270
		$uid = $user->getUID();
271
		$jsonEncodedData = json_encode($data);
272
		$query = $this->connection->getQueryBuilder();
273
		$query->update($this->table)
274
			->set('data', $query->createNamedParameter($jsonEncodedData))
275
			->where($query->expr()->eq('uid', $query->createNamedParameter($uid)))
276
			->execute();
277
	}
278
279
	/**
280
	 * build default user record in case not data set exists yet
281
	 *
282
	 * @param IUser $user
283
	 * @return array
284
	 */
285
	protected function buildDefaultUserRecord(IUser $user) {
286
		return [
287
			self::PROPERTY_DISPLAYNAME =>
288
				[
289
					'value' => $user->getDisplayName(),
290
					'scope' => self::VISIBILITY_CONTACTS_ONLY,
291
					'verified' => self::NOT_VERIFIED,
292
				],
293
			self::PROPERTY_ADDRESS =>
294
				[
295
					'value' => '',
296
					'scope' => self::VISIBILITY_PRIVATE,
297
					'verified' => self::NOT_VERIFIED,
298
				],
299
			self::PROPERTY_WEBSITE =>
300
				[
301
					'value' => '',
302
					'scope' => self::VISIBILITY_PRIVATE,
303
					'verified' => self::NOT_VERIFIED,
304
				],
305
			self::PROPERTY_EMAIL =>
306
				[
307
					'value' => $user->getEMailAddress(),
308
					'scope' => self::VISIBILITY_CONTACTS_ONLY,
309
					'verified' => self::NOT_VERIFIED,
310
				],
311
			self::PROPERTY_AVATAR =>
312
				[
313
					'scope' => self::VISIBILITY_CONTACTS_ONLY
314
				],
315
			self::PROPERTY_PHONE =>
316
				[
317
					'value' => '',
318
					'scope' => self::VISIBILITY_PRIVATE,
319
					'verified' => self::NOT_VERIFIED,
320
				],
321
			self::PROPERTY_TWITTER =>
322
				[
323
					'value' => '',
324
					'scope' => self::VISIBILITY_PRIVATE,
325
					'verified' => self::NOT_VERIFIED,
326
				],
327
		];
328
	}
329
330
	private function parseAccountData(IUser $user, $data): Account {
331
		$account = new Account($user);
332
		foreach($data as $property => $accountData) {
333
			$account->setProperty($property, $accountData['value'], $accountData['scope'], $accountData['verified']);
334
		}
335
		return $account;
336
	}
337
338
	public function getAccount(IUser $user): IAccount {
339
		return $this->parseAccountData($user, $this->getUser($user));
340
	}
341
342
}
343