Merge pull request #2050 from Tim-Craig/groups_api
Adding group handling in API
This commit is contained in:
commit
0422f93202
|
@ -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();
|
||||
|
||||
/**
|
||||
|
|
|
@ -0,0 +1,145 @@
|
|||
<?php
|
||||
App::uses('AppController', 'Controller');
|
||||
/**
|
||||
* Groups Controller
|
||||
*
|
||||
* @property Group $Group
|
||||
* @property PaginatorComponent $Paginator
|
||||
*/
|
||||
class GroupsController extends AppController {
|
||||
|
||||
|
||||
/**
|
||||
* Components
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $components = array('Paginator', 'RequestHandler');
|
||||
|
||||
|
||||
public function beforeFilter() {
|
||||
parent::beforeFilter();
|
||||
$canView = $this->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'));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,52 @@
|
|||
<?php
|
||||
App::uses('AppModel', 'Model');
|
||||
/**
|
||||
* Group Model
|
||||
*
|
||||
* @property Event $Event
|
||||
* @property Zone $Zone
|
||||
*/
|
||||
class Group extends AppModel {
|
||||
|
||||
/**
|
||||
* Use table
|
||||
*
|
||||
* @var mixed False or table name
|
||||
*/
|
||||
public $useTable = 'Groups';
|
||||
|
||||
/**
|
||||
* Primary key field
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $primaryKey = 'Id';
|
||||
|
||||
/**
|
||||
* Display field
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $displayField = 'Name';
|
||||
|
||||
public $recursive = -1;
|
||||
|
||||
/**
|
||||
* Validation rules
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $validate = array(
|
||||
'Id' => 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
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
echo json_encode($message);
|
||||
echo json_encode($group);
|
|
@ -0,0 +1 @@
|
|||
echo json_encode($groups);
|
|
@ -0,0 +1 @@
|
|||
echo json_encode($group);
|
|
@ -0,0 +1,2 @@
|
|||
$xml = Xml::fromArray(array('response' => $message));
|
||||
echo $xml->asXML();
|
|
@ -0,0 +1,2 @@
|
|||
$xml = Xml::fromArray(array('response' => $groups));
|
||||
echo $xml->asXML();
|
|
@ -0,0 +1,2 @@
|
|||
$xml = Xml::fromArray(array('response' => $group));
|
||||
echo $xml->asXML();
|
Loading…
Reference in New Issue