From 15187ddc40df03972d14ed8c1bade79d13143381 Mon Sep 17 00:00:00 2001
From: Simon Leipold <s.leipold@adito.de>
Date: Tue, 18 Feb 2020 15:47:24 +0100
Subject: [PATCH] [Projekt: Entwicklung - Neon][TicketNr.: 1049945][Rollen -
 Fehler bei Zuweisung von Kindrollen]

---
 .../child_role/dropDownProcess.js             | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)

diff --git a/entity/RoleChildren_entity/entityfields/child_role/dropDownProcess.js b/entity/RoleChildren_entity/entityfields/child_role/dropDownProcess.js
index 34affa61dc9..8867a68b1cb 100644
--- a/entity/RoleChildren_entity/entityfields/child_role/dropDownProcess.js
+++ b/entity/RoleChildren_entity/entityfields/child_role/dropDownProcess.js
@@ -7,15 +7,32 @@ var newChildRoles = [];
 var alias = SqlUtils.getSystemAlias();
 var parentRole = vars.get("$param.RoleId_param");
 var allCustomAndProjectRoles = tools.getAllRoles(["CUSTOM", "PROJECT"], true);
+
 var childRoles = newSelect("CHILD_ROLE", alias)
 .from("ASYS_ROLES_CHILDREN")
 .where("ASYS_ROLES_CHILDREN.PARENT_ROLE", parentRole)
 .table();
 
+var parentIsAlreadyChildOf = newSelect("PARENT_ROLE", alias)
+.from("ASYS_ROLES_CHILDREN")
+.where("ASYS_ROLES_CHILDREN.CHILD_ROLE", parentRole)
+.table();
+
 for each (let role in allCustomAndProjectRoles) {
-    if (role[3] != parentRole) { // dont show same role as parent in dropdown
+    // dont show current role as possible child in dropdown
+    if (role[3] != parentRole) {
         newChildRoles.push([role[3], role[3]]);
     }
 }
 
+// check for cyclces in hierarchy, A can't be child of B while it is already parent of B
+for each (let role in parentIsAlreadyChildOf) {
+    newChildRoles = newChildRoles.filter(function(childRole) {
+        if (role == childRole[0]) {
+            return false;
+        }
+        return true;
+    });
+}
+
 result.object(newChildRoles.sort());
\ No newline at end of file
-- 
GitLab