Skip to content

Commit 3ae3089

Browse files
CarlSchwanbackportbot[bot]
authored andcommitted
refactor(workflowengine): Check if class is correct
Signed-off-by: Carl Schwan <[email protected]> [skip ci]
1 parent c3b5417 commit 3ae3089

File tree

2 files changed

+45
-26
lines changed

2 files changed

+45
-26
lines changed

apps/workflowengine/lib/Manager.php

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -423,10 +423,6 @@ protected function validateEvents(string $entity, array $events, IOperation $ope
423423
throw new \UnexpectedValueException($this->l->t('Entity %s does not exist', [$entity]));
424424
}
425425

426-
if (!$instance instanceof IEntity) {
427-
throw new \UnexpectedValueException($this->l->t('Entity %s is invalid', [$entity]));
428-
}
429-
430426
if (empty($events)) {
431427
if (!$operation instanceof IComplexOperation) {
432428
throw new \UnexpectedValueException($this->l->t('No events are chosen.'));
@@ -464,10 +460,6 @@ public function validateOperation($class, $name, array $checks, $operation, Scop
464460
throw new \UnexpectedValueException($this->l->t('Operation %s does not exist', [$class]));
465461
}
466462

467-
if (!($instance instanceof IOperation)) {
468-
throw new \UnexpectedValueException($this->l->t('Operation %s is invalid', [$class]));
469-
}
470-
471463
if (!$instance->isAvailableForScope($scope->getScope())) {
472464
throw new \UnexpectedValueException($this->l->t('Operation %s is invalid', [$class]));
473465
}
@@ -489,27 +481,28 @@ public function validateOperation($class, $name, array $checks, $operation, Scop
489481
throw new \UnexpectedValueException($this->l->t('Invalid check provided'));
490482
}
491483

484+
if (strlen((string)$check['value']) > IManager::MAX_CHECK_VALUE_BYTES) {
485+
throw new \UnexpectedValueException($this->l->t('The provided check value is too long'));
486+
}
487+
488+
$reflection = new \ReflectionClass($check['class']);
489+
if ($check['class'] !== ICheck::class && !in_array(ICheck::class, $reflection->getInterfaceNames())) {
490+
throw new \UnexpectedValueException($this->l->t('Check %s is invalid', [$class]));
491+
}
492+
492493
try {
493494
/** @var ICheck $instance */
494495
$instance = $this->container->query($check['class']);
495496
} catch (QueryException $e) {
496497
throw new \UnexpectedValueException($this->l->t('Check %s does not exist', [$class]));
497498
}
498499

499-
if (!($instance instanceof ICheck)) {
500-
throw new \UnexpectedValueException($this->l->t('Check %s is invalid', [$class]));
501-
}
502-
503500
if (!empty($instance->supportedEntities())
504501
&& !in_array($entity, $instance->supportedEntities())
505502
) {
506503
throw new \UnexpectedValueException($this->l->t('Check %s is not allowed with this entity', [$class]));
507504
}
508505

509-
if (strlen((string)$check['value']) > IManager::MAX_CHECK_VALUE_BYTES) {
510-
throw new \UnexpectedValueException($this->l->t('The provided check value is too long'));
511-
}
512-
513506
$instance->validateCheck($check['operator'], $check['value']);
514507
}
515508
}

apps/workflowengine/tests/ManagerTest.php

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,41 @@
3131
use OCP\WorkflowEngine\IEntityEvent;
3232
use OCP\WorkflowEngine\IManager;
3333
use OCP\WorkflowEngine\IOperation;
34+
use OCP\WorkflowEngine\IRuleMatcher;
3435
use PHPUnit\Framework\MockObject\MockObject;
3536
use Psr\Log\LoggerInterface;
3637
use Test\TestCase;
3738

39+
class TestAdminOp implements IOperation {
40+
public function getDisplayName(): string {
41+
return 'Admin';
42+
}
43+
44+
public function getDescription(): string {
45+
return '';
46+
}
47+
48+
public function getIcon(): string {
49+
return '';
50+
}
51+
52+
public function isAvailableForScope(int $scope): bool {
53+
return true;
54+
}
55+
56+
public function validateOperation(string $name, array $checks, string $operation): void {
57+
}
58+
59+
public function onEvent(string $eventName, Event $event, IRuleMatcher $ruleMatcher): void {
60+
}
61+
}
62+
63+
class TestUserOp extends TestAdminOp {
64+
public function getDisplayName(): string {
65+
return 'User';
66+
}
67+
}
68+
3869
/**
3970
* Class ManagerTest
4071
*
@@ -405,19 +436,19 @@ public function testUpdateOperation(): void {
405436
$opId1 = $this->invokePrivate(
406437
$this->manager,
407438
'insertOperation',
408-
['OCA\WFE\TestAdminOp', 'Test01', [11, 22], 'foo', $entity, []]
439+
[TestAdminOp::class, 'Test01', [11, 22], 'foo', $entity, []]
409440
);
410441
$this->invokePrivate($this->manager, 'addScope', [$opId1, $adminScope]);
411442

412443
$opId2 = $this->invokePrivate(
413444
$this->manager,
414445
'insertOperation',
415-
['OCA\WFE\TestUserOp', 'Test02', [33, 22], 'bar', $entity, []]
446+
[TestUserOp::class, 'Test02', [33, 22], 'bar', $entity, []]
416447
);
417448
$this->invokePrivate($this->manager, 'addScope', [$opId2, $userScope]);
418449

419-
$check1 = ['class' => 'OCA\WFE\C22', 'operator' => 'eq', 'value' => 'asdf'];
420-
$check2 = ['class' => 'OCA\WFE\C33', 'operator' => 'eq', 'value' => 23456];
450+
$check1 = ['class' => ICheck::class, 'operator' => 'eq', 'value' => 'asdf'];
451+
$check2 = ['class' => ICheck::class, 'operator' => 'eq', 'value' => 23456];
421452

422453
/** @noinspection PhpUnhandledExceptionInspection */
423454
$op = $this->manager->updateOperation($opId1, 'Test01a', [$check1, $check2], 'foohur', $adminScope, $entity, ['\OCP\Files::postDelete']);
@@ -680,11 +711,6 @@ public function testValidateOperationDataLengthError(): void {
680711
->method('getScope')
681712
->willReturn(IManager::SCOPE_ADMIN);
682713

683-
$operationMock->expects($this->once())
684-
->method('isAvailableForScope')
685-
->with(IManager::SCOPE_ADMIN)
686-
->willReturn(true);
687-
688714
$operationMock->expects($this->never())
689715
->method('validateOperation');
690716

@@ -732,7 +758,7 @@ public function testValidateOperationScopeNotAvailable(): void {
732758
'operator' => 'is',
733759
'value' => 'barfoo',
734760
];
735-
$operationData = str_pad('', IManager::MAX_OPERATION_VALUE_BYTES + 1, 'FooBar');
761+
$operationData = str_pad('', IManager::MAX_OPERATION_VALUE_BYTES - 1, 'FooBar');
736762

737763
$operationMock = $this->createMock(IOperation::class);
738764
$entityMock = $this->createMock(IEntity::class);

0 commit comments

Comments
 (0)