/
PKPUserImportExportPlugin.inc.php
292 lines (264 loc) · 10.1 KB
/
PKPUserImportExportPlugin.inc.php
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
<?php
/**
* @file plugins/importexport/users/PKPUserImportExportPlugin.inc.php
*
* Copyright (c) 2014-2021 Simon Fraser University
* Copyright (c) 2003-2021 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class UserImportExportPlugin
* @ingroup plugins_importexport_users
*
* @brief User XML import/export plugin
*/
use APP\facades\Repo;
use APP\template\TemplateManager;
use PKP\core\JSONMessage;
use PKP\file\FileManager;
use PKP\file\TemporaryFileManager;
use PKP\plugins\ImportExportPlugin;
abstract class PKPUserImportExportPlugin extends ImportExportPlugin
{
/**
* @copydoc Plugin::register()
*
* @param null|mixed $mainContextId
*/
public function register($category, $path, $mainContextId = null)
{
$success = parent::register($category, $path, $mainContextId);
$this->addLocaleData();
import('lib.pkp.' . str_replace('/', '.', $this->getPluginPath()) . '.PKPUserImportExportDeployment');
return $success;
}
/**
* Get the name of this plugin. The name must be unique within
* its category.
*
* @return String name of plugin
*/
public function getName()
{
return 'UserImportExportPlugin';
}
/**
* Get the display name.
*
* @return string
*/
public function getDisplayName()
{
return __('plugins.importexport.users.displayName');
}
/**
* Get the display description.
*
* @return string
*/
public function getDescription()
{
return __('plugins.importexport.users.description');
}
/**
* @copydoc ImportExportPlugin::getPluginSettingsPrefix()
*/
public function getPluginSettingsPrefix()
{
return 'users';
}
/**
* Display the plugin.
*
* @param $args array
* @param $request PKPRequest
*/
public function display($args, $request)
{
$templateMgr = TemplateManager::getManager($request);
$context = $request->getContext();
parent::display($args, $request);
$templateMgr->assign('plugin', $this);
switch (array_shift($args)) {
case 'index':
case '':
$templateMgr->display($this->getTemplateResource('index.tpl'));
break;
case 'uploadImportXML':
$user = $request->getUser();
$temporaryFileManager = new TemporaryFileManager();
$temporaryFile = $temporaryFileManager->handleUpload('uploadedFile', $user->getId());
if ($temporaryFile) {
$json = new JSONMessage(true);
$json->setAdditionalAttributes([
'temporaryFileId' => $temporaryFile->getId()
]);
} else {
$json = new JSONMessage(false, __('common.uploadFailed'));
}
header('Content-Type: application/json');
return $json->getString();
case 'importBounce':
if (!$request->checkCSRF()) {
throw new Exception('CSRF mismatch!');
}
$json = new JSONMessage(true);
$json->setEvent('addTab', [
'title' => __('plugins.importexport.users.results'),
'url' => $request->url(null, null, null, ['plugin', $this->getName(), 'import'], ['temporaryFileId' => $request->getUserVar('temporaryFileId'), 'csrfToken' => $request->getSession()->getCSRFToken()]),
]);
header('Content-Type: application/json');
return $json->getString();
case 'import':
if (!$request->checkCSRF()) {
throw new Exception('CSRF mismatch!');
}
$temporaryFileId = $request->getUserVar('temporaryFileId');
$temporaryFileDao = DAORegistry::getDAO('TemporaryFileDAO'); /** @var TemporaryFileDAO $temporaryFileDao */
$user = $request->getUser();
$temporaryFile = $temporaryFileDao->getTemporaryFile($temporaryFileId, $user->getId());
if (!$temporaryFile) {
$json = new JSONMessage(true, __('plugins.importexport.users.uploadFile'));
header('Content-Type: application/json');
return $json->getString();
}
$temporaryFilePath = $temporaryFile->getFilePath();
libxml_use_internal_errors(true);
$filter = $this->getUserImportExportFilter($context, $user);
$users = $this->importUsers(file_get_contents($temporaryFilePath), $context, $user, $filter);
$validationErrors = array_filter(libxml_get_errors(), function ($a) {
return $a->level == LIBXML_ERR_ERROR || $a->level == LIBXML_ERR_FATAL;
});
$templateMgr->assign('validationErrors', $validationErrors);
libxml_clear_errors();
if ($filter->hasErrors()) {
$templateMgr->assign('filterErrors', $filter->getErrors());
}
$templateMgr->assign('users', $users);
$json = new JSONMessage(true, $templateMgr->fetch($this->getTemplateResource('results.tpl')));
header('Content-Type: application/json');
return $json->getString();
case 'export':
$filter = $this->getUserImportExportFilter($request->getContext(), $request->getUser(), false);
$exportXml = $this->exportUsers(
(array) $request->getUserVar('selectedUsers'),
$request->getContext(),
$request->getUser(),
$filter
);
$fileManager = new FileManager();
$exportFileName = $this->getExportFileName($this->getExportPath(), 'users', $context, '.xml');
$fileManager->writeFile($exportFileName, $exportXml);
$fileManager->downloadByPath($exportFileName);
$fileManager->deleteByPath($exportFileName);
break;
case 'exportAllUsers':
$filter = $this->getUserImportExportFilter($request->getContext(), $request->getUser(), false);
$exportXml = $this->exportAllUsers(
$request->getContext(),
$request->getUser(),
$filter
);
$fileManager = new FileManager();
$exportFileName = $this->getExportFileName($this->getExportPath(), 'users', $context, '.xml');
$fileManager->writeFile($exportFileName, $exportXml);
$fileManager->downloadByPath($exportFileName);
$fileManager->deleteByPath($exportFileName);
break;
default:
$dispatcher = $request->getDispatcher();
$dispatcher->handle404();
}
}
/**
* Get the XML for all of users.
*
* @param $context Context
* @param $user User
* @param $filter Filter byRef parameter - import/export filter used
*
* @return string XML contents representing the supplied user IDs.
*/
public function exportAllUsers($context, $user, &$filter = null)
{
$userGroupDao = DAORegistry::getDAO('UserGroupDAO'); /** @var UserGroupDAO $userGroupDao */
$users = $userGroupDao->getUsersByContextId($context->getId());
if (!$filter) {
$filter = $this->getUserImportExportFilter($context, $user, false);
}
return $this->exportUsers($users->toArray(), $context, $user, $filter);
}
/**
* Get the XML for a set of users.
*
* @param $ids array mixed Array of users or user IDs
* @param $context Context
* @param $user User
* @param $filter Filter byRef parameter - import/export filter used
*
* @return string XML contents representing the supplied user IDs.
*/
public function exportUsers($ids, $context, $user, &$filter = null)
{
$xml = '';
if (!$filter) {
$filter = $this->getUserImportExportFilter($context, $user, false);
}
$users = [];
foreach ($ids as $id) {
if (is_a($id, 'User')) {
$users[] = $id;
} else {
$user = Repo::user()->get($id, true);
if ($user) {
$users[] = $user;
}
}
}
$userXml = $filter->execute($users);
if ($userXml) {
$xml = $userXml->saveXml();
} else {
fatalError('Could not convert users.');
}
return $xml;
}
/**
* Get the XML for a set of users.
*
* @param $importXml string XML contents to import
* @param $context Context
* @param $user User
* @param $filter Filter byRef parameter - import/export filter used
*
* @return array Set of imported users
*/
public function importUsers($importXml, $context, $user, &$filter = null)
{
if (!$filter) {
$filter = $this->getUserImportExportFilter($context, $user);
}
return $filter->execute($importXml);
}
/**
* Return user filter for import purposes
*
* @param $context Context
* @param $user User
* @param $isImport bool return Import Filter if true - export if false
*
* @return Filter
*/
public function getUserImportExportFilter($context, $user, $isImport = true)
{
$filterDao = DAORegistry::getDAO('FilterDAO'); /** @var FilterDAO $filterDao */
if ($isImport) {
$userFilters = $filterDao->getObjectsByGroup('user-xml=>user');
} else {
$userFilters = $filterDao->getObjectsByGroup('user=>user-xml');
}
assert(count($userFilters) == 1); // Assert only a single unserialization filter
$filter = array_shift($userFilters);
$filter->setDeployment(new PKPUserImportExportDeployment($context, $user));
return $filter;
}
}