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.common.GrantedAuthorityImpl;
|
29
|
import eu.etaxonomy.cdm.model.common.Group;
|
30
|
import eu.etaxonomy.cdm.model.common.User;
|
31
|
import eu.etaxonomy.cdm.persistence.dao.common.IGrantedAuthorityDao;
|
32
|
import eu.etaxonomy.cdm.persistence.dao.common.IGroupDao;
|
33
|
import eu.etaxonomy.cdm.persistence.dao.common.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
|
|
67
|
@Override
|
68
|
@Transactional(readOnly=false)
|
69
|
public void deleteGroup(String groupUUID) {
|
70
|
Assert.notNull(groupUUID);
|
71
|
|
72
|
Group group = dao.findByUuid(UUID.fromString(groupUUID));
|
73
|
Iterator<User> it = group.getMembers().iterator();
|
74
|
group.getMembers().clear();
|
75
|
// while (it.hasNext()){
|
76
|
// it.remove();
|
77
|
// }
|
78
|
dao.delete(group);
|
79
|
|
80
|
}
|
81
|
|
82
|
@Override
|
83
|
@Transactional(readOnly=false)
|
84
|
public void renameGroup(String oldName, String newName) {
|
85
|
Assert.hasText(oldName);
|
86
|
Assert.hasText(newName);
|
87
|
|
88
|
Group group = dao.findGroupByName(oldName);
|
89
|
|
90
|
group.setName(newName);
|
91
|
dao.update(group);
|
92
|
}
|
93
|
|
94
|
@Override
|
95
|
@Transactional(readOnly=false)
|
96
|
public void addUserToGroup(String username, String groupName) {
|
97
|
Assert.hasText(username);
|
98
|
Assert.hasText(groupName);
|
99
|
|
100
|
Group group = dao.findGroupByName(groupName);
|
101
|
User user = userDao.findUserByUsername(username);
|
102
|
|
103
|
if(group != null && user != null){
|
104
|
if(group.addMember(user)) {
|
105
|
dao.update(group);
|
106
|
}
|
107
|
}
|
108
|
}
|
109
|
|
110
|
@Override
|
111
|
@Transactional(readOnly=false)
|
112
|
public void removeUserFromGroup(String username, String groupName) {
|
113
|
Assert.hasText(username);
|
114
|
Assert.hasText(groupName);
|
115
|
|
116
|
Group group = dao.findGroupByName(groupName);
|
117
|
User user = userDao.findUserByUsername(username);
|
118
|
|
119
|
if(group != null && user != null){
|
120
|
if(group.removeMember(user)){
|
121
|
dao.update(group);
|
122
|
}
|
123
|
}
|
124
|
}
|
125
|
|
126
|
@Override
|
127
|
public List<GrantedAuthority> findGroupAuthorities(String groupName) {
|
128
|
Assert.hasText(groupName);
|
129
|
Group group = dao.findGroupByName(groupName);
|
130
|
|
131
|
if (group != null){
|
132
|
return new ArrayList<>(group.getGrantedAuthorities());
|
133
|
}
|
134
|
|
135
|
return new ArrayList<>();
|
136
|
}
|
137
|
|
138
|
@Override
|
139
|
@Transactional(readOnly=false)
|
140
|
public void addGroupAuthority(String groupName, GrantedAuthority authority) {
|
141
|
Assert.hasText(groupName);
|
142
|
Assert.notNull(authority);
|
143
|
|
144
|
Group group = dao.findGroupByName(groupName);
|
145
|
|
146
|
if (group != null){
|
147
|
if(group.getGrantedAuthorities().add(authority)){
|
148
|
dao.update(group);
|
149
|
}
|
150
|
}
|
151
|
}
|
152
|
|
153
|
@Override
|
154
|
@Transactional(readOnly=false)
|
155
|
public void removeGroupAuthority(String groupName,
|
156
|
GrantedAuthority authority) {
|
157
|
Assert.hasText(groupName);
|
158
|
Assert.notNull(authority);
|
159
|
|
160
|
Group group = dao.findGroupByName(groupName);
|
161
|
|
162
|
if(group != null){
|
163
|
if(group.getGrantedAuthorities().remove(authority)) {
|
164
|
dao.update(group);
|
165
|
}
|
166
|
}
|
167
|
}
|
168
|
|
169
|
@Override
|
170
|
@Autowired
|
171
|
protected void setDao(IGroupDao dao) {
|
172
|
this.dao = dao;
|
173
|
}
|
174
|
|
175
|
@Autowired
|
176
|
public void setUserDao(IUserDao userDao){
|
177
|
this.userDao = userDao;
|
178
|
}
|
179
|
|
180
|
@Autowired
|
181
|
public void setGrantedAuthorityDao(IGrantedAuthorityDao grantedAuthorityDao){
|
182
|
this.grantedAuthorityDao = grantedAuthorityDao;
|
183
|
}
|
184
|
|
185
|
|
186
|
@Override
|
187
|
@Transactional(readOnly = true)
|
188
|
public List<Group> listByName(String queryString,MatchMode matchmode, List<Criterion> criteria, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {
|
189
|
long numberOfResults = dao.countByName(queryString, matchmode, criteria);
|
190
|
|
191
|
List<Group> results = new ArrayList<>();
|
192
|
if(numberOfResults > 0) {
|
193
|
results = dao.findByName(queryString, matchmode, criteria, pageSize, pageNumber, orderHints, propertyPaths);
|
194
|
}
|
195
|
return results;
|
196
|
}
|
197
|
|
198
|
@Override
|
199
|
@Transactional(readOnly=false)
|
200
|
public void createGroup(String groupName, List<GrantedAuthority> authorities) {
|
201
|
Assert.hasText(groupName);
|
202
|
Assert.notNull(authorities);
|
203
|
|
204
|
Group newGroup = Group.NewInstance(groupName);
|
205
|
for (GrantedAuthority grantedAuthority: authorities){
|
206
|
newGroup.addGrantedAuthority(grantedAuthority);
|
207
|
}
|
208
|
saveGroup(newGroup);
|
209
|
}
|
210
|
|
211
|
@Override
|
212
|
@Transactional(readOnly=false)
|
213
|
@PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_USER_MANAGER')")
|
214
|
public UUID saveGroup(Group group) {
|
215
|
return dao.save(group).getUuid();
|
216
|
}
|
217
|
|
218
|
@Override
|
219
|
@Transactional(readOnly=false)
|
220
|
public DeleteResult delete(UUID groupUUID ){
|
221
|
|
222
|
String groupUUIDString = groupUUID.toString();
|
223
|
Group group = dao.findByUuid(groupUUID);
|
224
|
//org.springframework.security.provisioning.GroupManager#deleteGroup needs a string argument
|
225
|
this.deleteGroup(groupUUIDString);
|
226
|
DeleteResult result = new DeleteResult();
|
227
|
result.addDeletedObject(group);
|
228
|
//there is no feedback from the deleteGroup method...
|
229
|
return result;
|
230
|
}
|
231
|
|
232
|
@Override
|
233
|
@Transactional(readOnly = false)
|
234
|
public MergeResult<Group> merge(Group newInstance, boolean returnTransientEntity) {
|
235
|
|
236
|
Set<GrantedAuthority> newAuthorities = newInstance.getGrantedAuthorities();
|
237
|
Map<GrantedAuthority, GrantedAuthority> mapOfAlreadyExistingAuthorities = new HashMap<GrantedAuthority, GrantedAuthority>();
|
238
|
GrantedAuthorityImpl alreadyInDB;
|
239
|
for (GrantedAuthority authority: newAuthorities){
|
240
|
if (authority instanceof GrantedAuthorityImpl){
|
241
|
alreadyInDB = grantedAuthorityDao.findAuthorityString(authority.getAuthority());
|
242
|
if (alreadyInDB != null){
|
243
|
if (alreadyInDB.getId() != ((GrantedAuthorityImpl)authority).getId()){
|
244
|
mapOfAlreadyExistingAuthorities.put(authority,alreadyInDB);
|
245
|
}
|
246
|
}
|
247
|
}
|
248
|
}
|
249
|
for (GrantedAuthority authority : mapOfAlreadyExistingAuthorities.keySet()){
|
250
|
newInstance.removeGrantedAuthority(authority);
|
251
|
newInstance.addGrantedAuthority(mapOfAlreadyExistingAuthorities.get(authority));
|
252
|
}
|
253
|
|
254
|
return dao.merge(newInstance, returnTransientEntity);
|
255
|
}
|
256
|
}
|