From 674093427c8b223e926a34901ab530d2f2d5db09 Mon Sep 17 00:00:00 2001 From: Ioannis Igoumenos Date: Wed, 28 Oct 2026 14:35:54 +0000 Subject: [PATCH 1/2] Register Actor and RuleBuilder Event Listeners for command line Jobs (CFM-558) --- app/src/Command/JobCommand.php | 5 ++ app/src/Controller/AppController.php | 11 +-- app/src/Lib/Events/ActorEventListener.php | 46 +++++++---- app/src/Lib/Util/EventUtilities.php | 97 +++++++++++++++++++++++ 4 files changed, 136 insertions(+), 23 deletions(-) create mode 100644 app/src/Lib/Util/EventUtilities.php diff --git a/app/src/Command/JobCommand.php b/app/src/Command/JobCommand.php index 450214873..6829d00ae 100644 --- a/app/src/Command/JobCommand.php +++ b/app/src/Command/JobCommand.php @@ -38,6 +38,7 @@ use Cake\Utility\Security; use App\Lib\Enum\JobStatusEnum; use App\Lib\Events\CoIdEventListener; +use App\Lib\Util\EventUtilities; class JobCommand extends BaseCommand { @@ -113,6 +114,10 @@ public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionPar public function execute(Arguments $args, ConsoleIo $io) { + // AppController::initialize() is not called for command line tools, so we + // register the Actor and Rule Builder Event Listeners here (CFM-558) + EventUtilities::registerCoreListeners(actor: EventUtilities::cliActor()); + $CosTable = $this->getTableLocator()->get('Cos'); $JobTable = $this->getTableLocator()->get('Jobs'); diff --git a/app/src/Controller/AppController.php b/app/src/Controller/AppController.php index b69c8e0a0..45e3d16a2 100644 --- a/app/src/Controller/AppController.php +++ b/app/src/Controller/AppController.php @@ -33,9 +33,8 @@ use App\Lib\Enum\TAndCLoginModeEnum; use App\Lib\Enum\TAndCStatusEnum; use App\Lib\Enum\TemplateableStatusEnum; -use App\Lib\Events\ActorEventListener; use App\Lib\Events\CoIdEventListener; -use App\Lib\Events\RuleBuilderEventListener; +use App\Lib\Util\EventUtilities; use App\Lib\Util\StringUtilities; use Cake\Controller\Controller; use Cake\Datasource\Exception\RecordNotFoundException; @@ -97,11 +96,9 @@ public function initialize(): void { // Breadcrumb Manager $this->loadComponent('Breadcrumb'); - $ActorEventListener = new ActorEventListener($this->RegistryAuth); - EventManager::instance()->on($ActorEventListener); - - $RuleBuilderEventListener = new RuleBuilderEventListener(); - EventManager::instance()->on($RuleBuilderEventListener); + // Register the Actor and Rule Builder Event Listeners. Command line tools + // perform the equivalent setup via EventUtilities directly (CFM-558). + EventUtilities::registerCoreListeners(auth: $this->RegistryAuth); if(!$this->request->is('restful')) { // Initialization for non-RESTful diff --git a/app/src/Lib/Events/ActorEventListener.php b/app/src/Lib/Events/ActorEventListener.php index e7264f608..a5b179127 100644 --- a/app/src/Lib/Events/ActorEventListener.php +++ b/app/src/Lib/Events/ActorEventListener.php @@ -39,16 +39,39 @@ class ActorEventListener Implements EventListenerInterface { // RegistryAuth Component protected $RegistryAuth; + + // Fixed actor identifier, used when there is no RegistryAuth (eg: command line tools) + protected ?string $actor; /** - * Constructor. + * Constructor. Either a RegistryAuthComponent (for web and API requests) or + * a fixed actor identifier (for command line tools) should be provided. * * @since COmanage Registry v5.0.0 - * @param RegistryAuthComponent $Auth RegistryAuthComponent + * @param RegistryAuthComponent|null $Auth RegistryAuthComponent + * @param string|null $actor Actor identifier, used if $Auth is not provided (since v5.4.0) */ - public function __construct(RegistryAuthComponent $Auth) { + public function __construct(?RegistryAuthComponent $Auth = null, ?string $actor = null) { $this->RegistryAuth = $Auth; + $this->actor = $actor; + } + + /** + * Inject the actor information into the save options. + * + * @since COmanage Registry v5.4.0 + * @param ArrayObject $options Save options + */ + + protected function setActorOptions(\ArrayObject $options): void { + if($this->RegistryAuth) { + $options['actor'] = $this->RegistryAuth->getAuthenticatedUser(); + $options['apiuser'] = $this->RegistryAuth->isApiUser(); + } elseif(!empty($this->actor)) { + $options['actor'] = $this->actor; + $options['apiuser'] = false; + } } /** @@ -61,14 +84,9 @@ public function __construct(RegistryAuthComponent $Auth) { */ public function beforeMarshal(EventInterface $event, \ArrayObject $data, \ArrayObject $options) { - // Tweak the options so HistoryRecords and similar models can see who performed the save - - if($this->RegistryAuth) { - $options['actor'] = $this->RegistryAuth->getAuthenticatedUser(); - $options['apiuser'] = $this->RegistryAuth->isApiUser(); - - // We don't autocalculate the actor_person_id here because most models won't need it - } + // Tweak the options so HistoryRecords and similar models can see who performed the save. + // We don't autocalculate the actor_person_id here because most models won't need it. + $this->setActorOptions($options); } /** @@ -82,11 +100,7 @@ public function beforeMarshal(EventInterface $event, \ArrayObject $data, \ArrayO public function beforeSave(Event $event, EntityInterface $entity, ArrayObject $options) { // Tweak the options so ChangelogBehavior can see who performed the save - - if($this->RegistryAuth) { - $options['actor'] = $this->RegistryAuth->getAuthenticatedUser(); - $options['apiuser'] = $this->RegistryAuth->isApiUser(); - } + $this->setActorOptions($options); } /** diff --git a/app/src/Lib/Util/EventUtilities.php b/app/src/Lib/Util/EventUtilities.php new file mode 100644 index 000000000..a6b751b3f --- /dev/null +++ b/app/src/Lib/Util/EventUtilities.php @@ -0,0 +1,97 @@ +off(self::$actorListener); + } + + if(self::$ruleBuilderListener !== null) { + $EventManager->off(self::$ruleBuilderListener); + } + + self::$actorListener = new ActorEventListener($auth, $actor); + $EventManager->on(self::$actorListener); + + self::$ruleBuilderListener = new RuleBuilderEventListener(); + $EventManager->on(self::$ruleBuilderListener); + } +} From ac8bd669f6e7ef8a4dfa6cee585ac6c6124d6b1c Mon Sep 17 00:00:00 2001 From: Ioannis Igoumenos Date: Thu, 5 Nov 2026 08:26:05 +0000 Subject: [PATCH 2/2] Fix comments --- app/src/Command/JobCommand.php | 2 +- app/src/Controller/AppController.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/Command/JobCommand.php b/app/src/Command/JobCommand.php index 6829d00ae..a9f9e8558 100644 --- a/app/src/Command/JobCommand.php +++ b/app/src/Command/JobCommand.php @@ -115,7 +115,7 @@ public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionPar public function execute(Arguments $args, ConsoleIo $io) { // AppController::initialize() is not called for command line tools, so we - // register the Actor and Rule Builder Event Listeners here (CFM-558) + // register the Actor and Rule Builder Event Listeners here EventUtilities::registerCoreListeners(actor: EventUtilities::cliActor()); $CosTable = $this->getTableLocator()->get('Cos'); diff --git a/app/src/Controller/AppController.php b/app/src/Controller/AppController.php index 45e3d16a2..eca86b237 100644 --- a/app/src/Controller/AppController.php +++ b/app/src/Controller/AppController.php @@ -97,7 +97,7 @@ public function initialize(): void { $this->loadComponent('Breadcrumb'); // Register the Actor and Rule Builder Event Listeners. Command line tools - // perform the equivalent setup via EventUtilities directly (CFM-558). + // perform the equivalent setup via EventUtilities directly. EventUtilities::registerCoreListeners(auth: $this->RegistryAuth); if(!$this->request->is('restful')) {