new: [encryption keys] controller/model/template added

remotes/origin/main
iglocska 2020-06-19 00:40:58 +02:00
parent 1fde2d7a8e
commit 21ad2e8b5e
No known key found for this signature in database
GPG Key ID: BEA224F1FEF113AC
3 changed files with 25 additions and 86 deletions

View File

@ -16,62 +16,30 @@ class EncryptionKeysController extends AppController
{ {
public function index() public function index()
{ {
$params = $this->_harvestParams($this->request, ['owner_type', 'owner_id']); $this->CRUD->index([
$query = $this->EncryptionKeys->find(); 'filters' => ['owner_type', 'organisation_id', 'individual_id'],
if (!empty($params['owner_type'])) { 'contain' => ['Individuals', 'Organisations']
$query->where(['owner_type' => $params['owner_type']]); ]);
if ($this->ParamHandler->isRest()) {
return $this->restResponsePayload;
} }
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->set('metaGroup', 'ContactDB');
} }
}
public function delete($id) public function delete($id)
{ {
if (empty($id)) { $this->CRUD->delete($id);
throw new NotFoundException(__('Invalid encryption key.')); if ($this->ParamHandler->isRest()) {
} return $this->restResponsePayload;
$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->set('metaGroup', 'ContactDB'); $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() public function add()
{ {
$params = $this->_harvestParams($this->request, ['owner_type', 'owner_id', 'organisation_id', 'individual_id', 'encryption_key', 'expires', 'uuid', 'revoked', 'type']); $this->CRUD->add();
$input = $this->request->getData(); if ($this->ParamHandler->isRest()) {
$encryptionKey = $this->EncryptionKeys->newEmptyEntity(); return $this->restResponsePayload;
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->loadModel('Organisations'); $this->loadModel('Organisations');
$this->loadModel('Individuals'); $this->loadModel('Individuals');
@ -83,42 +51,6 @@ class EncryptionKeysController extends AppController
'sort' => ['email' => 'asc'] '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(compact('dropdownData'));
$this->set('metaGroup', 'ContactDB'); $this->set('metaGroup', 'ContactDB');
} }

View File

@ -11,6 +11,7 @@ class EncryptionKeysTable extends AppTable
public function initialize(array $config): void public function initialize(array $config): void
{ {
parent::initialize($config); parent::initialize($config);
$this->addBehavior('UUID');
$this->belongsTo( $this->belongsTo(
'Individuals', 'Individuals',
[ [
@ -28,15 +29,24 @@ class EncryptionKeysTable extends AppTable
$this->setDisplayField('encryption_key'); $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 public function validationDefault(Validator $validator): Validator
{ {
$validator $validator
->notEmptyString('type') ->notEmptyString('type')
->notEmptyString('encryption_key') ->notEmptyString('encryption_key')
->notEmptyString('uuid')
->notEmptyString('owner_id') ->notEmptyString('owner_id')
->notEmptyString('owner_type') ->notEmptyString('owner_type')
->requirePresence(['type', 'encryption_key', 'uuid', 'owner_id', 'owner_type'], 'create'); ->requirePresence(['type', 'encryption_key', 'owner_id', 'owner_type'], 'create');
return $validator; return $validator;
} }
} }

View File

@ -1,9 +1,6 @@
<?php <?php
$primaryIdentifiers = ['individual' => 'email', 'organisation' => 'name'];
echo $this->element('genericElements/Form/genericForm', array( echo $this->element('genericElements/Form/genericForm', array(
'form' => $this->Form,
'data' => array( 'data' => array(
'entity' => $encryptionKey,
'title' => __('Add new encryption key'), '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.'), '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', 'model' => 'Organisations',