From c35b28a3d13dbf08a27c19fa8fb21b111b35bc53 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Wed, 30 Nov 2022 15:11:04 -0500 Subject: [PATCH] Rough in api support for snapshots --- .../app/Controller/SnapshotsController.php | 215 ++++++++++++++++++ web/api/app/Model/Snapshot.php | 77 +++++++ web/api/app/View/Snapshots/json/edit.ctp | 2 + web/api/app/View/Snapshots/json/index.ctp | 1 + web/api/app/View/Snapshots/json/view.ctp | 1 + web/api/app/View/Snapshots/xml/edit.ctp | 2 + web/api/app/View/Snapshots/xml/index.ctp | 2 + web/api/app/View/Snapshots/xml/view.ctp | 2 + 8 files changed, 302 insertions(+) create mode 100644 web/api/app/Controller/SnapshotsController.php create mode 100644 web/api/app/Model/Snapshot.php create mode 100644 web/api/app/View/Snapshots/json/edit.ctp create mode 100644 web/api/app/View/Snapshots/json/index.ctp create mode 100644 web/api/app/View/Snapshots/json/view.ctp create mode 100644 web/api/app/View/Snapshots/xml/edit.ctp create mode 100644 web/api/app/View/Snapshots/xml/index.ctp create mode 100644 web/api/app/View/Snapshots/xml/view.ctp diff --git a/web/api/app/Controller/SnapshotsController.php b/web/api/app/Controller/SnapshotsController.php new file mode 100644 index 000000000..b1562a3dc --- /dev/null +++ b/web/api/app/Controller/SnapshotsController.php @@ -0,0 +1,215 @@ +Snapshot->recursive = 0; + + if ( $this->request->params['named'] ) { + $this->FilterComponent = $this->Components->load('Filter'); + $conditions = $this->FilterComponent->buildFilter($this->request->params['named']); + } else { + $conditions = array(); + } + + $find_array = array( + 'conditions' => &$conditions, + 'contain' => array('Event'), + 'joins' => array( + array( + 'table' => 'Snapshots_Events', + 'type' => 'left', + 'conditions' => array( + 'Snapshots_Events.SnapshotId = Snapshot.Id', + ), + ), + ), + 'snapshot' => '`Snapshot`.`Id`', + ); + + $snapshots = $this->Snapshot->find('all', $find_array); + $this->set(array( + 'snapshots' => $snapshots, + '_serialize' => array('snapshots') + )); + } + +/** + * view method + * + * @throws NotFoundException + * @param string $id + * @return void + */ + public function view($id = null) { + $this->Snapshot->recursive = -1; + if (!$this->Snapshot->exists($id)) { + throw new NotFoundException(__('Invalid snapshot')); + } + $options = array('conditions' => array('Snapshot.' . $this->Snapshot->primaryKey => $id)); + $snapshot = $this->Snapshot->find('first', $options); + $this->set(array( + 'snapshot' => $snapshot, + '_serialize' => array('snapshot') + )); + } + +/** + * add method + * + * @return void + */ + public function add() { + if ( $this->request->is('post') ) { + + global $user; + # We already tested for auth in appController, + # so we just need to test for specific permission + $canEdit = (!$user) || ($user['Snapshots'] == 'Edit'); + if ( !$canEdit ) { + throw new UnauthorizedException(__('Insufficient Privileges')); + return; + } + + $this->Snapshot->create(); + + if ( $this->request->data['Snapshot']['EventIds'] and ! isset($this->request->data['Event']) ) { + $this->request->data['Event'] = explode(',', $this->request->data['Snapshot']['EventIds']); + unset($this->request->data['Snapshot']['EventIds']); + } + if ( $this->Snapshot->saveAssociated($this->request->data, array('atomic'=>true)) ) { + return $this->flash( + __('The snapshot has been saved.'), + array('action' => 'index') + ); + } else { + ZM\Error("Failed to save Snapshot"); + debug($this->Snapshot->invalidFields()); + } + } # end if post + $monitors = $this->Snapshot->Event->find('list'); + $this->set(compact('monitors')); + } # end add + +/** + * edit method + * + * @throws NotFoundException + * @param string $id + * @return void + */ + public function edit( $id = null ) { + if ( !$this->Snapshot->exists($id) ) { + throw new NotFoundException(__('Invalid snapshot')); + } + if ( $this->request->is(array('post', 'put'))) { + global $user; + # We already tested for auth in appController, + # so we just need to test for specific permission + $canEdit = (!$user) || ($user['Snapshots'] == 'Edit'); + if ( !$canEdit ) { + throw new UnauthorizedException(__('Insufficient Privileges')); + return; + } + $this->Snapshot->id = $id; + if ( $this->Snapshot->save($this->request->data) ) { + $message = 'Saved'; + } else { + $message = 'Error'; + // if there is a validation message, use it + if ( !$this->snapshot->validates() ) { + $message .= ': '.$this->Snapshot->validationErrors; + } + } + } # end if post/put + + $snapshot = $this->Snapshot->findById($id); + $this->set(array( + 'message' => $message, + 'snapshot' => $snapshot, + '_serialize' => array('snapshot') + )); + } + +/** + * delete method + * + * @throws NotFoundException + * @param string $id + * @return void + */ + public function delete($id = null) { + $this->Snapshot->id = $id; + if ( !$this->Snapshot->exists() ) { + throw new NotFoundException(__('Invalid snapshot')); + } + $this->request->allowMethod('post', 'delete'); + + global $user; + # We already tested for auth in appController, + # so we just need to test for specific permission + $canEdit = (!$user) || ($user['Snapshots'] == 'Edit'); + if ( !$canEdit ) { + throw new UnauthorizedException(__('Insufficient Privileges')); + return; + } + + if ( $this->Snapshot->delete() ) { + return $this->flash( + __('The snapshot has been deleted.'), + array('action' => 'index') + ); + } else { + return $this->flash( + __('The snapshot could not be deleted. Please, try again.'), + array('action' => 'index') + ); + } + } // end function delete + + // returns monitor associations + public function associations() { + $this->Snapshot->recursive = -1; + $snapshots = $this->Snapshot->find('all', array( + 'contain'=> array( + 'Event' => array( + 'fields'=>array('Id','Name') + ) + ) + ) + ); + $this->set(array( + 'snapshots' => $snapshots, + '_serialize' => array('snapshots') + )); + } // end associations + +} // end class SnapshotController diff --git a/web/api/app/Model/Snapshot.php b/web/api/app/Model/Snapshot.php new file mode 100644 index 000000000..4bd19a250 --- /dev/null +++ b/web/api/app/Model/Snapshot.php @@ -0,0 +1,77 @@ + array( + 'numeric' => array( + 'rule' => array('numeric'), + //'message' => 'Your custom message here', + //'allowEmpty' => false, + //'required' => false, + //'last' => false, // Stop validation after this rule + //'on' => 'create', // Limit validation to 'create' or 'update' operations + ), + ), + ); + + //The Associations below have been created with all possible keys, those that are not needed can be removed + +/** + * hasAndBelongsToMany associations + * + * @var array + */ + public $hasAndBelongsToMany = array( + 'Event' => array( + 'className' => 'Snapshot', + 'joinTable' => 'Snapshots_Events', + 'foreignKey' => 'EventId', + 'associationForeignKey' => 'SnapshotId', + 'unique'=>true, + 'dependent' => false, + 'conditions' => '', + 'fields' => '', + 'order' => '', + 'limit' => '', + 'offset' => '', + 'exclusive' => '', + 'finderQuery' => '', + 'counterQuery' => '' + ), + ); +} diff --git a/web/api/app/View/Snapshots/json/edit.ctp b/web/api/app/View/Snapshots/json/edit.ctp new file mode 100644 index 000000000..519e08bdd --- /dev/null +++ b/web/api/app/View/Snapshots/json/edit.ctp @@ -0,0 +1,2 @@ +echo json_encode($message); +echo json_encode($snapshot); diff --git a/web/api/app/View/Snapshots/json/index.ctp b/web/api/app/View/Snapshots/json/index.ctp new file mode 100644 index 000000000..81df737a7 --- /dev/null +++ b/web/api/app/View/Snapshots/json/index.ctp @@ -0,0 +1 @@ +echo json_encode($snapshots); diff --git a/web/api/app/View/Snapshots/json/view.ctp b/web/api/app/View/Snapshots/json/view.ctp new file mode 100644 index 000000000..55176bf1c --- /dev/null +++ b/web/api/app/View/Snapshots/json/view.ctp @@ -0,0 +1 @@ +echo json_encode($snapshot); diff --git a/web/api/app/View/Snapshots/xml/edit.ctp b/web/api/app/View/Snapshots/xml/edit.ctp new file mode 100644 index 000000000..09fb8979a --- /dev/null +++ b/web/api/app/View/Snapshots/xml/edit.ctp @@ -0,0 +1,2 @@ +$xml = Xml::fromArray(array('response' => $message)); +echo $xml->asXML(); diff --git a/web/api/app/View/Snapshots/xml/index.ctp b/web/api/app/View/Snapshots/xml/index.ctp new file mode 100644 index 000000000..8f45dfd14 --- /dev/null +++ b/web/api/app/View/Snapshots/xml/index.ctp @@ -0,0 +1,2 @@ +$xml = Xml::fromArray(array('response' => $groups)); +echo $xml->asXML(); diff --git a/web/api/app/View/Snapshots/xml/view.ctp b/web/api/app/View/Snapshots/xml/view.ctp new file mode 100644 index 000000000..b54cad5ca --- /dev/null +++ b/web/api/app/View/Snapshots/xml/view.ctp @@ -0,0 +1,2 @@ +$xml = Xml::fromArray(array('response' => $group)); +echo $xml->asXML();