new: [Authkeys] added

remotes/origin/main
iglocska 2020-06-21 21:31:30 +02:00
parent a7bb35b7cb
commit a99317734d
No known key found for this signature in database
GPG Key ID: BEA224F1FEF113AC
5 changed files with 194 additions and 0 deletions

View File

@ -0,0 +1,54 @@
<?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 AuthKeysController extends AppController
{
public function index()
{
$this->CRUD->index([
'filters' => ['users.username', 'authkey', 'comment', 'users.id'],
'quickFilters' => ['authkey', 'comment'],
'contain' => ['Users']
]);
if ($this->ParamHandler->isRest()) {
return $this->restResponsePayload;
}
$this->set('metaGroup', 'ContactDB');
}
public function delete($id)
{
$this->CRUD->delete($id);
if ($this->ParamHandler->isRest()) {
return $this->restResponsePayload;
}
$this->set('metaGroup', 'ContactDB');
}
public function add()
{
$this->CRUD->add();
if ($this->ParamHandler->isRest()) {
return $this->restResponsePayload;
}
$this->loadModel('Users');
$dropdownData = [
'user' => $this->Users->find('list', [
'sort' => ['username' => 'asc']
])
];
$this->set(compact('dropdownData'));
$this->set('metaGroup', 'ContactDB');
}
}

View File

@ -0,0 +1,11 @@
<?php
namespace App\Model\Entity;
use App\Model\Entity\AppModel;
use Cake\ORM\Entity;
class AuthKey extends AppModel
{
}

View File

@ -0,0 +1,48 @@
<?php
namespace App\Model\Table;
use App\Model\Table\AppTable;
use Cake\ORM\Table;
use Cake\Validation\Validator;
use Cake\Event\EventInterface;
use Cake\Utility\Security;
use ArrayObject;
class AuthKeysTable extends AppTable
{
public function initialize(array $config): void
{
parent::initialize($config);
$this->addBehavior('UUID');
$this->belongsTo(
'Users'
);
$this->setDisplayField('authkey');
}
public function beforeMarshal(EventInterface $event, ArrayObject $data, ArrayObject $options)
{
$data['created'] = time();
if (empty($data['valid_until'])) {
$data['valid_until'] = 0;
}
if (empty($data['authkey'])) {
$data['authkey'] = $this->generateAuthKey();
}
}
public function generateAuthKey()
{
return Security::randomString(40);
}
public function validationDefault(Validator $validator): Validator
{
$validator
->notEmptyString('authkey')
->notEmptyString('user_id')
->requirePresence(['authkey', 'user_id'], 'create');
return $validator;
}
}

View File

@ -0,0 +1,24 @@
<?php
echo $this->element('genericElements/Form/genericForm', array(
'data' => array(
'description' => __('Authkeys are used for API access. A user can have more than one authkey, so if you would like to use separate keys per tool that queries Cerebrate, add additional keys. Use the comment field to make identifying your keys easier.'),
'fields' => array(
array(
'field' => 'user_id',
'label' => __('User'),
'options' => $dropdownData['user'],
'type' => 'dropdown'
),
array(
'field' => 'comment'
),
array(
'field' => 'valid_until',
'label' => 'Validity'
)
),
'submit' => array(
'action' => $this->request->getParam('action')
)
)
));

View File

@ -0,0 +1,57 @@
<?php
echo $this->element('genericElements/IndexTable/index_table', [
'data' => [
'data' => $data,
'top_bar' => [
'pull' => 'right',
'children' => [
[
'type' => 'simple',
'children' => [
'data' => [
'type' => 'simple',
'text' => __('Add authentication key'),
'class' => 'btn btn-primary',
'popover_url' => '/authKeys/add'
]
]
],
[
'type' => 'search',
'button' => __('Filter'),
'placeholder' => __('Enter value to search'),
'data' => '',
'searchKey' => 'value'
]
]
],
'fields' => [
[
'name' => '#',
'sort' => 'id',
'data_path' => 'id',
],
[
'name' => __('User'),
'sort' => 'user.username',
'data_path' => 'user.username',
],
[
'name' => __('Auth key'),
'sort' => 'authkey',
'data_path' => 'authkey',
'privacy' => 1
]
],
'title' => __('Authentication key Index'),
'description' => __('A list of API keys bound to a user.'),
'pull' => 'right',
'actions' => [
[
'onclick' => 'populateAndLoadModal(\'/encryptionKeys/delete/[onclick_params_data_path]\');',
'onclick_params_data_path' => 'id',
'icon' => 'trash'
]
]
]
]);