forked from thoth-pub/thoth-omp-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThothSettingsForm.php
More file actions
154 lines (134 loc) · 4.59 KB
/
ThothSettingsForm.php
File metadata and controls
154 lines (134 loc) · 4.59 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
<?php
/**
* @file plugins/generic/thoth/ThothSettingsForm.php
*
* Copyright (c) 2014-2020 Simon Fraser University
* Copyright (c) 2003-2020 John Willinsky
* Copyright (c) 2024-2026 Lepidus Tecnologia
* Copyright (c) 2024-2026 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
*/
namespace APP\plugins\generic\thoth;
use APP\template\TemplateManager;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Support\Facades\Crypt;
use PKP\form\Form;
use PKP\form\validation\FormValidatorCSRF;
use PKP\form\validation\FormValidatorCustom;
use PKP\form\validation\FormValidatorPost;
use PKP\form\validation\FormValidatorUrl;
use ThothApi\Exception\QueryException;
use ThothApi\GraphQL\Client;
require_once(dirname(__FILE__) . '/vendor/autoload.php');
class ThothSettingsForm extends Form
{
private const SETTINGS = [
'email',
'password',
'customThothApi',
'customThothApiUrl',
];
public function __construct(
private ThothPlugin $plugin,
private int $contextId
) {
parent::__construct($plugin->getTemplateResource('settingsForm.tpl'));
$this->addCheck(new FormValidatorPost($this));
$this->addCheck(new FormValidatorCSRF($this));
$this->addCheck(new FormValidatorCustom(
$this,
'customThothApiUrl',
'required',
'plugins.generic.thoth.settings.customThothApiUrl.required',
function ($customThothApiUrl) {
if (!$this->getData('customThothApi')) {
return true;
}
return !empty(trim($customThothApiUrl));
}
));
$this->addCheck(new FormValidatorUrl(
$this,
'customThothApiUrl',
'optional',
'plugins.generic.thoth.settings.customThothApiUrl.invalid'
));
$this->addCheck(new FormValidatorCustom(
$this,
'customThothApiUrl',
'optional',
'plugins.generic.thoth.settings.customThothApiUrl.unreachable',
function ($customThothApiUrl) {
if (!$this->getData('customThothApi')) {
return true;
}
return $this->validateCustomThothApiUrl(trim($customThothApiUrl));
}
));
$this->addCheck(new FormValidatorCustom(
$this,
'password',
'required',
'plugins.generic.thoth.settings.invalidCredentials',
fn ($password) => $this->validateCredentials(trim($this->getData('email')), $password)
));
}
public function initData(): void
{
foreach (self::SETTINGS as $setting) {
$value = $this->plugin->getSetting($this->contextId, $setting);
$this->setData(
$setting,
$setting === 'password' && $value ? Crypt::decrypt($value) : $value
);
}
}
public function readInputData(): void
{
$this->readUserVars(self::SETTINGS);
}
public function fetch($request, $template = null, $display = false): string
{
$templateMgr = TemplateManager::getManager($request);
$templateMgr->assign('pluginName', $this->plugin->getName());
return parent::fetch($request, $template, $display);
}
public function execute(...$functionArgs): void
{
$this->setData('password', Crypt::encrypt($this->getData('password')));
foreach (self::SETTINGS as $setting) {
$this->plugin->updateSetting($this->contextId, $setting, trim($this->getData($setting)), 'string');
}
parent::execute(...$functionArgs);
}
private function validateCredentials(string $email, string $password): bool
{
$httpConfig = [];
if ($this->getData('customThothApi') && $this->getData('customThothApiUrl')) {
$httpConfig['base_uri'] = trim($this->getData('customThothApiUrl'));
}
try {
(new Client($httpConfig))->login($email, $password);
return true;
} catch (QueryException) {
return false;
} catch (GuzzleException) {
return false;
}
}
private function validateCustomThothApiUrl(string $customThothApiUrl): bool
{
try {
(new Client(['base_uri' => $customThothApiUrl]))->publisherCount();
return true;
} catch (GuzzleException) {
return false;
}
}
}