Skip to content

Commit 6cd597b

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

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
@@ -424,10 +424,6 @@ protected function validateEvents(string $entity, array $events, IOperation $ope
424424
throw new \UnexpectedValueException($this->l->t('Entity %s does not exist', [$entity]));
425425
}
426426

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

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

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

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

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

apps/workflowengine/tests/ManagerTest.php

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,41 @@
3333
use OCP\WorkflowEngine\IEntityEvent;
3434
use OCP\WorkflowEngine\IManager;
3535
use OCP\WorkflowEngine\IOperation;
36+
use OCP\WorkflowEngine\IRuleMatcher;
3637
use PHPUnit\Framework\MockObject\MockObject;
3738
use Psr\Log\LoggerInterface;
3839
use Test\TestCase;
3940

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

414445
$opId2 = $this->invokePrivate(
415446
$this->manager,
416447
'insertOperation',
417-
['OCA\WFE\TestUserOp', 'Test02', [33, 22], 'bar', $entity, []]
448+
[TestUserOp::class, 'Test02', [33, 22], 'bar', $entity, []]
418449
);
419450
$this->invokePrivate($this->manager, 'addScope', [$opId2, $userScope]);
420451

421-
$check1 = ['class' => 'OCA\WFE\C22', 'operator' => 'eq', 'value' => 'asdf'];
422-
$check2 = ['class' => 'OCA\WFE\C33', 'operator' => 'eq', 'value' => 23456];
452+
$check1 = ['class' => ICheck::class, 'operator' => 'eq', 'value' => 'asdf'];
453+
$check2 = ['class' => ICheck::class, 'operator' => 'eq', 'value' => 23456];
423454

424455
/** @noinspection PhpUnhandledExceptionInspection */
425456
$op = $this->manager->updateOperation($opId1, 'Test01a', [$check1, $check2], 'foohur', $adminScope, $entity, ['\OCP\Files::postDelete']);
@@ -682,11 +713,6 @@ public function testValidateOperationDataLengthError(): void {
682713
->method('getScope')
683714
->willReturn(IManager::SCOPE_ADMIN);
684715

685-
$operationMock->expects($this->once())
686-
->method('isAvailableForScope')
687-
->with(IManager::SCOPE_ADMIN)
688-
->willReturn(true);
689-
690716
$operationMock->expects($this->never())
691717
->method('validateOperation');
692718

@@ -734,7 +760,7 @@ public function testValidateOperationScopeNotAvailable(): void {
734760
'operator' => 'is',
735761
'value' => 'barfoo',
736762
];
737-
$operationData = str_pad('', IManager::MAX_OPERATION_VALUE_BYTES + 1, 'FooBar');
763+
$operationData = str_pad('', IManager::MAX_OPERATION_VALUE_BYTES - 1, 'FooBar');
738764

739765
$operationMock = $this->createMock(IOperation::class);
740766
$entityMock = $this->createMock(IEntity::class);

0 commit comments

Comments
 (0)