Files
zoneminder/web/api/app/Controller/LogsController.php
T

172 lines
4.2 KiB
PHP

<?php
App::uses('AppController', 'Controller');
/**
* Logs Controller
*
* @property Log $Log
* @property PaginatorComponent $Paginator
*/
class LogsController extends AppController {
/**
* Components
*
* @var array
*/
public $components = array('Paginator', 'RequestHandler');
public $paginate = array(
'limit' => 100,
'order' => array( 'Log.TimeKey' => 'asc' ),
'paramType' => 'querystring'
);
public function beforeFilter() {
parent::beforeFilter();
global $user;
# We already tested for auth in appController, so we just need to test for specific permission
$canView = (!$user) || ($user->System() != 'None');
if (!$canView) {
throw new UnauthorizedException(__('Insufficient Privileges'));
return;
}
}
/**
* index method
*
* @return void
*/
public function index() {
$this->Log->recursive = -1;
$this->Paginator->settings = $this->paginate;
if ( $this->request->params['named'] ) {
$this->FilterComponent = $this->Components->load('Filter');
$this->Paginator->settings['conditions'] = $this->FilterComponent->buildFilter($this->request->params['named'], array_keys($this->Log->schema()));
}
$this->requireScalarSort();
$logs = $this->Paginator->paginate('Log');
$this->set(compact('logs'));
}
/**
* view method
*
* @throws NotFoundException
* @param string $id
* @return void
*/
public function view($id = null) {
if (!$this->Log->exists($id)) {
throw new NotFoundException(__('Invalid log'));
}
$options = array('conditions' => array('Log.' . $this->Log->primaryKey => $id));
$this->set(array(
'log' => $this->Log->find('first', $options),
'_serialize' => array('log')
));
}
/**
* add method
*
* @return void
*/
public function add() {
global $user;
$canAdd = (!$user) || (($user->System() == 'Edit') || ZM_LOG_INJECT);
if (!$canAdd) {
throw new UnauthorizedException(__('Insufficient privileges'));
return;
}
if (!$this->request->is('post')) {
throw new BadRequestException(__('Invalid method. Should be post'));
}
if ($this->request->is('post')) {
// ZM_LOG_INJECT lets non-admins add entries; never let one overwrite an existing entry.
$this->pinRequestId($this->Log, null);
$this->Log->create();
if ($this->Log->save($this->request->data)) {
$this->set(array(
'message' => 'Saved',
'id' => $this->Log->id,
'_serialize' => array('message', 'id')
));
return;
}
$this->response->statusCode(400);
$this->set(array(
'message' => $this->Log->validationErrors ? $this->Log->validationErrors : 'Error',
'id' => null,
'_serialize' => array('message', 'id')
));
}
}
/**
* edit method
*
* @throws NotFoundException
* @param string $id
* @return void
*/
public function edit($id = null) {
global $user;
$canEdit = (!$user) || ($user->System() == 'Edit');
if (!$canEdit) {
throw new UnauthorizedException(__('Insufficient privileges'));
return;
}
if (!$this->Log->exists($id)) {
throw new NotFoundException(__('Invalid log'));
}
if ($this->request->is(array('post', 'put'))) {
$this->pinRequestId($this->Log, $id);
if ($this->Log->save($this->request->data)) {
$this->set(array(
'message' => 'Saved',
'_serialize' => array('message')
));
return;
}
$this->response->statusCode(400);
$this->set(array(
'message' => $this->Log->validationErrors ? $this->Log->validationErrors : 'Error',
'_serialize' => array('message')
));
} else {
throw new BadRequestException(__('Invalid method. Should be post or put'));
}
}
/**
* delete method
*
* @throws NotFoundException
* @param string $id
* @return void
*/
public function delete($id = null) {
global $user;
$canDelete = (!$user) || ($user->System() == 'Edit');
if (!$canDelete) {
throw new UnauthorizedException(__('Insufficient privileges'));
return;
}
$this->Log->id = $id;
if (!$this->Log->exists()) {
throw new NotFoundException(__('Invalid log'));
}
$this->request->allowMethod('post', 'delete');
if ($this->Log->delete()) {
$this->set(array(
'message' => 'Deleted',
'_serialize' => array('message'),
));
return;
} else {
throw new InternalErrorException(__('The log could not be deleted.'));
}
}}