From 21ad2e8b5ed3e15d1c83cd80cd7e32a8f3096a9a Mon Sep 17 00:00:00 2001 From: iglocska Date: Fri, 19 Jun 2020 00:40:58 +0200 Subject: [PATCH] new: [encryption keys] controller/model/template added --- .../Controller/EncryptionKeysController.php | 94 +++---------------- app/src/Model/Table/EncryptionKeysTable.php | 14 ++- app/templates/EncryptionKeys/add.php | 3 - 3 files changed, 25 insertions(+), 86 deletions(-) diff --git a/app/src/Controller/EncryptionKeysController.php b/app/src/Controller/EncryptionKeysController.php index a778de1..b11fda8 100644 --- a/app/src/Controller/EncryptionKeysController.php +++ b/app/src/Controller/EncryptionKeysController.php @@ -16,62 +16,30 @@ class EncryptionKeysController extends AppController { public function index() { - $params = $this->_harvestParams($this->request, ['owner_type', 'owner_id']); - $query = $this->EncryptionKeys->find(); - if (!empty($params['owner_type'])) { - $query->where(['owner_type' => $params['owner_type']]); - } - if (!empty($params['owner_id'])) { - $query->where(['owner_id' => $params['owner_id']]); - } - $query->contain(['Individuals', 'Organisations']); - if ($this->_isRest()) { - $alignments = $query->all(); - return $this->RestResponse->viewData($alignments, 'json'); - } else { - $this->loadComponent('Paginator'); - $encrpyion_keys = $this->Paginator->paginate($query); - $this->set('data', $encrpyion_keys); - $this->set('metaGroup', 'ContactDB'); + $this->CRUD->index([ + 'filters' => ['owner_type', 'organisation_id', 'individual_id'], + 'contain' => ['Individuals', 'Organisations'] + ]); + if ($this->ParamHandler->isRest()) { + return $this->restResponsePayload; } + $this->set('metaGroup', 'ContactDB'); } public function delete($id) { - if (empty($id)) { - throw new NotFoundException(__('Invalid encryption key.')); - } - $key = $this->EncryptionKeys->get($id); - if ($this->request->is('post') || $this->request->is('delete')) { - if ($this->EncryptionKey->delete($individual)) { - $message = __('Encryption key deleted.'); - if ($this->_isRest()) { - $individual = $this->EncryptionKeys->get($id); - return $this->RestResponse->saveSuccessResponse('EncryptionKeys', 'delete', $id, 'json', $message); - } else { - $this->Flash->success($message); - return $this->redirect($this->referer()); - } - } + $this->CRUD->delete($id); + if ($this->ParamHandler->isRest()) { + return $this->restResponsePayload; } $this->set('metaGroup', 'ContactDB'); - $this->set('scope', 'encryptionKeys'); - $this->set('id', $key['id']); - $this->set('key', $key); - $this->viewBuilder()->setLayout('ajax'); - $this->render('/genericTemplates/delete'); } public function add() { - $params = $this->_harvestParams($this->request, ['owner_type', 'owner_id', 'organisation_id', 'individual_id', 'encryption_key', 'expires', 'uuid', 'revoked', 'type']); - $input = $this->request->getData(); - $encryptionKey = $this->EncryptionKeys->newEmptyEntity(); - if (!empty($params['owner_type'])) { - if (!empty($params[$params['owner_type'] . '_id'])) { - $params['owner_id'] = $params[$params['owner_type'] . '_id']; - } - $params[$params['owner_type'] . '_id'] = $params['owner_id']; + $this->CRUD->add(); + if ($this->ParamHandler->isRest()) { + return $this->restResponsePayload; } $this->loadModel('Organisations'); $this->loadModel('Individuals'); @@ -83,42 +51,6 @@ class EncryptionKeysController extends AppController 'sort' => ['email' => 'asc'] ]) ]; - if ($this->request->is('post')) { - if (empty($params['owner_type']) || empty($params['owner_id'])) { - throw new NotAcceptableException(__('Invalid input. owner_type and owner_id expected as parameters in the format /encryption_keys/add/[owner_type]/[owner_id] or passed as a JSON.')); - } - if ($params['owner_type'] === 'individual') { - $owner = $this->Individuals->find()->where(['id' => $params['owner_id']])->first(); - if (empty($owner)) { - throw new NotFoundException(__('Invalid owner individual.')); - } - } else { - $owner = $this->Organisations->find()->where(['id' => $params['owner_id']])->first(); - if (empty($owner)) { - throw new NotFoundException(__('Invalid owner organisation.')); - } - } - $encryptionKey = $this->EncryptionKeys->patchEntity($encryptionKey, $params); - if ($this->EncryptionKeys->save($encryptionKey)) { - $message = __('EncryptionKey added.'); - if ($this->_isRest()) { - $encryptionKey = $this->EncryptionKeys->get($this->EncryptionKeys->id); - return $this->RestResponse->viewData($encryptionKey, 'json'); - } else { - $this->Flash->success($message); - return $this->redirect(['action' => 'index']); - } - } else { - $message = __('EncryptionKey could not be added.'); - if ($this->_isRest()) { - return $this->RestResponse->saveFailResponse('EncryptionKeys', 'add', false, $message); - } else { - $this->Flash->error($message); - $this->redirect($this->referer()); - } - } - } - $this->set('encryptionKey', $encryptionKey); $this->set(compact('dropdownData')); $this->set('metaGroup', 'ContactDB'); } diff --git a/app/src/Model/Table/EncryptionKeysTable.php b/app/src/Model/Table/EncryptionKeysTable.php index 74842fa..4ca2438 100644 --- a/app/src/Model/Table/EncryptionKeysTable.php +++ b/app/src/Model/Table/EncryptionKeysTable.php @@ -11,6 +11,7 @@ class EncryptionKeysTable extends AppTable public function initialize(array $config): void { parent::initialize($config); + $this->addBehavior('UUID'); $this->belongsTo( 'Individuals', [ @@ -28,15 +29,24 @@ class EncryptionKeysTable extends AppTable $this->setDisplayField('encryption_key'); } + public function beforeMarshal(EventInterface $event, ArrayObject $data, ArrayObject $options) + { + if (isset($data['owner_id'])) { + if (empty($data['owner_type']) || !in_array(['individual', 'organisation'], $data['owner_type'])) { + return false; + } + $data[$data['owner_type'] . '_id'] = $data['owner_id']; + } + } + public function validationDefault(Validator $validator): Validator { $validator ->notEmptyString('type') ->notEmptyString('encryption_key') - ->notEmptyString('uuid') ->notEmptyString('owner_id') ->notEmptyString('owner_type') - ->requirePresence(['type', 'encryption_key', 'uuid', 'owner_id', 'owner_type'], 'create'); + ->requirePresence(['type', 'encryption_key', 'owner_id', 'owner_type'], 'create'); return $validator; } } diff --git a/app/templates/EncryptionKeys/add.php b/app/templates/EncryptionKeys/add.php index a26beee..6ebf1ab 100644 --- a/app/templates/EncryptionKeys/add.php +++ b/app/templates/EncryptionKeys/add.php @@ -1,9 +1,6 @@ 'email', 'organisation' => 'name']; echo $this->element('genericElements/Form/genericForm', array( - 'form' => $this->Form, 'data' => array( - 'entity' => $encryptionKey, 'title' => __('Add new encryption key'), 'description' => __('Alignments indicate that an individual belongs to an organisation in one way or another. The type of relationship is defined by the type field.'), 'model' => 'Organisations',