From e7a0f3f2ff8e5f794d40e82d0f44a978d3ebd152 Mon Sep 17 00:00:00 2001 From: mokaddem Date: Thu, 18 Mar 2021 15:17:39 +0100 Subject: [PATCH] chg: [requestProcessor] Added placeholder for future request processors --- .../BroodRequestProcessor.php | 116 ++++++++++++++++++ .../GenericRequestProcessor.php | 9 ++ .../ProposalRequestProcessor.php | 69 +++++++++++ .../SynchronisationRequestProcessor.php | 69 +++++++++++ src/Controller/BroodsController.php | 18 +++ src/Controller/InboxController.php | 6 + src/Model/Table/RequestProcessorTable.php | 23 ++++ 7 files changed, 310 insertions(+) create mode 100644 libraries/RequestProcessors/BroodRequestProcessor.php create mode 100644 libraries/RequestProcessors/ProposalRequestProcessor.php create mode 100644 libraries/RequestProcessors/SynchronisationRequestProcessor.php diff --git a/libraries/RequestProcessors/BroodRequestProcessor.php b/libraries/RequestProcessors/BroodRequestProcessor.php new file mode 100644 index 0000000..8eebc5a --- /dev/null +++ b/libraries/RequestProcessors/BroodRequestProcessor.php @@ -0,0 +1,116 @@ +description = __('Handle tool interconnection request from other cerebrate instance'); + $this->Broods = TableRegistry::getTableLocator()->get('Broods'); + } + + protected function addValidatorRules($validator) + { + return $validator; + } + + public function create($requestData) { + $this->validateRequestData($requestData); + $requestData['title'] = __('Cerebrate instance {0} requested interconnection for tool {1}', 'Insert brood name', 'Insert tool name'); + return parent::create($requestData); + } + + public function setViewVariables($controller, $request) + { + } + + public function process($id, $requestData) + { + $connectionSuccessfull = false; + $interConnectionResult = []; + if ($connectionSuccessfull) { + $this->discard($id, $requestData); + } + return $this->genActionResult( + $interConnectionResult, + $connectionSuccessfull, + $connectionSuccessfull ? __('Interconnection for `{0}` created', 'Insert tool name') : __('Could interconnect tool `{0}`.', 'Insert tool name'), + [] + ); + } + + public function discard($id, $requestData) + { + return parent::discard($id, $requestData); + } +} + +class OneWaySynchronizationProcessor extends BroodRequestProcessor implements GenericProcessorActionI { + public $action = 'OneWaySynchronization'; + protected $description; + + public function __construct() { + parent::__construct(); + $this->description = __('Handle cerebrate connection request for another cerebrate instance'); + $this->Broods = TableRegistry::getTableLocator()->get('Broods'); + } + + protected function addValidatorRules($validator) + { + return $validator; + } + + public function create($requestData) { + $this->validateRequestData($requestData); + $requestData['title'] = __('Cerebrate instance {0} requested interconnection', 'Insert cerebrate name'); + return parent::create($requestData); + } + + public function setViewVariables($controller, $request) + { + } + + public function process($id, $requestData) + { + $connectionSuccessfull = false; + $interConnectionResult = []; + if ($connectionSuccessfull) { + $this->discard($id, $requestData); + } + return $this->genActionResult( + $interConnectionResult, + $connectionSuccessfull, + $connectionSuccessfull ? __('Interconnection with `{0}` created', 'Insert cerebrate name') : __('Could interconnect with `{0}`.', 'Insert cerebrate name'), + [] + ); + } + + public function discard($id, $requestData) + { + return parent::discard($id, $requestData); + } +} \ No newline at end of file diff --git a/libraries/RequestProcessors/GenericRequestProcessor.php b/libraries/RequestProcessors/GenericRequestProcessor.php index 3f1f792..c74a405 100644 --- a/libraries/RequestProcessors/GenericRequestProcessor.php +++ b/libraries/RequestProcessors/GenericRequestProcessor.php @@ -33,6 +33,15 @@ class GenericRequestProcessor $file->close(); } + public function getRegisteredActions() + { + return $this->registeredActions; + } + public function getScope() + { + return $this->scope; + } + private function getProcessingTemplatePath() { $class = str_replace('RequestProcessor', '', get_parent_class($this)); diff --git a/libraries/RequestProcessors/ProposalRequestProcessor.php b/libraries/RequestProcessors/ProposalRequestProcessor.php new file mode 100644 index 0000000..6af6039 --- /dev/null +++ b/libraries/RequestProcessors/ProposalRequestProcessor.php @@ -0,0 +1,69 @@ +description = __('Handle proposal from users for this cerebrate instance'); + $this->Users = TableRegistry::getTableLocator()->get('Users'); + } + + protected function addValidatorRules($validator) + { + return $validator; + } + + public function create($requestData) { + $this->validateRequestData($requestData); + $requestData['title'] = __('User `{0}` would like to modify record `{0}`', 'username', 'recordname'); + return parent::create($requestData); + } + + public function setViewVariables($controller, $request) + { + } + + public function process($id, $requestData) + { + $proposalAccepted = false; + $saveResult = []; + if ($proposalAccepted) { + $this->discard($id, $requestData); + } + return $this->genActionResult( + $saveResult, + $proposalAccepted, + $proposalAccepted ? __('Record `{0}` modify', 'recordname') : __('Could modify record `{0}`.', 'recordname'), + [] + ); + } + + public function discard($id, $requestData) + { + return parent::discard($id, $requestData); + } +} \ No newline at end of file diff --git a/libraries/RequestProcessors/SynchronisationRequestProcessor.php b/libraries/RequestProcessors/SynchronisationRequestProcessor.php new file mode 100644 index 0000000..b450e41 --- /dev/null +++ b/libraries/RequestProcessors/SynchronisationRequestProcessor.php @@ -0,0 +1,69 @@ +description = __('Handle exchange of data between two cerebrate instances'); + $this->Users = TableRegistry::getTableLocator()->get('Users'); + } + + protected function addValidatorRules($validator) + { + return $validator; + } + + public function create($requestData) { + $this->validateRequestData($requestData); + $requestData['title'] = __('Data exchange requested for record `{0}`', 'recordname'); + return parent::create($requestData); + } + + public function setViewVariables($controller, $request) + { + } + + public function process($id, $requestData) + { + $dataExchangeAccepted = false; + $saveResult = []; + if ($dataExchangeAccepted) { + $this->discard($id, $requestData); + } + return $this->genActionResult( + $saveResult, + $dataExchangeAccepted, + $dataExchangeAccepted ? __('Record `{0}` exchanged', 'recordname') : __('Could not exchange record `{0}`.', 'recordname'), + [] + ); + } + + public function discard($id, $requestData) + { + return parent::discard($id, $requestData); + } +} \ No newline at end of file diff --git a/src/Controller/BroodsController.php b/src/Controller/BroodsController.php index 7796324..3c3b27b 100644 --- a/src/Controller/BroodsController.php +++ b/src/Controller/BroodsController.php @@ -6,6 +6,7 @@ use App\Controller\AppController; use Cake\Utility\Hash; use Cake\Utility\Text; use \Cake\Database\Expression\QueryExpression; +use Cake\ORM\TableRegistry; class BroodsController extends AppController { @@ -154,4 +155,21 @@ class BroodsController extends AppController $this->redirect($this->referer()); } } + + public function interconnectTools() + { + $this->requestProcessor = TableRegistry::getTableLocator()->get('RequestProcessor'); + $processor = $this->requestProcessor->getProcessor('Brood', 'ToolInterconnection'); + $data = [ + 'origin' => '127.0.0.1', + 'comment' => 'Test comment', + 'data' => [ + 'foo' => 'foo', + 'bar' => 'bar', + 'baz' => 'baz', + ], + ]; + $processorResult = $processor->create($data); + return $processor->genHTTPReply($this, $processorResult, ['scope' => 'Brood', 'action' => 'ToolInterconnection'], ['controller' => 'Broods', 'action' => 'index']); + } } diff --git a/src/Controller/InboxController.php b/src/Controller/InboxController.php index 59e8863..225c31d 100644 --- a/src/Controller/InboxController.php +++ b/src/Controller/InboxController.php @@ -102,6 +102,7 @@ class InboxController extends AppController foreach ($requestProcessors as $scope => $processors) { foreach ($processors as $processor) { $data[] = [ + 'enabled' => $processor->enabled, 'scope' => $scope, 'action' => $processor->action ]; @@ -109,6 +110,11 @@ class InboxController extends AppController } $this->set('title', 'Available request processors'); $this->set('fields', [ + [ + 'name' => 'Enabled', + 'data_path' => 'enabled', + 'element' => 'boolean' + ], [ 'name' => 'Processor scope', 'data_path' => 'scope', diff --git a/src/Model/Table/RequestProcessorTable.php b/src/Model/Table/RequestProcessorTable.php index 25485a7..f116a3f 100644 --- a/src/Model/Table/RequestProcessorTable.php +++ b/src/Model/Table/RequestProcessorTable.php @@ -9,6 +9,21 @@ class RequestProcessorTable extends AppTable { private $processorsDirectory = ROOT . '/libraries/RequestProcessors'; private $requestProcessors; + private $enabledProcessors = [ // to be defined in config + 'Brood' => [ + 'ToolInterconnection' => false, + 'OneWaySynchronization' => false, + ], + 'Proposal' => [ + 'ProposalEdit' => false, + ], + 'Synchronisation' => [ + 'DataExchange' => false, + ], + 'User' => [ + 'Registration' => true, + ], + ]; public function initialize(array $config): void { @@ -65,6 +80,14 @@ class RequestProcessorTable extends AppTable $processorMainClass = $this->getProcessorClass($processorDir->pwd() . DS . $processorFile, $processorMainClassName); if ($processorMainClass !== false) { $this->requestProcessors[$processorMainClassNameShort] = $processorMainClass; + foreach ($this->requestProcessors[$processorMainClassNameShort]->getRegisteredActions() as $registeredAction) { + $scope = $this->requestProcessors[$processorMainClassNameShort]->getScope(); + if (!empty($this->enabledProcessors[$scope][$registeredAction])) { + $this->requestProcessors[$processorMainClassNameShort]->{$registeredAction}->enabled = true; + } else { + $this->requestProcessors[$processorMainClassNameShort]->{$registeredAction}->enabled = false; + } + } } } }