2020-05-29 13:41:58 +02:00
< ? php
namespace App\Controller ;
use App\Controller\AppController ;
use Cake\Utility\Hash ;
use Cake\Utility\Text ;
use \Cake\Database\Expression\QueryExpression ;
use Cake\Http\Exception\NotFoundException ;
use Cake\Http\Exception\MethodNotAllowedException ;
use Cake\Http\Exception\ForbiddenException ;
use Cake\Http\Exception\NotAcceptableException ;
use Cake\Error\Debugger ;
class EncryptionKeysController extends AppController
{
2020-06-04 10:05:45 +02:00
public function index ()
2020-05-29 13:41:58 +02:00
{
2020-06-04 10:05:45 +02:00
$params = $this -> _harvestParams ( $this -> request , [ 'owner_type' , 'owner_id' ]);
2020-05-29 13:41:58 +02:00
$query = $this -> EncryptionKeys -> find ();
2020-06-04 10:05:45 +02:00
if ( ! empty ( $params [ 'owner_type' ])) {
$query -> where ([ 'owner_type' => $params [ 'owner_type' ]]);
2020-05-29 13:41:58 +02:00
}
2020-06-04 10:05:45 +02:00
if ( ! empty ( $params [ 'owner_id' ])) {
$query -> where ([ 'owner_id' => $params [ 'owner_id' ]]);
2020-05-29 13:41:58 +02:00
}
2020-06-04 10:05:45 +02:00
$query -> contain ([ 'Individuals' , 'Organisations' ]);
2020-05-29 13:41:58 +02:00
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' );
}
}
public function delete ( $id )
{
if ( empty ( $id )) {
2020-06-04 10:05:45 +02:00
throw new NotFoundException ( __ ( 'Invalid encryption key.' ));
2020-05-29 13:41:58 +02:00
}
2020-06-04 10:05:45 +02:00
$key = $this -> EncryptionKeys -> get ( $id );
2020-05-29 13:41:58 +02:00
if ( $this -> request -> is ( 'post' ) || $this -> request -> is ( 'delete' )) {
2020-06-04 10:05:45 +02:00
if ( $this -> EncryptionKey -> delete ( $individual )) {
$message = __ ( 'Encryption key deleted.' );
2020-05-29 13:41:58 +02:00
if ( $this -> _isRest ()) {
2020-06-04 10:05:45 +02:00
$individual = $this -> EncryptionKeys -> get ( $id );
return $this -> RestResponse -> saveSuccessResponse ( 'EncryptionKeys' , 'delete' , $id , 'json' , $message );
2020-05-29 13:41:58 +02:00
} else {
$this -> Flash -> success ( $message );
return $this -> redirect ( $this -> referer ());
}
}
}
$this -> set ( 'metaGroup' , 'ContactDB' );
2020-06-04 10:05:45 +02:00
$this -> set ( 'scope' , 'encryptionKeys' );
$this -> set ( 'id' , $key [ 'id' ]);
$this -> set ( 'key' , $key );
2020-05-29 13:41:58 +02:00
$this -> viewBuilder () -> setLayout ( 'ajax' );
$this -> render ( '/genericTemplates/delete' );
}
2020-06-04 10:05:45 +02:00
public function add ()
2020-05-29 13:41:58 +02:00
{
2020-06-04 10:05:45 +02:00
$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' ];
2020-05-29 13:41:58 +02:00
}
2020-06-04 10:05:45 +02:00
$params [ $params [ 'owner_type' ] . '_id' ] = $params [ 'owner_id' ];
2020-05-29 13:41:58 +02:00
}
2020-06-04 10:05:45 +02:00
$this -> loadModel ( 'Organisations' );
$this -> loadModel ( 'Individuals' );
$dropdownData = [
'organisation' => $this -> Organisations -> find ( 'list' , [
'sort' => [ 'name' => 'asc' ]
]),
'individual' => $this -> Individuals -> find ( 'list' , [
'sort' => [ 'email' => 'asc' ]
])
];
2020-05-29 13:41:58 +02:00
if ( $this -> request -> is ( 'post' )) {
2020-06-04 10:05:45 +02:00
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 );
2020-05-29 13:41:58 +02:00
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 );
2020-06-04 10:05:45 +02:00
return $this -> redirect ([ 'action' => 'index' ]);
2020-05-29 13:41:58 +02:00
}
} 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 ());
}
}
}
2020-06-04 10:05:45 +02:00
$this -> set ( 'encryptionKey' , $encryptionKey );
$this -> set ( compact ( 'dropdownData' ));
2020-05-29 13:41:58 +02:00
$this -> set ( 'metaGroup' , 'ContactDB' );
}
}