Command masks: Add validator and change validation flow

refs #4355
This commit is contained in:
Marius Hein 2013-07-18 13:46:12 +02:00
parent e9c9c9de87
commit 89e7fa6b2d
12 changed files with 336 additions and 99 deletions

View File

@ -94,6 +94,22 @@ abstract class Form extends \Zend_Form
*/ */
abstract protected function create(); abstract protected function create();
/**
* Method called before validation
*/
protected function preValid(array $data)
{
}
/**
* Method called after validation
* @param array $data
* @param bool &$isValid
*/
protected function postValid(array $data, &$isValid)
{
}
/** /**
* Setter for request * Setter for request
* @param \Zend_Controller_Request_Abstract $request The request object of a session * @param \Zend_Controller_Request_Abstract $request The request object of a session
@ -139,20 +155,24 @@ abstract class Form extends \Zend_Form
*/ */
public function isValid($data) public function isValid($data)
{ {
$check = null; $checkData = null;
// Elements must be there to validate // Elements must be there to validate
$this->buildForm(); $this->buildForm();
if ($data === null) { if ($data === null) {
$check = $this->getRequest()->getParams(); $checkData = $this->getRequest()->getParams();
} elseif ($data instanceof \Zend_Controller_Request_Abstract) { } elseif ($data instanceof \Zend_Controller_Request_Abstract) {
$check = $data->getParams(); $checkData = $data->getParams();
} else { } else {
$check = $data; $checkData = $data;
} }
return parent::isValid($check); $this->preValid($checkData);
$checkValue = parent::isValid($checkData);
$this->postValid($checkData, $checkValue);
return $checkValue;
} }
/** /**

View File

@ -1,4 +1,6 @@
<?php <?php
// @codingStandardsIgnoreStart
// {{{ICINGA_LICENSE_HEADER}}} // {{{ICINGA_LICENSE_HEADER}}}
/** /**
* This file is part of Icinga 2 Web. * This file is part of Icinga 2 Web.
@ -26,9 +28,11 @@
*/ */
// {{{ICINGA_LICENSE_HEADER}}} // {{{ICINGA_LICENSE_HEADER}}}
use Icinga\Application\Benchmark;
use Icinga\Backend; use Icinga\Backend;
use Icinga\Application\Config; use Icinga\Application\Config;
use Icinga\Authentication\Manager; use Icinga\Authentication\Manager;
use Icinga\Web\Form;
use Icinga\Web\ModuleActionController; use Icinga\Web\ModuleActionController;
use Icinga\Protocol\Commandpipe\Comment; use Icinga\Protocol\Commandpipe\Comment;
use Icinga\Protocol\Commandpipe\CommandPipe; use Icinga\Protocol\Commandpipe\CommandPipe;
@ -55,9 +59,62 @@ class Monitoring_CommandController extends ModuleActionController
const DEFAULT_VIEW_SCRIPT = 'renderform'; const DEFAULT_VIEW_SCRIPT = 'renderform';
/** /**
* @var \Icinga\Protocol\Commandpipe\CommandPipe * Command target
* @var CommandPipe
*/ */
public $target; private $target;
/**
* Current form working on
* @var Form
*/
private $form;
/**
* Setter for form
* @param Form $form
*/
public function setForm($form)
{
$this->form = $form;
}
/**
* Getter for form
* @return Form
*/
public function getForm()
{
return $this->form;
}
/**
* Test if we have a valid form object
* @return bool
*/
public function issetForm()
{
return $this->getForm() !== null && ($this->getForm() instanceof Form);
}
/**
* Post dispatch method
*
* When we have a form put it into the view
*/
public function postDispatch()
{
if ($this->issetForm()) {
if ($this->getRequest()->isPost() && $this->getForm()->isValid(null) === true) {
$this->_helper->viewRenderer->setNoRender(true);
$this->_helper->layout()->disableLayout();
}
$this->view->form = $this->getForm();
}
parent::postDispatch();
}
/** /**
* Controller configuration * Controller configuration
@ -66,9 +123,6 @@ class Monitoring_CommandController extends ModuleActionController
public function init() public function init()
{ {
if ($this->_request->isPost()) { if ($this->_request->isPost()) {
// Save time and memory. We're only working on post
$this->_helper->viewRenderer->setNoRender(true);
$this->_helper->layout()->disableLayout();
$instance = $this->_request->getPost("instance"); $instance = $this->_request->getPost("instance");
@ -152,9 +206,9 @@ class Monitoring_CommandController extends ModuleActionController
$form->setRequest($this->getRequest()); $form->setRequest($this->getRequest());
$form->setSubmitLabel(t('Disable active checks')); $form->setSubmitLabel(t('Disable active checks'));
$form->addNote(t('Disable active checks for this object.')); $form->addNote(t('Disable active checks for this object.'));
$this->view->form = $form; $this->setForm($form);
if ($form->isValid(null) && $this->getRequest()->isPost()) { if ($this->getRequest()->isPost() && $form->isValid(null)) {
throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__);
} }
} }
@ -169,9 +223,9 @@ class Monitoring_CommandController extends ModuleActionController
$form->setRequest($this->getRequest()); $form->setRequest($this->getRequest());
$form->setSubmitLabel(t('Enable active checks')); $form->setSubmitLabel(t('Enable active checks'));
$form->addNote(t('Enable active checks for this object.')); $form->addNote(t('Enable active checks for this object.'));
$this->view->form = $form; $this->setForm($form);
if ($form->isValid(null) && $this->getRequest()->isPost()) { if ($this->getRequest()->isPost() && $form->isValid(null)) {
throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__);
} }
} }
@ -185,9 +239,9 @@ class Monitoring_CommandController extends ModuleActionController
$form = new RescheduleNextCheck(); $form = new RescheduleNextCheck();
$form->setRequest($this->getRequest()); $form->setRequest($this->getRequest());
$this->view->form = $form; $this->setForm($form);
if ($form->isValid(null) && $this->getRequest()->isPost()) { if ($this->getRequest()->isPost() && $form->isValid(null)) {
throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__);
} }
} }
@ -204,9 +258,9 @@ class Monitoring_CommandController extends ModuleActionController
$form->setRequest($this->getRequest()); $form->setRequest($this->getRequest());
$form->setType($type); $form->setType($type);
$this->view->form = $form; $this->setForm($form);
if ($form->isValid(null) && $this->getRequest()->isPost()) { if ($this->getRequest()->isPost() && $form->isValid(null)) {
throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__);
} }
} }
@ -221,9 +275,9 @@ class Monitoring_CommandController extends ModuleActionController
$form->setRequest($this->getRequest()); $form->setRequest($this->getRequest());
$form->setSubmitLabel(t('Stop obsessing')); $form->setSubmitLabel(t('Stop obsessing'));
$form->addNote(t('Stop obsessing over this object.')); $form->addNote(t('Stop obsessing over this object.'));
$this->view->form = $form; $this->setForm($form);
if ($form->isValid(null) && $this->getRequest()->isPost()) { if ($this->getRequest()->isPost() && $form->isValid(null)) {
throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__);
} }
} }
@ -238,9 +292,9 @@ class Monitoring_CommandController extends ModuleActionController
$form->setRequest($this->getRequest()); $form->setRequest($this->getRequest());
$form->setSubmitLabel(t('Start obsessing')); $form->setSubmitLabel(t('Start obsessing'));
$form->addNote(t('Start obsessing over this object.')); $form->addNote(t('Start obsessing over this object.'));
$this->view->form = $form; $this->setForm($form);
if ($form->isValid(null) && $this->getRequest()->isPost()) { if ($this->getRequest()->isPost() && $form->isValid(null)) {
throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__);
} }
} }
@ -255,9 +309,9 @@ class Monitoring_CommandController extends ModuleActionController
$form->setRequest($this->getRequest()); $form->setRequest($this->getRequest());
$form->setSubmitLabel(t('Stop accepting passive checks')); $form->setSubmitLabel(t('Stop accepting passive checks'));
$form->addNote(t('Passive checks for this object will be omitted.')); $form->addNote(t('Passive checks for this object will be omitted.'));
$this->view->form = $form; $this->setForm($form);
if ($form->isValid(null) && $this->getRequest()->isPost()) { if ($this->getRequest()->isPost() && $form->isValid(null)) {
throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__);
} }
} }
@ -272,9 +326,9 @@ class Monitoring_CommandController extends ModuleActionController
$form->setRequest($this->getRequest()); $form->setRequest($this->getRequest());
$form->setSubmitLabel(t('Start accepting passive checks')); $form->setSubmitLabel(t('Start accepting passive checks'));
$form->addNote(t('Passive checks for this object will be accepted.')); $form->addNote(t('Passive checks for this object will be accepted.'));
$this->view->form = $form; $this->setForm($form);
if ($form->isValid(null) && $this->getRequest()->isPost()) { if ($this->getRequest()->isPost() && $form->isValid(null)) {
throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__);
} }
} }
@ -289,9 +343,9 @@ class Monitoring_CommandController extends ModuleActionController
$form->setRequest($this->getRequest()); $form->setRequest($this->getRequest());
$form->setSubmitLabel(t('Disable notifications')); $form->setSubmitLabel(t('Disable notifications'));
$form->addNote(t('Notifications for this object will be disabled.')); $form->addNote(t('Notifications for this object will be disabled.'));
$this->view->form = $form; $this->setForm($form);
if ($form->isValid(null) && $this->getRequest()->isPost()) { if ($this->getRequest()->isPost() && $form->isValid(null)) {
throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__);
} }
} }
@ -306,9 +360,9 @@ class Monitoring_CommandController extends ModuleActionController
$form->setRequest($this->getRequest()); $form->setRequest($this->getRequest());
$form->setSubmitLabel(t('Enable notifications')); $form->setSubmitLabel(t('Enable notifications'));
$form->addNote(t('Notifications for this object will be enabled.')); $form->addNote(t('Notifications for this object will be enabled.'));
$this->view->form = $form; $this->setForm($form);
if ($form->isValid(null) && $this->getRequest()->isPost()) { if ($this->getRequest()->isPost() && $form->isValid(null)) {
throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__);
} }
} }
@ -321,9 +375,9 @@ class Monitoring_CommandController extends ModuleActionController
{ {
$form = new CustomNotification(); $form = new CustomNotification();
$form->setRequest($this->getRequest()); $form->setRequest($this->getRequest());
$this->view->form = $form; $this->setForm($form);
if ($form->isValid(null) && $this->getRequest()->isPost()) { if ($this->getRequest()->isPost() && $form->isValid(null)) {
throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__);
} }
} }
@ -337,9 +391,9 @@ class Monitoring_CommandController extends ModuleActionController
$form = new ScheduleDowntime(); $form = new ScheduleDowntime();
$form->setRequest($this->getRequest()); $form->setRequest($this->getRequest());
$form->setWithChildren(false); $form->setWithChildren(false);
$this->view->form = $form; $this->setForm($form);
if ($form->isValid(null) && $this->getRequest()->isPost()) { if ($this->getRequest()->isPost() && $form->isValid(null)) {
throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__);
} }
} }
@ -353,9 +407,9 @@ class Monitoring_CommandController extends ModuleActionController
$form = new ScheduleDowntime(); $form = new ScheduleDowntime();
$form->setRequest($this->getRequest()); $form->setRequest($this->getRequest());
$form->setWithChildren(true); $form->setWithChildren(true);
$this->view->form = $form; $this->setForm($form);
if ($form->isValid(null) && $this->getRequest()->isPost()) { if ($this->getRequest()->isPost() && $form->isValid(null)) {
throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__);
} }
} }
@ -370,9 +424,9 @@ class Monitoring_CommandController extends ModuleActionController
$form->setRequest($this->getRequest()); $form->setRequest($this->getRequest());
$form->setSubmitLabel(t('Remove downtime(s)')); $form->setSubmitLabel(t('Remove downtime(s)'));
$form->addNote(t('Remove downtime(s) from this host and its services.')); $form->addNote(t('Remove downtime(s) from this host and its services.'));
$this->view->form = $form; $this->setForm($form);
if ($form->isValid(null) && $this->getRequest()->isPost()) { if ($this->getRequest()->isPost() && $form->isValid(null)) {
throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__);
} }
} }
@ -387,9 +441,9 @@ class Monitoring_CommandController extends ModuleActionController
$form->setRequest($this->getRequest()); $form->setRequest($this->getRequest());
$form->setSubmitLabel(t('Disable notifications')); $form->setSubmitLabel(t('Disable notifications'));
$form->addNote(t('Notifications for this host and its services will be disabled.')); $form->addNote(t('Notifications for this host and its services will be disabled.'));
$this->view->form = $form; $this->setForm($form);
if ($form->isValid(null) && $this->getRequest()->isPost()) { if ($this->getRequest()->isPost() && $form->isValid(null)) {
throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__);
} }
} }
@ -404,9 +458,9 @@ class Monitoring_CommandController extends ModuleActionController
$form->setRequest($this->getRequest()); $form->setRequest($this->getRequest());
$form->setSubmitLabel(t('Enable notifications')); $form->setSubmitLabel(t('Enable notifications'));
$form->addNote(t('Notifications for this host and its services will be enabled.')); $form->addNote(t('Notifications for this host and its services will be enabled.'));
$this->view->form = $form; $this->setForm($form);
if ($form->isValid(null) && $this->getRequest()->isPost()) { if ($this->getRequest()->isPost() && $form->isValid(null)) {
throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__);
} }
} }
@ -422,9 +476,9 @@ class Monitoring_CommandController extends ModuleActionController
$form->setWithChildren(true); $form->setWithChildren(true);
$this->view->form = $form; $this->setForm($form);
if ($form->isValid(null) && $this->getRequest()->isPost()) { if ($this->getRequest()->isPost() && $form->isValid(null)) {
throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__);
} }
} }
@ -439,9 +493,9 @@ class Monitoring_CommandController extends ModuleActionController
$form->setRequest($this->getRequest()); $form->setRequest($this->getRequest());
$form->setSubmitLabel(t('Disable active checks')); $form->setSubmitLabel(t('Disable active checks'));
$form->addNote(t('Disable active checks for this host and its services.')); $form->addNote(t('Disable active checks for this host and its services.'));
$this->view->form = $form; $this->setForm($form);
if ($form->isValid(null) && $this->getRequest()->isPost()) { if ($this->getRequest()->isPost() && $form->isValid(null)) {
throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__);
} }
} }
@ -456,9 +510,9 @@ class Monitoring_CommandController extends ModuleActionController
$form->setRequest($this->getRequest()); $form->setRequest($this->getRequest());
$form->setSubmitLabel(t('Enable active checks')); $form->setSubmitLabel(t('Enable active checks'));
$form->addNote(t('Enable active checks for this host and its services.')); $form->addNote(t('Enable active checks for this host and its services.'));
$this->view->form = $form; $this->setForm($form);
if ($form->isValid(null) && $this->getRequest()->isPost()) { if ($this->getRequest()->isPost() && $form->isValid(null)) {
throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__);
} }
} }
@ -473,9 +527,9 @@ class Monitoring_CommandController extends ModuleActionController
$form->setRequest($this->getRequest()); $form->setRequest($this->getRequest());
$form->setSubmitLabel(t('Disable event handler')); $form->setSubmitLabel(t('Disable event handler'));
$form->addNote(t('Disable event handler for this object.')); $form->addNote(t('Disable event handler for this object.'));
$this->view->form = $form; $this->setForm($form);
if ($form->isValid(null) && $this->getRequest()->isPost()) { if ($this->getRequest()->isPost() && $form->isValid(null)) {
throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__);
} }
} }
@ -490,9 +544,9 @@ class Monitoring_CommandController extends ModuleActionController
$form->setRequest($this->getRequest()); $form->setRequest($this->getRequest());
$form->setSubmitLabel(t('Enable event handler')); $form->setSubmitLabel(t('Enable event handler'));
$form->addNote(t('Enable event handler for this object.')); $form->addNote(t('Enable event handler for this object.'));
$this->view->form = $form; $this->setForm($form);
if ($form->isValid(null) && $this->getRequest()->isPost()) { if ($this->getRequest()->isPost() && $form->isValid(null)) {
throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__);
} }
} }
@ -507,9 +561,9 @@ class Monitoring_CommandController extends ModuleActionController
$form->setRequest($this->getRequest()); $form->setRequest($this->getRequest());
$form->setSubmitLabel(t('Disable flapping detection')); $form->setSubmitLabel(t('Disable flapping detection'));
$form->addNote(t('Disable flapping detection for this object.')); $form->addNote(t('Disable flapping detection for this object.'));
$this->view->form = $form; $this->setForm($form);
if ($form->isValid(null) && $this->getRequest()->isPost()) { if ($this->getRequest()->isPost() && $form->isValid(null)) {
throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__);
} }
} }
@ -524,9 +578,9 @@ class Monitoring_CommandController extends ModuleActionController
$form->setRequest($this->getRequest()); $form->setRequest($this->getRequest());
$form->setSubmitLabel(t('Enable flapping detection')); $form->setSubmitLabel(t('Enable flapping detection'));
$form->addNote(t('Enable flapping detection for this object.')); $form->addNote(t('Enable flapping detection for this object.'));
$this->view->form = $form; $this->setForm($form);
if ($form->isValid(null) && $this->getRequest()->isPost()) { if ($this->getRequest()->isPost() && $form->isValid(null)) {
throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__);
} }
} }
@ -540,9 +594,9 @@ class Monitoring_CommandController extends ModuleActionController
$form = new CommentForm(); $form = new CommentForm();
$form->setRequest($this->getRequest()); $form->setRequest($this->getRequest());
$this->view->form = $form; $this->setForm($form);
if ($form->isValid(null) && $this->getRequest()->isPost()) { if ($this->getRequest()->isPost() && $form->isValid(null)) {
throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__);
} }
} }
@ -557,9 +611,9 @@ class Monitoring_CommandController extends ModuleActionController
$form->setRequest($this->getRequest()); $form->setRequest($this->getRequest());
$form->setSubmitLabel(t('Reset attributes')); $form->setSubmitLabel(t('Reset attributes'));
$form->addNote(t('Reset modified attributes to its default.')); $form->addNote(t('Reset modified attributes to its default.'));
$this->view->form = $form; $this->setForm($form);
if ($form->isValid(null) && $this->getRequest()->isPost()) { if ($this->getRequest()->isPost() && $form->isValid(null)) {
throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__);
} }
} }
@ -573,9 +627,9 @@ class Monitoring_CommandController extends ModuleActionController
$form = new Acknowledge(); $form = new Acknowledge();
$form->setRequest($this->getRequest()); $form->setRequest($this->getRequest());
$this->view->form = $form; $this->setForm($form);
if ($form->isValid(null) && $this->getRequest()->isPost()) { if ($this->getRequest()->isPost() && $form->isValid(null)) {
throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__);
} }
} }
@ -590,9 +644,9 @@ class Monitoring_CommandController extends ModuleActionController
$form->setRequest($this->getRequest()); $form->setRequest($this->getRequest());
$form->setSubmitLabel(t('Remove problem acknowledgement')); $form->setSubmitLabel(t('Remove problem acknowledgement'));
$form->addNote(t('Remove problem acknowledgement for this object.')); $form->addNote(t('Remove problem acknowledgement for this object.'));
$this->view->form = $form; $this->setForm($form);
if ($form->isValid(null) && $this->getRequest()->isPost()) { if ($this->getRequest()->isPost() && $form->isValid(null)) {
throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__);
} }
} }
@ -606,9 +660,9 @@ class Monitoring_CommandController extends ModuleActionController
$form = new DelayNotification(); $form = new DelayNotification();
$form->setRequest($this->getRequest()); $form->setRequest($this->getRequest());
$this->view->form = $form; $this->setForm($form);
if ($form->isValid(null) && $this->getRequest()->isPost()) { if ($this->getRequest()->isPost() && $form->isValid(null)) {
throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__);
} }
} }
@ -627,10 +681,12 @@ class Monitoring_CommandController extends ModuleActionController
$form->setFieldLabel(t('Downtime id')); $form->setFieldLabel(t('Downtime id'));
$form->addNote(t('Delete a single downtime with the id shown above')); $form->addNote(t('Delete a single downtime with the id shown above'));
$this->view->form = $form; $this->setForm($form);
if ($form->isValid(null) && $this->getRequest()->isPost()) { if ($this->getRequest()->isPost() && $form->isValid(null)) {
throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__); throw new \Icinga\Exception\ProgrammingError('Command sender not implemented: '. __FUNCTION__);
} }
} }
} }
// @codingStandardsIgnoreStop

View File

@ -31,12 +31,23 @@ namespace Monitoring\Form\Command;
use Icinga\Web\Form; use Icinga\Web\Form;
use Icinga\Web\Form\Element\Note; use Icinga\Web\Form\Element\Note;
use \Zend_Form_Element_Hidden; use \Zend_Form_Element_Hidden;
use \Zend_Validate_Date;
/** /**
* Class AbstractCommand * Class AbstractCommand
*/ */
abstract class AbstractCommand extends Form abstract class AbstractCommand extends Form
{ {
/**
* Default date format
*/
const DEFAULT_DATE_FORMAT = 'Y-m-d H:i:s';
/**
* Default format for validation
*/
const DEFAULT_DATE_VALIDATION = 'yyyy-MM-dd hh:ii:ss';
/** /**
* Label for submit button * Label for submit button
* *
@ -196,9 +207,10 @@ abstract class AbstractCommand extends Form
$authorField = new Zend_Form_Element_Hidden( $authorField = new Zend_Form_Element_Hidden(
array( array(
'name' => 'author', 'name' => 'author',
'value' => $authorName, 'label' => t('Author name'),
'label' => t('Author name') 'value' => $authorName,
'required' => true
) )
); );
@ -213,4 +225,34 @@ abstract class AbstractCommand extends Form
return $authorField; return $authorField;
} }
/**
* Getter for date format
* TODO(mh): Should be user preferences
* @return string
*/
protected function getDateFormat()
{
return self::DEFAULT_DATE_FORMAT;
}
/**
* Getter for date validation format
* @return string
*/
protected function getDateValidationFormat()
{
return self::DEFAULT_DATE_VALIDATION;
}
/**
* Create a new date validator
* @return Zend_Validate_Date
*/
protected function createDateTimeValidator()
{
$validator = new Zend_Validate_Date();
$validator->setFormat($this->getDateValidationFormat());
return $validator;
}
} }

