1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.cms.security;
35
36 import java.util.ArrayList;
37 import java.util.Collection;
38
39 import info.magnolia.cms.beans.config.ContentRepository;
40 import info.magnolia.cms.core.Content;
41 import info.magnolia.cms.core.ItemType;
42 import info.magnolia.cms.core.HierarchyManager;
43 import info.magnolia.context.MgnlContext;
44
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 import javax.jcr.PathNotFoundException;
49
50
51
52
53
54 public class MgnlGroupManager implements GroupManager {
55 private static final Logger log = LoggerFactory.getLogger(MgnlGroupManager.class);
56
57
58
59
60
61
62
63
64 public Group createGroup(String name) throws UnsupportedOperationException, AccessDeniedException {
65 try {
66 Content node = getHierarchyManager().createContent("/", name, ItemType.GROUP.getSystemName());
67 getHierarchyManager().save();
68 return newGroupInstance(node);
69 }
70 catch (Exception e) {
71 log.error("can't create group [" + name + "]", e);
72 return null;
73 }
74 }
75
76
77
78
79
80
81
82
83 public Group getGroup(String name) throws UnsupportedOperationException, AccessDeniedException {
84 try {
85 return newGroupInstance(getHierarchyManager().getContent(name));
86 } catch (PathNotFoundException e) {
87
88 log.warn("can't find group [" + name + "] in magnolia");
89 log.debug("can't find group [" + name + "] in magnolia", e);
90 } catch (AccessDeniedException e) {
91 throw e;
92 } catch (Throwable e) {
93 log.error("Exception while retrieving group", e);
94 }
95 return null;
96 }
97
98
99
100
101 public Collection<Group> getAllGroups() {
102 Collection<Group> groups = new ArrayList<Group>();
103 try {
104 Collection<Content> nodes = getHierarchyManager().getRoot().getChildren(ItemType.GROUP);
105 for (Content node : nodes) {
106 groups.add(newGroupInstance(node));
107 }
108 }
109 catch (Exception e) {
110 log.error("can't find user");
111 }
112 return groups;
113 }
114
115 protected Group newGroupInstance(Content node) {
116 return new MgnlGroup(node);
117 }
118
119
120
121
122 protected HierarchyManager getHierarchyManager() {
123 return MgnlContext.getSystemContext().getHierarchyManager(ContentRepository.USER_GROUPS);
124 }
125 }