From f207df4ae1e1cf032cdcec1e4c0f202b095bc192 Mon Sep 17 00:00:00 2001
From: Simon Leipold <s.leipold@adito.de>
Date: Fri, 25 Oct 2019 16:32:55 +0200
Subject: [PATCH] Permissions - #1039594 - creating/updating/deleting custom
 roles in client

---
 entity/Role_entity/Role_entity.aod            | 27 +++++++++++++++++--
 .../is_assignable/dropDownProcess.js          |  7 +++++
 .../is_assignable/valueProcess.js             |  7 +++++
 .../children/roletitle_param/valueProcess.js  |  2 +-
 .../children/roletitle_param/valueProcess.js  |  2 +-
 entity/Role_entity/grantDeleteProcess.js      |  8 ++++++
 entity/Role_entity/grantUpdateProcess.js      |  8 ++++++
 .../recordcontainers/jdito/contentProcess.js  |  2 +-
 .../recordcontainers/jdito/onDelete.js        |  5 ++++
 .../recordcontainers/jdito/onInsert.js        |  5 ++++
 .../recordcontainers/jdito/onUpdate.js        |  5 ++++
 neonView/RoleEdit_view/RoleEdit_view.aod      | 12 ++++++---
 neonView/RoleFilter_view/RoleFilter_view.aod  |  2 +-
 .../RolePreview_view/RolePreview_view.aod     |  2 +-
 14 files changed, 83 insertions(+), 11 deletions(-)
 create mode 100644 entity/Role_entity/entityfields/is_assignable/dropDownProcess.js
 create mode 100644 entity/Role_entity/entityfields/is_assignable/valueProcess.js
 create mode 100644 entity/Role_entity/grantDeleteProcess.js
 create mode 100644 entity/Role_entity/grantUpdateProcess.js
 create mode 100644 entity/Role_entity/recordcontainers/jdito/onDelete.js
 create mode 100644 entity/Role_entity/recordcontainers/jdito/onInsert.js
 create mode 100644 entity/Role_entity/recordcontainers/jdito/onUpdate.js

diff --git a/entity/Role_entity/Role_entity.aod b/entity/Role_entity/Role_entity.aod
index 9ccd151de0..d0d3f2a242 100644
--- a/entity/Role_entity/Role_entity.aod
+++ b/entity/Role_entity/Role_entity.aod
@@ -5,7 +5,8 @@
   <documentation>%aditoprj%/entity/Role_entity/documentation.adoc</documentation>
   <icon>VAADIN:USER_CHECK</icon>
   <title>Roles</title>
-  <grantCreate v="false" />
+  <grantUpdateProcess>%aditoprj%/entity/Role_entity/grantUpdateProcess.js</grantUpdateProcess>
+  <grantDeleteProcess>%aditoprj%/entity/Role_entity/grantDeleteProcess.js</grantDeleteProcess>
   <contentTitleProcess>%aditoprj%/entity/Role_entity/contentTitleProcess.js</contentTitleProcess>
   <iconId>VAADIN:USER_CHECK</iconId>
   <titlePlural>Roles</titlePlural>
@@ -17,6 +18,7 @@
     <entityField>
       <name>ROLETITLE</name>
       <title>Title</title>
+      <mandatory v="true" />
     </entityField>
     <entityField>
       <name>ROLETYPE</name>
@@ -27,7 +29,9 @@
       <title>Description</title>
     </entityField>
     <entityField>
-      <name>UID</name>
+      <name>ROLENAME</name>
+      <title>Name</title>
+      <mandatory v="true" />
     </entityField>
     <entityField>
       <name>USERCOUNT</name>
@@ -101,6 +105,16 @@
         </entityDependency>
       </dependencies>
     </entityProvider>
+    <entityField>
+      <name>IS_ASSIGNABLE</name>
+      <title>is assignable</title>
+      <contentType>BOOLEAN</contentType>
+      <dropDownProcess>%aditoprj%/entity/Role_entity/entityfields/is_assignable/dropDownProcess.js</dropDownProcess>
+      <valueProcess>%aditoprj%/entity/Role_entity/entityfields/is_assignable/valueProcess.js</valueProcess>
+    </entityField>
+    <entityField>
+      <name>UID</name>
+    </entityField>
   </entityFields>
   <recordContainers>
     <jDitoRecordContainer>
@@ -108,10 +122,16 @@
       <jDitoRecordAlias>_____SYSTEMALIAS</jDitoRecordAlias>
       <isFilterable v="true" />
       <contentProcess>%aditoprj%/entity/Role_entity/recordcontainers/jdito/contentProcess.js</contentProcess>
+      <onInsert>%aditoprj%/entity/Role_entity/recordcontainers/jdito/onInsert.js</onInsert>
+      <onUpdate>%aditoprj%/entity/Role_entity/recordcontainers/jdito/onUpdate.js</onUpdate>
+      <onDelete>%aditoprj%/entity/Role_entity/recordcontainers/jdito/onDelete.js</onDelete>
       <recordFieldMappings>
         <jDitoRecordFieldMapping>
           <name>UID.value</name>
         </jDitoRecordFieldMapping>