View File

@ -50,8 +50,9 @@ class Acknowledge extends AbstractCommand
'textarea', 'textarea',
'comment', 'comment',
array( array(
'label' => t('Comment'), 'label' => t('Comment'),
'rows' => 4 'rows' => 4,
'required' => true
) )
); );
@ -68,8 +69,7 @@ class Acknowledge extends AbstractCommand
'checkbox', 'checkbox',
'expire', 'expire',
array( array(
'label' => t('Use expire time'), 'label' => t('Use expire time')
'value' => false
) )
); );
@ -81,7 +81,7 @@ class Acknowledge extends AbstractCommand
array( array(
'name' => 'expiretime', 'name' => 'expiretime',
'label' => t('Expire time'), 'label' => t('Expire time'),
'value' => $now->format('Y-m-d H:i:s') 'value' => $now->format($this->getDateFormat())
) )
); );
@ -128,4 +128,17 @@ class Acknowledge extends AbstractCommand
parent::create(); parent::create();
} }
/**
* Add validator for dependent fields
* @param array $data
*/
protected function preValid(array $data)
{
if (isset($data['expire']) && $data['expire'] === '1') {
$expireTime = $this->getElement('expiretime');
$expireTime->setRequired(true);
$expireTime->addValidator($this->createDateTimeValidator(), true);
}
}
} }

