diff --git a/app/src/Controller/AuthKeysController.php b/app/src/Controller/AuthKeysController.php new file mode 100644 index 0000000..157d28d --- /dev/null +++ b/app/src/Controller/AuthKeysController.php @@ -0,0 +1,54 @@ +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'); + } +} diff --git a/app/src/Model/Entity/AuthKey.php b/app/src/Model/Entity/AuthKey.php new file mode 100644 index 0000000..7755225 --- /dev/null +++ b/app/src/Model/Entity/AuthKey.php @@ -0,0 +1,11 @@ +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; + } +} diff --git a/app/templates/AuthKeys/add.php b/app/templates/AuthKeys/add.php new file mode 100644 index 0000000..dfaa367 --- /dev/null +++ b/app/templates/AuthKeys/add.php @@ -0,0 +1,24 @@ +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') + ) + ) +)); diff --git a/app/templates/AuthKeys/index.php b/app/templates/AuthKeys/index.php new file mode 100644 index 0000000..5e7e46f --- /dev/null +++ b/app/templates/AuthKeys/index.php @@ -0,0 +1,57 @@ +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' + ] + ] + ] +]);