Hide last authors
author | version | line-number | content |
---|---|---|---|
![]() |
7.1 | 1 | {{groovy}} |
2 | import org.xwiki.model.reference.DocumentReference | ||
![]() |
1.1 | 3 | |
![]() |
12.1 | 4 | // ------------------------------------------------------------------ |
5 | // 1. References | ||
6 | // ------------------------------------------------------------------ | ||
7 | def groupDocRef = new DocumentReference('xwiki', 'XWiki', 'XWikiAllGroup') | ||
8 | def groupClassRef = new DocumentReference('xwiki', 'XWiki', 'XWikiGroups') | ||
![]() |
8.1 | 9 | |
![]() |
12.1 | 10 | // ------------------------------------------------------------------ |
11 | // 2. Load (or create) the XWikiAllGroup page | ||
12 | // ------------------------------------------------------------------ | ||
![]() |
13.1 | 13 | def groupDocAPI = xwiki.getDocument(groupDocRef) // Document API wrapper |
14 | def groupDoc = groupDocAPI.getDocument() // Raw XWikiDocument | ||
![]() |
8.1 | 15 | |
16 | if (groupDoc.isNew()) { | ||
![]() |
12.1 | 17 | xwiki.saveDocument(groupDocAPI, 'Created XWikiAllGroup') |
![]() |
13.1 | 18 | println " Created XWiki.XWikiAllGroup" |
![]() |
8.1 | 19 | } |
20 | |||
![]() |
12.1 | 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 | // ------------------------------------------------------------------ | ||
![]() |
8.1 | 31 | def added = [] |
32 | |||
33 | allUsers.each { userDocName -> | ||
![]() |
12.1 | 34 | // already in group? |
35 | def alreadyMember = groupDoc.getXObjects(groupClassRef)?.any { | ||
36 | it?.getStringValue('member') == userDocName | ||
![]() |
8.1 | 37 | } |
38 | |||
39 | if (!alreadyMember) { | ||
![]() |
13.1 | 40 | // Try to create XObject using the simplest method first |
![]() |
12.1 | 41 | try { |
![]() |
13.1 | 42 | // Use the string class name instead of the DocumentReference |
43 | def obj = groupDoc.newXObject("XWiki.XWikiGroups", xcontext) | ||
![]() |
12.1 | 44 | obj.setStringValue('member', userDocName) |
45 | added << userDocName | ||
![]() |
13.1 | 46 | } catch (Exception e) { |
47 | println "Could not add user ${userDocName}: ${e.message}" | ||
![]() |
12.1 | 48 | } |
![]() |
8.1 | 49 | } |
50 | } | ||
51 | |||
![]() |
12.1 | 52 | // ------------------------------------------------------------------ |
53 | // 5. Persist if we changed anything | ||
54 | // ------------------------------------------------------------------ | ||
![]() |
8.1 | 55 | if (added) { |
![]() |
12.1 | 56 | xwiki.saveDocument(groupDocAPI, "Added users: ${added.join(', ')}") |
57 | println " Added users to main wiki group: ${added.join(', ')}" | ||
![]() |
8.1 | 58 | } else { |
![]() |
13.1 | 59 | println " All users are already members of XWikiAllGroup" |
![]() |
7.1 | 60 | } |
61 | {{/groovy}} | ||
62 |