... |
... |
@@ -1,28 +1,62 @@ |
1 |
1 |
{{groovy}} |
2 |
|
-def mainGroupDoc = xwiki.getDocument("XWiki.XWikiAllGroup").getDocument() |
3 |
|
-def groupClassRef = "XWiki.XWikiGroups" |
|
2 |
+import org.xwiki.model.reference.DocumentReference |
4 |
4 |
|
5 |
|
-def allUsers = xwiki.searchDocuments("where doc.fullName like 'XWiki.%' and doc.fullName != 'XWiki.XWikiAllGroup'") |
|
4 |
+// ------------------------------------------------------------------ |
|
5 |
+// 1. References |
|
6 |
+// ------------------------------------------------------------------ |
|
7 |
+def groupDocRef = new DocumentReference('xwiki', 'XWiki', 'XWikiAllGroup') |
|
8 |
+def groupClassRef = new DocumentReference('xwiki', 'XWiki', 'XWikiGroups') |
|
9 |
+ |
|
10 |
+// ------------------------------------------------------------------ |
|
11 |
+// 2. Load (or create) the XWikiAllGroup page |
|
12 |
+// ------------------------------------------------------------------ |
|
13 |
+def groupDocAPI = xwiki.getDocument(groupDocRef) // Document API wrapper |
|
14 |
+def groupDoc = groupDocAPI.getDocument() // Raw XWikiDocument |
|
15 |
+ |
|
16 |
+if (groupDoc.isNew()) { |
|
17 |
+ xwiki.saveDocument(groupDocAPI, 'Created XWikiAllGroup') |
|
18 |
+ println " Created XWiki.XWikiAllGroup" |
|
19 |
+} |
|
20 |
+ |
|
21 |
+// ------------------------------------------------------------------ |
|
22 |
+// 3. Collect all users except the AllGroup page itself |
|
23 |
+// ------------------------------------------------------------------ |
|
24 |
+def allUsers = xwiki.searchDocuments( |
|
25 |
+ "where doc.fullName like 'XWiki.%' and doc.fullName <> 'XWiki.XWikiAllGroup'" |
|
26 |
+) |
|
27 |
+ |
|
28 |
+// ------------------------------------------------------------------ |
|
29 |
+// 4. Add the missing users |
|
30 |
+// ------------------------------------------------------------------ |
6 |
6 |
def added = [] |
7 |
7 |
|
8 |
8 |
allUsers.each { userDocName -> |
9 |
|
- def userDoc = xwiki.getDocument(userDocName) |
10 |
|
- def alreadyMember = mainGroupDoc.getXObjects(groupClassRef)?.any { |
11 |
|
- it?.getStringValue("member") == userDocName |
|
34 |
+ // already in group? |
|
35 |
+ def alreadyMember = groupDoc.getXObjects(groupClassRef)?.any { |
|
36 |
+ it?.getStringValue('member') == userDocName |
12 |
12 |
} |
13 |
13 |
|
14 |
14 |
if (!alreadyMember) { |
15 |
|
- def newObj = mainGroupDoc.newXObject(groupClassRef) |
16 |
|
- newObj.setStringValue("member", userDocName) |
17 |
|
- added << userDocName |
|
40 |
+ // Try to create XObject using the simplest method first |
|
41 |
+ try { |
|
42 |
+ // Use the string class name instead of the DocumentReference |
|
43 |
+ def obj = groupDoc.newXObject("XWiki.XWikiGroups", xcontext) |
|
44 |
+ obj.setStringValue('member', userDocName) |
|
45 |
+ added << userDocName |
|
46 |
+ } catch (Exception e) { |
|
47 |
+ println "Could not add user ${userDocName}: ${e.message}" |
|
48 |
+ } |
18 |
18 |
} |
19 |
19 |
} |
20 |
20 |
|
|
52 |
+// ------------------------------------------------------------------ |
|
53 |
+// 5. Persist if we changed anything |
|
54 |
+// ------------------------------------------------------------------ |
21 |
21 |
if (added) { |
22 |
|
- xwiki.saveDocument(mainGroupDoc.newDocument(), "Added missing users to XWikiAllGroup: ${added.join(', ')}") |
23 |
|
- println "✅ Added users to main wiki: ${added.join(', ')}" |
|
56 |
+ xwiki.saveDocument(groupDocAPI, "Added users: ${added.join(', ')}") |
|
57 |
+ println " Added users to main wiki group: ${added.join(', ')}" |
24 |
24 |
} else { |
25 |
|
- println "✅ All users are already members of the main wiki." |
|
59 |
+ println " All users are already members of XWikiAllGroup" |
26 |
26 |
} |
27 |
27 |
{{/groovy}} |
28 |
28 |
|