-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCspUserPlugin.php
384 lines (330 loc) · 15.1 KB
/
CspUserPlugin.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
<?php
/**
* @file plugins /generic/cspUser/CspUserPlugin.inc.php
*
* Copyright (c) 2020-2023 Lívia Gouvêa
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class CspUserPlugin
* @brief Customizes User profile fields
*/
namespace APP\plugins\generic\cspUser;
use PKP\plugins\GenericPlugin;
use APP\core\Application;
use PKP\plugins\Hook;
use APP\template\TemplateManager;
use APP\facades\Repo;
use PKP\security\Role;
use PKP\services\PKPSchemaService;
use APP\core\Services;
use PKP\core\Core;
use PKP\security\Validation;
use PKP\session\SessionManager;
use Illuminate\Support\Facades\DB;
use PKP\facades\Locale;
use PKP\user\InterestManager;
class CspUserPlugin extends GenericPlugin {
/**
* @copydoc Plugin::register()
*
* @param null|mixed $mainContextId
*/
public function register($category, $path, $mainContextId = null)
{
$success = parent::register($category, $path, $mainContextId);
if ($success && $this->getEnabled()) {
Hook::add('Schema::get::user', [$this, 'addToUserSchema']);
Services::get('schema')->get(PKPSchemaService::SCHEMA_USER, true);
Hook::add('registrationform::readuservars', [$this, 'registrationFormReadUserVars']);
Hook::add('registrationform::Constructor', [$this, 'registrationFormConstructor']);
Hook::add('registrationform::execute', [$this, 'registrationFormExecute']);
Hook::add('registrationform::display', [$this, 'registrationFormDisplay']);
Hook::add('contactform::display', [$this, 'contactFormDisplay']);
Hook::add('contactform::readuservars', [$this, 'contactFormReaduservars']);
Hook::add('contactform::execute', [$this, 'contactFormExecute']);
Hook::add('identityform::display', array($this, 'identityFormDisplay'));
Hook::add('identityform::readuservars', array($this, 'identityFormReaduservars'));
Hook::add('identityform::execute', array($this, 'identityFormExecute'));
Hook::add('TemplateResource::getFilename', [$this, '_overridePluginTemplates']);
Hook::add('LoadHandler', [$this, 'loadHandler']);
}
return $success;
}
/**
* Provide a name for this plugin
*
* The name will appear in the Plugin Gallery where editors can
* install, enable and disable plugins.
*/
public function getDisplayName()
{
return __('plugins.generic.cspUser.displayName');
}
/**
* Provide a description for this plugin
*
* The description will appear in the Plugin Gallery where editors can
* install, enable and disable plugins.
*/
public function getDescription()
{
return __('plugins.generic.cspUser.description');
}
public function addToUserSchema(string $hookName, array $args)
{
$schema = $args[0]; /** @var stdClass */
$schema->properties->gender = (object) [
'type' => 'string',
'multilingual' => false,
"apiSummary" => true,
'validation' => ['nullable']
];
$schema->properties->breed = (object) [
'type' => 'string',
'multilingual' => false,
"apiSummary" => true,
'validation' => ['nullable']
];
$schema->properties->affiliation2 = (object) [
"type" => "string",
"multilingual" => false,
"apiSummary" => true,
"validation" => ["nullable"]
];
$schema->properties->city = (object) [
'type' => 'string',
'multilingual' => false,
"apiSummary" => true,
'validation' => ['nullable']
];
$schema->properties->region = (object) [
'type' => 'string',
'multilingual' => false,
"apiSummary" => true,
'validation' => ['nullable']
];
$schema->properties->zipCode = (object) [
'type' => 'string',
'multilingual' => false,
"apiSummary" => true,
'validation' => ['nullable']
];
return false;
}
public function registrationFormConstructor(string $hookName, array $args)
{
$form =& $args[0];
$form->addCheck(new \PKP\form\validation\FormValidatorORCID($form, 'orcid', 'required', 'user.orcid.orcidInvalid'));
}
public function registrationFormDisplay(string $hookName, array $args){
$form = &$args[0];
$request = Application::get()->getRequest();
$templateMgr = TemplateManager::getManager($request);
$genders['M'] = __('plugins.themes.csp.user.gender.male');
$genders['F'] = __('plugins.themes.csp.user.gender.female');
$genders['NB'] = __('plugins.themes.csp.user.gender.non-binary');
$genders['NI'] = __('plugins.themes.csp.user.gender.not.inform');
$breeds['amarela'] = __('plugins.themes.csp.user.breed.amarela');
$breeds['branca'] = __('plugins.themes.csp.user.breed.branca');
$breeds['indigena'] = __('plugins.themes.csp.user.breed.indigena');
$breeds['parda'] = __('plugins.themes.csp.user.breed.parda');
$breeds['preta'] = __('plugins.themes.csp.user.breed.preta');
$templateMgr->assign('genders', $genders);
$templateMgr->assign('breeds', $breeds);
}
public function registrationformReadUserVars(string $hookName, array $args)
{
$args[1][] = 'url';
$args[1][] = 'gender';
$args[1][] = 'breed';
$args[1][] = 'affiliation2';
$args[1][] = 'mailingAddress';
$args[1][] = 'city';
$args[1][] = 'region';
$args[1][] = 'zipCode';
$args[1][] = 'orcid';
}
public function registrationFormExecute(string $hookName, array $args)
{
$form = &$args[0];
$newUser = $form->user;
$newUser->setData('url', $form->getData('url'));
$newUser->setData('gender', $form->getData('gender'));
$newUser->setData('breed', $form->getData('breed'));
$newUser->setData('affiliation2', $form->getData('affiliation2'));
$newUser->setData('mailingAddress', $form->getData('mailingAddress'));
$newUser->setData('city', $form->getData('city'));
$newUser->setData('region', $form->getData('region'));
$newUser->setData('zipCode', $form->getData('zipCode'));
$newUser->setData('orcid', $form->getData('orcid'));
$supportedLocales = Locale::getSupportedLocales();
foreach ($supportedLocales as $key => $value) {
$newUser->setData('affiliation', $form->getData('affiliation'), $key);
$newUser->setData('givenName', $form->getData('givenName'), $key);
$newUser->setData('familyName', $form->getData('familyName'), $key);
}
$request = Application::get()->getRequest();
$reviewerGroup = Repo::userGroup()->getByRoleIds([Role::ROLE_ID_REVIEWER], $request->getContext()->getId(), true)->first()->getId();
$readerGroup = Repo::userGroup()->getByRoleIds([Role::ROLE_ID_READER], $request->getContext()->getId(), true)->first()->getId();
$form->setData('reviewerGroup', array($reviewerGroup => $reviewerGroup));
$form->setData('readerGroup', array($readerGroup => $readerGroup));
}
public function contactFormDisplay(string $hookName, array $args){
$user = Repo::user()->get($args[0]->_user->getData('id'), true);
$args[0]->_data["affiliation2"] = $user->getData('affiliation2');
$args[0]->_data["city"] = $user->getData('city');
$args[0]->_data["region"] = $user->getData('region');
$args[0]->_data["zipCode"] = $user->getData('zipCode');
}
public function contactFormReaduservars(string $hookName, array $args){
$args[1][] = 'affiliation2';
$args[1][] = 'city';
$args[1][] = 'region';
$args[1][] = 'zipCode';
}
public function contactFormExecute(string $hookName, array $args){
$form = &$args[0];
$editUser = $form->_user;
$editUser->setData('affiliation2', $form->getData('affiliation2'));
$editUser->setData('city', $form->getData('city'));
$editUser->setData('region', $form->getData('region'));
$editUser->setData('zipCode', $form->getData('zipCode'));
$user = Repo::user()->get($args[0]->_user->getData('id'), true);
$editUser->setData('gender', $user->getData('gender'));
$editUser->setData('breed', $user->getData('breed'));
$currentLocale = Locale::getLocale();
$supportedLocales = Locale::getSupportedLocales();
foreach ($supportedLocales as $key => $value) {
$editUser->setData('affiliation', $form->_data["affiliation"][$currentLocale], $key);
}
}
public function identityFormDisplay(string $hookName, array $args){
$request = Application::get()->getRequest();
$templateMgr = TemplateManager::getManager($request);
$genders['M'] = __('plugins.themes.csp.user.gender.male');
$genders['F'] = __('plugins.themes.csp.user.gender.female');
$genders['NB'] = __('plugins.themes.csp.user.gender.non-binary');
$genders['NI'] = __('plugins.themes.csp.user.gender.not.inform');
$breeds['amarela'] = __('plugins.themes.csp.user.breed.amarela');
$breeds['branca'] = __('plugins.themes.csp.user.breed.branca');
$breeds['indigena'] = __('plugins.themes.csp.user.breed.indigena');
$breeds['parda'] = __('plugins.themes.csp.user.breed.parda');
$breeds['preta'] = __('plugins.themes.csp.user.breed.preta');
$user = Repo::user()->get($args[0]->_user->getData('id'), true);
if ($user->getData('orcid') == "") {
$orcidNotification = '<div class="pkpNotification pkpNotification--warning">
<h2>Favor preencher o seu Orcid na aba "Público"!</h2>
</div><br>';
}
$templateMgr->assign([
'genders' => $genders,
'gender' => $user->getData('gender'),
'breeds' => $breeds,
'breed' => $user->getData('breed'),
'orcidNotification' => $orcidNotification,
]);
}
public function identityFormReaduservars(string $hookName, array $args){
$args[1][] = 'gender';
$args[1][] = 'breed';
}
public function identityFormExecute(string $hookName, array $args)
{
$form = &$args[0];
$editUser = $form->_user;
$editUser->setData('gender', $form->getData('gender'));
$editUser->setData('breed', $form->getData('breed'));
$user = Repo::user()->get($args[0]->_user->getData('id'), true);
$editUser->setData('affiliation2', $user->getData('affiliation2'));
$editUser->setData('city', $user->getData('city'));
$editUser->setData('region', $user->getData('region'));
$editUser->setData('zipCode', $user->getData('zipCode'));
$currentLocale = Locale::getLocale();
$supportedLocales = Locale::getSupportedLocales();
foreach ($supportedLocales as $key => $value) {
$editUser->setData('givenName', $form->_data["givenName"][$currentLocale], $key);
$editUser->setData('familyName', $form->_data["familyName"][$currentLocale], $key);
}
}
// Integração de login Sagas com OJS
public function loadHandler($hookName, $args){
if( $args[0] == "login" && ($args[1] == "signIn" or $args[1] == "requestResetPassword")){
$request = Application::get()->getRequest();
if($args[1] == "requestResetPassword"){
$user = Repo::user()->getByEmail($request->getUserVar('email'));
if ($user){
return;
}
}
$results = DB::table('csp.Login as l')
->select(
'login AS username',
'p.email',
'p.telefone AS phone',
'p.orcid AS orcid',
'p.nome AS givenName',
'p.idioma',
'p.lattes',
'p.sexo',
'p.observacao',
'p.instituicao1',
'p.instituicao2',
'p.endereco',
'p.cidade',
'p.estado',
'p.cep'
)
->leftJoin('ojs.users as ou', 'ou.username', '=', 'l.login')
->join('csp.Pessoa as p', 'l.idPessoaFK', '=', 'p.idPessoa');
if ($args[1] == "signIn") {
$results->where('login','=', $request->getUserVar('username'));
$results->where('l.senha', '=', sha1($request->getUserVar('password')));
}
if($args[1] == "requestResetPassword"){
$results->where('p.email','=', $request->getUserVar('email'));
}
$results->whereNull('ou.user_id');
$row = $results->first();
if($row){
$user = Repo::user()->newDataObject();
$user->setUsername($row->username);
$user->setEmail($row->email);
// $user->setPhone($row->phone);
$user->setOrcid($row->orcid);
$user->setUrl($row->lattes);
$user->setData('zipCode',$row->cep);
$user->setData('city',$row->cidade);
$user->setData('region',$row->estado);
$user->setData('affiliation2',$row->instituicao2);
$user->setData('mailingAddress',$row->endereco);
$user->setData('gender',$row->sexo);
$supportedLocales = Locale::getSupportedLocales();
foreach ($supportedLocales as $key => $value) {
$user->setGivenName($row->givenName, $key);
$user->setAffiliation($row->instituicao1, $key);
}
$password = $request->getUserVar('password') ? $request->getUserVar('password') : base64_encode(random_bytes(10));
$user->setDateRegistered(Core::getCurrentDate());
$user->setInlineHelp(1); // default new users to having inline help visible.
$user->setPassword(Validation::encryptCredentials($row->username, $password));
Repo::user()->add($user);
$userId = $user->getId();
if (!$userId) {
return false;
}
// Associate the new user with the existing session
$sessionManager = SessionManager::getManager();
$session = $sessionManager->getUserSession();
$session->setSessionVar('username', $user->getUsername());
$defaultReaderGroup = Repo::userGroup()->getByRoleIds([Role::ROLE_ID_READER], $request->getContext()->getId(), true)->first();
$reviewerGroup = Repo::userGroup()->getByRoleIds([Role::ROLE_ID_REVIEWER], $request->getContext()->getId(), true)->first();
Repo::userGroup()->assignUserToGroup($user->getId(), $defaultReaderGroup->getId(), $request->getContext()->getId());
Repo::userGroup()->assignUserToGroup($user->getId(), $reviewerGroup->getId(), $request->getContext()->getId());
$basePath = $request->getBasePath();
$contextPath = $request->getContext()->getPath();
$basePath = $request->getBasePath();
$request->_requestVars["source"] = $basePath.'/index.php/'.$contextPath."/user/profile";
}
}
}
}