Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions app/src/Command/JobCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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
EventUtilities::registerCoreListeners(actor: EventUtilities::cliActor());

$CosTable = $this->getTableLocator()->get('Cos');
$JobTable = $this->getTableLocator()->get('Jobs');

Expand Down
11 changes: 4 additions & 7 deletions app/src/Controller/AppController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
EventUtilities::registerCoreListeners(auth: $this->RegistryAuth);

if(!$this->request->is('restful')) {
// Initialization for non-RESTful
Expand Down
46 changes: 30 additions & 16 deletions app/src/Lib/Events/ActorEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand Down
97 changes: 97 additions & 0 deletions app/src/Lib/Util/EventUtilities.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php
/**
* COmanage Registry Event Utilities
*
* Portions licensed to the University Corporation for Advanced Internet
* Development, Inc. ("UCAID") under one or more contributor license agreements.
* See the NOTICE file distributed with this work for additional information
* regarding copyright ownership.
*
* UCAID licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @link https://www.internet2.edu/comanage COmanage Project
* @package registry
* @since COmanage Registry v5.4.0
* @license Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
*/

declare(strict_types = 1);

namespace App\Lib\Util;

use App\Controller\Component\RegistryAuthComponent;
use App\Lib\Events\ActorEventListener;
use App\Lib\Events\RuleBuilderEventListener;
use Cake\Event\EventManager;

class EventUtilities {
// The currently registered core listeners, so they can be replaced rather
// than registered more than once (eg: multiple requests in the same process)
protected static ?ActorEventListener $actorListener = null;
protected static ?RuleBuilderEventListener $ruleBuilderListener = null;

/**
* Determine the actor identifier to use for command line operations, based
* on the operating system user running the command.
*
* @since COmanage Registry v5.4.0
* @return string Actor identifier
*/

public static function cliActor(): string {
$username = null;

if(function_exists('posix_getpwuid') && function_exists('posix_geteuid')) {
$pwent = posix_getpwuid(posix_geteuid());

$username = $pwent['name'] ?? null;
}

return 'CLI:' . ($username ?? get_current_user());
}

/**
* Register the core event listeners (ActorEventListener and
* RuleBuilderEventListener) with the global EventManager. Any listeners
* previously registered via this function are replaced.
*
* Web and API requests register via AppController, using RegistryAuth to
* determine the actor. Command line tools (which do not instantiate
* AppController) provide an actor identifier instead.
*
* @since COmanage Registry v5.4.0
* @param RegistryAuthComponent|null $auth RegistryAuth Component, for web and API requests
* @param string|null $actor Actor identifier, for command line tools
*/

public static function registerCoreListeners(
?RegistryAuthComponent $auth = null,
?string $actor = null
): void {
$EventManager = EventManager::instance();

if(self::$actorListener !== null) {
$EventManager->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);
}
}