Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 70 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 1 |
| tao_actions_WebHooks | |
0.00% |
0 / 70 |
|
0.00% |
0 / 8 |
420 | |
0.00% |
0 / 1 |
| addInstanceForm | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| authTpl | |
0.00% |
0 / 33 |
|
0.00% |
0 / 1 |
30 | |||
| editInstance | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getClassService | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| saveInstance | |
0.00% |
0 / 29 |
|
0.00% |
0 / 1 |
90 | |||
| getWebhookAuthService | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getWebhookEventsService | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getWebhookClassService | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2023 (original work) Open Assessment Technologies SA; |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | use oat\tao\model\auth\AbstractAuthType; |
| 24 | use oat\tao\model\webhooks\WebhookAuthService; |
| 25 | use oat\tao\model\webhooks\WebHookClassService; |
| 26 | use oat\tao\model\webhooks\WebhookEventsService; |
| 27 | use Psr\Container\ContainerExceptionInterface; |
| 28 | use Psr\Container\NotFoundExceptionInterface; |
| 29 | |
| 30 | class tao_actions_WebHooks extends tao_actions_SaSModule |
| 31 | { |
| 32 | /** |
| 33 | * @throws ContainerExceptionInterface |
| 34 | * @throws NotFoundExceptionInterface |
| 35 | * @throws common_Exception |
| 36 | * @throws tao_models_classes_dataBinding_GenerisFormDataBindingException |
| 37 | */ |
| 38 | public function addInstanceForm(): void |
| 39 | { |
| 40 | $this->setData('formTitle', __('Create instance')); |
| 41 | $this->saveInstance(); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * @throws core_kernel_persistence_Exception |
| 46 | * @throws NotFoundExceptionInterface |
| 47 | * @throws ContainerExceptionInterface |
| 48 | * @throws tao_models_classes_MissingRequestParameterException |
| 49 | * @throws common_Exception |
| 50 | */ |
| 51 | public function authTpl(): void |
| 52 | { |
| 53 | $webhookAuthService = $this->getWebhookAuthService(); |
| 54 | |
| 55 | /** @var AbstractAuthType $authType */ |
| 56 | $authType = null; |
| 57 | $instance = null; |
| 58 | |
| 59 | $parsedBody = $this->getPsrRequest()->getParsedBody(); |
| 60 | |
| 61 | if ($parsedBody['uri']) { |
| 62 | $instance = $this->getCurrentInstance(); |
| 63 | $authType = $webhookAuthService->getAuthType( |
| 64 | $instance->getOnePropertyValue($this->getProperty(WebHookClassService::PROPERTY_AUTH_TYPE)) |
| 65 | ); |
| 66 | |
| 67 | $authType->setInstance($instance); |
| 68 | } else { |
| 69 | $authType = $webhookAuthService->getAuthType(); |
| 70 | } |
| 71 | |
| 72 | $webhookEventsService = $this->getWebhookEventsService(); |
| 73 | |
| 74 | $setEvents = $instance |
| 75 | ? $instance->getPropertyValues($this->getProperty(WebHookClassService::PROPERTY_WEBHOOK_EVENT)) |
| 76 | : []; |
| 77 | |
| 78 | $events = []; |
| 79 | foreach ($webhookEventsService->getRegisteredEvents() as $eventClass => $enabled) { |
| 80 | if ($enabled) { |
| 81 | $eventLabel = substr($eventClass, strrpos($eventClass, '\\') + 1); |
| 82 | $eventLabel = trim(preg_replace('/[A-Z]/', ' $0', $eventLabel)); |
| 83 | |
| 84 | $events[$eventClass] = [ |
| 85 | 'label' => $eventLabel, |
| 86 | 'set' => in_array($eventClass, $setEvents, true) |
| 87 | ]; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | $this->setData('events', $events); |
| 92 | $this->setData('authType', $authType); |
| 93 | $this->setData('allowedTypes', $webhookAuthService->getTypes()); |
| 94 | $this->setView('auth/form.tpl'); |
| 95 | |
| 96 | $this->returnJson([ |
| 97 | 'data' => $this->getRenderer()->render(), |
| 98 | 'success' => true, |
| 99 | ]); |
| 100 | |
| 101 | // prevent further render |
| 102 | $this->renderer = null; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * @throws ContainerExceptionInterface |
| 107 | * @throws NotFoundExceptionInterface |
| 108 | * @throws common_Exception |
| 109 | * @throws tao_models_classes_MissingRequestParameterException |
| 110 | * @throws tao_models_classes_dataBinding_GenerisFormDataBindingException |
| 111 | */ |
| 112 | public function editInstance(): void |
| 113 | { |
| 114 | $this->setData('formTitle', __('Edit Instance')); |
| 115 | $this->saveInstance($this->getCurrentInstance()); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * @throws ContainerExceptionInterface |
| 120 | * @throws NotFoundExceptionInterface |
| 121 | */ |
| 122 | public function getClassService(): WebHookClassService |
| 123 | { |
| 124 | return $this->getPsrContainer()->get(WebHookClassService::class); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * @param null $instance |
| 129 | * |
| 130 | * @return void |
| 131 | * @throws ContainerExceptionInterface |
| 132 | * @throws NotFoundExceptionInterface |
| 133 | * @throws common_Exception |
| 134 | * @throws tao_models_classes_dataBinding_GenerisFormDataBindingException |
| 135 | */ |
| 136 | public function saveInstance($instance = null): void |
| 137 | { |
| 138 | if (!tao_helpers_Request::isAjax()) { |
| 139 | throw new InvalidArgumentException('wrong request mode'); |
| 140 | } |
| 141 | |
| 142 | $clazz = $this->getCurrentClass(); |
| 143 | $myFormContainer = new tao_actions_form_Instance($clazz, $instance); |
| 144 | |
| 145 | $myForm = $myFormContainer->getForm(); |
| 146 | |
| 147 | if ($myForm && $myForm->isSubmited() && $myForm->isValid()) { |
| 148 | $webhookAuthService = $this->getWebhookAuthService(); |
| 149 | $values = $myForm->getValues(); |
| 150 | |
| 151 | $parsedBody = $this->getPsrRequest()->getParsedBody(); |
| 152 | |
| 153 | $authType = $webhookAuthService->getAuthType( |
| 154 | $this->getResource( |
| 155 | $parsedBody[tao_helpers_Uri::encode(WebHookClassService::PROPERTY_AUTH_TYPE)] |
| 156 | ) |
| 157 | ); |
| 158 | |
| 159 | // according to the auth type we need to add properties for the authenticator |
| 160 | $values[WebHookClassService::PROPERTY_AUTH_TYPE] = $authType->getAuthClass()->getUri(); |
| 161 | foreach ($authType->getAuthProperties() as $authProperty) { |
| 162 | $propertyUri = $authProperty->getUri(); |
| 163 | $values[$propertyUri] = $parsedBody[tao_helpers_Uri::encode($propertyUri)]; |
| 164 | } |
| 165 | |
| 166 | if ($parsedBody['events']) { |
| 167 | $values[WebHookClassService::PROPERTY_WEBHOOK_EVENT] = []; |
| 168 | foreach ($parsedBody['events'] as $eventClass => $eventLabel) { |
| 169 | $values[WebHookClassService::PROPERTY_WEBHOOK_EVENT][] = $eventClass; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | try { |
| 174 | $this->getWebhookClassService()->saveWebhookInstance($values, $instance); |
| 175 | |
| 176 | $this->setData('message', __('Instance saved')); |
| 177 | } catch (Exception $e) { |
| 178 | $this->setData('message', __('Undefined Instance can not be saved')); |
| 179 | } |
| 180 | $this->setData('reload', true); |
| 181 | } |
| 182 | |
| 183 | $this->setData('myForm', $myForm->render()); |
| 184 | $this->setView('form.tpl', 'tao'); |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * @throws ContainerExceptionInterface |
| 189 | * @throws NotFoundExceptionInterface |
| 190 | */ |
| 191 | private function getWebhookAuthService(): WebhookAuthService |
| 192 | { |
| 193 | return $this->getPsrContainer()->get(WebhookAuthService::class); |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * @throws ContainerExceptionInterface |
| 198 | * @throws NotFoundExceptionInterface |
| 199 | */ |
| 200 | private function getWebhookEventsService(): WebhookEventsService |
| 201 | { |
| 202 | return $this->getPsrContainer()->get(WebhookEventsService::class); |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * @throws ContainerExceptionInterface |
| 207 | * @throws NotFoundExceptionInterface |
| 208 | */ |
| 209 | private function getWebhookClassService(): WebHookClassService |
| 210 | { |
| 211 | return $this->getPsrContainer()->get(WebHookClassService::class); |
| 212 | } |
| 213 | } |