... |
... |
@@ -1,18 +1,67 @@ |
1 |
1 |
{{groovy}} |
2 |
|
-def groupDocRef = "XWiki.XWikiAllGroup" |
|
2 |
+import org.xwiki.model.reference.DocumentReference |
|
3 |
+ |
|
4 |
+// References |
|
5 |
+def groupDocRef = new DocumentReference("xwiki", "XWiki", "XWikiAllGroup") |
|
6 |
+def groupClassRef = new DocumentReference("xwiki", "XWiki", "XWikiGroups") |
|
7 |
+ |
|
8 |
+// Load or create the group page |
|
9 |
+def groupDocAPI = xwiki.getDocument(groupDocRef) |
|
10 |
+def groupDoc = groupDocAPI.getDocument() |
|
11 |
+ |
|
12 |
+if (groupDoc.isNew()) { |
|
13 |
+ xwiki.saveDocument(groupDocAPI, "🆕 Created XWikiAllGroup") |
|
14 |
+ println "🆕 Created XWiki.XWikiAllGroup" |
|
15 |
+} |
|
16 |
+ |
|
17 |
+// Add missing users to group |
|
18 |
+def allUsers = xwiki.searchDocuments("where doc.fullName like 'XWiki.%' and doc.fullName != 'XWiki.XWikiAllGroup'") |
|
19 |
+def added = [] |
|
20 |
+ |
|
21 |
+allUsers.each { userDocName -> |
|
22 |
+ def alreadyMember = groupDoc.getXObjects(groupClassRef)?.any { |
|
23 |
+ it?.getStringValue("member") == userDocName |
|
24 |
+ } |
|
25 |
+ |
|
26 |
+ if (!alreadyMember) { |
|
27 |
+ def obj = groupDoc.newXObject(groupClassRef) |
|
28 |
+ obj.setStringValue("member", userDocName) |
|
29 |
+ added << userDocName |
|
30 |
+ } |
|
31 |
+} |
|
32 |
+ |
|
33 |
+// Save if needed |
|
34 |
+if (added) { |
|
35 |
+ def wrapper = xwiki.getDocument(groupDoc.documentReference) |
|
36 |
+ xwiki.saveDocument(wrapper, "✅ Added users to group: ${added.join(', ')}") |
|
37 |
+ println "✅ Added: ${added.join(', ')}" |
|
38 |
+} else { |
|
39 |
+ println "✅ All users already |
|
40 |
+ |
|
41 |
+def groupDocRef = new DocumentReference("xwiki", "XWiki", "XWikiAllGroup") |
3 |
3 |
def groupClassName = "XWiki.XWikiGroups" |
4 |
4 |
|
5 |
|
-def mainGroupDoc = xwiki.getDocument(groupDocRef).getDocument() |
|
44 |
+// 1. Ensure the group document exists |
|
45 |
+def groupDocAPI = xwiki.getDocument(groupDocRef) |
|
46 |
+def groupDoc = groupDocAPI.getDocument() |
|
47 |
+ |
|
48 |
+if (groupDoc.isNew()) { |
|
49 |
+ def apiDoc = xwiki.getDocument(groupDoc.documentReference) |
|
50 |
+ xwiki.saveDocument(apiDoc, "Created XWikiAllGroup page") |
|
51 |
+ println "🆕 Created XWiki.XWikiAllGroup page" |
|
52 |
+} |
|
53 |
+ |
|
54 |
+// 2. Proceed as before |
6 |
6 |
def allUsers = xwiki.searchDocuments("where doc.fullName like 'XWiki.%' and doc.fullName != 'XWiki.XWikiAllGroup'") |
7 |
7 |
def added = [] |
8 |
8 |
|
9 |
9 |
allUsers.each { userDocName -> |
10 |
|
- def alreadyMember = mainGroupDoc.getXObjects(groupClassName)?.any { |
|
59 |
+ def alreadyMember = groupDoc.getXObjects(groupClassName)?.any { |
11 |
11 |
it?.getStringValue("member") == userDocName |
12 |
12 |
} |
13 |
13 |
|
14 |
14 |
if (!alreadyMember) { |
15 |
|
- def newObj = mainGroupDoc.newXObject(groupClassName) |
|
64 |
+ def newObj = groupDoc.newXObject(groupClassName) |
16 |
16 |
newObj.setStringValue("member", userDocName) |
17 |
17 |
added << userDocName |
18 |
18 |
} |
... |
... |
@@ -19,7 +19,7 @@ |
19 |
19 |
} |
20 |
20 |
|
21 |
21 |
if (added) { |
22 |
|
- def wrappedDoc = xwiki.getDocument(mainGroupDoc.documentReference) |
|
71 |
+ def wrappedDoc = xwiki.getDocument(groupDoc.documentReference) |
23 |
23 |
xwiki.saveDocument(wrappedDoc, "✅ Added users to XWikiAllGroup: ${added.join(', ')}") |
24 |
24 |
println "✅ Added users to main wiki: ${added.join(', ')}" |
25 |
25 |
} else { |