1 | package clarin.cmdi.componentregistry.impl.database; |
---|
2 | |
---|
3 | import java.util.ArrayList; |
---|
4 | import java.util.Collections; |
---|
5 | import java.util.Comparator; |
---|
6 | import java.util.HashSet; |
---|
7 | import java.util.List; |
---|
8 | import java.util.Set; |
---|
9 | |
---|
10 | import org.springframework.beans.factory.annotation.Autowired; |
---|
11 | import org.springframework.jmx.export.annotation.ManagedOperation; |
---|
12 | import org.springframework.jmx.export.annotation.ManagedOperationParameter; |
---|
13 | import org.springframework.jmx.export.annotation.ManagedOperationParameters; |
---|
14 | import org.springframework.jmx.export.annotation.ManagedResource; |
---|
15 | import org.springframework.stereotype.Service; |
---|
16 | import org.springframework.transaction.annotation.Transactional; |
---|
17 | |
---|
18 | import clarin.cmdi.componentregistry.impl.ComponentUtils; |
---|
19 | import clarin.cmdi.componentregistry.model.BaseDescription; |
---|
20 | import clarin.cmdi.componentregistry.model.Group; |
---|
21 | import clarin.cmdi.componentregistry.model.GroupMembership; |
---|
22 | import clarin.cmdi.componentregistry.model.Ownership; |
---|
23 | import clarin.cmdi.componentregistry.model.ProfileDescription; |
---|
24 | import clarin.cmdi.componentregistry.model.RegistryUser; |
---|
25 | import clarin.cmdi.componentregistry.persistence.ComponentDao; |
---|
26 | import clarin.cmdi.componentregistry.persistence.jpa.GroupDao; |
---|
27 | import clarin.cmdi.componentregistry.persistence.jpa.GroupMembershipDao; |
---|
28 | import clarin.cmdi.componentregistry.persistence.jpa.OwnershipDao; |
---|
29 | import clarin.cmdi.componentregistry.persistence.jpa.UserDao; |
---|
30 | |
---|
31 | /** |
---|
32 | * Service that manages groups, memberships and ownerships. It exposes some |
---|
33 | * functions over JMX, that's why some methods use human-friendly names (user |
---|
34 | * principal names, group names) rather than ID arguments. |
---|
35 | * |
---|
36 | * @author george.georgovassilis@mpi.nl |
---|
37 | * |
---|
38 | */ |
---|
39 | @ManagedResource(objectName = "componentregistry:name=GroupService", description = "Operations for managing groups") |
---|
40 | @Service("GroupService") |
---|
41 | @Transactional |
---|
42 | public class GroupServiceImpl implements GroupService { |
---|
43 | |
---|
44 | @Autowired |
---|
45 | private GroupDao groupDao; |
---|
46 | @Autowired |
---|
47 | private GroupMembershipDao groupMembershipDao; |
---|
48 | @Autowired |
---|
49 | private OwnershipDao ownershipDao; |
---|
50 | @Autowired |
---|
51 | private ComponentDao componentDao; |
---|
52 | @Autowired |
---|
53 | private UserDao userDao; |
---|
54 | |
---|
55 | public void setGroupDao(GroupDao groupDao) { |
---|
56 | this.groupDao = groupDao; |
---|
57 | } |
---|
58 | |
---|
59 | public void setGroupMembershipDao(GroupMembershipDao groupMembershipDao) { |
---|
60 | this.groupMembershipDao = groupMembershipDao; |
---|
61 | } |
---|
62 | |
---|
63 | public void setOwnershipDao(OwnershipDao ownershipDao) { |
---|
64 | this.ownershipDao = ownershipDao; |
---|
65 | } |
---|
66 | |
---|
67 | @Override |
---|
68 | public List<Group> getGroupsOwnedByUser(String ownerPrincipalName) { |
---|
69 | RegistryUser owner = userDao.getByPrincipalName(ownerPrincipalName); |
---|
70 | return groupDao.findGroupOwnedByUser(owner.getId().longValue()); |
---|
71 | } |
---|
72 | |
---|
73 | @Override |
---|
74 | public boolean isUserOwnerOfGroup(long groupId, RegistryUser user) { |
---|
75 | List<Group> groups = getGroupsOwnedByUser(user.getPrincipalName()); |
---|
76 | for (Group group : groups) |
---|
77 | if (group.getId() == groupId) |
---|
78 | return true; |
---|
79 | return false; |
---|
80 | } |
---|
81 | |
---|
82 | private void checkOwnership(Ownership ownership) { |
---|
83 | if (ownership.getComponentId() == null) |
---|
84 | throw new RuntimeException("Ownership needs a componentId"); |
---|
85 | if (ownership.getUserId() == 0 && ownership.getGroupId() == 0) |
---|
86 | throw new RuntimeException("Ownership needs a groupId or userId"); |
---|
87 | if (ownership.getUserId() != 0 && ownership.getGroupId() != 0) |
---|
88 | throw new RuntimeException("Ownership has both a groupId and a userId "); |
---|
89 | } |
---|
90 | |
---|
91 | private void assertOwnershipDoesNotExist(Ownership ownership) { |
---|
92 | Ownership o = ownershipDao.findOwnershipByGroupAndComponent(ownership.getGroupId(), ownership.getComponentId()); |
---|
93 | if (o != null) |
---|
94 | throw new ValidationException("Ownership exists"); |
---|
95 | } |
---|
96 | |
---|
97 | @Override |
---|
98 | public void addOwnership(Ownership ownership) { |
---|
99 | checkOwnership(ownership); |
---|
100 | assertOwnershipDoesNotExist(ownership); |
---|
101 | ownershipDao.save(ownership); |
---|
102 | } |
---|
103 | |
---|
104 | @Override |
---|
105 | public void removeOwnership(Ownership ownership) { |
---|
106 | throw new RuntimeException("not implemented"); |
---|
107 | } |
---|
108 | |
---|
109 | protected boolean canUserAccessAbstractDescriptionEitherOnHisOwnOrThroughGroupMembership(RegistryUser user, |
---|
110 | BaseDescription description) { |
---|
111 | // TODO make some joins and multi-id queries to speed the entire method |
---|
112 | // up |
---|
113 | boolean isProfile = (description instanceof ProfileDescription); |
---|
114 | long userId = user.getId().longValue(); |
---|
115 | // anyone can access public profile |
---|
116 | if (componentDao.isPublic(description.getId())) |
---|
117 | return true; |
---|
118 | // the creator can also access any profile |
---|
119 | if (description.getUserId().equals(user.getId() + "")) |
---|
120 | return true; |
---|
121 | |
---|
122 | // a co-ownership on the profile also allows access |
---|
123 | Ownership ownership = ownershipDao.findOwnershipByUserAndComponent(userId, description.getId()); |
---|
124 | if (ownership != null) |
---|
125 | return true; |
---|
126 | |
---|
127 | // get a list of groups the user owns and is a member of |
---|
128 | List<Group> groups = groupDao.findGroupOwnedByUser(userId); |
---|
129 | Set<Long> groupIds = new HashSet<Long>(); |
---|
130 | for (Group group : groups) |
---|
131 | groupIds.add(group.getId()); |
---|
132 | |
---|
133 | List<GroupMembership> memberships = groupMembershipDao.findGroupsTheUserIsAmemberOf(userId); |
---|
134 | for (GroupMembership gm : memberships) |
---|
135 | groupIds.add(gm.getGroupId()); |
---|
136 | |
---|
137 | for (Long groupId : groupIds) { |
---|
138 | ownership = ownershipDao.findOwnershipByGroupAndComponent(groupId, description.getId()); |
---|
139 | if (ownership != null) |
---|
140 | return true; |
---|
141 | } |
---|
142 | return false; |
---|
143 | } |
---|
144 | |
---|
145 | @Override |
---|
146 | public boolean canUserAccessComponentEitherOnHisOwnOrThroughGroupMembership(RegistryUser user, |
---|
147 | BaseDescription baseDescription) { |
---|
148 | return canUserAccessAbstractDescriptionEitherOnHisOwnOrThroughGroupMembership(user, baseDescription); |
---|
149 | } |
---|
150 | |
---|
151 | @Override |
---|
152 | @ManagedOperation(description = "Make a user member of a group") |
---|
153 | @ManagedOperationParameters({ |
---|
154 | @ManagedOperationParameter(name = "principalName", description = "Principal name of the user to make a member"), |
---|
155 | @ManagedOperationParameter(name = "groupName", description = "Name of the group") }) |
---|
156 | public long makeMember(String userPrincipalName, String groupName) { |
---|
157 | RegistryUser user = userDao.getByPrincipalName(userPrincipalName); |
---|
158 | Group group = groupDao.findGroupByName(groupName); |
---|
159 | GroupMembership gm = groupMembershipDao.findMembership(user.getId().longValue(), group.getId()); |
---|
160 | if (gm != null) |
---|
161 | return gm.getId(); |
---|
162 | gm = new GroupMembership(); |
---|
163 | gm.setGroupId(group.getId()); |
---|
164 | gm.setUserId(user.getId().longValue()); |
---|
165 | return groupMembershipDao.save(gm).getId(); |
---|
166 | } |
---|
167 | |
---|
168 | @ManagedOperation(description = "Create a new group") |
---|
169 | @ManagedOperationParameters({ |
---|
170 | @ManagedOperationParameter(name = "name", description = "Name of the group, must be unique"), |
---|
171 | @ManagedOperationParameter(name = "ownerPrincipalName", description = "Principal name of the user") }) |
---|
172 | @Override |
---|
173 | public long createNewGroup(String name, String ownerPrincipalName) { |
---|
174 | RegistryUser owner = userDao.getByPrincipalName(ownerPrincipalName); |
---|
175 | if (owner == null) |
---|
176 | throw new ValidationException("No principal '" + ownerPrincipalName + "' found"); |
---|
177 | Group group = groupDao.findGroupByName(name); |
---|
178 | if (group != null) |
---|
179 | throw new ValidationException("Group '" + name + "' already exists"); |
---|
180 | group = new Group(); |
---|
181 | group.setName(name); |
---|
182 | group.setOwnerId(owner.getId().longValue()); |
---|
183 | group = groupDao.save(group); |
---|
184 | return group.getId(); |
---|
185 | } |
---|
186 | |
---|
187 | @ManagedOperation(description = "List available groups") |
---|
188 | @Override |
---|
189 | public List<String> listGroupNames() { |
---|
190 | List<String> groupNames = new ArrayList<String>(); |
---|
191 | for (Group group : groupDao.findAll()) |
---|
192 | groupNames.add(group.getName()); |
---|
193 | return groupNames; |
---|
194 | } |
---|
195 | |
---|
196 | @Override |
---|
197 | public List<Group> getGroupsOfWhichUserIsAMember(String principal) { |
---|
198 | RegistryUser user = userDao.getByPrincipalName(principal); |
---|
199 | if (user == null || user.getId() == null) |
---|
200 | return new ArrayList<Group>(); |
---|
201 | List<GroupMembership> memberships = groupMembershipDao.findGroupsTheUserIsAmemberOf(user.getId().longValue()); |
---|
202 | List<Group> groups = new ArrayList<Group>(); |
---|
203 | for (GroupMembership m : memberships) |
---|
204 | groups.add(groupDao.findOne(m.getGroupId())); |
---|
205 | return groups; |
---|
206 | } |
---|
207 | |
---|
208 | @Override |
---|
209 | public List<String> getComponentIdsInGroup(long groupId) { |
---|
210 | List<Ownership> ownerships = ownershipDao.findOwnershipByGroup(groupId); |
---|
211 | Set<String> componentIds = new HashSet<String>(); |
---|
212 | for (Ownership o : ownerships) |
---|
213 | if (ComponentUtils.isComponentId(o.getComponentId())) |
---|
214 | componentIds.add(o.getComponentId()); |
---|
215 | List<String> idsList = new ArrayList<String>(componentIds); |
---|
216 | Collections.sort(idsList); |
---|
217 | return idsList; |
---|
218 | } |
---|
219 | |
---|
220 | @Override |
---|
221 | public List<String> getProfileIdsInGroup(long groupId) { |
---|
222 | List<Ownership> ownerships = ownershipDao.findOwnershipByGroup(groupId); |
---|
223 | Set<String> profileIds = new HashSet<String>(); |
---|
224 | for (Ownership o : ownerships) |
---|
225 | if (ComponentUtils.isProfileId(o.getComponentId())) |
---|
226 | profileIds.add(o.getComponentId()); |
---|
227 | List<String> idsList = new ArrayList<String>(profileIds); |
---|
228 | Collections.sort(idsList); |
---|
229 | return idsList; |
---|
230 | } |
---|
231 | |
---|
232 | @Override |
---|
233 | public List<Group> getGroupsTheItemIsAMemberOf(String itemId) { |
---|
234 | Set<Ownership> ownerships = new HashSet<Ownership>(); |
---|
235 | ownerships.addAll(ownershipDao.findOwnershipByComponentId(itemId)); |
---|
236 | Set<Group> groups = new HashSet<Group>(); |
---|
237 | for (Ownership ownership : ownerships) |
---|
238 | groups.add(groupDao.findOne(ownership.getGroupId())); |
---|
239 | List<Group> groupList = new ArrayList<Group>(groups); |
---|
240 | Collections.sort(groupList, new Comparator<Group>() { |
---|
241 | |
---|
242 | @Override |
---|
243 | public int compare(Group g1, Group g2) { |
---|
244 | return (int) (g1.getId() - g2.getId()); |
---|
245 | } |
---|
246 | }); |
---|
247 | return groupList; |
---|
248 | } |
---|
249 | |
---|
250 | @ManagedOperation(description = "Make a component owned by a group instead of a user") |
---|
251 | @ManagedOperationParameters({ |
---|
252 | @ManagedOperationParameter(name = "principal", description = "Name of the principal who owns the component"), |
---|
253 | @ManagedOperationParameter(name = "groupName", description = "Name of the group to move the component to"), |
---|
254 | @ManagedOperationParameter(name = "componentId", description = "Id of component") }) |
---|
255 | @Override |
---|
256 | public void transferItemOwnershipFromUserToGroup(String principal, String groupName, String itemId) { |
---|
257 | |
---|
258 | BaseDescription item = null; |
---|
259 | item = componentDao.getByCmdId(itemId); |
---|
260 | if (item == null) |
---|
261 | throw new ValidationException("No profile or component found with ID " + itemId); |
---|
262 | Group group = groupDao.findGroupByName(groupName); |
---|
263 | if (group == null) |
---|
264 | throw new ValidationException("No group found with name " + groupName); |
---|
265 | Ownership ownership = null; |
---|
266 | List<Ownership> oldOwnerships = ownershipDao.findOwnershipByComponentId(itemId); |
---|
267 | ownershipDao.delete(oldOwnerships); |
---|
268 | ownership = new Ownership(); |
---|
269 | ownership.setComponentId(itemId); |
---|
270 | ownership.setGroupId(group.getId()); |
---|
271 | addOwnership(ownership); |
---|
272 | } |
---|
273 | |
---|
274 | @Override |
---|
275 | public void transferItemOwnershipFromUserToGroupId(String principal, long groupId, String componentId) { |
---|
276 | Group group = groupDao.findOne(groupId); |
---|
277 | if (group == null) |
---|
278 | throw new ValidationException("No group found with id " + groupId); |
---|
279 | transferItemOwnershipFromUserToGroup(principal, group.getName(), componentId); |
---|
280 | } |
---|
281 | |
---|
282 | } |
---|