summaryrefslogtreecommitdiffstats
path: root/application/forms
diff options
context:
space:
mode:
Diffstat (limited to 'application/forms')
-rw-r--r--application/forms/AddToBasketForm.php54
-rw-r--r--application/forms/BasketForm.php12
-rw-r--r--application/forms/BasketUploadForm.php28
-rw-r--r--application/forms/DeploymentLinkForm.php3
-rw-r--r--application/forms/DirectorDatafieldForm.php9
-rw-r--r--application/forms/IcingaAddServiceForm.php3
-rw-r--r--application/forms/IcingaCloneObjectForm.php15
-rw-r--r--application/forms/IcingaDependencyForm.php114
-rw-r--r--application/forms/IcingaHostForm.php21
-rw-r--r--application/forms/IcingaNotificationForm.php82
-rw-r--r--application/forms/IcingaObjectFieldForm.php158
-rw-r--r--application/forms/IcingaServiceForm.php23
-rw-r--r--application/forms/IcingaServiceSetForm.php3
-rw-r--r--application/forms/IcingaTemplateChoiceForm.php4
-rw-r--r--application/forms/ImportRowModifierForm.php35
-rw-r--r--application/forms/RemoveLinkForm.php7
-rw-r--r--application/forms/SyncCheckForm.php2
17 files changed, 331 insertions, 242 deletions
diff --git a/application/forms/AddToBasketForm.php b/application/forms/AddToBasketForm.php
index 44b5357..36091fe 100644
--- a/application/forms/AddToBasketForm.php
+++ b/application/forms/AddToBasketForm.php
@@ -11,9 +11,6 @@ use Icinga\Module\Director\Web\Form\DirectorForm;
class AddToBasketForm extends DirectorForm
{
- /** @var Basket */
- private $basket;
-
private $type = '(has not been set)';
private $names = [];
@@ -30,7 +27,6 @@ class AddToBasketForm extends DirectorForm
'b' => 'basket_name',
])->order('basket_name'));
- $names = [];
$basket = null;
if ($this->hasBeenSent()) {
$basketName = $this->getSentValue('basket');
@@ -38,25 +34,17 @@ class AddToBasketForm extends DirectorForm
$basket = Basket::load($basketName, $this->getDb());
}
}
- $count = 0;
- $type = $this->type;
+
+ $names = [];
foreach ($this->names as $name) {
- if (! empty($names)) {
- $names[] = ', ';
- }
- if ($basket && $basket->hasObject($type, $name)) {
- $names[] = Html::tag('span', [
- 'style' => 'text-decoration: line-through'
- ], $name);
- } else {
- $count++;
+ if (! $basket || ! $basket->hasObject($this->type, $name)) {
$names[] = $name;
}
}
- $this->addHtmlHint((new HtmlDocument())->add([
- 'The following objects will be added: ',
- $names
- ]));
+ $this->addHtmlHint(
+ (new HtmlDocument())
+ ->add(sprintf('The following objects will be added: %s', implode(", ", $names)))
+ );
$this->addElement('select', 'basket', [
'label' => $this->translate('Basket'),
'multiOptions' => $this->optionalEnum($enum),
@@ -64,10 +52,10 @@ class AddToBasketForm extends DirectorForm
'class' => 'autosubmit',
]);
- if ($count > 0) {
+ if (! empty($names)) {
$this->setSubmitLabel(sprintf(
$this->translate('Add %s objects'),
- $count
+ count($names)
));
} else {
$this->setSubmitLabel($this->translate('Add'));
@@ -112,18 +100,18 @@ class AddToBasketForm extends DirectorForm
'Configuration objects have been added to the chosen basket "%s"'
), $basketName));
return parent::onSuccess();
- } else {
- $this->addHtmlHint(Hint::error(Html::sprintf($this->translate(
- 'Please check your Basket configuration, %s does not support'
- . ' single "%s" configuration objects'
- ), Link::create(
- $basketName,
- 'director/basket',
- ['name' => $basketName],
- ['data-base-target' => '_next']
- ), $type)));
-
- return false;
}
+
+ $this->addHtmlHint(Hint::error(Html::sprintf($this->translate(
+ 'Please check your Basket configuration, %s does not support'
+ . ' single "%s" configuration objects'
+ ), Link::create(
+ $basketName,
+ 'director/basket',
+ ['name' => $basketName],
+ ['data-base-target' => '_next']
+ ), $type)));
+
+ return false;
}
}
diff --git a/application/forms/BasketForm.php b/application/forms/BasketForm.php
index 8ff6cca..4ac942c 100644
--- a/application/forms/BasketForm.php
+++ b/application/forms/BasketForm.php
@@ -52,9 +52,9 @@ class BasketForm extends DirectorObjectForm
$types = $this->getAvailableTypes();
$options = [
- 'IGNORE' => $this->translate('Ignore'),
- 'ALL' => $this->translate('All of them'),
- '[]' => $this->translate('Custom Selection'),
+ Basket::SELECTION_NONE => $this->translate('Ignore'),
+ Basket::SELECTION_ALL => $this->translate('All of them'),
+ Basket::SELECTION_CUSTOM => $this->translate('Custom Selection'),
];
$this->addHtmlHint($this->translate(
@@ -92,13 +92,13 @@ class BasketForm extends DirectorObjectForm
/** @var Basket $object */
$values = [];
foreach ($this->getAvailableTypes() as $type => $label) {
- $values[$type] = 'IGNORE';
+ $values[$type] = Basket::SELECTION_NONE;
}
foreach ($object->getChosenObjects() as $type => $selection) {
if ($selection === true) {
- $values[$type] = 'ALL';
+ $values[$type] = Basket::SELECTION_ALL;
} elseif (is_array($selection)) {
- $values[$type] = '[]';
+ $values[$type] = Basket::SELECTION_CUSTOM;
}
}
diff --git a/application/forms/BasketUploadForm.php b/application/forms/BasketUploadForm.php
index a88dc06..aa26258 100644
--- a/application/forms/BasketUploadForm.php
+++ b/application/forms/BasketUploadForm.php
@@ -25,10 +25,12 @@ class BasketUploadForm extends DirectorObjectForm
*/
public function setup()
{
- $this->addElement('text', 'basket_name', [
- 'label' => $this->translate('Basket Name'),
- 'required' => true,
- ]);
+ if ($this->object === null) {
+ $this->addElement('text', 'basket_name', [
+ 'label' => $this->translate('Basket Name'),
+ 'required' => true,
+ ]);
+ }
$this->setAttrib('enctype', 'multipart/form-data');
$this->addElement('file', 'uploaded_file', [
@@ -53,16 +55,6 @@ class BasketUploadForm extends DirectorObjectForm
return '\\Icinga\\Module\\Director\\DirectorObject\\Automation\\Basket';
}
- protected function setObjectSuccessUrl()
- {
- /** @var Basket $basket */
- $basket = $this->object();
- $this->setSuccessUrl(
- 'director/basket',
- ['name' => $basket->get('basket_name')]
- );
- }
-
/**
* @return bool
* @throws IcingaException
@@ -134,13 +126,17 @@ class BasketUploadForm extends DirectorObjectForm
$basket->set('owner_type', 'user');
$basket->set('owner_value', $this->getAuth()->getUser()->getUsername());
- $basket->store($this->db);
+ if ($basket->hasBeenLoadedFromDb()) {
+ $this->setSuccessUrl('director/basket/snapshots', ['name' => $basket->get('basket_name')]);
+ } else {
+ $this->setSuccessUrl('director/basket', ['name' => $basket->get('basket_name')]);
+ $basket->store($this->db);
+ }
BasketSnapshot::forBasketFromJson(
$basket,
$this->rawUpload
)->store($this->db);
- $this->setObjectSuccessUrl();
$this->beforeSuccessfulRedirect();
$this->redirectOnSuccess($this->translate('Basket has been uploaded'));
}
diff --git a/application/forms/DeploymentLinkForm.php b/application/forms/DeploymentLinkForm.php
index f42a627..f5cd368 100644
--- a/application/forms/DeploymentLinkForm.php
+++ b/application/forms/DeploymentLinkForm.php
@@ -4,6 +4,7 @@ namespace Icinga\Module\Director\Forms;
use Icinga\Authentication\Auth;
use Icinga\Exception\IcingaException;
+use Icinga\Module\Director\Auth\Permission;
use Icinga\Module\Director\Core\DeploymentApiInterface;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\Deployment\DeploymentInfo;
@@ -100,7 +101,7 @@ class DeploymentLinkForm extends DirectorForm
protected function canDeploy()
{
- return $this->auth->hasPermission('director/deploy');
+ return $this->auth->hasPermission(Permission::DEPLOY);
}
public function render(Zend_View_Interface $view = null)
diff --git a/application/forms/DirectorDatafieldForm.php b/application/forms/DirectorDatafieldForm.php
index a306bd7..ab0dcd8 100644
--- a/application/forms/DirectorDatafieldForm.php
+++ b/application/forms/DirectorDatafieldForm.php
@@ -140,8 +140,7 @@ class DirectorDatafieldForm extends DirectorObjectForm
$this->addElement('text', 'varname', array(
'label' => $this->translate('Field name'),
'description' => $this->translate(
- 'The unique name of the field. This will be the name of the custom'
- . ' variable in the rendered Icinga configuration.'
+ 'This will be the name of the custom variable in the rendered Icinga configuration.'
),
'required' => true,
));
@@ -166,7 +165,7 @@ class DirectorDatafieldForm extends DirectorObjectForm
$this->addElement('select', 'category_id', [
'label' => $this->translate('Data Field Category'),
- 'multiOptions' => $this->optionalEnum($this->enumCategpories()),
+ 'multiOptions' => $this->optionalEnum($this->enumCategories()),
]);
$error = false;
@@ -282,7 +281,7 @@ class DirectorDatafieldForm extends DirectorObjectForm
protected function enumDataTypes()
{
$hooks = Hook::all('Director\\DataType');
- $enum = array(null => '- please choose -');
+ $enum = [null => $this->translate('- please choose -')];
/** @var DataTypeHook $hook */
foreach ($hooks as $hook) {
$enum[get_class($hook)] = $hook->getName();
@@ -291,7 +290,7 @@ class DirectorDatafieldForm extends DirectorObjectForm
return $enum;
}
- protected function enumCategpories()
+ protected function enumCategories()
{
$db = $this->getDb()->getDbAdapter();
return $db->fetchPairs(
diff --git a/application/forms/IcingaAddServiceForm.php b/application/forms/IcingaAddServiceForm.php
index df2302e..60ccb6f 100644
--- a/application/forms/IcingaAddServiceForm.php
+++ b/application/forms/IcingaAddServiceForm.php
@@ -3,6 +3,7 @@
namespace Icinga\Module\Director\Forms;
use gipfl\IcingaWeb2\Link;
+use Icinga\Module\Director\Auth\Permission;
use Icinga\Module\Director\Web\Form\DirectorObjectForm;
use Icinga\Module\Director\Objects\IcingaHost;
use Icinga\Module\Director\Objects\IcingaService;
@@ -82,7 +83,7 @@ class IcingaAddServiceForm extends DirectorObjectForm
if ($this->hasBeenSent()) {
$this->addError($this->translate('No service has been chosen'));
} else {
- if ($this->hasPermission('director/admin')) {
+ if ($this->hasPermission(Permission::ADMIN)) {
$html = sprintf(
$this->translate('Please define a %s first'),
Link::create(
diff --git a/application/forms/IcingaCloneObjectForm.php b/application/forms/IcingaCloneObjectForm.php
index 6ee99ba..8381eee 100644
--- a/application/forms/IcingaCloneObjectForm.php
+++ b/application/forms/IcingaCloneObjectForm.php
@@ -5,8 +5,10 @@ namespace Icinga\Module\Director\Forms;
use gipfl\Web\Widget\Hint;
use Icinga\Exception\IcingaException;
use Icinga\Module\Director\Acl;
+use Icinga\Module\Director\Auth\Permission;
use Icinga\Module\Director\Data\Db\DbObjectStore;
use Icinga\Module\Director\Db\Branch\Branch;
+use Icinga\Module\Director\Objects\IcingaCommand;
use Icinga\Module\Director\Objects\IcingaHost;
use Icinga\Module\Director\Objects\IcingaObject;
use Icinga\Module\Director\Objects\IcingaService;
@@ -41,7 +43,7 @@ class IcingaCloneObjectForm extends DirectorForm
'value' => $name,
));
- if (!$branchOnly && Acl::instance()->hasPermission('director/admin')) {
+ if (!$branchOnly && Acl::instance()->hasPermission(Permission::ADMIN)) {
$this->addElement('select', 'clone_type', array(
'label' => 'Clone type',
'required' => true,
@@ -95,7 +97,9 @@ class IcingaCloneObjectForm extends DirectorForm
}
}
- if ($this->object->isTemplate() && $this->object->supportsFields()) {
+ if (($this->object->isTemplate() || $this->object instanceof IcingaCommand)
+ && $this->object->supportsFields()
+ ) {
$this->addBoolean('clone_fields', [
'label' => $this->translate('Clone Template Fields'),
'description' => $this->translate(
@@ -132,7 +136,7 @@ class IcingaCloneObjectForm extends DirectorForm
$connection = $object->getConnection();
$db = $connection->getDbAdapter();
$newName = $this->getValue('new_object_name');
- $resolve = Acl::instance()->hasPermission('director/admin')
+ $resolve = Acl::instance()->hasPermission(Permission::ADMIN)
&& $this->getValue('clone_type') === 'flat';
$msg = sprintf(
@@ -189,7 +193,7 @@ class IcingaCloneObjectForm extends DirectorForm
$fields = $db->fetchAll(
$db->select()
->from($table . '_field')
- ->where("${type}_id = ?", $object->get('id'))
+ ->where("{$type}_id = ?", $object->get('id'))
);
} else {
$fields = [];
@@ -221,7 +225,7 @@ class IcingaCloneObjectForm extends DirectorForm
}
foreach ($fields as $row) {
- $row->{"${type}_id"} = $newId;
+ $row->{"{$type}_id"} = $newId;
$db->insert($table . '_field', (array) $row);
}
@@ -247,6 +251,7 @@ class IcingaCloneObjectForm extends DirectorForm
return $db->fetchPairs(
$db->select()
->from('icinga_service_set', ['id', 'object_name'])
+ ->where('object_type = ?', 'template')
->order('object_name')
);
}
diff --git a/application/forms/IcingaDependencyForm.php b/application/forms/IcingaDependencyForm.php
index ab30844..56597f9 100644
--- a/application/forms/IcingaDependencyForm.php
+++ b/application/forms/IcingaDependencyForm.php
@@ -5,6 +5,7 @@ namespace Icinga\Module\Director\Forms;
use Icinga\Module\Director\Data\Db\DbObject;
use Icinga\Module\Director\Web\Form\DirectorObjectForm;
use Icinga\Module\Director\Objects\IcingaDependency;
+use Zend_Validate_Callback;
class IcingaDependencyForm extends DirectorObjectForm
{
@@ -164,7 +165,7 @@ class IcingaDependencyForm extends DirectorObjectForm
], null);
$this->addBoolean('disable_notifications', [
- 'label' => $this->translate('Disable Notificiations'),
+ 'label' => $this->translate('Disable Notifications'),
'description' => $this->translate(
'Whether to disable notifications when this dependency fails.'
. ' Defaults to true.'
@@ -192,38 +193,83 @@ class IcingaDependencyForm extends DirectorObjectForm
$parentHost = $dependency->get('parent_host');
if ($parentHost === null) {
$parentHostVar = $dependency->get('parent_host_var');
- if (\strlen($parentHostVar) > 0) {
+ if ($parentHostVar !== null && \strlen($parentHostVar) > 0) {
$parentHost = '$' . $dependency->get('parent_host_var') . '$';
}
}
+
+ $parentHostDescription = $this->translate('Optional. The parent host.');
+ $applyTo = $this->getSentOrObjectValue('apply_to');
+ $parentHostValidator = new Zend_Validate_Callback(function ($value) use ($applyTo) {
+ if ($applyTo === 'host' && $this->isCustomVar($value)) {
+ return explode('.', trim($value, '$'))[0] === 'host';
+ }
+
+ return true;
+ });
+
+ $parentHostValidator->setMessage(
+ $this->translate('The parent host cannot be a service custom variable for a host dependency'),
+ Zend_Validate_Callback::INVALID_VALUE
+ );
+
+ if ($applyTo === 'service') {
+ $additionalDescription = $this->translate(
+ 'You might want to refer to Host or Service Custom Variables via $host|service.vars.varname$'
+ );
+ } else {
+ $additionalDescription = $this->translate(
+ 'You might want to refer to Host Custom Variables via $host.vars.varname$'
+ );
+ }
+
+ $parentHostDescription .= ' ' . $additionalDescription;
+
$this->addElement('text', 'parent_host', [
- 'label' => $this->translate('Parent Host'),
- 'description' => $this->translate(
- 'The parent host. You might want to refer Host Custom Variables'
- . ' via $host.vars.varname$'
- ),
- 'class' => "autosubmit director-suggest",
+ 'label' => $this->translate('Parent Host'),
+ 'description' => $parentHostDescription,
+ 'class' => "autosubmit director-suggest",
'data-suggestion-context' => 'hostnames',
- 'order' => 10,
- 'required' => $this->isObject(),
- 'value' => $parentHost
+ 'order' => 10,
+ 'required' => $this->isObject(),
+ 'value' => $parentHost,
+ 'validators' => [$parentHostValidator]
]);
$sentParent = $this->getSentOrObjectValue('parent_host');
if (!empty($sentParent) || $dependency->isApplyRule()) {
$parentService = $dependency->get('parent_service');
+ if ($parentService === null) {
+ $parentServiceVar = $dependency->get('parent_service_by_name');
+ if ($parentServiceVar) {
+ $parentService = '$' . $parentServiceVar . '$';
+ }
+ }
+
+ $parentServiceDescription = $this->translate(
+ 'Optional. The parent service. If omitted this dependency'
+ . ' object is treated as host dependency.'
+ );
+
+ $parentServiceDescription .= ' ' . $additionalDescription;
+
+ $parentServiceValidator = clone $parentHostValidator;
+
+ $parentServiceValidator->setMessage(
+ $this->translate('The parent service cannot be a service custom variable for a host dependency'),
+ Zend_Validate_Callback::INVALID_VALUE
+ );
+
$this->addElement('text', 'parent_service', [
- 'label' => $this->translate('Parent Service'),
- 'description' => $this->translate(
- 'Optional. The parent service. If omitted this dependency'
- . ' object is treated as host dependency.'
- ),
- 'class' => "autosubmit director-suggest",
- 'data-suggestion-context' => 'servicenames',
- 'data-suggestion-for-host' => $sentParent,
- 'order' => 20,
- 'value' => $parentService
- ]);
+ 'label' => $this->translate('Parent Service'),
+ 'description' => $parentServiceDescription,
+ 'class' => "autosubmit director-suggest",
+ 'data-suggestion-context' => 'servicenames',
+ 'data-suggestion-for-host' => $sentParent,
+ 'order' => 20,
+ 'value' => $parentService,
+ 'validators' => [$parentServiceValidator]
+ ]);
}
// If configuring Object, allow selection of child host and/or service,
@@ -290,11 +336,22 @@ class IcingaDependencyForm extends DirectorObjectForm
protected function handleProperties(DbObject $object, &$values)
{
if ($this->hasBeenSent()) {
- if (isset($values['parent_host'])
- && $this->isCustomVar($values['parent_host'])
- ) {
- $values['parent_host_var'] = \trim($values['parent_host'], '$');
- $values['parent_host'] = '';
+ if (isset($values['parent_host'])) {
+ if ($this->isCustomVar($values['parent_host'])) {
+ $values['parent_host_var'] = \trim($values['parent_host'], '$');
+ $values['parent_host'] = '';
+ } else {
+ $values['parent_host_var'] = '';
+ }
+ }
+
+ if (isset($values['parent_service'])) {
+ if ($this->isCustomVar($values['parent_service'])) {
+ $values['parent_service_by_name'] = trim($values['parent_service'], '$');
+ $values['parent_service'] = '';
+ } else {
+ $values['parent_service_by_name'] = '';
+ }
}
}
@@ -303,7 +360,6 @@ class IcingaDependencyForm extends DirectorObjectForm
protected function isCustomVar($string)
{
- return \preg_match('/^\$(?:host)\.vars\..+\$$/', $string);
- // Eventually: return \preg_match('/^\$(?:host|service)\.vars\..+\$$/', $string);
+ return preg_match('/^\$(?:host|service)\.vars\..+\$$/', $string);
}
}
diff --git a/application/forms/IcingaHostForm.php b/application/forms/IcingaHostForm.php
index ec71471..ed57251 100644
--- a/application/forms/IcingaHostForm.php
+++ b/application/forms/IcingaHostForm.php
@@ -3,6 +3,8 @@
namespace Icinga\Module\Director\Forms;
use Icinga\Exception\AuthenticationException;
+use Icinga\Module\Director\Auth\Permission;
+use Icinga\Module\Director\Auth\Restriction;
use Icinga\Module\Director\Repository\IcingaTemplateRepository;
use Icinga\Module\Director\Restriction\HostgroupRestriction;
use Icinga\Module\Director\Web\Form\DirectorObjectForm;
@@ -162,7 +164,7 @@ class IcingaHostForm extends DirectorObjectForm
if ($this->hasBeenSent()) {
$this->addError($this->translate('No Host template has been chosen'));
} else {
- if ($this->hasPermission('director/admin')) {
+ if ($this->hasPermission(Permission::ADMIN)) {
$html = sprintf(
$this->translate('Please define a %s first'),
Link::create(
@@ -208,7 +210,7 @@ class IcingaHostForm extends DirectorObjectForm
protected function addGroupsElement()
{
if ($this->hasHostGroupRestriction()
- && ! $this->getAuth()->hasPermission('director/groups-for-restricted-hosts')
+ && ! $this->getAuth()->hasPermission(Permission::GROUPS_FOR_RESTRICTED_HOSTS)
) {
return $this;
}
@@ -262,15 +264,6 @@ class IcingaHostForm extends DirectorObjectForm
return $this;
}
- protected function strikeGroupLinks(BaseHtmlElement $links)
- {
- /** @var BaseHtmlElement $link */
- foreach ($links->getContent() as $link) {
- $link->getAttributes()->add('style', 'text-decoration: strike');
- }
- $links->add('aha');
- }
-
protected function getInheritedGroups()
{
if ($this->hasObject()) {
@@ -295,9 +288,7 @@ class IcingaHostForm extends DirectorObjectForm
);
}
- return Html::tag('span', [
- 'style' => 'line-height: 2.5em; padding-left: 0.5em'
- ], $links);
+ return Html::tag('span', ['class' => 'host-group-links'], $links);
}
protected function getAppliedGroups()
@@ -311,7 +302,7 @@ class IcingaHostForm extends DirectorObjectForm
protected function hasHostGroupRestriction()
{
- return $this->getAuth()->getRestrictions('director/filter/hostgroups');
+ return $this->getAuth()->getRestrictions(Restriction::FILTER_HOSTGROUPS);
}
/**
diff --git a/application/forms/IcingaNotificationForm.php b/application/forms/IcingaNotificationForm.php
index 0fca6b8..bf9f75d 100644
--- a/application/forms/IcingaNotificationForm.php
+++ b/application/forms/IcingaNotificationForm.php
@@ -2,6 +2,7 @@
namespace Icinga\Module\Director\Forms;
+use Icinga\Module\Director\DataType\DataTypeDirectorObject;
use Icinga\Module\Director\Web\Form\DirectorObjectForm;
class IcingaNotificationForm extends DirectorObjectForm
@@ -121,20 +122,32 @@ class IcingaNotificationForm extends DirectorObjectForm
{
$users = $this->enumUsers();
if (empty($users)) {
- return $this;
- }
-
- $this->addElement(
- 'extensibleSet',
- 'users',
- array(
+ $this->addElement('select', 'users', [
+ 'label' => $this->translate('Users'),
+ 'description' => $this->translate('No User object has been created yet'),
+ 'multiOptions' => $this->optionalEnum([]),
+ ]);
+ } else {
+ $this->addElement('extensibleSet', 'users', [
'label' => $this->translate('Users'),
'description' => $this->translate(
'Users that should be notified by this notifications'
),
'multiOptions' => $this->optionalEnum($users)
+ ]);
+ }
+
+ $this->addElement('select', 'users_var', [
+ 'label' => $this->translate('Users Custom Variable'),
+ 'multiOptions' => $this->enumDirectorObjectFields('user'),
+ 'description' => $this->translate(
+ 'If defined, Users from this Custom Variable will be combined with single users chosen below. '
+ . ' e.g.: when set to notification_contacts, this notification will pick Users from the Array'
+ . ' service.vars.notification_contacts and fall back to host.vars.notification_contacts, in'
+ . ' case the former one does not exist.'
+ . ' Only Array type DirectorObject Fields for User objects are eligible for this feature.'
)
- );
+ ]);
return $this;
}
@@ -146,24 +159,59 @@ class IcingaNotificationForm extends DirectorObjectForm
{
$groups = $this->enumUsergroups();
if (empty($groups)) {
- return $this;
- }
-
- $this->addElement(
- 'extensibleSet',
- 'user_groups',
- array(
+ $this->addElement('select', 'user_groups', [
+ 'label' => $this->translate('Users'),
+ 'description' => $this->translate('No UserGroup object has been created yet'),
+ 'multiOptions' => $this->optionalEnum([]),
+ ]);
+ } else {
+ $this->addElement('extensibleSet', 'user_groups', [
'label' => $this->translate('User groups'),
'description' => $this->translate(
'User groups that should be notified by this notifications'
),
'multiOptions' => $this->optionalEnum($groups)
+ ]);
+ }
+
+ $this->addElement('select', 'user_groups_var', [
+ 'label' => $this->translate('User Groups Custom Variable'),
+ 'multiOptions' => $this->enumDirectorObjectFields('usergroup'),
+ 'description' => $this->translate(
+ 'If defined, User Groups from this Custom Variable will be combined with single Groups chosen below. '
+ . ' e.g.: when set to notification_groups, this notification will pick User Groups from the Array'
+ . ' service.vars.notification_groups and fall back to host.vars.notification_groups, in'
+ . ' case the former one does not exist.'
+ . ' Only Array type DirectorObject Fields for User objects are eligible for this feature.'
)
- );
+ ]);
return $this;
}
+ protected function enumDirectorObjectFields($objectType, $dataType = 'array')
+ {
+ $db = $this->db->getDbAdapter();
+ $query = $db->select()
+ ->from(['df' => 'director_datafield'], ['k' => 'df.varname', 'v' => 'df.varname'])
+ ->join(
+ ['dfs' => 'director_datafield_setting'],
+ $db->quoteInto('df.id = dfs.datafield_id AND dfs.setting_name = ?', 'icinga_object_type'),
+ []
+ )
+ ->join(
+ ['dft' => 'director_datafield_setting'],
+ $db->quoteInto('df.id = dft.datafield_id AND dft.setting_name = ?', 'data_type'),
+ []
+ )
+ ->where('df.datatype = ?', DataTypeDirectorObject::class)
+ ->where('dfs.setting_value = ?', $objectType)
+ ->where('dft.setting_value = ?', $dataType)
+ ->order('df.varname');
+
+ return $this->optionalEnum($db->fetchPairs($query));
+ }
+
/**
* @return self
*/
@@ -196,7 +244,7 @@ class IcingaNotificationForm extends DirectorObjectForm
array(
'label' => $this->translate('First notification delay'),
'description' => $this->translate(
- 'Delay unless the first notification should be sent'
+ 'Delay until the first notification should be sent'
) . '. ' . $this->getTimeValueInfo()
)
);
diff --git a/application/forms/IcingaObjectFieldForm.php b/application/forms/IcingaObjectFieldForm.php
index 537c95e..3b503fc 100644
--- a/application/forms/IcingaObjectFieldForm.php
+++ b/application/forms/IcingaObjectFieldForm.php
@@ -2,6 +2,9 @@
namespace Icinga\Module\Director\Forms;
+use Icinga\Module\Director\DataType\DataTypeBoolean;
+use Icinga\Module\Director\DataType\DataTypeString;
+use Icinga\Module\Director\Field\FormFieldSuggestion;
use Icinga\Module\Director\Objects\IcingaCommand;
use Icinga\Module\Director\Objects\IcingaHost;
use Icinga\Module\Director\Objects\IcingaObject;
@@ -15,6 +18,9 @@ class IcingaObjectFieldForm extends DirectorObjectForm
/** @var IcingaObject Please note that $object would conflict with logic in parent class */
protected $icingaObject;
+ /** @var FormFieldSuggestion */
+ protected $fieldSuggestion;
+
public function setIcingaObject($object)
{
$this->icingaObject = $object;
@@ -36,22 +42,9 @@ class IcingaObjectFieldForm extends DirectorObjectForm
. ' a specific set, shown as a dropdown.'
);
- // TODO: remove assigned ones!
- $existingFields = $this->db->enumDatafields();
- $blacklistedVars = array();
- $suggestedFields = array();
-
- foreach ($existingFields as $id => $field) {
- if (preg_match('/ \(([^\)]+)\)$/', $field, $m)) {
- $blacklistedVars['$' . $m[1] . '$'] = $id;
- }
- }
-
// TODO: think about imported existing vars without fields
// TODO: extract vars from command line (-> dummy)
// TODO: do not suggest chosen ones
- $argumentVars = array();
- $argumentVarDescriptions = array();
if ($object instanceof IcingaCommand) {
$command = $object;
} elseif ($object->hasProperty('check_command_id')) {
@@ -60,56 +53,16 @@ class IcingaObjectFieldForm extends DirectorObjectForm
$command = null;
}
- if ($command) {
- foreach ($command->arguments() as $arg) {
- if ($arg->argument_format === 'string') {
- $val = $arg->argument_value;
- // TODO: create var::extractMacros or so
-
- if (preg_match_all('/(\$[a-z0-9_]+\$)/i', $val, $m, PREG_PATTERN_ORDER)) {
- foreach ($m[1] as $val) {
- if (array_key_exists($val, $blacklistedVars)) {
- $id = $blacklistedVars[$val];
-
- // Hint: if not set it might already have been
- // removed in this loop
- if (array_key_exists($id, $existingFields)) {
- $suggestedFields[$id] = $existingFields[$id];
- unset($existingFields[$id]);
- }
- } else {
- $argumentVars[$val] = $val;
- $argumentVarDescriptions[$val] = $arg->description;
- }
- }
- }
- }
- }
- }
-
- // Prepare combined fields array
- $fields = array();
- if (! empty($suggestedFields)) {
- asort($existingFields);
- $fields[$this->translate('Suggested fields')] = $suggestedFields;
- }
-
- if (! empty($argumentVars)) {
- ksort($argumentVars);
- $fields[$this->translate('Argument macros')] = $argumentVars;
- }
-
- if (! empty($existingFields)) {
- $fields[$this->translate('Other available fields')] = $existingFields;
- }
+ $suggestions = $this->fieldSuggestion = new FormFieldSuggestion($command, $this->db->enumDatafields());
+ $fields = $suggestions->getCommandFields();
- $this->addElement('select', 'datafield_id', array(
+ $this->addElement('select', 'datafield_id', [
'label' => 'Field',
'required' => true,
'description' => 'Field to assign',
'class' => 'autosubmit',
'multiOptions' => $this->optionalEnum($fields)
- ));
+ ]);
if (empty($fields)) {
// TODO: show message depending on permissions
@@ -121,67 +74,58 @@ class IcingaObjectFieldForm extends DirectorObjectForm
}
if (($id = $this->getSentValue('datafield_id')) && ! ctype_digit($id)) {
- $this->addElement('text', 'caption', array(
+ $this->addElement('text', 'caption', [
'label' => $this->translate('Caption'),
'required' => true,
'ignore' => true,
'value' => trim($id, '$'),
- 'description' => $this->translate('The caption which should be displayed')
- ));
+ 'description' => $this->translate(
+ 'The caption which should be displayed to your users when this field'
+ . ' is shown'
+ )
+ ]);
- $this->addElement('textarea', 'description', array(
+ $this->addElement('textarea', 'description', [
'label' => $this->translate('Description'),
- 'description' => $this->translate('A description about the field'),
+ 'description' => $this->translate(
+ 'An extended description for this field. Will be shown as soon as a'
+ . ' user puts the focus on this field'
+ ),
'ignore' => true,
- 'value' => array_key_exists($id, $argumentVarDescriptions) ? $argumentVarDescriptions[$id] : null,
+ 'value' => $command ? $suggestions->getDescription($id) : null,
'rows' => '3',
- ));
+ ]);
}
- $this->addElement('select', 'is_required', array(
+ $this->addElement('select', 'is_required', [
'label' => $this->translate('Mandatory'),
'description' => $this->translate('Whether this field should be mandatory'),
'required' => true,
- 'multiOptions' => array(
+ 'multiOptions' => [
'n' => $this->translate('Optional'),
'y' => $this->translate('Mandatory'),
- )
- ));
-
- $filterFields = array();
- $prefix = null;
- if ($object instanceof IcingaHost) {
- $prefix = 'host.vars.';
- } elseif ($object instanceof IcingaService) {
- $prefix = 'service.vars.';
- }
+ ]
+ ]);
- if ($prefix) {
- $loader = new IcingaObjectFieldLoader($object);
- $fields = $loader->getFields();
-
- foreach ($fields as $varName => $field) {
- $filterFields[$prefix . $field->varname] = $field->caption;
- }
-
- $this->addFilterElement('var_filter', array(
+ if ($filterFields = $this->getFilterFields($object)) {
+ $this->addFilterElement('var_filter', [
'description' => $this->translate(
'You might want to show this field only when certain conditions are met.'
. ' Otherwise it will not be available and values eventually set before'
. ' will be cleared once stored'
),
'columns' => $filterFields,
- ));
+ ]);
- $this->addDisplayGroup(array($this->getElement('var_filter')), 'field_filter', array(
- 'decorators' => array(
+ $this->addDisplayGroup([$this->getElement('var_filter')], 'field_filter', [
+ 'decorators' => [
'FormElements',
- array('HtmlTag', array('tag' => 'dl')),
+ ['HtmlTag', ['tag' => 'dl']],
'Fieldset',
- ),
+ ],
'order' => 30,
'legend' => $this->translate('Show based on filter')
- ));
+ ]);
}
$this->setButtons();
@@ -202,18 +146,42 @@ class IcingaObjectFieldForm extends DirectorObjectForm
$fieldId = $this->getValue('datafield_id');
if (! ctype_digit($fieldId)) {
- $field = DirectorDatafield::create(array(
+ $field = DirectorDatafield::create([
'varname' => trim($fieldId, '$'),
'caption' => $this->getValue('caption'),
'description' => $this->getValue('description'),
- 'datatype' => 'Icinga\Module\Director\DataType\DataTypeString',
- ));
+ 'datatype' => $this->fieldSuggestion && $this->fieldSuggestion->isBoolean($fieldId)
+ ? DataTypeBoolean::class
+ : DataTypeString::class
+ ]);
$field->store($this->getDb());
$this->setElementValue('datafield_id', $field->get('id'));
$this->object()->set('datafield_id', $field->get('id'));
}
$this->object()->set('var_filter', $this->getValue('var_filter'));
- return parent::onSuccess();
+ parent::onSuccess();
+ }
+
+ protected static function getFilterFields(IcingaObject $object): array
+ {
+ $filterFields = [];
+ $prefix = null;
+ if ($object instanceof IcingaHost) {
+ $prefix = 'host.vars.';
+ } elseif ($object instanceof IcingaService) {
+ $prefix = 'service.vars.';
+ }
+
+ if ($prefix) {
+ $loader = new IcingaObjectFieldLoader($object);
+ $fields = $loader->getFields();
+
+ foreach ($fields as $varName => $field) {
+ $filterFields[$prefix . $field->get('varname')] = $field->get('caption');
+ }
+ }
+
+ return $filterFields;
}
}
diff --git a/application/forms/IcingaServiceForm.php b/application/forms/IcingaServiceForm.php
index f22f9e6..5744d8d 100644
--- a/application/forms/IcingaServiceForm.php
+++ b/application/forms/IcingaServiceForm.php
@@ -6,8 +6,10 @@ use gipfl\Web\Widget\Hint;
use Icinga\Data\Filter\Filter;
use Icinga\Exception\IcingaException;
use Icinga\Exception\ProgrammingError;
+use Icinga\Module\Director\Auth\Permission;
use Icinga\Module\Director\Data\PropertiesFilter\ArrayCustomVariablesFilter;
use Icinga\Module\Director\Exception\NestingError;
+use Icinga\Module\Director\Objects\IcingaObject;
use Icinga\Module\Director\Web\Form\DirectorObjectForm;
use Icinga\Module\Director\Objects\IcingaHost;
use Icinga\Module\Director\Objects\IcingaService;
@@ -196,14 +198,16 @@ class IcingaServiceForm extends DirectorObjectForm
}
/**
- * @param IcingaService $service
- * @return IcingaService
+ * Hint: could be moved elsewhere
+ *
+ * @param IcingaService $object
+ * @return IcingaObject|IcingaService|IcingaServiceSet
* @throws \Icinga\Exception\NotFoundError
*/
- protected function getFirstParent(IcingaService $service)
+ protected static function getFirstParent(IcingaObject $object)
{
- /** @var IcingaService[] $objects */
- $objects = $service->imports()->getObjects();
+ /** @var IcingaObject[] $objects */
+ $objects = $object->imports()->getObjects();
if (empty($objects)) {
throw new RuntimeException('Something went wrong, got no parent');
}
@@ -300,7 +304,7 @@ class IcingaServiceForm extends DirectorObjectForm
if ($this->set) {
return $this->object;
} else {
- return $this->getFirstParent($this->object);
+ return self::getFirstParent($this->object);
}
}
@@ -355,7 +359,7 @@ class IcingaServiceForm extends DirectorObjectForm
$objectType = 'template';
}
$this->addHidden('object_type', $objectType);
- $forceCommandElements = $this->hasPermission('director/admin');
+ $forceCommandElements = $this->hasPermission(Permission::ADMIN);
$this->addNameElement()
->addHostObjectElement()
@@ -550,7 +554,7 @@ class IcingaServiceForm extends DirectorObjectForm
->addGroupsElement()
->groupMainProperties();
- if ($this->hasPermission('director/admin')) {
+ if ($this->hasPermission(Permission::ADMIN)) {
$this->addCheckCommandElements(true)
->addCheckExecutionElements(true)
->addExtraInfoElements();
@@ -721,8 +725,7 @@ class IcingaServiceForm extends DirectorObjectForm
protected function enumHosts()
{
$db = $this->db->getDbAdapter();
- $table = new ObjectsTableHost($this->db);
- $table->setAuth($this->getAuth());
+ $table = new ObjectsTableHost($this->db, $this->getAuth());
if ($this->branch && $this->branch->isBranch()) {
$table->setBranchUuid($this->branch->getUuid());
}
diff --git a/application/forms/IcingaServiceSetForm.php b/application/forms/IcingaServiceSetForm.php
index 21508d5..38b848a 100644
--- a/application/forms/IcingaServiceSetForm.php
+++ b/application/forms/IcingaServiceSetForm.php
@@ -2,6 +2,7 @@
namespace Icinga\Module\Director\Forms;
+use Icinga\Module\Director\Auth\Permission;
use Icinga\Module\Director\Objects\IcingaHost;
use Icinga\Module\Director\Web\Form\DirectorObjectForm;
@@ -113,7 +114,7 @@ class IcingaServiceSetForm extends DirectorObjectForm
protected function addAssignmentElements()
{
- if (! $this->hasPermission('director/service_set/apply')) {
+ if (! $this->hasPermission(Permission::SERVICE_SET_APPLY)) {
return $this;
}
diff --git a/application/forms/IcingaTemplateChoiceForm.php b/application/forms/IcingaTemplateChoiceForm.php
index 31fe610..27af5d2 100644
--- a/application/forms/IcingaTemplateChoiceForm.php
+++ b/application/forms/IcingaTemplateChoiceForm.php
@@ -19,7 +19,7 @@ class IcingaTemplateChoiceForm extends DirectorObjectForm
{
if ($name !== null) {
/** @var IcingaTemplateChoice $class - cheating IDE */
- $class = $this->getObjectClassName();
+ $class = $this->getObjectClassname();
$this->setObject($class::load($name, $this->getDb()));
}
@@ -133,7 +133,7 @@ class IcingaTemplateChoiceForm extends DirectorObjectForm
/** @var IcingaTemplateChoice $object */
$object = $this->object();
$this->setSuccessUrl(
- 'director/templatechoice/' . $object->getObjectshortTableName(),
+ 'director/templatechoice/' . $object->getObjectShortTableName(),
$object->getUrlParams()
);
}
diff --git a/application/forms/ImportRowModifierForm.php b/application/forms/ImportRowModifierForm.php
index 9e53bd9..7033f4c 100644
--- a/application/forms/ImportRowModifierForm.php
+++ b/application/forms/ImportRowModifierForm.php
@@ -64,6 +64,41 @@ class ImportRowModifierForm extends DirectorObjectForm
$error = $e->getMessage();
$mods = $this->optionalEnum([]);
}
+ $this->addElement('YesNo', 'use_filter', [
+ 'label' => $this->translate('Set based on filter'),
+ 'ignore' => true,
+ 'class' => 'autosubmit',
+ 'required' => true,
+ ]);
+
+ if ($this->hasBeenSent()) {
+ $useFilter = $this->getSentValue('use_filter');
+ if ($useFilter === null) {
+ $this->setElementValue('use_filter', $useFilter = 'n');
+ }
+ } elseif ($object = $this->getObject()) {
+ $expression = $object->get('filter_expression');
+ $useFilter = ($expression === null || strlen($expression) === 0) ? 'n' : 'y';
+ $this->setElementValue('use_filter', $useFilter);
+ } else {
+ $this->setElementValue('use_filter', $useFilter = 'n');
+ }
+
+ if ($useFilter === 'y') {
+ $this->addElement('text', 'filter_expression', [
+ 'label' => $this->translate('Filter Expression'),
+ 'description' => $this->translate(
+ 'This allows to filter for specific parts within the given source expression.'
+ . ' You are allowed to refer all imported columns. Examples: host=www* would'
+ . ' set this property only for rows imported with a host property starting'
+ . ' with "www". Complex example: host=www*&!(address=127.*|address6=::1).'
+ . ' Please note, that CIDR notation based matches are also supported: '
+ . ' address=192.0.2.128/25| address=2001:db8::/32| address=::ffff:192.0.2.0/96'
+ ),
+ 'required' => true,
+ // TODO: validate filter
+ ]);
+ }
$this->addElement('select', 'provider_class', [
'label' => $this->translate('Modifier'),
diff --git a/application/forms/RemoveLinkForm.php b/application/forms/RemoveLinkForm.php
index 6f0c7cc..fb43db0 100644
--- a/application/forms/RemoveLinkForm.php
+++ b/application/forms/RemoveLinkForm.php
@@ -17,10 +17,7 @@ class RemoveLinkForm extends DirectorForm
{
// Required to detect the right instance
$this->formName = 'RemoveSet' . sha1(json_encode($params));
- parent::__construct([
- 'style' => 'float: right',
- 'data-base-target' => '_self'
- ]);
+ parent::__construct(['data-base-target' => '_self']);
$this->label = $label;
$this->title = $title;
foreach ($params as $name => $value) {
@@ -38,7 +35,7 @@ class RemoveLinkForm extends DirectorForm
public function setup()
{
- $this->setAttrib('class', 'inline');
+ $this->addAttribs(['class' => ['inline', 'remove-link-form']]);
$this->addHtml(Icon::create('cancel'));
$this->addSubmitButton($this->label, [
'class' => 'link-button',
diff --git a/application/forms/SyncCheckForm.php b/application/forms/SyncCheckForm.php
index 8fb3bd0..b180d3d 100644
--- a/application/forms/SyncCheckForm.php
+++ b/application/forms/SyncCheckForm.php
@@ -55,7 +55,7 @@ class SyncCheckForm extends DirectorForm
} elseif ($sum['modify'] > 1) {
}
*/
- $html = '<pre>' . print_r($sum, 1) . '</pre>';
+ $html = '<pre>' . print_r($sum, true) . '</pre>';
$this->addHtml($html);
} elseif ($this->rule->get('sync_state') === 'in-sync') {