new: [localtools] functionality added

connector
iglocska 2021-06-01 07:45:01 +02:00
parent 357c147c13
commit 3806831633
No known key found for this signature in database
GPG Key ID: BEA224F1FEF113AC
2 changed files with 184 additions and 26 deletions

View File

@ -20,11 +20,12 @@ class LocalToolsController extends AppController
if ($this->request->is('ajax')) {
$this->viewBuilder()->disableAutoLayout();
}
$this->set('metaGroup', 'LocalTools');
$this->set('metaGroup', 'Administration');
}
public function connectorIndex()
{
$this->set('metaGroup', 'Admin');
$this->CRUD->index([
'filters' => ['name', 'connector'],
'quickFilters' => ['name', 'connector'],
@ -38,17 +39,67 @@ class LocalToolsController extends AppController
if ($this->ParamHandler->isRest()) {
return $this->restResponsePayload;
}
$this->set('metaGroup', 'LocalTools');
$this->set('metaGroup', 'Administration');
}
public function action()
public function action($connectionId, $actionName)
{
$params = [];
$results = $this->LocalTools->runAction();
$this->render('add');
$connection = $this->LocalTools->query()->where(['id' => $connectionId])->first();
if (empty($connection)) {
throw new NotFoundException(__('Invalid connector.'));
}
$params = $this->ParamHandler->harvestParams($this->LocalTools->getActionFilterOptions($connection->connector, $actionName));
$actionDetails = $this->LocalTools->getActionDetails($actionName);
$params['connection'] = $connection;
$results = $this->LocalTools->action($this->ACL->getUser()['id'], $connection->connector, $actionName, $params, $this->request);
if (!empty($results['redirect'])) {
$this->redirect($results['redirect']);
}
if (!empty($results['restResponse'])) {
return $results['restResponse'];
}
if ($this->ParamHandler->isRest()) {
return $results['data']['data'];
}
$this->set('data', $results);
$this->set('metaGroup', 'Administration');
if ($actionDetails['type'] === 'formAction') {
if ($this->request->is(['post', 'put'])) {
if ($this->ParamHandler->isAjax()) {
if (!empty($results['success'])) {
return $this->RestResponse->ajaxSuccessResponse(
'LocalTools',
'action',
$connection,
empty($results['message']) ? __('Success.') : $results['message']
);
} else {
return $this->RestResponse->ajaxSuccessResponse(
'LocalTools',
'action',
false,
empty($results['message']) ? __('Success.') : $results['message']
//['displayOnSuccess' => $displayOnSuccess]
);
}
} else {
if (!empty($results['success'])) {
$this->Flash->success(empty($results['message']) ? __('Success.') : $results['message']);
$this->redirect(['controller' => 'localTools', 'action' => 'action', $connectionId, $actionDetails['redirect']]);
} else {
$this->Flash->error(empty($results['message']) ? __('Could not execute the requested action.') : $results['message']);
$this->redirect(['controller' => 'localTools', 'action' => 'action', $connectionId, $actionDetails['redirect']]);
}
}
} else {
$this->render('/Common/getForm');
}
} else {
$this->render('/Common/' . $actionDetails['type']);
}
}
public function add()
public function add($connector = false)
{
$this->CRUD->add();
if ($this->ParamHandler->isRest()) {
@ -60,7 +111,7 @@ class LocalToolsController extends AppController
$dropdownData['connectors'][$connector['connector']] = $connector['name'];
}
$this->set(compact('dropdownData'));
$this->set('metaGroup', 'LocalTools');
$this->set('metaGroup', 'Administration');
}
public function viewConnector($connector_name)
@ -73,10 +124,10 @@ class LocalToolsController extends AppController
}
}
if ($this->ParamHandler->isRest()) {
$this->restResponsePayload = $this->Controller->RestResponse->viewData($connector, 'json');
return $this->RestResponse->viewData($connector, 'json');
}
$this->set('entity', $connector);
$this->set('metaGroup', 'LocalTools');
$this->set('metaGroup', 'Administration');
}
public function edit($id)
@ -85,13 +136,16 @@ class LocalToolsController extends AppController
if ($this->ParamHandler->isRest()) {
return $this->restResponsePayload;
}
if ($this->ParamHandler->isAjax() && !empty($this->ajaxResponsePayload)) {
return $this->ajaxResponsePayload;
}
$connectors = $this->LocalTools->extractMeta($this->LocalTools->getConnectors());
$dropdownData = ['connectors' => []];
foreach ($connectors as $connector) {
$dropdownData['connectors'][$connector['connector']] = $connector['name'];
}
$this->set(compact('dropdownData'));
$this->set('metaGroup', 'LocalTools');
$this->set('metaGroup', 'Administration');
$this->render('add');
}
@ -101,22 +155,60 @@ class LocalToolsController extends AppController
if ($this->ParamHandler->isRest()) {
return $this->restResponsePayload;
}
$this->set('metaGroup', 'LocalTools');
$this->set('metaGroup', 'Administration');
}
public function view($id)
{
$this->CRUD->view($id);
$localTools = $this->LocalTools;
$this->CRUD->view($id, [
'afterFind' => function ($data) use($id, $localTools) {
$data['children'] = $localTools->getChildParameters($id);
return $data;
}
]);
$responsePayload = $this->CRUD->getResponsePayload();
if (!empty($responsePayload)) {
return $responsePayload;
}
$this->set('metaGroup', 'LocalTools');
$this->set('metaGroup', 'Administration');
}
public function test()
public function exposedTools()
{
$connectors = $this->LocalTools->getConnectors();
$connectors['MispConnector']->test();
$this->CRUD->index([
'filters' => ['name', 'connector'],
'quickFilters' => ['name', 'connector'],
'fields' => ['id', 'name', 'connector', 'description'],
'filterFunction' => function($query) {
$query->where(['exposed' => 1]);
return $query;
},
'afterFind' => function($data) {
foreach ($data as $connector) {
$connector = [
'id' => $connector['id'],
'name' => $connector['name'],
'connector' => $connector['connector']
];
}
return $data;
}
]);
if ($this->ParamHandler->isRest()) {
return $this->restResponsePayload;
}
$this->set('metaGroup', 'Administration');
}
public function broodTools($id)
{
$this->loadModel('Broods');
$tools = $this->Broods->queryLocalTools($id);
if ($this->ParamHandler->isRest()) {
return $this->RestResponse->viewData($tools, 'json');
}
$this->set('data', $tools);
$this->set('metaGroup', 'Administration');
}
}

