From 0654c7e3b24ded1037394e288a070ff101f040fa Mon Sep 17 00:00:00 2001 From: tim Date: Sun, 4 Mar 2018 18:20:48 -0800 Subject: [PATCH] Adding group handling in API --- web/api/app/Config/routes.php | 3 +- web/api/app/Controller/GroupsController.php | 145 ++++++++++++++++++++ web/api/app/Model/Group.php | 52 +++++++ web/api/app/View/Groups/json/edit.ctp | 2 + web/api/app/View/Groups/json/index.ctp | 1 + web/api/app/View/Groups/json/view.ctp | 1 + web/api/app/View/Groups/xml/edit.ctp | 2 + web/api/app/View/Groups/xml/index.ctp | 2 + web/api/app/View/Groups/xml/view.ctp | 2 + 9 files changed, 209 insertions(+), 1 deletion(-) create mode 100644 web/api/app/Controller/GroupsController.php create mode 100644 web/api/app/Model/Group.php create mode 100644 web/api/app/View/Groups/json/edit.ctp create mode 100644 web/api/app/View/Groups/json/index.ctp create mode 100644 web/api/app/View/Groups/json/view.ctp create mode 100644 web/api/app/View/Groups/xml/edit.ctp create mode 100644 web/api/app/View/Groups/xml/index.ctp create mode 100644 web/api/app/View/Groups/xml/view.ctp diff --git a/web/api/app/Config/routes.php b/web/api/app/Config/routes.php index 0f9343644..1eaaa9cd4 100644 --- a/web/api/app/Config/routes.php +++ b/web/api/app/Config/routes.php @@ -35,7 +35,8 @@ /* Add new API to retrieve camera controls - for PTZ */ /* refer to https://github.com/ZoneMinder/ZoneMinder/issues/799#issuecomment-105233112 */ - Router::mapResources('controls'); + Router::mapResources('controls'); + Router::mapResources('groups'); Router::parseExtensions(); /** diff --git a/web/api/app/Controller/GroupsController.php b/web/api/app/Controller/GroupsController.php new file mode 100644 index 000000000..907b7f59e --- /dev/null +++ b/web/api/app/Controller/GroupsController.php @@ -0,0 +1,145 @@ +Session->Read('groupsPermission'); + if ($canView =='None') + { + throw new UnauthorizedException(__('Insufficient Privileges')); + return; + } + +} + + +/** + * index method + * + * @return void + */ + public function index() { + $this->Group->recursive = 0; + $groups = $this->Group->find('all'); + $this->set(array( + 'groups' => $groups, + '_serialize' => array('groups') + )); + } + +/** + * view method + * + * @throws NotFoundException + * @param string $id + * @return void + */ + public function view($id = null) { + $this->Group->recursive = 0; + if (!$this->Group->exists($id)) { + throw new NotFoundException(__('Invalid group')); + } + + $group = $this->Group->find('first'); + $this->set(array( + 'group' => $group, + '_serialize' => array('group') + )); + } + +/** + * add method + * + * @return void + */ + public function add() { + if ($this->request->is('post')) { + + if ($this->Session->Read('groupPermission') != 'Edit') + { + throw new UnauthorizedException(__('Insufficient privileges')); + return; + } + + $this->Group->create(); + if ($this->Group->save($this->request->data)) { + return $this->flash(__('The group has been saved.'), array('action' => 'index')); + } + } + } + +/** + * edit method + * + * @throws NotFoundException + * @param string $id + * @return void + */ + public function edit($id = null) { + $this->Group->id = $id; + + if (!$this->Group->exists($id)) { + throw new NotFoundException(__('Invalid group')); + } + if ($this->Session->Read('groupPermission') != 'Edit') + { + throw new UnauthorizedException(__('Insufficient privileges')); + return; + } + if ($this->Group->save($this->request->data)) { + $message = 'Saved'; + } else { + $message = 'Error'; + } + + $this->set(array( + 'message' => $message, + '_serialize' => array('message') + )); + } + +/** + * delete method + * + * @throws NotFoundException + * @param string $id + * @return void + */ + public function delete($id = null) { + $this->Group->id = $id; + if (!$this->Group->exists()) { + throw new NotFoundException(__('Invalid group')); + } + if ($this->Session->Read('groupPermission') != 'Edit') + { + throw new UnauthorizedException(__('Insufficient privileges')); + return; + } + $this->request->allowMethod('post', 'delete'); + + + if ($this->Group->delete()) { + return $this->flash(__('The group has been deleted.'), array('action' => 'index')); + } else { + return $this->flash(__('The group could not be deleted. Please, try again.'), array('action' => 'index')); + } + } + +} + diff --git a/web/api/app/Model/Group.php b/web/api/app/Model/Group.php new file mode 100644 index 000000000..99b0c7227 --- /dev/null +++ b/web/api/app/Model/Group.php @@ -0,0 +1,52 @@ + 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 + ), + ), + ); + +} diff --git a/web/api/app/View/Groups/json/edit.ctp b/web/api/app/View/Groups/json/edit.ctp new file mode 100644 index 000000000..ce19d17e8 --- /dev/null +++ b/web/api/app/View/Groups/json/edit.ctp @@ -0,0 +1,2 @@ +echo json_encode($message); +echo json_encode($group); diff --git a/web/api/app/View/Groups/json/index.ctp b/web/api/app/View/Groups/json/index.ctp new file mode 100644 index 000000000..330bf9262 --- /dev/null +++ b/web/api/app/View/Groups/json/index.ctp @@ -0,0 +1 @@ +echo json_encode($groups); diff --git a/web/api/app/View/Groups/json/view.ctp b/web/api/app/View/Groups/json/view.ctp new file mode 100644 index 000000000..04dc11392 --- /dev/null +++ b/web/api/app/View/Groups/json/view.ctp @@ -0,0 +1 @@ +echo json_encode($group); diff --git a/web/api/app/View/Groups/xml/edit.ctp b/web/api/app/View/Groups/xml/edit.ctp new file mode 100644 index 000000000..09fb8979a --- /dev/null +++ b/web/api/app/View/Groups/xml/edit.ctp @@ -0,0 +1,2 @@ +$xml = Xml::fromArray(array('response' => $message)); +echo $xml->asXML(); diff --git a/web/api/app/View/Groups/xml/index.ctp b/web/api/app/View/Groups/xml/index.ctp new file mode 100644 index 000000000..8f45dfd14 --- /dev/null +++ b/web/api/app/View/Groups/xml/index.ctp @@ -0,0 +1,2 @@ +$xml = Xml::fromArray(array('response' => $groups)); +echo $xml->asXML(); diff --git a/web/api/app/View/Groups/xml/view.ctp b/web/api/app/View/Groups/xml/view.ctp new file mode 100644 index 000000000..b54cad5ca --- /dev/null +++ b/web/api/app/View/Groups/xml/view.ctp @@ -0,0 +1,2 @@ +$xml = Xml::fromArray(array('response' => $group)); +echo $xml->asXML();