+        <jDitoRecordFieldMapping>
+          <name>ROLENAME.value</name>
+        </jDitoRecordFieldMapping>
         <jDitoRecordFieldMapping>
           <name>ROLETITLE.value</name>
           <isFilterable v="true" />
@@ -128,6 +148,9 @@
           <name>USERCOUNT.value</name>
           <isFilterable v="false" />
         </jDitoRecordFieldMapping>
+        <jDitoRecordFieldMapping>
+          <name>IS_ASSIGNABLE.value</name>
+        </jDitoRecordFieldMapping>
       </recordFieldMappings>
     </jDitoRecordContainer>
   </recordContainers>
diff --git a/entity/Role_entity/entityfields/is_assignable/dropDownProcess.js b/entity/Role_entity/entityfields/is_assignable/dropDownProcess.js
new file mode 100644
index 0000000000..781915d0ae
--- /dev/null
+++ b/entity/Role_entity/entityfields/is_assignable/dropDownProcess.js
@@ -0,0 +1,7 @@
+import("system.translate");
+import("system.result");
+
+result.object([
+["1", translate.text("yes")]
+,["0", translate.text("no")]
+]);
\ No newline at end of file
diff --git a/entity/Role_entity/entityfields/is_assignable/valueProcess.js b/entity/Role_entity/entityfields/is_assignable/valueProcess.js
new file mode 100644
index 0000000000..eb9de161ae
--- /dev/null
+++ b/entity/Role_entity/entityfields/is_assignable/valueProcess.js
@@ -0,0 +1,7 @@
+import("system.result");
+import("system.vars");
+import("system.neon");
+
+if (vars.get("$sys.operatingstate") == neon.OPERATINGSTATE_NEW && vars.get("$this.value") == null) {
+    result.string("1");
+}
\ No newline at end of file
diff --git a/entity/Role_entity/entityfields/permissionoverviews/children/roletitle_param/valueProcess.js b/entity/Role_entity/entityfields/permissionoverviews/children/roletitle_param/valueProcess.js
index 2c71e53a49..c4fc6bffb9 100644
--- a/entity/Role_entity/entityfields/permissionoverviews/children/roletitle_param/valueProcess.js
+++ b/entity/Role_entity/entityfields/permissionoverviews/children/roletitle_param/valueProcess.js
@@ -1,4 +1,4 @@
 import("system.vars");
 import("system.result");
 
-result.string(vars.get("$field.UID"));
\ No newline at end of file
+result.string(vars.get("$field.ROLENAME"));
\ No newline at end of file
diff --git a/entity/Role_entity/entityfields/permissions/children/roletitle_param/valueProcess.js b/entity/Role_entity/entityfields/permissions/children/roletitle_param/valueProcess.js
index 2c71e53a49..c4fc6bffb9 100644
--- a/entity/Role_entity/entityfields/permissions/children/roletitle_param/valueProcess.js
+++ b/entity/Role_entity/entityfields/permissions/children/roletitle_param/valueProcess.js
@@ -1,4 +1,4 @@
 import("system.vars");
 import("system.result");
 
-result.string(vars.get("$field.UID"));
\ No newline at end of file
+result.string(vars.get("$field.ROLENAME"));
\ No newline at end of file
diff --git a/entity/Role_entity/grantDeleteProcess.js b/entity/Role_entity/grantDeleteProcess.js
new file mode 100644
index 0000000000..81c7d9f200
--- /dev/null
+++ b/entity/Role_entity/grantDeleteProcess.js
@@ -0,0 +1,8 @@
+import("system.vars");
+import("system.result");
+
+if (vars.get("$field.ROLETYPE") == "CUSTOM") {
+    result.string(true);
+} else {
+    result.string(false);
+}
\ No newline at end of file
diff --git a/entity/Role_entity/grantUpdateProcess.js b/entity/Role_entity/grantUpdateProcess.js
new file mode 100644
index 0000000000..81c7d9f200
--- /dev/null
+++ b/entity/Role_entity/grantUpdateProcess.js
@@ -0,0 +1,8 @@
+import("system.vars");
+import("system.result");
+
+if (vars.get("$field.ROLETYPE") == "CUSTOM") {
+    result.string(true);
+} else {
+    result.string(false);
+}
\ No newline at end of file
diff --git a/entity/Role_entity/recordcontainers/jdito/contentProcess.js b/entity/Role_entity/recordcontainers/jdito/contentProcess.js
index 1044e52c48..0c6c46b70b 100644
--- a/entity/Role_entity/recordcontainers/jdito/contentProcess.js
+++ b/entity/Role_entity/recordcontainers/jdito/contentProcess.js
@@ -23,7 +23,7 @@ for each (let role in allRoles) {
     var roleName = role[3];
     if (!excludeRoles[roleName]) {
         let numberOfUsersInSelectedRole = tools.getUsersWithRole(roleName).length;
-        res.push([roleName, role[0], role[1], role[2], numberOfUsersInSelectedRole + " " + translate.text("User", locale)]);
+        res.push([roleName, roleName, role[0], role[1], role[2], numberOfUsersInSelectedRole + " " + translate.text("User", locale), role[4] ? "1" : "0"]);
     }
 }
 
