chg: [api] fixed restresponse for blacklists

pull/6172/head
iglocska 2020-08-03 16:34:01 +02:00
parent ec52cc5b8d
commit 42896beaaf
No known key found for this signature in database
GPG Key ID: BEA224F1FEF113AC
5 changed files with 43 additions and 19 deletions

View File

@ -16,14 +16,13 @@ class BlackListComponent extends Component
if (!empty($filters)) {
$this->controller->paginate['conditions'] = $filters;
}
if ($this->controller->response->type() === 'application/json' || $this->controller->response->type() == 'application/xml' || $rest) {
if ($rest) {
$blackList = $this->controller->paginate();
$blacklist= array();
foreach ($blackList as $item) {
$blacklist[] = $item[$this->controller->defaultModel];
}
$this->controller->set($this->controller->defaultModel, $blacklist);
$this->controller->set('_serialize', $this->controller->defaultModel);
return $this->RestResponse->viewData($blacklist);
} else {
$this->controller->set('response', $this->controller->paginate());
}
@ -54,7 +53,7 @@ class BlackListComponent extends Component
if (is_array($data[$this->controller->defaultModel]['uuids'])) {
$uuids = $data[$this->controller->defaultModel]['uuids'];
} else {
$uuids = explode(PHP_EOL, $data[$this->controller->defaultModel]['uuids']);
$uuids = explode(PHP_EOL, trim($data[$this->controller->defaultModel]['uuids']));
}
$successes = array();
$fails = array();
@ -64,7 +63,7 @@ class BlackListComponent extends Component
$this->controller->{$this->controller->defaultModel}->create();
$object = array();
foreach ($this->controller->{$this->controller->defaultModel}->blacklistFields as $f) {
if (strpos($f, '_uuid')) {
if ($f === $this->controller->{$this->controller->defaultModel}->blacklistTarget . '_uuid') {
$object[$f] = $uuid;
} else {
$object[$f] = !empty($data[$this->controller->defaultModel][$f]) ? $data[$this->controller->defaultModel][$f] : '';
@ -98,8 +97,12 @@ class BlackListComponent extends Component
public function edit($rest = false, $id)
{
if (strlen($id) == 36) {
$blockEntry = $this->controller->{$this->controller->defaultModel}->find('first', array('conditions' => array('uuid' => $id)));
if (Validation::uuid($id)) {
$blockEntry = $this->controller->{$this->controller->defaultModel}->find('first', [
'conditions' => array(
$this->controller->{$this->controller->defaultModel}->blacklistTarget . '_uuid' => $id
)
]);
} else {
$blockEntry = $this->controller->{$this->controller->defaultModel}->find('first', array('conditions' => array('id' => $id)));
}
@ -109,7 +112,7 @@ class BlackListComponent extends Component
$this->controller->set('blockEntry', $blockEntry);
if ($this->controller->request->is('post')) {
if ($rest) {
if ($this->response->type() === 'application/json') {
if ($this->controller->response->type() === 'application/json') {
$isJson = true;
$data = $this->controller->request->input('json_decode', true);
} else {
@ -118,6 +121,9 @@ class BlackListComponent extends Component
if (isset($data['request'])) {
$data = $data['request'];
}
if (!isset($data[$this->controller->defaultModel])) {
$data = [$this->controller->defaultModel => $data];
}
} else {
$data = $this->controller->request->data;
}
@ -132,8 +138,14 @@ class BlackListComponent extends Component
}
if ($this->controller->{$this->controller->defaultModel}->save($blockEntry)) {
if ($rest) {
$this->controller->set('message', array('Blacklist item added.'));
$this->controller->set('_serialize', array('message'));
return $this->RestResponse->viewData(
$this->controller->{$this->controller->defaultModel}->find('first', [
'recursive' => -1,
'conditions' => [
'id' => $this->controller->{$this->controller->defaultModel}->id
]
])
);
} else {
$this->controller->Session->setFlash(__('Blacklist item added.'));
$this->controller->redirect(array('action' => 'index'));
@ -168,9 +180,17 @@ class BlackListComponent extends Component
}
if ($this->controller->{$this->controller->defaultModel}->delete()) {
$this->controller->Session->setFlash(__('Blacklist entry removed'));
$message = __('Blacklist entry removed');
if ($rest) {
return $this->RestResponse->saveSuccessResponse($this->controller->defaultModel, 'delete', $id, false, $message);
}
$this->controller->Flash->success($message);
} else {
$this->controller->Session->setFlash(__('Could not remove the blacklist entry'));
$message = __('Could not remove the blacklist entry');
if ($rest) {
return $this->RestResponse->saveFailResponse($this->controller->defaultModel, 'delete', $id, $message);
}
$this->controller->error($message);
}
$this->controller->redirect(array('action' => 'index'));
}

View File

@ -46,7 +46,7 @@ class EventBlacklistsController extends AppController
}
$this->set('passedArgs', json_encode($passedArgs));
$this->set('passedArgsArray', $passedArgsArray);
$this->BlackList->index($this->_isRest(), $params);
return $this->BlackList->index($this->_isRest(), $params);
}
public function add()
@ -56,12 +56,12 @@ class EventBlacklistsController extends AppController
public function edit($id)
{
$this->BlackList->edit($this->_isRest(), $id);
return $this->BlackList->edit($this->_isRest(), $id);
}
public function delete($id)
{
$this->BlackList->delete($this->_isRest(), $id);
return $this->BlackList->delete($this->_isRest(), $id);
}
public function massDelete()

View File

@ -27,21 +27,21 @@ class OrgBlacklistsController extends AppController
public function index()
{
$this->BlackList->index($this->_isRest());
return $this->BlackList->index($this->_isRest());
}
public function add()
{
$this->BlackList->add($this->_isRest());
return $this->BlackList->add($this->_isRest());
}
public function edit($id)
{
$this->BlackList->edit($this->_isRest(), $id);
return $this->BlackList->edit($this->_isRest(), $id);
}
public function delete($id)
{
$this->BlackList->delete($this->_isRest(), $id);
return $this->BlackList->delete($this->_isRest(), $id);
}
}

View File

@ -16,6 +16,8 @@ class EventBlacklist extends AppModel
public $blacklistFields = array('event_uuid', 'comment', 'event_info', 'event_orgc');
public $blacklistTarget = 'event';
public $validate = array(
'event_uuid' => array(
'unique' => array(

View File

@ -15,6 +15,8 @@ class OrgBlacklist extends AppModel
);
public $blacklistFields = array('org_uuid', 'comment', 'org_name');
public $blacklistTarget = 'event';
public $validate = array(
'org_uuid' => array(
'unique' => array(