Skip to content

Commit f1d7635

Browse files
authored
Restrict webhooks by itemtype (#21989)
1 parent 7ede2ec commit f1d7635

File tree

2 files changed

+55
-9
lines changed

2 files changed

+55
-9
lines changed

src/Webhook.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,18 @@ public static function canDelete(): bool
117117
return static::canUpdate();
118118
}
119119

120+
public function canCreateItem(): bool
121+
{
122+
$itemtype = $this->fields['itemtype'];
123+
return empty($itemtype) || (is_subclass_of($itemtype, CommonGLPI::class) && $itemtype::canView());
124+
}
125+
126+
public function canUpdateItem(): bool
127+
{
128+
$itemtype = $this->fields['itemtype'];
129+
return empty($itemtype) || (is_subclass_of($itemtype, CommonGLPI::class) && $itemtype::canView());
130+
}
131+
120132
public function defineTabs($options = [])
121133
{
122134
$parent_tabs = parent::defineTabs();
@@ -457,7 +469,9 @@ public static function getItemtypesDropdownValues(): array
457469
// Move leaf values to the keys and make the value the ::getTypeName
458470
foreach ($values as $category => $itemtypes) {
459471
foreach ($itemtypes as $i => $itemtype) {
460-
$values[$category][$itemtype] = $itemtype::getTypeName(1);
472+
if ($itemtype::canView()) {
473+
$values[$category][$itemtype] = $itemtype::getTypeName(1);
474+
}
461475
unset($values[$category][$i]);
462476
}
463477
}

tests/functional/WebhookTest.php

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
use Glpi\Api\HL\Controller\AbstractController;
3838
use Glpi\Search\SearchOption;
3939
use Psr\Log\LogLevel;
40+
use Webhook;
4041

4142
class WebhookTest extends \DbTestCase
4243
{
@@ -46,7 +47,7 @@ class WebhookTest extends \DbTestCase
4647
*/
4748
public function testWebhookTypesHaveIDOpt()
4849
{
49-
$supported = \Webhook::getItemtypesDropdownValues();
50+
$supported = Webhook::getItemtypesDropdownValues();
5051
$itemtypes = [];
5152
foreach ($supported as $types) {
5253
$itemtypes = array_merge($itemtypes, array_keys($types));
@@ -228,7 +229,7 @@ public function testWebhookHeaderTemplate()
228229
public function testGetResultForPath()
229230
{
230231
$this->login();
231-
/** @var \Webhook $webhook */
232+
/** @var Webhook $webhook */
232233
$webhook = $this->createItem('Webhook', [
233234
'name' => 'Test webhook',
234235
'entities_id' => $_SESSION['glpiactive_entity'],
@@ -249,7 +250,7 @@ public function testGetAPIItemtypeData()
249250
$this->login();
250251
$this->initAssetDefinition();
251252

252-
$supported_types = \Webhook::getAPIItemtypeData();
253+
$supported_types = Webhook::getAPIItemtypeData();
253254
foreach ($supported_types as $controller => $type_data) {
254255
$this->assertTrue(is_subclass_of($controller, AbstractController::class));
255256
foreach ($type_data as $category => $types) {
@@ -266,7 +267,7 @@ public function testGetAPIPath()
266267
{
267268
$this->login();
268269

269-
$webhook = new \Webhook();
270+
$webhook = new Webhook();
270271
$computer = getItemByTypeName('Computer', '_test_pc01');
271272
$this->assertEquals('/Assets/Computer/' . $computer->getID(), $webhook->getAPIPath($computer));
272273

@@ -283,7 +284,7 @@ public function testWithHLAPIDisabled(): void
283284
global $CFG_GLPI;
284285
$this->login();
285286
$CFG_GLPI['enable_hlapi'] = 0;
286-
/** @var \Webhook $webhook */
287+
/** @var Webhook $webhook */
287288
$webhook = $this->createItem('Webhook', [
288289
'name' => 'Test webhook',
289290
'entities_id' => $_SESSION['glpiactive_entity'],
@@ -301,12 +302,12 @@ public function testWithHLAPIDisabled(): void
301302

302303
public function testGetMonacoSuggestions()
303304
{
304-
$itemtypes = \Webhook::getItemtypesDropdownValues();
305+
$itemtypes = Webhook::getItemtypesDropdownValues();
305306

306307
foreach ($itemtypes as $types) {
307308
$this->assertIsArray($types);
308309
foreach ($types as $itemtype => $label) {
309-
$suggestions = \Webhook::getMonacoSuggestions($itemtype);
310+
$suggestions = Webhook::getMonacoSuggestions($itemtype);
310311
$this->assertNotEmpty($suggestions, "Missing suggestions for $itemtype");
311312
}
312313
}
@@ -316,7 +317,7 @@ public function testWebhookNotBlocker(): void
316317
{
317318
global $DB;
318319

319-
$this->createItem(\Webhook::class, [
320+
$this->createItem(Webhook::class, [
320321
'name' => 'Test webhook',
321322
'entities_id' => $_SESSION['glpiactive_entity'],
322323
'url' => 'http://localhost',
@@ -360,4 +361,35 @@ public function testWebhookNotBlocker(): void
360361
]
361362
);
362363
}
364+
365+
public function testItemtypeDropdownExcludesNoReadItemtypes()
366+
{
367+
$this->login();
368+
$this->assertContains('Computer', Webhook::getItemtypesDropdownValues()['Assets']);
369+
$this->assertContains('Monitor', Webhook::getItemtypesDropdownValues()['Assets']);
370+
$_SESSION['glpiactiveprofile']['computer'] = ALLSTANDARDRIGHT & ~READ;
371+
$this->assertNotContains('Computer', Webhook::getItemtypesDropdownValues()['Assets']);
372+
$this->assertContains('Monitor', Webhook::getItemtypesDropdownValues()['Assets']);
373+
}
374+
375+
public function testCreateUpdateNoReadItemtypes()
376+
{
377+
$this->login();
378+
$webhook = $this->createItem('Webhook', [
379+
'name' => 'Test webhook',
380+
'entities_id' => $_SESSION['glpiactive_entity'],
381+
'url' => 'http://localhost',
382+
'itemtype' => 'Computer',
383+
'event' => 'new',
384+
'is_active' => 1,
385+
'use_default_payload' => 1,
386+
]);
387+
$this->assertTrue($webhook->canUpdateItem());
388+
$_SESSION['glpiactiveprofile']['computer'] = ALLSTANDARDRIGHT & ~READ;
389+
$this->assertFalse($webhook->canUpdateItem());
390+
391+
$this->assertFalse($webhook->canCreateItem());
392+
$webhook->fields['itemtype'] = 'Monitor';
393+
$this->assertTrue($webhook->canCreateItem());
394+
}
363395
}

0 commit comments

Comments
 (0)