diff --git a/entity/Role_entity/recordcontainers/jdito/onDelete.js b/entity/Role_entity/recordcontainers/jdito/onDelete.js
new file mode 100644
index 0000000000..fde2f29b37
--- /dev/null
+++ b/entity/Role_entity/recordcontainers/jdito/onDelete.js
@@ -0,0 +1,5 @@
+import("system.tools");
+import("system.vars");
+
+var name = vars.get("$field.ROLENAME"); // field got prefix "CUSTOM_" already
+tools.deleteRole(name);
\ No newline at end of file
diff --git a/entity/Role_entity/recordcontainers/jdito/onInsert.js b/entity/Role_entity/recordcontainers/jdito/onInsert.js
new file mode 100644
index 0000000000..bfad3b29f3
--- /dev/null
+++ b/entity/Role_entity/recordcontainers/jdito/onInsert.js
@@ -0,0 +1,5 @@
+import("system.tools");
+import("system.vars");
+
+var name = "CUSTOM_" + vars.get("$field.ROLENAME");  // field gets prefix "CUSTOM_"
+tools.createRole(name, vars.get("$field.ROLETITLE"), vars.get("$field.ROLEDESCRIPTION"), vars.get("$field.IS_ASSIGNABLE") == "0" ? false : true, null);
\ No newline at end of file
diff --git a/entity/Role_entity/recordcontainers/jdito/onUpdate.js b/entity/Role_entity/recordcontainers/jdito/onUpdate.js
new file mode 100644
index 0000000000..d6e58304bb
--- /dev/null
+++ b/entity/Role_entity/recordcontainers/jdito/onUpdate.js
@@ -0,0 +1,5 @@
+import("system.tools");
+import("system.vars");
+
+var name = vars.get("$field.ROLENAME");  // field got prefix "CUSTOM_" already
+tools.updateRole(name, vars.get("$field.ROLETITLE"), vars.get("$field.ROLEDESCRIPTION"), true, null);
\ No newline at end of file
diff --git a/neonView/RoleEdit_view/RoleEdit_view.aod b/neonView/RoleEdit_view/RoleEdit_view.aod
index a8bb06d0e8..a15091cae4 100644
--- a/neonView/RoleEdit_view/RoleEdit_view.aod
+++ b/neonView/RoleEdit_view/RoleEdit_view.aod
@@ -14,17 +14,21 @@
       <entityField>#ENTITY</entityField>
       <fields>
         <entityFieldLink>
-          <name>13372a89-9e3c-441f-a80a-2ebb2dd0d54d</name>
-          <entityField>ROLETITLE</entityField>
+          <name>851dc6d8-7522-4fb8-a46a-15fb8010f6e7</name>
+          <entityField>ROLENAME</entityField>
         </entityFieldLink>
         <entityFieldLink>
-          <name>2818c096-b602-406d-8765-ec85356da84c</name>
-          <entityField>ROLETYPE</entityField>
+          <name>13372a89-9e3c-441f-a80a-2ebb2dd0d54d</name>
+          <entityField>ROLETITLE</entityField>
         </entityFieldLink>
         <entityFieldLink>
           <name>c1008a40-23d4-43f9-a5f4-e96b9ad4e480</name>
           <entityField>ROLEDESCRIPTION</entityField>
         </entityFieldLink>
+        <entityFieldLink>
+          <name>e09d4310-1cc4-4e91-b93c-aba335456ccb</name>
+          <entityField>IS_ASSIGNABLE</entityField>
+        </entityFieldLink>
       </fields>
     </genericViewTemplate>
   </children>
diff --git a/neonView/RoleFilter_view/RoleFilter_view.aod b/neonView/RoleFilter_view/RoleFilter_view.aod
index 113dc06078..48282af2d1 100644
--- a/neonView/RoleFilter_view/RoleFilter_view.aod
+++ b/neonView/RoleFilter_view/RoleFilter_view.aod
@@ -26,7 +26,7 @@
         </neonTableColumn>
         <neonTableColumn>
           <name>954cb883-bf05-4807-b303-98b31bf8f75e</name>
-          <entityField>UID</entityField>
+          <entityField>ROLENAME</entityField>
         </neonTableColumn>
         <neonTableColumn>
           <name>e6d9050e-3139-48cc-b186-085dab1cec89</name>
diff --git a/neonView/RolePreview_view/RolePreview_view.aod b/neonView/RolePreview_view/RolePreview_view.aod
index 5cc6460a76..402b33e312 100644
--- a/neonView/RolePreview_view/RolePreview_view.aod
+++ b/neonView/RolePreview_view/RolePreview_view.aod
@@ -11,7 +11,7 @@
     <cardViewTemplate>
       <name>Card</name>
       <iconField>#ICON</iconField>
-      <titleField>UID</titleField>
+      <titleField>ROLENAME</titleField>
       <subtitleField>ROLETITLE</subtitleField>
       <descriptionField>USERCOUNT</descriptionField>
       <entityField>#ENTITY</entityField>
-- 
GitLab