2017-12-05 04:52:16 +08:00
|
|
|
<?php
|
|
|
|
App::uses('AppController', 'Controller');
|
|
|
|
/**
|
|
|
|
* Groups Controller
|
|
|
|
*
|
|
|
|
* @property Group $Group
|
2018-03-05 10:20:48 +08:00
|
|
|
* @property PaginatorComponent $Paginator
|
2017-12-05 04:52:16 +08:00
|
|
|
*/
|
|
|
|
class GroupsController extends AppController {
|
|
|
|
/**
|
|
|
|
* Components
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2018-03-05 10:20:48 +08:00
|
|
|
public $components = array('Paginator', 'RequestHandler');
|
|
|
|
|
2018-03-07 01:29:59 +08:00
|
|
|
public function beforeFilter() {
|
|
|
|
parent::beforeFilter();
|
2018-12-21 23:01:48 +08:00
|
|
|
global $user;
|
|
|
|
# We already tested for auth in appController, so we just need to test for specific permission
|
|
|
|
$canView = (!$user) || ($user['Groups'] != 'None');
|
|
|
|
if ( !$canView ) {
|
2018-03-07 01:29:59 +08:00
|
|
|
throw new UnauthorizedException(__('Insufficient Privileges'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2017-12-05 04:52:16 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* index method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function index() {
|
2021-03-03 04:10:15 +08:00
|
|
|
$this->Group->recursive = 0;
|
|
|
|
|
|
|
|
if ( $this->request->params['named'] ) {
|
|
|
|
$this->FilterComponent = $this->Components->load('Filter');
|
|
|
|
$conditions = $this->FilterComponent->buildFilter($this->request->params['named']);
|
|
|
|
} else {
|
|
|
|
$conditions = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
$find_array = array(
|
|
|
|
'conditions' => &$conditions,
|
|
|
|
'contain' => array('Monitor'),
|
|
|
|
'joins' => array(
|
|
|
|
array(
|
|
|
|
'table' => 'Groups_Monitors',
|
|
|
|
'type' => 'left',
|
|
|
|
'conditions' => array(
|
|
|
|
'Groups_Monitors.GroupId = Group.Id',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
'group' => '`Group`.`Id`',
|
|
|
|
);
|
|
|
|
|
|
|
|
$groups = $this->Group->find('all', $find_array);
|
2017-12-05 04:52:16 +08:00
|
|
|
$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 = -1;
|
|
|
|
if (!$this->Group->exists($id)) {
|
|
|
|
throw new NotFoundException(__('Invalid group'));
|
|
|
|
}
|
|
|
|
$options = array('conditions' => array('Group.' . $this->Group->primaryKey => $id));
|
|
|
|
$group = $this->Group->find('first', $options);
|
|
|
|
$this->set(array(
|
|
|
|
'group' => $group,
|
|
|
|
'_serialize' => array('group')
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* add method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function add() {
|
2018-12-21 23:01:48 +08:00
|
|
|
if ( $this->request->is('post') ) {
|
2018-03-05 10:20:48 +08:00
|
|
|
|
2018-12-21 23:01:48 +08:00
|
|
|
global $user;
|
|
|
|
# We already tested for auth in appController,
|
|
|
|
# so we just need to test for specific permission
|
|
|
|
$canEdit = (!$user) || ($user['Groups'] == 'Edit');
|
|
|
|
if ( !$canEdit ) {
|
|
|
|
throw new UnauthorizedException(__('Insufficient Privileges'));
|
2018-03-07 01:29:59 +08:00
|
|
|
return;
|
2018-12-21 23:01:48 +08:00
|
|
|
}
|
2018-03-05 10:20:48 +08:00
|
|
|
|
2017-12-05 04:52:16 +08:00
|
|
|
$this->Group->create();
|
2019-12-16 04:31:40 +08:00
|
|
|
|
|
|
|
if ( $this->request->data['Group']['MonitorIds'] and ! isset($this->request->data['Monitor']) ) {
|
|
|
|
$this->request->data['Monitor'] = explode(',', $this->request->data['Group']['MonitorIds']);
|
|
|
|
unset($this->request->data['Group']['MonitorIds']);
|
|
|
|
}
|
|
|
|
if ( $this->Group->saveAssociated($this->request->data, array('atomic'=>true)) ) {
|
2018-12-21 23:01:48 +08:00
|
|
|
return $this->flash(
|
|
|
|
__('The group has been saved.'),
|
|
|
|
array('action' => 'index')
|
|
|
|
);
|
2019-12-16 04:31:40 +08:00
|
|
|
} else {
|
|
|
|
ZM\Error("Failed to save Group");
|
|
|
|
debug($this->Group->invalidFields());
|
|
|
|
}
|
|
|
|
} # end if post
|
|
|
|
$monitors = $this->Group->Monitor->find('list');
|
2017-12-05 04:52:16 +08:00
|
|
|
$this->set(compact('monitors'));
|
2019-12-16 04:31:40 +08:00
|
|
|
} # end add
|
2017-12-05 04:52:16 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* edit method
|
|
|
|
*
|
|
|
|
* @throws NotFoundException
|
|
|
|
* @param string $id
|
|
|
|
* @return void
|
|
|
|
*/
|
2018-12-21 23:01:48 +08:00
|
|
|
public function edit( $id = null ) {
|
|
|
|
if ( !$this->Group->exists($id) ) {
|
2017-12-05 04:52:16 +08:00
|
|
|
throw new NotFoundException(__('Invalid group'));
|
|
|
|
}
|
2018-03-07 01:29:59 +08:00
|
|
|
if ( $this->request->is(array('post', 'put'))) {
|
2018-12-21 23:01:48 +08:00
|
|
|
global $user;
|
|
|
|
# We already tested for auth in appController,
|
|
|
|
# so we just need to test for specific permission
|
|
|
|
$canEdit = (!$user) || ($user['Groups'] == 'Edit');
|
|
|
|
if ( !$canEdit ) {
|
|
|
|
throw new UnauthorizedException(__('Insufficient Privileges'));
|
2018-03-07 01:29:59 +08:00
|
|
|
return;
|
|
|
|
}
|
2020-12-04 02:14:19 +08:00
|
|
|
$this->Group->id = $id;
|
2018-12-21 23:01:48 +08:00
|
|
|
if ( $this->Group->save($this->request->data) ) {
|
2020-12-04 02:14:19 +08:00
|
|
|
$message = 'Saved';
|
2018-03-07 01:29:59 +08:00
|
|
|
} else {
|
|
|
|
$message = 'Error';
|
2020-12-04 02:14:19 +08:00
|
|
|
// if there is a validation message, use it
|
|
|
|
if ( !$this->group->validates() ) {
|
|
|
|
$message .= ': '.$this->Group->validationErrors;
|
|
|
|
}
|
2017-12-05 04:52:16 +08:00
|
|
|
}
|
2020-12-04 02:14:19 +08:00
|
|
|
} # end if post/put
|
|
|
|
|
|
|
|
$group = $this->Group->findById($id);
|
2018-03-05 10:20:48 +08:00
|
|
|
$this->set(array(
|
|
|
|
'message' => $message,
|
2020-12-04 02:14:19 +08:00
|
|
|
'group' => $group,
|
|
|
|
'_serialize' => array('group')
|
2018-03-05 10:20:48 +08:00
|
|
|
));
|
2017-12-05 04:52:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* delete method
|
|
|
|
*
|
|
|
|
* @throws NotFoundException
|
|
|
|
* @param string $id
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function delete($id = null) {
|
|
|
|
$this->Group->id = $id;
|
2018-12-21 23:01:48 +08:00
|
|
|
if ( !$this->Group->exists() ) {
|
2017-12-05 04:52:16 +08:00
|
|
|
throw new NotFoundException(__('Invalid group'));
|
|
|
|
}
|
|
|
|
$this->request->allowMethod('post', 'delete');
|
2018-03-05 10:20:48 +08:00
|
|
|
|
2018-12-21 23:01:48 +08:00
|
|
|
global $user;
|
|
|
|
# We already tested for auth in appController,
|
|
|
|
# so we just need to test for specific permission
|
|
|
|
$canEdit = (!$user) || ($user['Groups'] == 'Edit');
|
|
|
|
if ( !$canEdit ) {
|
|
|
|
throw new UnauthorizedException(__('Insufficient Privileges'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $this->Group->delete() ) {
|
|
|
|
return $this->flash(
|
|
|
|
__('The group has been deleted.'),
|
|
|
|
array('action' => 'index')
|
|
|
|
);
|
2017-12-05 04:52:16 +08:00
|
|
|
} else {
|
2018-12-21 23:01:48 +08:00
|
|
|
return $this->flash(
|
|
|
|
__('The group could not be deleted. Please, try again.'),
|
|
|
|
array('action' => 'index')
|
|
|
|
);
|
2017-12-05 04:52:16 +08:00
|
|
|
}
|
2020-05-06 04:32:54 +08:00
|
|
|
} // end function delete
|
|
|
|
|
|
|
|
// returns monitor associations
|
|
|
|
public function associations() {
|
|
|
|
$this->Group->recursive = -1;
|
|
|
|
$groups = $this->Group->find('all', array(
|
|
|
|
'contain'=> array(
|
|
|
|
'Monitor' => array(
|
|
|
|
'fields'=>array('Id','Name')
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->set(array(
|
|
|
|
'groups' => $groups,
|
|
|
|
'_serialize' => array('groups')
|
|
|
|
));
|
|
|
|
} // end associations
|
|
|
|
|
2018-03-07 01:29:59 +08:00
|
|
|
} // end class GroupController
|