Skip to content

Commit

Permalink
Use ResultableEvent to handle boolean and null results
Browse files Browse the repository at this point in the history
  • Loading branch information
glensc committed May 6, 2020
1 parent c5e2042 commit 795ae78
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 20 deletions.
5 changes: 1 addition & 4 deletions lib/eventum/class.access.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,7 @@ public static function canAccessIssue($issue_id, $usr_id, $log = true)
$return = true;
}

$workflow = Workflow::canAccessIssue($prj_id, $issue_id, $usr_id);
if ($workflow !== null) {
$return = $workflow;
}
$return = Workflow::canAccessIssue($prj_id, $issue_id, $usr_id, $return);

$access[$issue_id . '-' . $usr_id] = $return;

Expand Down
19 changes: 8 additions & 11 deletions lib/eventum/class.workflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -974,25 +974,22 @@ public static function getAccessLevels($prj_id)
}

/**
* Performs additional checks on if a user can access an issue.
* Returns true if a user can access an issue.
*
* @param $prj_id
* @param $issue_id
* @param $usr_id
* @return mixed null to use default rules, true or false otherwise
* @deprecated since 3.8.11 use ACCESS_ISSUE event
*/
public static function canAccessIssue($prj_id, $issue_id, $usr_id)
public static function canAccessIssue(int $prj_id, int $issue_id, int $usr_id, bool $return): bool
{
$arguments = [
'prj_id' => (int)$prj_id,
'issue_id' => (int)$issue_id,
'usr_id' => (int)$usr_id,
'prj_id' => $prj_id,
'issue_id' => $issue_id,
'usr_id' => $usr_id,
];
$event = new GenericEvent(null, $arguments);
$event = new ResultableEvent(null, $arguments);
$event->setResult($return);
EventManager::dispatch(SystemEvents::ACCESS_ISSUE, $event);

return !$event->isPropagationStopped();
return $event->getResult();
}

/**
Expand Down
10 changes: 5 additions & 5 deletions src/Extension/Legacy/WorkflowLegacyExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@

namespace Eventum\Extension\Legacy;

use Eventum\Event\ResultableEvent;
use Eventum\Event\SystemEvents;
use Eventum\Extension\Provider;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
use Workflow;

/**
Expand All @@ -42,15 +42,15 @@ public static function getSubscribedEvents(): array
/**
* @see Workflow::canAccessIssue
*/
public function canAccessIssue(GenericEvent $event): void
public function canAccessIssue(ResultableEvent $event): void
{
if (!$backend = Workflow::getBackend($event['prj_id'])) {
return;
}

$canAccess = $backend->canAccessIssue($event['prj_id'], $event['issue_id'], $event['usr_id']);
if ($canAccess === false) {
$event->stopPropagation();
$result = $backend->canAccessIssue($event['prj_id'], $event['issue_id'], $event['usr_id']);
if ($result !== null) {
$event->setResult($result);
}
}
}

0 comments on commit 795ae78

Please sign in to comment.