user.php
6.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<?php
/**
* @package Joomla.Plugin
* @subpackage Privacy.user
*
* @copyright Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
defined('_JEXEC') or die;
use Joomla\Utilities\ArrayHelper;
JLoader::register('PrivacyPlugin', JPATH_ADMINISTRATOR . '/components/com_privacy/helpers/plugin.php');
JLoader::register('PrivacyRemovalStatus', JPATH_ADMINISTRATOR . '/components/com_privacy/helpers/removal/status.php');
/**
* Privacy plugin managing Joomla user data
*
* @since 3.9.0
*/
class PlgPrivacyUser extends PrivacyPlugin
{
/**
* Performs validation to determine if the data associated with a remove information request can be processed
*
* This event will not allow a super user account to be removed
*
* @param PrivacyTableRequest $request The request record being processed
* @param JUser $user The user account associated with this request if available
*
* @return PrivacyRemovalStatus
*
* @since 3.9.0
*/
public function onPrivacyCanRemoveData(PrivacyTableRequest $request, JUser $user = null)
{
$status = new PrivacyRemovalStatus;
if (!$user)
{
return $status;
}
if ($user->authorise('core.admin'))
{
$status->canRemove = false;
$status->reason = JText::_('PLG_PRIVACY_USER_ERROR_CANNOT_REMOVE_SUPER_USER');
}
return $status;
}
/**
* Processes an export request for Joomla core user data
*
* This event will collect data for the following core tables:
*
* - #__users (excluding the password, otpKey, and otep columns)
* - #__user_notes
* - #__user_profiles
* - User custom fields
*
* @param PrivacyTableRequest $request The request record being processed
* @param JUser $user The user account associated with this request if available
*
* @return PrivacyExportDomain[]
*
* @since 3.9.0
*/
public function onPrivacyExportRequest(PrivacyTableRequest $request, JUser $user = null)
{
if (!$user)
{
return array();
}
/** @var JTableUser $userTable */
$userTable = JUser::getTable();
$userTable->load($user->id);
$domains = array();
$domains[] = $this->createUserDomain($userTable);
$domains[] = $this->createNotesDomain($userTable);
$domains[] = $this->createProfileDomain($userTable);
$domains[] = $this->createCustomFieldsDomain('com_users.user', array($userTable));
return $domains;
}
/**
* Removes the data associated with a remove information request
*
* This event will pseudoanonymise the user account
*
* @param PrivacyTableRequest $request The request record being processed
* @param JUser $user The user account associated with this request if available
*
* @return void
*
* @since 3.9.0
*/
public function onPrivacyRemoveData(PrivacyTableRequest $request, JUser $user = null)
{
// This plugin only processes data for registered user accounts
if (!$user)
{
return;
}
$pseudoanonymisedData = array(
'name' => 'User ID ' . $user->id,
'username' => bin2hex(random_bytes(12)),
'email' => 'UserID' . $user->id . 'removed@email.invalid',
'block' => true,
);
$user->bind($pseudoanonymisedData);
$user->save();
// Destroy all sessions for the user account
$sessionIds = $this->db->setQuery(
$this->db->getQuery(true)
->select($this->db->quoteName('session_id'))
->from($this->db->quoteName('#__session'))
->where($this->db->quoteName('userid') . ' = ' . (int) $user->id)
)->loadColumn();
// If there aren't any active sessions then there's nothing to do here
if (empty($sessionIds))
{
return;
}
$storeName = JFactory::getConfig()->get('session_handler', 'none');
$store = JSessionStorage::getInstance($storeName);
$quotedIds = array();
// Destroy the sessions and quote the IDs to purge the session table
foreach ($sessionIds as $sessionId)
{
$store->destroy($sessionId);
$quotedIds[] = $this->db->quoteBinary($sessionId);
}
$this->db->setQuery(
$this->db->getQuery(true)
->delete($this->db->quoteName('#__session'))
->where($this->db->quoteName('session_id') . ' IN (' . implode(', ', $quotedIds) . ')')
)->execute();
}
/**
* Create the domain for the user notes data
*
* @param JTableUser $user The JTableUser object to process
*
* @return PrivacyExportDomain
*
* @since 3.9.0
*/
private function createNotesDomain(JTableUser $user)
{
$domain = $this->createDomain('user_notes', 'joomla_user_notes_data');
$query = $this->db->getQuery(true)
->select('*')
->from($this->db->quoteName('#__user_notes'))
->where($this->db->quoteName('user_id') . ' = ' . $this->db->quote($user->id));
$items = $this->db->setQuery($query)->loadAssocList();
// Remove user ID columns
foreach (array('user_id', 'created_user_id', 'modified_user_id') as $column)
{
$items = ArrayHelper::dropColumn($items, $column);
}
foreach ($items as $item)
{
$domain->addItem($this->createItemFromArray($item, $item['id']));
}
return $domain;
}
/**
* Create the domain for the user profile data
*
* @param JTableUser $user The JTableUser object to process
*
* @return PrivacyExportDomain
*
* @since 3.9.0
*/
private function createProfileDomain(JTableUser $user)
{
$domain = $this->createDomain('user_profile', 'joomla_user_profile_data');
$query = $this->db->getQuery(true)
->select('*')
->from($this->db->quoteName('#__user_profiles'))
->where($this->db->quoteName('user_id') . ' = ' . $this->db->quote($user->id))
->order($this->db->quoteName('ordering') . ' ASC');
$items = $this->db->setQuery($query)->loadAssocList();
foreach ($items as $item)
{
$domain->addItem($this->createItemFromArray($item));
}
return $domain;
}
/**
* Create the domain for the user record
*
* @param JTableUser $user The JTableUser object to process
*
* @return PrivacyExportDomain
*
* @since 3.9.0
*/
private function createUserDomain(JTableUser $user)
{
$domain = $this->createDomain('users', 'joomla_users_data');
$domain->addItem($this->createItemForUserTable($user));
return $domain;
}
/**
* Create an item object for a JTableUser object
*
* @param JTableUser $user The JTableUser object to convert
*
* @return PrivacyExportItem
*
* @since 3.9.0
*/
private function createItemForUserTable(JTableUser $user)
{
$data = array();
$exclude = array('password', 'otpKey', 'otep');
foreach (array_keys($user->getFields()) as $fieldName)
{
if (!in_array($fieldName, $exclude))
{
$data[$fieldName] = $user->$fieldName;
}
}
return $this->createItemFromArray($data, $user->id);
}
}