... |
... |
@@ -1,62 +1,78 @@ |
1 |
1 |
{{groovy}} |
2 |
2 |
import org.xwiki.model.reference.DocumentReference |
3 |
3 |
|
4 |
|
-// ------------------------------------------------------------------ |
5 |
|
-// 1. References |
6 |
|
-// ------------------------------------------------------------------ |
7 |
|
-def groupDocRef = new DocumentReference('xwiki', 'XWiki', 'XWikiAllGroup') |
8 |
|
-def groupClassRef = new DocumentReference('xwiki', 'XWiki', 'XWikiGroups') |
|
4 |
+// References |
|
5 |
+def groupDocRef = new DocumentReference("xwiki", "XWiki", "XWikiAllGroup") |
|
6 |
+def groupClassRef = new DocumentReference("xwiki", "XWiki", "XWikiGroups") |
9 |
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 |
|
8 |
+// Load or create the group page |
|
9 |
+def groupDocAPI = xwiki.getDocument(groupDocRef) |
|
10 |
+def groupDoc = groupDocAPI.getDocument() |
15 |
15 |
|
16 |
16 |
if (groupDoc.isNew()) { |
17 |
|
- xwiki.saveDocument(groupDocAPI, 'Created XWikiAllGroup') |
18 |
|
- println " Created XWiki.XWikiAllGroup" |
|
13 |
+ xwiki.saveDocument(groupDocAPI, "🆕 Created XWikiAllGroup") |
|
14 |
+ println "🆕 Created XWiki.XWikiAllGroup" |
19 |
19 |
} |
20 |
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 |
|
-// ------------------------------------------------------------------ |
|
17 |
+// Add missing users to group |
|
18 |
+def allUsers = xwiki.searchDocuments("where doc.fullName like 'XWiki.%' and doc.fullName != 'XWiki.XWikiAllGroup'") |
31 |
31 |
def added = [] |
32 |
32 |
|
33 |
33 |
allUsers.each { userDocName -> |
34 |
|
- // already in group? |
35 |
35 |
def alreadyMember = groupDoc.getXObjects(groupClassRef)?.any { |
36 |
|
- it?.getStringValue('member') == userDocName |
|
23 |
+ it?.getStringValue("member") == userDocName |
37 |
37 |
} |
38 |
38 |
|
39 |
39 |
if (!alreadyMember) { |
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 |
|
- } |
|
27 |
+ def obj = groupDoc.newXObject(groupClassRef) |
|
28 |
+ obj.setStringValue("member", userDocName) |
|
29 |
+ added << userDocName |
49 |
49 |
} |
50 |
50 |
} |
51 |
51 |
|
52 |
|
-// ------------------------------------------------------------------ |
53 |
|
-// 5. Persist if we changed anything |
54 |
|
-// ------------------------------------------------------------------ |
|
33 |
+// Save if needed |
55 |
55 |
if (added) { |
56 |
|
- xwiki.saveDocument(groupDocAPI, "Added users: ${added.join(', ')}") |
57 |
|
- println " Added users to main wiki group: ${added.join(', ')}" |
|
35 |
+ def wrapper = xwiki.getDocument(groupDoc.documentReference) |
|
36 |
+ xwiki.saveDocument(wrapper, "✅ Added users to group: ${added.join(', ')}") |
|
37 |
+ println "✅ Added: ${added.join(', ')}" |
58 |
58 |
} else { |
59 |
|
- println " All users are already members of XWikiAllGroup" |
|
39 |
+ println "✅ All users already |
|
40 |
+ |
|
41 |
+def groupDocRef = new DocumentReference("xwiki", "XWiki", "XWikiAllGroup") |
|
42 |
+def groupClassName = "XWiki.XWikiGroups" |
|
43 |
+ |
|
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" |
60 |
60 |
} |
|
53 |
+ |
|
54 |
+// 2. Proceed as before |
|
55 |
+def allUsers = xwiki.searchDocuments("where doc.fullName like 'XWiki.%' and doc.fullName != 'XWiki.XWikiAllGroup'") |
|
56 |
+def added = [] |
|
57 |
+ |
|
58 |
+allUsers.each { userDocName -> |
|
59 |
+ def alreadyMember = groupDoc.getXObjects(groupClassName)?.any { |
|
60 |
+ it?.getStringValue("member") == userDocName |
|
61 |
+ } |
|
62 |
+ |
|
63 |
+ if (!alreadyMember) { |
|
64 |
+ def newObj = groupDoc.newXObject(groupClassName) |
|
65 |
+ newObj.setStringValue("member", userDocName) |
|
66 |
+ added << userDocName |
|
67 |
+ } |
|
68 |
+} |
|
69 |
+ |
|
70 |
+if (added) { |
|
71 |
+ def wrappedDoc = xwiki.getDocument(groupDoc.documentReference) |
|
72 |
+ xwiki.saveDocument(wrappedDoc, "✅ Added users to XWikiAllGroup: ${added.join(', ')}") |
|
73 |
+ println "✅ Added users to main wiki: ${added.join(', ')}" |
|
74 |
+} else { |
|
75 |
+ println "✅ All users are already members of the main wiki." |
|
76 |
+} |
61 |
61 |
{{/groovy}} |
62 |
62 |
|