2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.cdm
.api
.service
;
11 import java
.util
.ArrayList
;
12 import java
.util
.HashMap
;
13 import java
.util
.Iterator
;
14 import java
.util
.List
;
17 import java
.util
.UUID
;
19 import org
.hibernate
.criterion
.Criterion
;
20 import org
.springframework
.beans
.factory
.annotation
.Autowired
;
21 import org
.springframework
.security
.access
.prepost
.PreAuthorize
;
22 import org
.springframework
.security
.core
.GrantedAuthority
;
23 import org
.springframework
.stereotype
.Service
;
24 import org
.springframework
.transaction
.annotation
.Transactional
;
25 import org
.springframework
.util
.Assert
;
27 import eu
.etaxonomy
.cdm
.model
.permission
.GrantedAuthorityImpl
;
28 import eu
.etaxonomy
.cdm
.model
.permission
.Group
;
29 import eu
.etaxonomy
.cdm
.model
.permission
.User
;
30 import eu
.etaxonomy
.cdm
.persistence
.dao
.permission
.IGrantedAuthorityDao
;
31 import eu
.etaxonomy
.cdm
.persistence
.dao
.permission
.IGroupDao
;
32 import eu
.etaxonomy
.cdm
.persistence
.dao
.permission
.IUserDao
;
33 import eu
.etaxonomy
.cdm
.persistence
.dto
.MergeResult
;
34 import eu
.etaxonomy
.cdm
.persistence
.query
.MatchMode
;
35 import eu
.etaxonomy
.cdm
.persistence
.query
.OrderHint
;
42 @Transactional(readOnly
= true)
43 @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_USER_MANAGER')")
44 public class GroupServiceImpl
extends ServiceBase
<Group
,IGroupDao
> implements IGroupService
{
46 private IUserDao userDao
;
48 private IGrantedAuthorityDao grantedAuthorityDao
;
51 public List
<String
> findAllGroups() {
52 return dao
.listNames(null,null);
56 public List
<String
> findUsersInGroup(String groupName
) {
57 Assert
.hasText(groupName
, "Parameter 'groupName' must not be empty.");
58 Group group
= dao
.findGroupByName(groupName
);
60 List
<String
> users
= dao
.listMembers(group
, null, null);
66 public boolean groupExists(String groupName
) {
67 Assert
.hasText(groupName
, "Parameter 'groupName' must not be empty.");
68 Group group
= dao
.findGroupByName(groupName
);
73 public Group
findGroup(String groupName
) {
74 Assert
.hasText(groupName
, "Parameter 'groupname' must not be empty.");
75 Group group
= dao
.findGroupByName(groupName
);
80 @Transactional(readOnly
=false)
81 public void deleteGroup(String groupUUID
) {
82 Assert
.notNull(groupUUID
, "Parameter 'groupUUID' must not be empty.");
84 Group group
= dao
.findByUuid(UUID
.fromString(groupUUID
));
85 Iterator
<User
> it
= group
.getMembers().iterator();
86 group
.getMembers().clear();
87 // while (it.hasNext()){
94 @Transactional(readOnly
=false)
95 public void renameGroup(String oldName
, String newName
) {
96 Assert
.hasText(oldName
, "Parameter 'oldName' must not be empty.");
97 Assert
.hasText(newName
, "Parameter 'newName' must not be empty.");
99 Group group
= dao
.findGroupByName(oldName
);
101 group
.setName(newName
);
106 @Transactional(readOnly
=false)
107 public void addUserToGroup(String username
, String groupName
) {
108 Assert
.hasText(username
, "Parameter 'username' must not be empty.");
109 Assert
.hasText(groupName
, "Parameter 'groupName' must not be empty.");
111 Group group
= dao
.findGroupByName(groupName
);
112 User user
= userDao
.findUserByUsername(username
);
114 if(group
!= null && user
!= null){
115 if(group
.addMember(user
)) {
122 @Transactional(readOnly
=false)
123 public void removeUserFromGroup(String username
, String groupName
) {
124 Assert
.hasText(username
, "Parameter 'username' must not be empty.");
125 Assert
.hasText(groupName
, "Parameter 'groupName' must not be empty.");
127 Group group
= dao
.findGroupByName(groupName
);
128 User user
= userDao
.findUserByUsername(username
);
130 if(group
!= null && user
!= null){
131 if(group
.removeMember(user
)){
138 public List
<GrantedAuthority
> findGroupAuthorities(String groupName
) {
139 Assert
.hasText(groupName
, "Parameter 'groupName' must not be empty.");
140 Group group
= dao
.findGroupByName(groupName
);
143 return new ArrayList
<>(group
.getGrantedAuthorities());
146 return new ArrayList
<>();
150 @Transactional(readOnly
=false)
151 public void addGroupAuthority(String groupName
, GrantedAuthority authority
) {
152 Assert
.hasText(groupName
, "Parameter 'groupName' must not be empty.");
153 Assert
.notNull(authority
, "Parameter 'authority' must not be empty.");
155 Group group
= dao
.findGroupByName(groupName
);
158 if(group
.getGrantedAuthorities().add(authority
)){
165 @Transactional(readOnly
=false)
166 public void removeGroupAuthority(String groupName
,
167 GrantedAuthority authority
) {
168 Assert
.hasText(groupName
, "Parameter 'groupName' must not be empty.");
169 Assert
.notNull(authority
, "Parameter 'authority' must not be empty.");
171 Group group
= dao
.findGroupByName(groupName
);
174 if(group
.getGrantedAuthorities().remove(authority
)) {
182 protected void setDao(IGroupDao dao
) {
187 public void setUserDao(IUserDao userDao
){
188 this.userDao
= userDao
;
192 public void setGrantedAuthorityDao(IGrantedAuthorityDao grantedAuthorityDao
){
193 this.grantedAuthorityDao
= grantedAuthorityDao
;
198 @Transactional(readOnly
= true)
199 public List
<Group
> listByName(String queryString
,MatchMode matchmode
, List
<Criterion
> criteria
, Integer pageSize
, Integer pageNumber
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
200 long numberOfResults
= dao
.countByName(queryString
, matchmode
, criteria
);
202 List
<Group
> results
= new ArrayList
<>();
203 if(numberOfResults
> 0) {
204 results
= dao
.findByName(queryString
, matchmode
, criteria
, pageSize
, pageNumber
, orderHints
, propertyPaths
);
210 @Transactional(readOnly
=false)
211 public void createGroup(String groupName
, List
<GrantedAuthority
> authorities
) {
212 Assert
.hasText(groupName
, "Parameter 'groupName' must not be empty.");
213 Assert
.notNull(authorities
, "Parameter 'authorities' must not be empty.");
215 Group newGroup
= Group
.NewInstance(groupName
);
216 for (GrantedAuthority grantedAuthority
: authorities
){
217 newGroup
.addGrantedAuthority(grantedAuthority
);
223 @Transactional(readOnly
=false)
224 @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_USER_MANAGER')")
225 public UUID
saveGroup(Group group
) {
226 return dao
.save(group
).getUuid();
230 @Transactional(readOnly
=false)
231 public DeleteResult
delete(UUID groupUUID
){
233 String groupUUIDString
= groupUUID
.toString();
234 Group group
= dao
.findByUuid(groupUUID
);
235 //org.springframework.security.provisioning.GroupManager#deleteGroup needs a string argument
236 this.deleteGroup(groupUUIDString
);
237 DeleteResult result
= new DeleteResult();
238 result
.addDeletedObject(group
);
239 //there is no feedback from the deleteGroup method...
244 @Transactional(readOnly
= false)
245 public MergeResult
<Group
> merge(Group newInstance
, boolean returnTransientEntity
) {
247 Set
<GrantedAuthority
> newAuthorities
= newInstance
.getGrantedAuthorities();
248 Map
<GrantedAuthority
, GrantedAuthority
> mapOfAlreadyExistingAuthorities
= new HashMap
<GrantedAuthority
, GrantedAuthority
>();
249 GrantedAuthorityImpl alreadyInDB
;
250 for (GrantedAuthority authority
: newAuthorities
){
251 if (authority
instanceof GrantedAuthorityImpl
){
252 alreadyInDB
= grantedAuthorityDao
.findAuthorityString(authority
.getAuthority());
253 if (alreadyInDB
!= null){
254 if (alreadyInDB
.getId() != ((GrantedAuthorityImpl
)authority
).getId()){
255 mapOfAlreadyExistingAuthorities
.put(authority
,alreadyInDB
);
260 for (GrantedAuthority authority
: mapOfAlreadyExistingAuthorities
.keySet()){
261 newInstance
.removeGrantedAuthority(authority
);
262 newInstance
.addGrantedAuthority(mapOfAlreadyExistingAuthorities
.get(authority
));
265 return dao
.merge(newInstance
, returnTransientEntity
);