View File

@ -42,8 +42,9 @@ class Comment extends AbstractCommand
'textarea', 'textarea',
'comment', 'comment',
array( array(
'label' => t('Comment'), 'label' => t('Comment'),
'rows' => 4 'rows' => 4,
'required' => true
) )
); );

View File

@ -31,7 +31,7 @@ namespace Monitoring\Form\Command;
/** /**
* Simple confirmation form * Simple confirmation form
* *
* Exist to make the abstract form concrete. * Exist to make the abstract available as concrete class
*/ */
class Confirmation extends AbstractCommand class Confirmation extends AbstractCommand
{ {

View File

@ -96,6 +96,13 @@ class ConfirmationWithIdentifier extends Confirmation
$fieldLabel = $this->getFieldLabel(); $fieldLabel = $this->getFieldLabel();
$hiddenField = new Zend_Form_Element_Hidden($this->getFieldName()); $hiddenField = new Zend_Form_Element_Hidden($this->getFieldName());
$hiddenField->setValue($value);
$hiddenField->setRequired(true);
$hiddenField->addValidator(
'digits',
true
);
$hiddenField->removeDecorator('Label'); $hiddenField->removeDecorator('Label');
$hiddenField->addDecorator( $hiddenField->addDecorator(

View File

@ -47,8 +47,9 @@ class CustomNotification extends AbstractCommand
'textarea', 'textarea',
'comment', 'comment',
array( array(
'label' => t('Comment'), 'label' => t('Comment'),
'rows' => 4 'rows' => 4,
'required' => true
) )
); );

View File

@ -33,6 +33,10 @@ namespace Monitoring\Form\Command;
*/ */
class DelayNotification extends AbstractCommand class DelayNotification extends AbstractCommand
{ {
/**
* Biggest value for minutes
*/
const MAX_VALUE = 1440;
/** /**
* Interface method to build the form * Interface method to build the form
* @see Form::create() * @see Form::create()
@ -43,9 +47,20 @@ class DelayNotification extends AbstractCommand
'text', 'text',
'minutes', 'minutes',
array( array(
'label' => t('Notification delay'), 'label' => t('Notification delay'),
'style' => 'width: 80px;', 'style' => 'width: 80px;',
'value' => 0 'value' => 0,
'required' => true,
'validators' => array(
array(
'between',
true,
array(
'min' => 1,
'max' => self::MAX_VALUE
)
)
)
) )
); );

View File

@ -54,6 +54,9 @@ class RescheduleNextCheck extends WithChildrenCommand
) )
); );
$dateElement->setRequired(true);
$dateElement->addValidator($this->createDateTimeValidator(), true);
$this->addElement($dateElement); $this->addElement($dateElement);
$checkBox = new Zend_Form_Element_Checkbox( $checkBox = new Zend_Form_Element_Checkbox(

View File

@ -32,6 +32,8 @@ use Icinga\Web\Form\Element\DateTime;
use \DateTime as PhpDateTime; use \DateTime as PhpDateTime;
use \DateInterval; use \DateInterval;
use \Zend_Form_Element_Text; use \Zend_Form_Element_Text;
use \Zend_Validate_GreaterThan;
use \Zend_Validate_Digits;
/** /**
* Form for any ScheduleDowntime command * Form for any ScheduleDowntime command
@ -44,12 +46,6 @@ class ScheduleDowntime extends WithChildrenCommand
*/ */
const DEFAULT_ENDTIME_INTERVAL = 'PT1H'; const DEFAULT_ENDTIME_INTERVAL = 'PT1H';
/**
* Default time format
* TODO(mh): Should be configurable on a central place (#4424)
*/
const DEFAULT_DATE_FORMAT = 'Y-m-d H:i:s';
/** /**
* Type constant for fixed downtimes * Type constant for fixed downtimes
*/ */
@ -69,11 +65,11 @@ class ScheduleDowntime extends WithChildrenCommand
$out = array(); $out = array();
$dateTimeObject = new PhpDateTime(); $dateTimeObject = new PhpDateTime();
$out[] = $dateTimeObject->format(self::DEFAULT_DATE_FORMAT); $out[] = $dateTimeObject->format($this->getDateFormat());
$dateInterval = new DateInterval(self::DEFAULT_ENDTIME_INTERVAL); $dateInterval = new DateInterval(self::DEFAULT_ENDTIME_INTERVAL);
$dateTimeObject->add($dateInterval); $dateTimeObject->add($dateInterval);
$out[] = $dateTimeObject->format(self::DEFAULT_DATE_FORMAT); $out[] = $dateTimeObject->format($this->getDateFormat());
return $out; return $out;
} }
@ -102,8 +98,9 @@ class ScheduleDowntime extends WithChildrenCommand
'textarea', 'textarea',
'comment', 'comment',
array( array(
'label' => t('Comment'), 'label' => t('Comment'),
'rows' => 4 'rows' => 4,
'required' => true
) )
); );
@ -111,7 +108,22 @@ class ScheduleDowntime extends WithChildrenCommand
'text', 'text',
'triggered', 'triggered',
array( array(
'label' => t('Triggered by') 'label' => t('Triggered by'),
'value' => 0,
'required' => true,
'validators' => array(
array(
'Digits',
true
),
array(
'GreaterThan',
true,
array(
'min' => -1
)
)
)
) )
); );
@ -124,6 +136,8 @@ class ScheduleDowntime extends WithChildrenCommand
'value' => $timestampStart 'value' => $timestampStart
) )
); );
$dateTimeStart->setRequired(true);
$dateTimeStart->addValidator($this->createDateTimeValidator(), true);
$dateTimeEnd = new DateTime( $dateTimeEnd = new DateTime(
array( array(
@ -132,6 +146,8 @@ class ScheduleDowntime extends WithChildrenCommand
'value' => $timestampEnd 'value' => $timestampEnd
) )
); );
$dateTimeEnd->setRequired(true);
$dateTimeEnd->addValidator($this->createDateTimeValidator(), true);
$this->addElements(array($dateTimeStart, $dateTimeEnd)); $this->addElements(array($dateTimeStart, $dateTimeEnd));
@ -140,7 +156,17 @@ class ScheduleDowntime extends WithChildrenCommand
'type', 'type',
array( array(
'label' => t('Downtime type'), 'label' => t('Downtime type'),
'multiOptions' => $this->getDowntimeTypeOptions() 'multiOptions' => $this->getDowntimeTypeOptions(),
'required' => true,
'validators' => array(
array(
'InArray',
true,
array(
array_keys($this->getDowntimeTypeOptions())
)
)
)
) )
); );
@ -188,6 +214,19 @@ class ScheduleDowntime extends WithChildrenCommand
0 => t('Do nothing with child objects'), 0 => t('Do nothing with child objects'),
1 => t('Schedule triggered downtime for all child objects'), 1 => t('Schedule triggered downtime for all child objects'),
2 => t('Schedule non-triggered downtime for all child objects') 2 => t('Schedule non-triggered downtime for all child objects')
),
'validators' => array(
array(
'Digits',
true
),
array(
'InArray',
true,
array(
array(0, 1, 2)
)
)
) )
) )
); );
@ -199,4 +238,29 @@ class ScheduleDowntime extends WithChildrenCommand
parent::create(); parent::create();
} }
/**
* Change validators at runtime
* @param array $data
*/
protected function preValid(array $data)
{
/*
* Other values needed when downtime type change to flexible
*/
if (isset($data['type']) && $data['type'] === self::TYPE_FLEXIBLE) {
$greaterThanValidator = new Zend_Validate_GreaterThan(-1);
$digitsValidator = new Zend_Validate_Digits();
$hours = $this->getElement('hours');
$hours->setRequired(true);
$hours->addValidator($digitsValidator, true);
$hours->addValidator($greaterThanValidator, true);
$minutes = $this->getElement('minutes');
$minutes->setRequired(true);
$minutes->addValidator($digitsValidator, true);
$minutes->addValidator($greaterThanValidator, true);
}
}
} }

View File

@ -122,7 +122,21 @@ class SubmitPassiveCheckResult extends AbstractCommand
'pluginstate', 'pluginstate',
array( array(
'label' => t('Plugin state'), 'label' => t('Plugin state'),
'multiOptions' => $this->getOptions() 'multiOptions' => $this->getOptions(),
'required' => true,
'validators' => array(
array(
'Digits',
true
),
array(
'InArray',
true,
array(
array_keys($this->getOptions())
)
)
)
) )
); );
@ -130,8 +144,9 @@ class SubmitPassiveCheckResult extends AbstractCommand
'textarea', 'textarea',
'checkoutput', 'checkoutput',
array( array(
'label' => t('Check output'), 'label' => t('Check output'),
'rows' => 2 'rows' => 2,
'required' => true
) )
); );