summaryrefslogtreecommitdiffstats
path: root/library/Director/Objects/IcingaObjectLegacyAssignments.php
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-14 13:17:31 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-14 13:17:31 +0000
commitf66ab8dae2f3d0418759f81a3a64dc9517a62449 (patch)
treefbff2135e7013f196b891bbde54618eb050e4aaf /library/Director/Objects/IcingaObjectLegacyAssignments.php
parentInitial commit. (diff)
downloadicingaweb2-module-director-f66ab8dae2f3d0418759f81a3a64dc9517a62449.tar.xz
icingaweb2-module-director-f66ab8dae2f3d0418759f81a3a64dc9517a62449.zip
Adding upstream version 1.10.2.upstream/1.10.2
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'library/Director/Objects/IcingaObjectLegacyAssignments.php')
-rw-r--r--library/Director/Objects/IcingaObjectLegacyAssignments.php79
1 files changed, 79 insertions, 0 deletions
diff --git a/library/Director/Objects/IcingaObjectLegacyAssignments.php b/library/Director/Objects/IcingaObjectLegacyAssignments.php
new file mode 100644
index 0000000..6ab75c8
--- /dev/null
+++ b/library/Director/Objects/IcingaObjectLegacyAssignments.php
@@ -0,0 +1,79 @@
+<?php
+
+namespace Icinga\Module\Director\Objects;
+
+use LogicException;
+
+/**
+ * This class is required for historical reasons
+ *
+ * Objects with assignments in your activity log would otherwise not be able
+ * to render themselves
+ */
+class IcingaObjectLegacyAssignments
+{
+ public static function applyToObject(IcingaObject $object, $values)
+ {
+ if (! $object->supportsAssignments()) {
+ throw new LogicException(sprintf(
+ 'I can only assign for applied objects, got %s',
+ $object->object_type
+ ));
+ }
+
+ if ($values === null) {
+ return $object;
+ }
+
+ if (! is_array($values)) {
+ static::throwCompatError();
+ }
+
+ if (empty($values)) {
+ return $object;
+ }
+
+ $assigns = array();
+ $ignores = array();
+ foreach ($values as $type => $value) {
+ if (strpos($value, '|') !== false || strpos($value, '&' !== false)) {
+ $value = '(' . $value . ')';
+ }
+
+ if ($type === 'assign') {
+ $assigns[] = $value;
+ } elseif ($type === 'ignore') {
+ $ignores[] = $value;
+ } else {
+ static::throwCompatError();
+ }
+ }
+
+ $assign = implode('|', $assigns);
+ $ignore = implode('&', $ignores);
+ if (empty($assign)) {
+ $filter = $ignore;
+ } elseif (empty($ignore)) {
+ $filter = $assign;
+ } else {
+ if (count($assigns) === 1) {
+ $filter = $assign . '&' . $ignore;
+ } else {
+ $filter = '(' . $assign . ')&(' . $ignore . ')';
+ }
+ }
+
+ $object->assign_filter = $filter;
+
+ return $object;
+ }
+
+ protected static function throwCompatError()
+ {
+ throw new LogicException(
+ 'You ran into an unexpected compatibility issue. Please report'
+ . ' this with details helping us to reproduce this to the'
+ . ' Icinga project'
+ );
+ }
+}