diff --git a/app/Controller/ApiController.php b/app/Controller/ApiController.php index 6251b6aa9..f58196f4d 100644 --- a/app/Controller/ApiController.php +++ b/app/Controller/ApiController.php @@ -120,8 +120,6 @@ class ApiController extends AppController } $rest_history_item = array( - 'org_id' => $this->Auth->user('org_id'), - 'user_id' => $this->Auth->user('id'), 'headers' => $logHeaders, 'body' => $historyBody, 'url' => $request['url'], @@ -223,9 +221,7 @@ class ApiController extends AppController $rest_history_item['outcome'] = $response->code; $this->loadModel('RestClientHistory'); - $this->RestClientHistory->create(); - $this->RestClientHistory->save($rest_history_item); - $this->RestClientHistory->cleanup($this->Auth->user('id')); + $this->RestClientHistory->insert($this->Auth->user(), $rest_history_item); return $viewData; } diff --git a/app/Controller/RestClientHistoryController.php b/app/Controller/RestClientHistoryController.php index fa84a4bba..91380f6c3 100644 --- a/app/Controller/RestClientHistoryController.php +++ b/app/Controller/RestClientHistoryController.php @@ -1,7 +1,9 @@ _isRest()) { $list = $this->RestClientHistory->find('all', $params); - } else { - $this->paginate = array_merge($this->paginate, $params); - $list = $this->paginate(); - } - if ($this->_isRest()) { return $this->RestResponse->viewData($list, $this->response->type()); - } else { - $this->set('bookmarked', $bookmarked); - $this->set('list', $list); - $this->layout = false; - $this->autoRender = false; - $this->render('index'); } + + $this->paginate = array_merge($this->paginate, $params); + $list = $this->paginate(); + $this->set('bookmarked', $bookmarked); + $this->set('list', array_column($list, 'RestClientHistory')); + $this->layout = false; + $this->autoRender = false; + $this->render('index'); } public function delete($id) diff --git a/app/Model/RestClientHistory.php b/app/Model/RestClientHistory.php index e6121dc91..bc64d1885 100644 --- a/app/Model/RestClientHistory.php +++ b/app/Model/RestClientHistory.php @@ -4,19 +4,35 @@ App::uses('AppModel', 'Model'); class RestClientHistory extends AppModel { public $belongsTo = array( - 'Org' => array( - 'className' => 'Organisation', - 'foreignKey' => 'org_id', - 'order' => array(), - 'fields' => array('id', 'name', 'uuid') - ), - 'User' => array( - 'className' => 'User', - 'foreignKey' => 'user_id', - 'order' => array(), - 'fields' => array('id', 'email') - ), - ); + 'Org' => array( + 'className' => 'Organisation', + 'foreignKey' => 'org_id', + 'order' => array(), + 'fields' => array('id', 'name', 'uuid') + ), + 'User' => array( + 'className' => 'User', + 'foreignKey' => 'user_id', + 'order' => array(), + 'fields' => array('id', 'email') + ), + ); + + /** + * @param array $user + * @param array $history + * @return void + * @throws Exception + */ + public function insert(array $user, array $history) + { + $history['org_id'] = $user['org_id']; + $history['user_id'] = $user['id']; + + $this->create(); + $this->save($history, ['atomic' => false]); + $this->cleanup($user['id']); + } public function cleanup($user_id) { @@ -35,6 +51,6 @@ class RestClientHistory extends AppModel 'NOT' => array( 'RestClientHistory.id' => $keepIds ) - )); + ), false); } } diff --git a/app/View/RestClientHistory/index.ctp b/app/View/RestClientHistory/index.ctp index a753cc0cc..28e96f4f2 100644 --- a/app/View/RestClientHistory/index.ctp +++ b/app/View/RestClientHistory/index.ctp @@ -2,46 +2,46 @@ $data_container = $bookmarked ? 'rest_client_bookmarks' : 'rest_client_history'; foreach ($list as $k => $item) { $name = ''; - if (!empty($item['RestClientHistory']['bookmark_name'])) { + if (!empty($item['bookmark_name'])) { $name = sprintf( '%s - ', - h($item['RestClientHistory']['bookmark_name']) + h($item['bookmark_name']) ); } $name .= sprintf( '%s - %s', - h($item['RestClientHistory']['http_method']), + h($item['http_method']), sprintf( - '%s', + '%s', $k, $data_container, - h($item['RestClientHistory']['url']) + h($item['url']) ) ); - $colour = 'green'; - if (intval($item['RestClientHistory']['outcome']) >= 300) { - $colour = 'orange'; - } - if (intval($item['RestClientHistory']['outcome']) >= 400) { + if (intval($item['outcome']) >= 400) { $colour = 'red'; + } else if (intval($item['outcome']) >= 300) { + $colour = 'orange'; + } else { + $colour = 'green'; } echo sprintf( '