Merge branch 'feature/require-mandatory-params-8886'

resolves #8886
This commit is contained in:
Eric Lippmann 2015-04-07 13:06:43 +02:00
commit 6b63f3d8a2
5 changed files with 102 additions and 11 deletions

View File

@ -3,6 +3,7 @@
use Icinga\Application\Icinga;
use Icinga\Application\Logger;
use Icinga\Exception\MissingParameterException;
use Icinga\Security\SecurityException;
use Icinga\Web\Controller\ActionController;
@ -42,19 +43,26 @@ class ErrorController extends ActionController
}
break;
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_OTHER:
if ($exception instanceof SecurityException) {
$this->getResponse()->setHttpResponseCode(403);
$this->view->message = $exception->getMessage();
break;
}
// Move to default
default:
switch (true) {
case $exception instanceof SecurityException:
$this->getResponse()->setHttpResponseCode(403);
break;
case $exception instanceof MissingParameterException:
$this->getResponse()->setHttpResponseCode(400);
$this->getResponse()->setHeader(
'X-Status-Reason',
'Missing parameter ' . $exception->getParameter()
);
break;
default:
$this->getResponse()->setHttpResponseCode(500);
break;
}
$title = preg_replace('/\r?\n.*$/s', '', $exception->getMessage());
$this->getResponse()->setHttpResponseCode(500);
$this->view->title = 'Server error: ' . $title;
$this->view->message = $exception->getMessage();
if ($this->getInvokeArg('displayExceptions') == true) {
if ($this->getInvokeArg('displayExceptions')) {
$this->view->stackTrace = $exception->getTraceAsString();
}
break;

View File

@ -3,6 +3,8 @@
namespace Icinga\Cli;
use Icinga\Exception\MissingParameterException;
/**
* Params
*
@ -155,6 +157,29 @@ class Params
return $default;
}
/**
* Require a parameter
*
* @param string $name Name of the parameter
* @param bool $strict Whether the parameter's value must not be the empty string
*
* @return mixed
*
* @throws MissingParameterException If the parameter was not given
*/
public function req($name, $strict = true)
{
if ($this->has($name)) {
$value = $this->get($name);
if (! $strict || strlen($value) > 0) {
return $value;
}
}
$e = new MissingParameterException(t('Required parameter \'%s\' missing'), $name);
$e->setParameter($name);
throw $e;
}
/**
* Set a value for the given option
*

View File

@ -4,9 +4,37 @@
namespace Icinga\Exception;
/**
* Class MissingParameterException
* @package Icinga\Exception
* Exception thrown if a mandatory parameter was not given
*/
class MissingParameterException extends IcingaException
{
/**
* Name of the missing parameter
*
* @var string
*/
protected $parameter;
/**
* Get the name of the missing parameter
*
* @return string
*/
public function getParameter()
{
return $this->parameter;
}
/**
* Set the name of the missing parameter
*
* @param string $name
*
* @return $this
*/
public function setParameter($name)
{
$this->parameter = (string) $name;
return $this;
}
}

View File

@ -55,6 +55,11 @@ class ActionController extends Zend_Controller_Action
*/
private $auth;
/**
* URL parameters
*
* @var UrlParams
*/
protected $params;
/**

View File

@ -3,6 +3,8 @@
namespace Icinga\Web;
use Icinga\Exception\MissingParameterException;
class UrlParams
{
protected $separator = '&';
@ -42,6 +44,29 @@ class UrlParams
return rawurldecode($this->params[ end($this->index[$param]) ][ 1 ]);
}
/**
* Require a parameter
*
* @param string $name Name of the parameter
* @param bool $strict Whether the parameter's value must not be the empty string
*
* @return mixed
*
* @throws MissingParameterException If the parameter was not given
*/
public function req($name, $strict = true)
{
if ($this->has($name)) {
$value = $this->get($name);
if (! $strict || strlen($value) > 0) {
return $value;
}
}
$e = new MissingParameterException(t('Required parameter \'%s\' missing'), $name);
$e->setParameter($name);
throw $e;
}
/**
* Get all instances of the given parameter
*