1
|
/**
|
2
|
* Copyright (C) 2007 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
|
10
|
package eu.etaxonomy.cdm.api.service;
|
11
|
|
12
|
import java.util.ArrayList;
|
13
|
import java.util.HashMap;
|
14
|
import java.util.Iterator;
|
15
|
import java.util.List;
|
16
|
import java.util.Map;
|
17
|
import java.util.Set;
|
18
|
import java.util.UUID;
|
19
|
|
20
|
import org.hibernate.criterion.Criterion;
|
21
|
import org.springframework.beans.factory.annotation.Autowired;
|
22
|
import org.springframework.security.access.prepost.PreAuthorize;
|
23
|
import org.springframework.security.core.GrantedAuthority;
|
24
|
import org.springframework.stereotype.Service;
|
25
|
import org.springframework.transaction.annotation.Transactional;
|
26
|
import org.springframework.util.Assert;
|
27
|
|
28
|
import eu.etaxonomy.cdm.model.permission.GrantedAuthorityImpl;
|
29
|
import eu.etaxonomy.cdm.model.permission.Group;
|
30
|
import eu.etaxonomy.cdm.model.permission.User;
|
31
|
import eu.etaxonomy.cdm.persistence.dao.permission.IGrantedAuthorityDao;
|
32
|
import eu.etaxonomy.cdm.persistence.dao.permission.IGroupDao;
|
33
|
import eu.etaxonomy.cdm.persistence.dao.permission.IUserDao;
|
34
|
import eu.etaxonomy.cdm.persistence.dto.MergeResult;
|
35
|
import eu.etaxonomy.cdm.persistence.query.MatchMode;
|
36
|
import eu.etaxonomy.cdm.persistence.query.OrderHint;
|
37
|
|
38
|
/**
|
39
|
* @author n.hoffmann
|
40
|
* @since Mar 9, 2011
|
41
|
*/
|
42
|
@Service
|
43
|
@Transactional(readOnly = true)
|
44
|
@PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_USER_MANAGER')")
|
45
|
public class GroupServiceImpl extends ServiceBase<Group,IGroupDao> implements IGroupService {
|
46
|
|
47
|
protected IUserDao userDao;
|
48
|
|
49
|
protected IGrantedAuthorityDao grantedAuthorityDao;
|
50
|
|
51
|
@Override
|
52
|
public List<String> findAllGroups() {
|
53
|
return dao.listNames(null,null);
|
54
|
}
|
55
|
|
56
|
@Override
|
57
|
public List<String> findUsersInGroup(String groupName) {
|
58
|
Assert.hasText(groupName);
|
59
|
Group group = dao.findGroupByName(groupName);
|
60
|
|
61
|
List<String> users = dao.listMembers(group, null, null);
|
62
|
|
63
|
return users;
|
64
|
}
|
65
|
|
66
|
@Override
|
67
|
public boolean groupExists(String groupName) {
|
68
|
Assert.hasText(groupName, "Parameter 'groupName' must not be empty.");
|
69
|
Group group = dao.findGroupByName(groupName);
|
70
|
return group != null;
|
71
|
}
|
72
|
|
73
|
@Override
|
74
|
public Group findGroup(String groupName) {
|
75
|
Assert.hasText(groupName, "Parameter 'groupname' must not be empty.");
|
76
|
Group group = dao.findGroupByName(groupName);
|
77
|
return group;
|
78
|
}
|
79
|
|
80
|
@Override
|
81
|
@Transactional(readOnly=false)
|
82
|
public void deleteGroup(String groupUUID) {
|
83
|
Assert.notNull(groupUUID);
|
84
|
|
85
|
Group group = dao.findByUuid(UUID.fromString(groupUUID));
|
86
|
Iterator<User> it = group.getMembers().iterator();
|
87
|
group.getMembers().clear();
|
88
|
// while (it.hasNext()){
|
89
|
// it.remove();
|
90
|
// }
|
91
|
dao.delete(group);
|
92
|
|
93
|
}
|
94
|
|
95
|
@Override
|
96
|
@Transactional(readOnly=false)
|
97
|
public void renameGroup(String oldName, String newName) {
|
98
|
Assert.hasText(oldName);
|
99
|
Assert.hasText(newName);
|
100
|
|
101
|
Group group = dao.findGroupByName(oldName);
|
102
|
|
103
|
group.setName(newName);
|
104
|
dao.update(group);
|
105
|
}
|
106
|
|
107
|
@Override
|
108
|
@Transactional(readOnly=false)
|
109
|
public void addUserToGroup(String username, String groupName) {
|
110
|
Assert.hasText(username);
|
111
|
Assert.hasText(groupName);
|
112
|
|
113
|
Group group = dao.findGroupByName(groupName);
|
114
|
User user = userDao.findUserByUsername(username);
|
115
|
|
116
|
if(group != null && user != null){
|
117
|
if(group.addMember(user)) {
|
118
|
dao.update(group);
|
119
|
}
|
120
|
}
|
121
|
}
|
122
|
|
123
|
@Override
|
124
|
@Transactional(readOnly=false)
|
125
|
public void removeUserFromGroup(String username, String groupName) {
|
126
|
Assert.hasText(username);
|
127
|
Assert.hasText(groupName);
|
128
|
|
129
|
Group group = dao.findGroupByName(groupName);
|
130
|
User user = userDao.findUserByUsername(username);
|
131
|
|
132
|
if(group != null && user != null){
|
133
|
if(group.removeMember(user)){
|
134
|
dao.update(group);
|
135
|
}
|
136
|
}
|
137
|
}
|
138
|
|
139
|
@Override
|
140
|
public List<GrantedAuthority> findGroupAuthorities(String groupName) {
|
141
|
Assert.hasText(groupName);
|
142
|
Group group = dao.findGroupByName(groupName);
|
143
|
|
144
|
if (group != null){
|
145
|
return new ArrayList<>(group.getGrantedAuthorities());
|
146
|
}
|
147
|
|
148
|
return new ArrayList<>();
|
149
|
}
|
150
|
|
151
|
@Override
|
152
|
@Transactional(readOnly=false)
|
153
|
public void addGroupAuthority(String groupName, GrantedAuthority authority) {
|
154
|
Assert.hasText(groupName);
|
155
|
Assert.notNull(authority);
|
156
|
|
157
|
Group group = dao.findGroupByName(groupName);
|
158
|
|
159
|
if (group != null){
|
160
|
if(group.getGrantedAuthorities().add(authority)){
|
161
|
dao.update(group);
|
162
|
}
|
163
|
}
|
164
|
}
|
165
|
|
166
|
@Override
|
167
|
@Transactional(readOnly=false)
|
168
|
public void removeGroupAuthority(String groupName,
|
169
|
GrantedAuthority authority) {
|
170
|
Assert.hasText(groupName);
|
171
|
Assert.notNull(authority);
|
172
|
|
173
|
Group group = dao.findGroupByName(groupName);
|
174
|
|
175
|
if(group != null){
|
176
|
if(group.getGrantedAuthorities().remove(authority)) {
|
177
|
dao.update(group);
|
178
|
}
|
179
|
}
|
180
|
}
|
181
|
|
182
|
@Override
|
183
|
@Autowired
|
184
|
protected void setDao(IGroupDao dao) {
|
185
|
this.dao = dao;
|
186
|
}
|
187
|
|
188
|
@Autowired
|
189
|
public void setUserDao(IUserDao userDao){
|
190
|
this.userDao = userDao;
|
191
|
}
|
192
|
|
193
|
@Autowired
|
194
|
public void setGrantedAuthorityDao(IGrantedAuthorityDao grantedAuthorityDao){
|
195
|
this.grantedAuthorityDao = grantedAuthorityDao;
|
196
|
}
|
197
|
|
198
|
|
199
|
@Override
|
200
|
@Transactional(readOnly = true)
|
201
|
public List<Group> listByName(String queryString,MatchMode matchmode, List<Criterion> criteria, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {
|
202
|
long numberOfResults = dao.countByName(queryString, matchmode, criteria);
|
203
|
|
204
|
List<Group> results = new ArrayList<>();
|
205
|
if(numberOfResults > 0) {
|
206
|
results = dao.findByName(queryString, matchmode, criteria, pageSize, pageNumber, orderHints, propertyPaths);
|
207
|
}
|
208
|
return results;
|
209
|
}
|
210
|
|
211
|
@Override
|
212
|
@Transactional(readOnly=false)
|
213
|
public void createGroup(String groupName, List<GrantedAuthority> authorities) {
|
214
|
Assert.hasText(groupName);
|
215
|
Assert.notNull(authorities);
|
216
|
|
217
|
Group newGroup = Group.NewInstance(groupName);
|
218
|
for (GrantedAuthority grantedAuthority: authorities){
|
219
|
newGroup.addGrantedAuthority(grantedAuthority);
|
220
|
}
|
221
|
saveGroup(newGroup);
|
222
|
}
|
223
|
|
224
|
@Override
|
225
|
@Transactional(readOnly=false)
|
226
|
@PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_USER_MANAGER')")
|
227
|
public UUID saveGroup(Group group) {
|
228
|
return dao.save(group).getUuid();
|
229
|
}
|
230
|
|
231
|
@Override
|
232
|
@Transactional(readOnly=false)
|
233
|
public DeleteResult delete(UUID groupUUID ){
|
234
|
|
235
|
String groupUUIDString = groupUUID.toString();
|
236
|
Group group = dao.findByUuid(groupUUID);
|
237
|
//org.springframework.security.provisioning.GroupManager#deleteGroup needs a string argument
|
238
|
this.deleteGroup(groupUUIDString);
|
239
|
DeleteResult result = new DeleteResult();
|
240
|
result.addDeletedObject(group);
|
241
|
//there is no feedback from the deleteGroup method...
|
242
|
return result;
|
243
|
}
|
244
|
|
245
|
@Override
|
246
|
@Transactional(readOnly = false)
|
247
|
public MergeResult<Group> merge(Group newInstance, boolean returnTransientEntity) {
|
248
|
|
249
|
Set<GrantedAuthority> newAuthorities = newInstance.getGrantedAuthorities();
|
250
|
Map<GrantedAuthority, GrantedAuthority> mapOfAlreadyExistingAuthorities = new HashMap<GrantedAuthority, GrantedAuthority>();
|
251
|
GrantedAuthorityImpl alreadyInDB;
|
252
|
for (GrantedAuthority authority: newAuthorities){
|
253
|
if (authority instanceof GrantedAuthorityImpl){
|
254
|
alreadyInDB = grantedAuthorityDao.findAuthorityString(authority.getAuthority());
|
255
|
if (alreadyInDB != null){
|
256
|
if (alreadyInDB.getId() != ((GrantedAuthorityImpl)authority).getId()){
|
257
|
mapOfAlreadyExistingAuthorities.put(authority,alreadyInDB);
|
258
|
}
|
259
|
}
|
260
|
}
|
261
|
}
|
262
|
for (GrantedAuthority authority : mapOfAlreadyExistingAuthorities.keySet()){
|
263
|
newInstance.removeGrantedAuthority(authority);
|
264
|
newInstance.addGrantedAuthority(mapOfAlreadyExistingAuthorities.get(authority));
|
265
|
}
|
266
|
|
267
|
return dao.merge(newInstance, returnTransientEntity);
|
268
|
}
|
269
|
}
|