-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebhookAPIHandler.inc.php
114 lines (93 loc) · 3.38 KB
/
WebhookAPIHandler.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
<?php
import('lib.pkp.classes.handler.APIHandler');
import('lib.pkp.classes.security.authorization.ContextAccessPolicy');
import('lib.pkp.classes.security.authorization.RoleBasedHandlerOperationPolicy');
import('lib.pkp.classes.security.authorization.PolicySet');
use Slim\Http\Request;
use Slim\Http\Response;
class WebhookAPIHandler extends APIHandler
{
/**
* @var WebhookPlugin
*/
protected $plugin;
public function __construct()
{
$this->plugin = PluginRegistry::getPlugin('generic', 'webhookplugin');
$this->_handlerPath = 'webhook';
$this->_endpoints = array(
'POST' => array(
array(
'pattern' => $this->getEndpointPattern(),
'handler' => array($this, 'handleWebhook'),
'roles' => [ROLE_ID_SITE_ADMIN, ROLE_ID_MANAGER],
),
),
);
parent::__construct();
}
/**
* @copydoc APIHandler::authorize
*/
function authorize(\Request $request, &$args, $roleAssignments)
{
error_log("HHohashtasht");
import('lib.pkp.classes.security.authorization.PolicySet');
$rolePolicy = new PolicySet(COMBINING_PERMIT_OVERRIDES);
import('lib.pkp.classes.security.authorization.RoleBasedHandlerOperationPolicy');
foreach ($roleAssignments as $role => $operations) {
$rolePolicy->addPolicy(new RoleBasedHandlerOperationPolicy($request, $role, $operations));
}
$this->addPolicy($rolePolicy);
return parent::authorize($request, $args, $roleAssignments);
}
/**
* @param $slimRequest Request Slim request object
* @param $response Response object
* @param array $args arguments
*/
public function handleWebhook(Request $slimRequest, Response $response, $args)
{
// Check if the user has the required permissions
$event = $slimRequest->getParam('event');
$data = $slimRequest->getParam('data');
$urls = $slimRequest->getParam('urls');
error_log($event . json_encode($data) . json_encode($urls));
if (!$event || !$data) {
return $response->withJson(
[
'success' => false,
'message' => 'The event and data parameters are required.',
],
400
);
}
if ($urls && !is_array($urls)) {
$urls = [$urls];
}
if (!$urls || empty($urls)) {
error_log("shouldnt be here");
$registeredWebhooks = $this->plugin->getRegisteredWebhooks();
// filter out the urls which have the passed event disabled
$urls = array_filter(
$registeredWebhooks,
function ($webhook) use ($event) {
return !$webhook['disabled'];
}
);
}
if (empty($urls)) {
return $response->withJson(
[
'success' => false,
'message' => 'No webhook URLs found.',
],
400
);
}
// Call the fireWebhook method from the plugin
$result = $this->plugin->fireWebhook($event, $data, $urls);
// Return the result as a JSON response
return $response->withJSON(['success' => true, 'message' => $result], 200);
}
}