... |
... |
@@ -1,83 +1,42 @@ |
1 |
1 |
{{groovy}} |
2 |
2 |
import org.xwiki.model.reference.DocumentReference |
3 |
|
-import org.xwiki.model.reference.EntityReference |
4 |
|
-import org.xwiki.model.reference.EntityType |
5 |
5 |
|
6 |
|
-// ------------------------------------------------------------------ |
7 |
|
-// 1. References |
8 |
|
-// ------------------------------------------------------------------ |
9 |
|
-def groupDocRef = new DocumentReference('xwiki', 'XWiki', 'XWikiAllGroup') |
10 |
|
-def groupClassRef = new DocumentReference('xwiki', 'XWiki', 'XWikiGroups') |
|
4 |
+// References |
|
5 |
+def groupDocRef = new DocumentReference("xwiki", "XWiki", "XWikiAllGroup") |
|
6 |
+def groupClassName = "XWiki.XWikiGroups" as String // Force string type! |
11 |
11 |
|
12 |
|
-// ------------------------------------------------------------------ |
13 |
|
-// 2. Load (or create) the XWikiAllGroup page |
14 |
|
-// ------------------------------------------------------------------ |
15 |
|
-def groupDocAPI = xwiki.getDocument(groupDocRef) // Document API wrapper |
16 |
|
-def groupDoc = groupDocAPI.getDocument() // Raw XWikiDocument |
|
8 |
+// Load or create the group page |
|
9 |
+def groupDocAPI = xwiki.getDocument(groupDocRef) |
|
10 |
+def groupDoc = groupDocAPI.getDocument() |
17 |
17 |
|
18 |
18 |
if (groupDoc.isNew()) { |
19 |
|
- xwiki.saveDocument(groupDocAPI, 'Created XWikiAllGroup') |
20 |
|
- println ' Created XWiki.XWikiAllGroup' |
|
13 |
+ xwiki.saveDocument(groupDocAPI, "Created XWikiAllGroup") |
|
14 |
+ println "Created XWiki.XWikiAllGroup" |
21 |
21 |
} |
22 |
22 |
|
23 |
|
-// ------------------------------------------------------------------ |
24 |
|
-// 3. Collect all users except the AllGroup page itself |
25 |
|
-// ------------------------------------------------------------------ |
26 |
|
-def allUsers = xwiki.searchDocuments( |
27 |
|
- "where doc.fullName like 'XWiki.%' and doc.fullName <> 'XWiki.XWikiAllGroup'" |
28 |
|
-) |
29 |
|
- |
30 |
|
-// ------------------------------------------------------------------ |
31 |
|
-// 4. Add the missing users |
32 |
|
-// ------------------------------------------------------------------ |
|
17 |
+// Add missing users to group |
|
18 |
+def allUsers = xwiki.searchDocuments("where doc.fullName like 'XWiki.%' and doc.fullName != 'XWiki.XWikiAllGroup'") |
33 |
33 |
def added = [] |
34 |
34 |
|
35 |
35 |
allUsers.each { userDocName -> |
36 |
|
- // already in group? |
37 |
|
- def alreadyMember = groupDoc.getXObjects(groupClassRef)?.any { |
38 |
|
- it?.getStringValue('member') == userDocName |
|
22 |
+ def alreadyMember = groupDoc.getXObjects(groupClassName)?.any { |
|
23 |
+ it?.getStringValue("member") == userDocName |
39 |
39 |
} |
40 |
40 |
|
41 |
41 |
if (!alreadyMember) { |
42 |
|
- // Try different methods to create the XObject. |
43 |
|
- def obj = null |
44 |
|
- try { |
45 |
|
- // Method 1: Use xcontext |
46 |
|
- def context = xcontext ?: contextProvider.get() |
47 |
|
- obj = groupDoc.newXObject(groupClassRef, context) |
48 |
|
- } catch (Exception e1) { |
49 |
|
- try { |
50 |
|
- // Method 2: Use EntityReference |
51 |
|
- obj = groupDoc.newXObject( |
52 |
|
- new EntityReference( |
53 |
|
- 'XWikiGroups', |
54 |
|
- EntityType.DOCUMENT, |
55 |
|
- new EntityReference('XWiki', EntityType.SPACE) |
56 |
|
- ) |
57 |
|
- ) |
58 |
|
- } catch (Exception e2) { |
59 |
|
- println "Failed to create XObject using either method:" |
60 |
|
- println "Method 1 Exception: ${e1.getMessage()}" |
61 |
|
- println "Method 2 Exception: ${e2.getMessage()}" |
62 |
|
- return // Skip this user if we can't create the object |
63 |
|
- } |
64 |
|
- } |
65 |
|
- |
66 |
|
- if (obj != null) { |
67 |
|
- obj.setStringValue('member', userDocName) |
68 |
|
- added << userDocName |
69 |
|
- } |
|
27 |
+ def obj = groupDoc.newXObject(groupClassName) |
|
28 |
+ obj.setStringValue("member", userDocName) |
|
29 |
+ added << userDocName |
70 |
70 |
} |
71 |
71 |
} |
72 |
72 |
|
73 |
|
-// ------------------------------------------------------------------ |
74 |
|
-// 5. Persist if we changed anything |
75 |
|
-// ------------------------------------------------------------------ |
|
33 |
+// Save if needed |
76 |
76 |
if (added) { |
77 |
|
- xwiki.saveDocument(groupDocAPI, "Added users: ${added.join(', ')}") |
78 |
|
- println " Added users to main wiki group: ${added.join(', ')}" |
|
35 |
+ def wrappedDoc = xwiki.getDocument(groupDoc.documentReference) |
|
36 |
+ xwiki.saveDocument(wrappedDoc, "Added users to group: ${added.join(', ')}") |
|
37 |
+ println "Added users: ${added.join(', ')}" |
79 |
79 |
} else { |
80 |
|
- println ' All users are already members of XWikiAllGroup' |
|
39 |
+ println "All users are already members of the main wiki." |
81 |
81 |
} |
82 |
82 |
{{/groovy}} |
83 |
83 |
|