-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebhookSettingsForm.php
More file actions
99 lines (86 loc) · 3.32 KB
/
Copy pathWebhookSettingsForm.php
File metadata and controls
99 lines (86 loc) · 3.32 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
<?php
/**
* @file WebhookSettingsForm.php
*
* @brief Form for journal managers to configure the publish webhook.
*/
namespace APP\plugins\generic\webhook;
use APP\core\Application;
use APP\notification\NotificationManager;
use PKP\core\PKPApplication;
use PKP\form\Form;
use PKP\form\validation\FormValidatorCSRF;
use PKP\form\validation\FormValidatorCustom;
use PKP\form\validation\FormValidatorPost;
use PKP\notification\Notification;
class WebhookSettingsForm extends Form
{
public WebhookPlugin $plugin;
public function __construct(WebhookPlugin $plugin)
{
parent::__construct($plugin->getTemplateResource('settings.tpl'));
$this->plugin = $plugin;
$this->addCheck(new FormValidatorPost($this));
$this->addCheck(new FormValidatorCSRF($this));
// FormValidatorUrl also applies jQuery Validation's client-side "url" rule, whose regex
// requires a dotted TLD and explicitly rejects private/loopback hosts (e.g. localhost,
// 127.0.0.1) - too strict for webhook endpoints used during local development.
// Type is "optional" (not "required") so managers can clear the URL again to reset the
// configuration; WebhookPlugin::hasValidWebhookConfig() treats an empty value as
// "not configured" and disables the plugin's "Enable" toggle accordingly.
$this->addCheck(new FormValidatorCustom(
$this,
'webhookUrl',
'optional',
'plugins.generic.webhook.settings.webhookUrl.invalid',
fn (string $value): bool => WebhookUrlValidator::isValid($value)
));
}
protected function getContextId(): int
{
return Application::get()->getRequest()->getContext()?->getId() ?? PKPApplication::CONTEXT_SITE;
}
/**
* @copydoc Form::initData
*/
public function initData()
{
$contextId = $this->getContextId();
$this->setData('webhookUrl', $this->plugin->getSetting($contextId, 'webhookUrl'));
$this->setData('webhookSecret', $this->plugin->getSetting($contextId, 'webhookSecret'));
parent::initData();
}
/**
* @copydoc Form::readInputData
*/
public function readInputData()
{
$this->readUserVars(['webhookUrl', 'webhookSecret']);
}
/**
* @copydoc Form::fetch
*/
public function fetch($request, $template = null, $display = false)
{
$templateMgr = \APP\template\TemplateManager::getManager($request);
$templateMgr->assign('pluginName', $this->plugin->getName());
return parent::fetch($request, $template, $display);
}
/**
* @copydoc Form::execute
*/
public function execute(...$functionArgs)
{
$contextId = $this->getContextId();
$this->plugin->updateSetting($contextId, 'webhookUrl', trim((string) $this->getData('webhookUrl')));
$this->plugin->updateSetting($contextId, 'webhookSecret', trim((string) $this->getData('webhookSecret')) ?: null);
$request = Application::get()->getRequest();
$notificationMgr = new NotificationManager();
$notificationMgr->createTrivialNotification(
$request->getUser()->getId(),
Notification::NOTIFICATION_TYPE_SUCCESS,
['contents' => __('common.changesSaved')]
);
return parent::execute(...$functionArgs);
}
}