NamePattern: allow to combine multiple patterns

fixes #1331
This commit is contained in:
Thomas Gelf 2017-12-12 15:49:22 +01:00
parent 5d52eaefd0
commit 550959d858
4 changed files with 28 additions and 31 deletions

View File

@ -3,7 +3,6 @@
namespace Icinga\Module\Director\Forms; namespace Icinga\Module\Director\Forms;
use Icinga\Module\Director\Web\Form\DirectorObjectForm; use Icinga\Module\Director\Web\Form\DirectorObjectForm;
use Icinga\Module\Director\Web\Form\Validate\NamePattern;
class IcingaNotificationForm extends DirectorObjectForm class IcingaNotificationForm extends DirectorObjectForm
{ {
@ -28,13 +27,10 @@ class IcingaNotificationForm extends DirectorObjectForm
'description' => $this->translate('Name for the Icinga notification you are going to create') 'description' => $this->translate('Name for the Icinga notification you are going to create')
)); ));
$rName = 'director/notification/apply/filter-by-name'; $this->eventuallyAddNameRestriction(
foreach ($this->getAuth()->getRestrictions($rName) as $restriction) { 'director/notification/apply/filter-by-name'
$this->getElement('object_name')->addValidator(
new NamePattern($restriction)
); );
} }
}
$this->addDisabledElement() $this->addDisabledElement()
->addImportsElement() ->addImportsElement()

View File

@ -10,7 +10,6 @@ use Icinga\Module\Director\Web\Form\DirectorObjectForm;
use Icinga\Module\Director\Objects\IcingaHost; use Icinga\Module\Director\Objects\IcingaHost;
use Icinga\Module\Director\Objects\IcingaService; use Icinga\Module\Director\Objects\IcingaService;
use Icinga\Module\Director\Objects\IcingaServiceSet; use Icinga\Module\Director\Objects\IcingaServiceSet;
use Icinga\Module\Director\Web\Form\Validate\NamePattern;
use dipl\Html\Html; use dipl\Html\Html;
use dipl\Html\Link; use dipl\Html\Link;
@ -345,12 +344,7 @@ class IcingaServiceForm extends DirectorObjectForm
)); ));
if ($this->object()->isApplyRule()) { if ($this->object()->isApplyRule()) {
$rName = 'director/service/apply/filter-by-name'; $this->eventuallyAddNameRestriction('director/service/apply/filter-by-name');
foreach ($this->getAuth()->getRestrictions($rName) as $restriction) {
$this->getElement('object_name')->addValidator(
new NamePattern($restriction)
);
}
} }
return $this; return $this;

View File

@ -4,7 +4,6 @@ namespace Icinga\Module\Director\Forms;
use Icinga\Module\Director\Objects\IcingaHost; use Icinga\Module\Director\Objects\IcingaHost;
use Icinga\Module\Director\Web\Form\DirectorObjectForm; use Icinga\Module\Director\Web\Form\DirectorObjectForm;
use Icinga\Module\Director\Web\Form\Validate\NamePattern;
class IcingaServiceSetForm extends DirectorObjectForm class IcingaServiceSetForm extends DirectorObjectForm
{ {
@ -25,23 +24,16 @@ class IcingaServiceSetForm extends DirectorObjectForm
protected function setupTemplate() protected function setupTemplate()
{ {
$this->addElement('text', 'object_name', array( $this->addElement('text', 'object_name', [
'label' => $this->translate('Service set name'), 'label' => $this->translate('Service set name'),
'description' => $this->translate( 'description' => $this->translate(
'A short name identifying this set of services' 'A short name identifying this set of services'
), ),
'required' => true, 'required' => true,
)); ])
->eventuallyAddNameRestriction('director/service_set/filter-by-name')
$rName = 'director/service_set/filter-by-name'; ->addHidden('object_type', 'template')
foreach ($this->getAuth()->getRestrictions($rName) as $restriction) { ->addDescriptionElement()
$this->getElement('object_name')->addValidator(
new NamePattern($restriction)
);
}
$this->addHidden('object_type', 'template');
$this->addDescriptionElement()
->addAssignmentElements(); ->addAssignmentElements();
} }

View File

@ -3,6 +3,7 @@
namespace Icinga\Module\Director\Web\Form\Validate; namespace Icinga\Module\Director\Web\Form\Validate;
use Icinga\Data\Filter\FilterMatch; use Icinga\Data\Filter\FilterMatch;
use Icinga\Data\Filter\FilterOr;
use Zend_Validate_Abstract; use Zend_Validate_Abstract;
class NamePattern extends Zend_Validate_Abstract class NamePattern extends Zend_Validate_Abstract
@ -15,7 +16,12 @@ class NamePattern extends Zend_Validate_Abstract
public function __construct($pattern) public function __construct($pattern)
{ {
if (is_array($pattern) && count($pattern) === 1) {
$this->pattern = current($pattern);
} else {
$this->pattern = $pattern; $this->pattern = $pattern;
}
$this->_messageTemplates[self::INVALID] = sprintf( $this->_messageTemplates[self::INVALID] = sprintf(
'Does not match %s', 'Does not match %s',
$pattern $pattern
@ -25,9 +31,18 @@ class NamePattern extends Zend_Validate_Abstract
protected function matches($value) protected function matches($value)
{ {
if ($this->filter === null) { if ($this->filter === null) {
if (is_array($this->pattern)) {
$this->filter = new FilterOr();
foreach ($this->pattern as $pattern) {
$filter = new FilterMatch('prop', '=', $pattern);
$filter->setCaseSensitive(false);
$this->filter->addFilter($filter);
}
} else {
$this->filter = new FilterMatch('prop', '=', $this->pattern); $this->filter = new FilterMatch('prop', '=', $this->pattern);
$this->filter->setCaseSensitive(false); $this->filter->setCaseSensitive(false);
} }
}
return $this->filter->matches($value); return $this->filter->matches($value);
} }