Skip to content

Commit

Permalink
new: [API] Fast check object or attribute existence by HEAD method
Browse files Browse the repository at this point in the history
  • Loading branch information
JakubOnderka committed Nov 6, 2020
1 parent 1542d6b commit 19c5d32
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
8 changes: 8 additions & 0 deletions app/Controller/AttributesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,14 @@ public function editField($id)

public function view($id)
{
if ($this->request->is('head')) { // Just check if attribute exists
$attribute = $this->Attribute->fetchAttributesSimple($this->Auth->user(), [
'conditions' => $this->__idToConditions($id),
'fields' => ['Attribute.id'],
]);
return new CakeResponse(['status' => $attribute ? 200 : 404]);
}

$attribute = $this->__fetchAttribute($id);
if (empty($attribute)) {
throw new MethodNotAllowedException(__('Invalid attribute'));
Expand Down
36 changes: 23 additions & 13 deletions app/Controller/ObjectsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -887,22 +887,32 @@ private function __delete($id, $hard)

public function view($id)
{
if ($this->_isRest()) {
$objects = $this->MispObject->fetchObjects($this->Auth->user(), array(
if ($this->request->is('head')) { // Just check if object exists
$exists = $this->MispObject->fetchObjects($this->Auth->user(), [
'conditions' => $this->__objectIdToConditions($id),
));
if (!empty($objects)) {
$object = $objects[0];
if (!empty($object['Event'])) {
$object['Object']['Event'] = $object['Event'];
}
if (!empty($object['Attribute'])) {
$object['Object']['Attribute'] = $object['Attribute'];
}
return $this->RestResponse->viewData(array('Object' => $object['Object']), $this->response->type());
}
'metadata' => true,
]);
return new CakeResponse(['status' => $exists ? 200 : 404]);
}

$objects = $this->MispObject->fetchObjects($this->Auth->user(), array(
'conditions' => $this->__objectIdToConditions($id),
));
if (empty($objects)) {
throw new NotFoundException(__('Invalid object.'));
}
$object = $objects[0];
if ($this->_isRest()) {
if (!empty($object['Event'])) {
$object['Object']['Event'] = $object['Event'];
}
if (!empty($object['Attribute'])) {
$object['Object']['Attribute'] = $object['Attribute'];
}
return $this->RestResponse->viewData(array('Object' => $object['Object']), $this->response->type());
} else {
$this->redirect('/events/view/' . $object['Object']['event_id']);
}
}

public function orphanedObjectDiagnostics()
Expand Down

0 comments on commit 19c5d32

Please sign in to comment.