mirror of
https://github.com/ZoneMinder/zoneminder.git
synced 2026-10-07 09:52:03 -04:00
The State model still declared primaryKey = 'Name' from before
zm_update-1.28.99 gave States an Id primary key. The REST routes only
match numeric ids, so states/<id>.json view/edit/delete looked up a
state *named* <id> and always failed, and names don't match the routes.
Through states/edit/<name>.json, edit never set the record id and
inserted a new nameless state instead of updating; view had no
_serialize and returned 500.
Use the default Id key. view serializes the state; edit sets the id,
accepts POST/PUT only and, like add, answers {message: Saved} or the
validation errors (as Monitors and Zones do) instead of an empty flash.
Names must be non-empty and unique, since zmpkg.pl and
states/change/<name> select states by name. index, change and delete
are unchanged.
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
172 lines
3.8 KiB
PHP
172 lines
3.8 KiB
PHP
<?php
|
|
App::uses('AppController', 'Controller');
|
|
/**
|
|
* States Controller
|
|
*
|
|
* @property State $State
|
|
* @property PaginatorComponent $Paginator
|
|
*/
|
|
|
|
class StatesController extends AppController {
|
|
|
|
public $components = array('RequestHandler');
|
|
|
|
public function beforeFilter() {
|
|
parent::beforeFilter();
|
|
global $user;
|
|
$canView = (!$user) || ($user->System() != 'None');
|
|
if ( !$canView ) {
|
|
throw new UnauthorizedException(__('Insufficient Privileges'));
|
|
return;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* index method
|
|
*
|
|
* @return void
|
|
*/
|
|
public function index() {
|
|
$this->State->recursive = 0;
|
|
$states = $this->State->find('all');
|
|
$this->set(array(
|
|
'states' => $states,
|
|
'_serialize' => array('states')
|
|
));
|
|
}
|
|
|
|
/**
|
|
* view method
|
|
*
|
|
* @throws NotFoundException
|
|
* @param string $id
|
|
* @return void
|
|
*/
|
|
public function view($id = null) {
|
|
if ( !$this->State->exists($id) ) {
|
|
throw new NotFoundException(__('Invalid state'));
|
|
}
|
|
$options = array('conditions' => array('State.' . $this->State->primaryKey => $id));
|
|
$this->set(array(
|
|
'state' => $this->State->find('first', $options),
|
|
'_serialize' => array('state')
|
|
));
|
|
}
|
|
|
|
/**
|
|
* add method
|
|
*
|
|
* @return void
|
|
*/
|
|
public function add() {
|
|
|
|
if ($this->request->is('post')) {
|
|
|
|
global $user;
|
|
$canEdit = (!$user) || ($user->System() == 'Edit');
|
|
if ( !$canEdit ) {
|
|
throw new UnauthorizedException(__('Insufficient privileges'));
|
|
return;
|
|
}
|
|
|
|
$this->State->create();
|
|
if ($this->State->save($this->request->data)) {
|
|
$message = 'Saved';
|
|
} else {
|
|
$message = $this->State->validationErrors ? $this->State->validationErrors : 'Error';
|
|
}
|
|
$this->set(array(
|
|
'message' => $message,
|
|
'_serialize' => array('message')
|
|
));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* edit method
|
|
*
|
|
* @throws NotFoundException
|
|
* @param string $id
|
|
* @return void
|
|
*/
|
|
public function edit($id = null) {
|
|
if (!$this->State->exists($id)) {
|
|
throw new NotFoundException(__('Invalid state'));
|
|
}
|
|
|
|
global $user;
|
|
$canEdit = (!$user) || ($user->System() == 'Edit');
|
|
if ( !$canEdit ) {
|
|
throw new UnauthorizedException(__('Insufficient privileges'));
|
|
return;
|
|
}
|
|
|
|
$this->request->allowMethod('post', 'put');
|
|
// Without the id, save() inserts a new state instead of updating this one.
|
|
$this->State->id = $id;
|
|
if ( $this->State->save($this->request->data) ) {
|
|
$message = 'Saved';
|
|
} else {
|
|
$message = $this->State->validationErrors ? $this->State->validationErrors : 'Error';
|
|
}
|
|
$this->set(array(
|
|
'message' => $message,
|
|
'_serialize' => array('message')
|
|
));
|
|
}
|
|
|
|
/**
|
|
* delete method
|
|
*
|
|
* @throws NotFoundException
|
|
* @param string $id
|
|
* @return void
|
|
*/
|
|
public function delete($id = null) {
|
|
$this->State->id = $id;
|
|
global $user;
|
|
$canEdit = (!$user) || ($user->System() == 'Edit');
|
|
if ( !$canEdit ) {
|
|
throw new UnauthorizedException(__('Insufficient privileges'));
|
|
return;
|
|
}
|
|
|
|
if (!$this->State->exists()) {
|
|
throw new NotFoundException(__('Invalid state'));
|
|
}
|
|
$this->request->allowMethod('post', 'delete');
|
|
if ($this->State->delete()) {
|
|
return $this->flash(__('The state has been deleted.'), array('action' => 'index'));
|
|
} else {
|
|
return $this->flash(__('The state could not be deleted. Please, try again.'), array('action' => 'index'));
|
|
}
|
|
}
|
|
|
|
public function change() {
|
|
global $user;
|
|
$canEdit = (!$user) || ($user->System() == 'Edit');
|
|
if ( !$canEdit ) {
|
|
throw new UnauthorizedException(__('Insufficient privileges'));
|
|
return;
|
|
}
|
|
|
|
$newState = $this->request->params['pass'][0];
|
|
$blah = $this->packageControl($newState);
|
|
|
|
$this->set(array(
|
|
'blah' => $blah,
|
|
'_serialize' => array('blah')
|
|
));
|
|
}
|
|
|
|
public function packageControl( $command ) {
|
|
$zm_path_bin = Configure::read('ZM_PATH_BIN');
|
|
$string = $zm_path_bin.'/zmpkg.pl '.escapeshellarg( $command );
|
|
$status = exec( $string );
|
|
|
|
return $status;
|
|
}
|
|
|
|
|
|
}
|