IcingaServiceForm: add NamePattern validator

This commit is contained in:
Thomas Gelf 2017-10-02 08:45:32 +02:00
parent d41fbbf634
commit 45eaf0e987
2 changed files with 55 additions and 1 deletions

View File

@ -10,6 +10,7 @@ use Icinga\Module\Director\Web\Form\DirectorObjectForm;
use Icinga\Module\Director\Objects\IcingaHost;
use Icinga\Module\Director\Objects\IcingaService;
use Icinga\Module\Director\Objects\IcingaServiceSet;
use Icinga\Module\Director\Web\Form\Validate\NamePattern;
use ipl\Html\Html;
use ipl\Html\Link;
@ -73,7 +74,7 @@ class IcingaServiceForm extends DirectorObjectForm
protected function providesOverrides()
{
return $this->applyGenerated
return $this->applyGenerated
|| $this->inheritedFrom
|| ($this->host && $this->set)
|| ($this->object && $this->object->usesVarOverrides());
@ -340,6 +341,15 @@ class IcingaServiceForm extends DirectorObjectForm
)
));
if ($this->object()->isApplyRule()) {
$rName = 'director/service/apply/filter-by-name';
foreach ($this->getAuth()->getRestrictions($rName) as $restriction) {
$this->getElement('object_name')->addValidator(
new NamePattern($restriction)
);
}
}
return $this;
}

View File

@ -0,0 +1,44 @@
<?php
namespace Icinga\Module\Director\Web\Form\Validate;
use Icinga\Data\Filter\FilterMatch;
use Zend_Validate_Abstract;
class NamePattern extends Zend_Validate_Abstract
{
const INVALID = 'intInvalid';
private $pattern;
private $filter;
public function __construct($pattern)
{
$this->pattern = $pattern;
$this->_messageTemplates[self::INVALID] = sprintf(
'Does not match %s',
$pattern
);
}
protected function matches($value)
{
if ($this->filter === null) {
$this->filter = new FilterMatch('prop', '=', $this->pattern);
}
return $this->filter->matches($value);
}
public function isValid($value)
{
if ($this->matches((object) ['prop' => $value])) {
return true;
} else {
$this->_error(self::INVALID, $value);
return false;
}
}
}