From 87f5effe4996e8d742433c488769b05197a20d67 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Wed, 27 Oct 2021 00:12:50 -0700 Subject: [PATCH] CRM_Queue_Task - Track an optional `$runAs` property --- CRM/Queue/Task.php | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/CRM/Queue/Task.php b/CRM/Queue/Task.php index ab8fa06b24..da73d76379 100644 --- a/CRM/Queue/Task.php +++ b/CRM/Queue/Task.php @@ -41,6 +41,16 @@ class CRM_Queue_Task { */ public $title; + /** + * @var array|null + * serializable + * + * If specified, it may include these keys: + * - contactId: int|null + * - domainId: int|null + */ + public $runAs; + /** * @param mixed $callback * Serializable, a callable PHP item; must accept at least one argument @@ -69,13 +79,29 @@ class CRM_Queue_Task { $args = $this->arguments; array_unshift($args, $taskCtx); + if ($this->runAs !== NULL) { + $equals = function($a, $b) { + return $a === $b || (is_numeric($a) && is_numeric($b) && $a == $b); + }; + if (array_key_exists('contactId', $this->runAs) && !$equals(CRM_Core_Session::getLoggedInContactID(), $this->runAs['contactId'])) { + throw new Exception(sprintf('Cannot execute queue task. Unexpected contact "%s" for job "%s"', CRM_Core_Session::getLoggedInContactID(), $this->getSummary())); + } + if (array_key_exists('domainId', $this->runAs) && !$equals(CRM_Core_BAO_Domain::getDomain()->id, $this->runAs['domainId'])) { + throw new Exception(sprintf('Cannot execute queue task. Unexpected domain "%s" for job "%s"', CRM_Core_BAO_Domain::getDomain()->id, $this->getSummary())); + } + } + if (is_callable($this->callback)) { $result = call_user_func_array($this->callback, $args); return $result; } else { - throw new Exception('Failed to call callback: ' . print_r($this->callback)); + throw new Exception('Failed to call callback: ' . $this->getSummary()); } } + private function getSummary(): string { + return json_encode(['title' => $this->title, 'runAs' => $this->runAs, 'callback' => $this->callback], JSON_UNESCAPED_SLASHES); + } + } -- 2.25.1