View File

@ -8,6 +8,7 @@ use Cake\Validation\Validator;
use Migrations\Migrations;
use Cake\Filesystem\Folder;
use Cake\Filesystem\File;
use Cake\Http\Exception\NotFoundException;
class LocalToolsTable extends AppTable
{
@ -19,6 +20,10 @@ class LocalToolsTable extends AppTable
3 => 'ERROR',
];
public $exposedFunctions = [];
private $currentConnector = null;
private $connectors = null;
public function initialize(array $config): void
@ -31,6 +36,43 @@ class LocalToolsTable extends AppTable
return $validator;
}
public function loadConnector(string $connectorName): void
{
if (empty($currentConnector) || get_class($currentConnector) !== $connectorName) {
$connectors = $this->getConnectors($connectorName);
if (empty($connectors[$connectorName])) {
throw new NotFoundException(__('Invalid connector module requested.'));
} else {
$this->currentConnector = $connectors[$connectorName];
}
}
}
public function action(int $user_id, string $connectorName, string $actionName, array $params, \Cake\Http\ServerRequest $request): array
{
$this->loadConnector($connectorName);
$params['request'] = $request;
$params['user_id'] = $user_id;
return $this->currentConnector->{$actionName}($params);
}
public function getActionDetails(string $actionName): array
{
if (!empty($this->currentConnector->exposedFunctions[$actionName])) {
return $this->currentConnector->exposedFunctions[$actionName];
}
throw new NotFoundException(__('Invalid connector module action requested.'));
}
public function getActionFilterOptions(string $connectorName, string $actionName): array
{
$this->loadConnector($connectorName);
if (!empty($this->currentConnector->exposedFunctions[$actionName])) {
return $this->currentConnector->exposedFunctions[$actionName]['params'] ?? [];
}
throw new NotFoundException(__('Invalid connector module action requested.'));
}
public function getConnectors(string $name = null): array
{
$connectors = [];
@ -51,7 +93,6 @@ class LocalToolsTable extends AppTable
}
}
return $connectors;
}
public function extractMeta(array $connector_classes, bool $includeConnections = false): array
@ -80,24 +121,49 @@ class LocalToolsTable extends AppTable
]);
$connections = $query->all()->toList();
foreach ($connections as &$connection) {
$connection = $this->healthCheckIndividual($connector_class, $connection);
$connection = $this->healthCheckIndividual($connection);
}
return $connections;
}
public function healthCheckIndividual(Object $connector): array
public function healthCheckIndividual(Object $connection): array
{
$connector_class = $this->getConnectors($connector['connector']);
if (empty($connector_class[$connector['connector']])) {
$connector_class = $this->getConnectors($connection->connector);
if (empty($connector_class[$connection->connector])) {
return [];
}
$connector_class = $connector_class[$connector['connector']];
$health = $connector_class->health($connector);
$connector_class = $connector_class[$connection->connector];
$health = $connector_class->health($connection);
return $connection = [
'name' => $connector->name,
'name' => $connection->name,
'health' => $health['status'],
'message' => $health['message'],
'url' => '/localTools/viewConnection/' . $connector['id']
'url' => '/localTools/view/' . $connection['id']
];
}
public function getConnectorByConnectionId($id): array
{
$connection = $this->find()->where(['id' => $id])->first();
if (empty($connection)) {
throw new NotFoundException(__('Invalid connection.'));
}
return $this->getConnectors($connection->connector);
}
public function getChildParameters($id): array
{
$connectors = $this->getConnectorByConnectionId($id);
if (empty($connectors)) {
throw new NotFoundException(__('Invalid connector.'));
}
$connector = array_values($connectors)[0];
$children = [];
foreach ($connector->exposedFunctions as $functionName => $function) {
if ($function['type'] === 'index') {
$children[] = $functionName;
}
}
return $children;
}
}