|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Björn Schießle <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc. |
|
6
|
|
|
* @copyright Copyright (c) 2016, Björn Schießle |
|
7
|
|
|
* @license AGPL-3.0 |
|
8
|
|
|
* |
|
9
|
|
|
* This code is free software: you can redistribute it and/or modify |
|
10
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
|
11
|
|
|
* as published by the Free Software Foundation. |
|
12
|
|
|
* |
|
13
|
|
|
* This program is distributed in the hope that it will be useful, |
|
14
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
16
|
|
|
* GNU Affero General Public License for more details. |
|
17
|
|
|
* |
|
18
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
|
19
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
|
20
|
|
|
* |
|
21
|
|
|
*/ |
|
22
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
namespace OC\Accounts; |
|
25
|
|
|
|
|
26
|
|
|
use OCP\IDBConnection; |
|
27
|
|
|
use OCP\IUser; |
|
28
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
29
|
|
|
use Symfony\Component\EventDispatcher\GenericEvent; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Class AccountManager |
|
33
|
|
|
* |
|
34
|
|
|
* Manage system accounts table |
|
35
|
|
|
* |
|
36
|
|
|
* @group DB |
|
37
|
|
|
* @package OC\Accounts |
|
38
|
|
|
*/ |
|
39
|
|
|
class AccountManager { |
|
40
|
|
|
|
|
41
|
|
|
/** nobody can see my account details */ |
|
42
|
|
|
const VISIBILITY_PRIVATE = 'private'; |
|
43
|
|
|
/** only contacts, especially trusted servers can see my contact details */ |
|
44
|
|
|
const VISIBILITY_CONTACTS_ONLY = 'contacts'; |
|
45
|
|
|
/** every body ca see my contact detail, will be published to the lookup server */ |
|
46
|
|
|
const VISIBILITY_PUBLIC = 'public'; |
|
47
|
|
|
|
|
48
|
|
|
const PROPERTY_AVATAR = 'avatar'; |
|
49
|
|
|
const PROPERTY_DISPLAYNAME = 'displayname'; |
|
50
|
|
|
const PROPERTY_PHONE = 'phone'; |
|
51
|
|
|
const PROPERTY_EMAIL = 'email'; |
|
52
|
|
|
const PROPERTY_WEBSITE = 'website'; |
|
53
|
|
|
const PROPERTY_ADDRESS = 'address'; |
|
54
|
|
|
const PROPERTY_TWITTER = 'twitter'; |
|
55
|
|
|
|
|
56
|
|
|
/** @var IDBConnection database connection */ |
|
57
|
|
|
private $connection; |
|
58
|
|
|
|
|
59
|
|
|
/** @var string table name */ |
|
60
|
|
|
private $table = 'accounts'; |
|
61
|
|
|
|
|
62
|
|
|
/** @var EventDispatcherInterface */ |
|
63
|
|
|
private $eventDispatcher; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* AccountManager constructor. |
|
67
|
|
|
* |
|
68
|
|
|
* @param IDBConnection $connection |
|
69
|
|
|
* @param EventDispatcherInterface $eventDispatcher |
|
70
|
|
|
*/ |
|
71
|
|
|
public function __construct(IDBConnection $connection, EventDispatcherInterface $eventDispatcher) { |
|
72
|
|
|
$this->connection = $connection; |
|
73
|
|
|
$this->eventDispatcher = $eventDispatcher; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* update user record |
|
78
|
|
|
* |
|
79
|
|
|
* @param IUser $user |
|
80
|
|
|
* @param $data |
|
81
|
|
|
*/ |
|
82
|
|
|
public function updateUser(IUser $user, $data) { |
|
83
|
|
|
$userData = $this->getUser($user); |
|
84
|
|
|
if (empty($userData)) { |
|
85
|
|
|
$this->insertNewUser($user, $data); |
|
86
|
|
|
} else { |
|
87
|
|
|
$this->updateExistingUser($user, $data); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
$this->eventDispatcher->dispatch( |
|
91
|
|
|
'OC\AccountManager::userUpdated', |
|
92
|
|
|
new GenericEvent($user) |
|
93
|
|
|
); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* get stored data from a given user |
|
98
|
|
|
* |
|
99
|
|
|
* @param IUser $user |
|
100
|
|
|
* @return array |
|
101
|
|
|
*/ |
|
102
|
|
|
public function getUser(IUser $user) { |
|
103
|
|
|
$uid = $user->getUID(); |
|
104
|
|
|
$query = $this->connection->getQueryBuilder(); |
|
105
|
|
|
$query->select('data')->from($this->table) |
|
106
|
|
|
->where($query->expr()->eq('uid', $query->createParameter('uid'))) |
|
107
|
|
|
->setParameter('uid', $uid); |
|
108
|
|
|
$query->execute(); |
|
109
|
|
|
$result = $query->execute()->fetchAll(); |
|
110
|
|
|
|
|
111
|
|
|
if (empty($result)) { |
|
112
|
|
|
$userData = $this->buildDefaultUserRecord($user); |
|
113
|
|
|
$this->insertNewUser($user, $userData); |
|
114
|
|
|
return $userData; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
return json_decode($result[0]['data'], true); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* add new user to accounts table |
|
122
|
|
|
* |
|
123
|
|
|
* @param IUser $user |
|
124
|
|
|
* @param array $data |
|
125
|
|
|
*/ |
|
126
|
|
|
protected function insertNewUser(IUser $user, $data) { |
|
127
|
|
|
$uid = $user->getUID(); |
|
128
|
|
|
$jsonEncodedData = json_encode($data); |
|
129
|
|
|
$query = $this->connection->getQueryBuilder(); |
|
130
|
|
|
$query->insert($this->table) |
|
131
|
|
|
->values( |
|
132
|
|
|
[ |
|
133
|
|
|
'uid' => $query->createNamedParameter($uid), |
|
134
|
|
|
'data' => $query->createNamedParameter($jsonEncodedData), |
|
135
|
|
|
] |
|
136
|
|
|
) |
|
137
|
|
|
->execute(); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* update existing user in accounts table |
|
142
|
|
|
* |
|
143
|
|
|
* @param IUser $user |
|
144
|
|
|
* @param array $data |
|
145
|
|
|
*/ |
|
146
|
|
View Code Duplication |
protected function updateExistingUser(IUser $user, $data) { |
|
|
|
|
|
|
147
|
|
|
$uid = $user->getUID(); |
|
148
|
|
|
$jsonEncodedData = json_encode($data); |
|
149
|
|
|
$query = $this->connection->getQueryBuilder(); |
|
150
|
|
|
$query->update($this->table) |
|
151
|
|
|
->set('data', $query->createNamedParameter($jsonEncodedData)) |
|
152
|
|
|
->where($query->expr()->eq('uid', $query->createNamedParameter($uid))) |
|
153
|
|
|
->execute(); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* build default user record in case not data set exists yet |
|
158
|
|
|
* |
|
159
|
|
|
* @param IUser $user |
|
160
|
|
|
* @return array |
|
161
|
|
|
*/ |
|
162
|
|
|
protected function buildDefaultUserRecord(IUser $user) { |
|
163
|
|
|
return [ |
|
164
|
|
|
self::PROPERTY_DISPLAYNAME => |
|
165
|
|
|
[ |
|
166
|
|
|
'value' => $user->getDisplayName(), |
|
167
|
|
|
'scope' => self::VISIBILITY_CONTACTS_ONLY, |
|
168
|
|
|
], |
|
169
|
|
|
self::PROPERTY_ADDRESS => |
|
170
|
|
|
[ |
|
171
|
|
|
'value' => '', |
|
172
|
|
|
'scope' => self::VISIBILITY_PRIVATE, |
|
173
|
|
|
], |
|
174
|
|
|
self::PROPERTY_WEBSITE => |
|
175
|
|
|
[ |
|
176
|
|
|
'value' => '', |
|
177
|
|
|
'scope' => self::VISIBILITY_PRIVATE, |
|
178
|
|
|
], |
|
179
|
|
|
self::PROPERTY_EMAIL => |
|
180
|
|
|
[ |
|
181
|
|
|
'value' => $user->getEMailAddress(), |
|
182
|
|
|
'scope' => self::VISIBILITY_CONTACTS_ONLY, |
|
183
|
|
|
], |
|
184
|
|
|
self::PROPERTY_AVATAR => |
|
185
|
|
|
[ |
|
186
|
|
|
'scope' => self::VISIBILITY_CONTACTS_ONLY |
|
187
|
|
|
], |
|
188
|
|
|
self::PROPERTY_PHONE => |
|
189
|
|
|
[ |
|
190
|
|
|
'value' => '', |
|
191
|
|
|
'scope' => self::VISIBILITY_PRIVATE, |
|
192
|
|
|
], |
|
193
|
|
|
self::PROPERTY_TWITTER => |
|
194
|
|
|
[ |
|
195
|
|
|
'value' => '', |
|
196
|
|
|
'scope' => self::VISIBILITY_PRIVATE, |
|
197
|
|
|
], |
|
198
|
|
|
]; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
} |
|
202
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.