forked from thoth-pub/thoth-omp-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThothSettingsForm.inc.php
111 lines (97 loc) · 3.47 KB
/
ThothSettingsForm.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
<?php
/**
* @file plugins/generic/thoth/ThothSettingsForm.inc.php
*
* Copyright (c) 2014-2020 Simon Fraser University
* Copyright (c) 2003-2020 John Willinsky
* Copyright (c) 2024 Lepidus Tecnologia
* Copyright (c) 2024 Thoth
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class ThothSettingsForm
*
* @ingroup plugins_generic_thoth
*
* @brief Form for managers to modify Thoth plugin settings
*/
use APP\plugins\generic\thoth\classes\APIKeyEncryption;
use PKP\form\Form;
use PKP\form\validation\FormValidatorCSRF;
use PKP\form\validation\FormValidatorCustom;
use PKP\form\validation\FormValidatorPost;
import('plugins.generic.thoth.lib.thothAPI.ThothClient');
class ThothSettingsForm extends Form
{
private $contextId;
private $plugin;
private const SETTINGS = [
'email',
'password',
'testEnvironment',
];
public function __construct($plugin, $contextId)
{
$this->contextId = $contextId;
$this->plugin = $plugin;
$template = APIKeyEncryption::secretConfigExists() ? 'settingsForm.tpl' : 'tokenError.tpl';
parent::__construct($plugin->getTemplateResource($template));
$form = $this;
$this->addCheck(new FormValidatorCustom(
$this,
'password',
'required',
'plugins.generic.thoth.settings.invalidCredentials',
function ($password) use ($form) {
$email = trim($this->getData('email'));
$testEnvironment = $this->getData('testEnvironment');
$thothClient = new ThothClient($testEnvironment);
try {
$thothClient->login(
$email,
$password
);
} catch (ThothException $e) {
return false;
}
return true;
}
));
$this->addCheck(new FormValidatorPost($this));
$this->addCheck(new FormValidatorCSRF($this));
}
public function initData()
{
foreach (self::SETTINGS as $setting) {
if ($setting == 'password') {
$password = $this->plugin->getSetting($this->contextId, $setting);
$this->_data[$setting] = (APIKeyEncryption::secretConfigExists() && $password) ?
APIKeyEncryption::decryptString($password) :
null;
continue;
}
$this->_data[$setting] = $this->plugin->getSetting($this->contextId, $setting);
}
}
public function readInputData()
{
$this->readUserVars(self::SETTINGS);
}
public function fetch($request, $template = null, $display = false)
{
$templateMgr = TemplateManager::getManager($request);
$templateMgr->assign('pluginName', $this->plugin->getName());
return parent::fetch($request, $template, $display);
}
public function execute(...$functionArgs)
{
foreach (self::SETTINGS as $setting) {
if ($setting == 'password') {
$encryptedPassword = APIKeyEncryption::encryptString(trim($this->getData($setting)));
$this->plugin->updateSetting($this->contextId, $setting, $encryptedPassword, 'string');
continue;
}
$this->plugin->updateSetting($this->contextId, $setting, trim($this->getData($setting)), 'string');
}
parent::execute(...$functionArgs);
}
}