IcingaServiceForm: add NamePattern validator
This commit is contained in:
parent
d41fbbf634
commit
45eaf0e987
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue