2016-05-02 22:37:19 +08:00
|
|
|
<?php
|
|
|
|
App::uses('AppController', 'Controller');
|
|
|
|
/**
|
|
|
|
* Servers Controller
|
|
|
|
*
|
|
|
|
* @property Server $Server
|
|
|
|
* @property PaginatorComponent $Paginator
|
|
|
|
*/
|
|
|
|
class ServersController extends AppController {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Components
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public $components = array('Paginator', 'RequestHandler');
|
|
|
|
|
2018-08-08 21:59:46 +08:00
|
|
|
public function beforeFilter() {
|
|
|
|
parent::beforeFilter();
|
2020-03-03 05:39:12 +08:00
|
|
|
/*
|
|
|
|
* A user needs the server data to calculate how to view a monitor, and there really isn't anything sensitive in this data.
|
|
|
|
* So it has been decided for now to just let everyone read it.
|
|
|
|
|
2018-08-08 21:59:46 +08:00
|
|
|
global $user;
|
|
|
|
$canView = (!$user) || ($user['System'] != 'None');
|
|
|
|
if ( !$canView ) {
|
|
|
|
throw new UnauthorizedException(__('Insufficient Privileges'));
|
|
|
|
return;
|
|
|
|
}
|
2020-03-03 05:39:12 +08:00
|
|
|
*/
|
2016-05-02 22:37:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* index method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function index() {
|
|
|
|
$this->Server->recursive = 0;
|
|
|
|
|
2018-08-08 21:59:46 +08:00
|
|
|
$options = '';
|
2020-03-03 05:39:12 +08:00
|
|
|
$servers = $this->Server->find('all', $options);
|
2016-05-02 22:37:19 +08:00
|
|
|
$this->set(array(
|
|
|
|
'servers' => $servers,
|
|
|
|
'_serialize' => array('servers')
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* view method
|
|
|
|
*
|
|
|
|
* @throws NotFoundException
|
|
|
|
* @param string $id
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function view($id = null) {
|
|
|
|
$this->Server->recursive = 0;
|
2020-03-03 05:39:12 +08:00
|
|
|
if ( !$this->Server->exists($id) ) {
|
2016-05-02 22:37:19 +08:00
|
|
|
throw new NotFoundException(__('Invalid server'));
|
|
|
|
}
|
|
|
|
$restricted = '';
|
|
|
|
|
|
|
|
$options = array('conditions' => array(
|
2020-03-03 05:39:12 +08:00
|
|
|
array('Server.'.$this->Server->primaryKey => $id),
|
2016-05-02 22:37:19 +08:00
|
|
|
$restricted
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$server = $this->Server->find('first', $options);
|
|
|
|
$this->set(array(
|
|
|
|
'server' => $server,
|
|
|
|
'_serialize' => array('server')
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* add method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function add() {
|
2018-08-08 21:59:46 +08:00
|
|
|
if ( $this->request->is('post') ) {
|
2016-05-02 22:37:19 +08:00
|
|
|
|
2018-08-08 21:59:46 +08:00
|
|
|
global $user;
|
|
|
|
$canEdit = (!$user) || ($user['System'] == 'Edit');
|
|
|
|
if ( !$canEdit ) {
|
|
|
|
throw new UnauthorizedException(__('Insufficient privileges'));
|
2016-05-02 22:37:19 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->Server->create();
|
2018-08-08 21:59:46 +08:00
|
|
|
if ( $this->Server->save($this->request->data) ) {
|
2016-05-02 22:37:19 +08:00
|
|
|
return $this->flash(__('The server has been saved.'), array('action' => 'index'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* edit method
|
|
|
|
*
|
|
|
|
* @throws NotFoundException
|
|
|
|
* @param string $id
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function edit($id = null) {
|
|
|
|
$this->Server->id = $id;
|
|
|
|
|
2018-08-08 21:59:46 +08:00
|
|
|
global $user;
|
|
|
|
$canEdit = (!$user) || ($user['System'] == 'Edit');
|
|
|
|
if ( !$canEdit ) {
|
|
|
|
throw new UnauthorizedException(__('Insufficient privileges'));
|
2016-05-02 22:37:19 +08:00
|
|
|
return;
|
|
|
|
}
|
2018-08-08 21:59:46 +08:00
|
|
|
|
|
|
|
if ( !$this->Server->exists($id) ) {
|
|
|
|
throw new NotFoundException(__('Invalid server'));
|
|
|
|
}
|
|
|
|
if ( $this->Server->save($this->request->data) ) {
|
2016-05-02 22:37:19 +08:00
|
|
|
$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) {
|
2018-08-08 21:59:46 +08:00
|
|
|
global $user;
|
|
|
|
$canEdit = (!$user) || ($user['System'] == 'Edit');
|
|
|
|
if ( !$canEdit ) {
|
|
|
|
throw new UnauthorizedException(__('Insufficient privileges'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-02 22:37:19 +08:00
|
|
|
$this->Server->id = $id;
|
2018-08-08 21:59:46 +08:00
|
|
|
if ( !$this->Server->exists() ) {
|
2016-05-02 22:37:19 +08:00
|
|
|
throw new NotFoundException(__('Invalid server'));
|
|
|
|
}
|
|
|
|
$this->request->allowMethod('post', 'delete');
|
|
|
|
|
2018-08-08 21:59:46 +08:00
|
|
|
if ( $this->Server->delete() ) {
|
2016-05-02 22:37:19 +08:00
|
|
|
return $this->flash(__('The server has been deleted.'), array('action' => 'index'));
|
|
|
|
} else {
|
|
|
|
return $this->flash(__('The server could not be deleted. Please, try again.'), array('action' => 'index'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|