Project

General

Profile

Download (7.93 KB) Statistics
| Branch: | Tag: | Revision:
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
        while (it.hasNext()){
75
            it.remove();
76
        }
77
        dao.delete(group);
78

    
79
    }
80

    
81
    @Override
82
    @Transactional(readOnly=false)
83
    public void renameGroup(String oldName, String newName) {
84
        Assert.hasText(oldName);
85
        Assert.hasText(newName);
86

    
87
        Group group = dao.findGroupByName(oldName);
88

    
89
        group.setName(newName);
90
        dao.update(group);
91
    }
92

    
93
    @Override
94
    @Transactional(readOnly=false)
95
    public void addUserToGroup(String username, String groupName) {
96
        Assert.hasText(username);
97
        Assert.hasText(groupName);
98

    
99
        Group group = dao.findGroupByName(groupName);
100
        User user = userDao.findUserByUsername(username);
101

    
102
        if(group != null && user != null){
103
            if(group.addMember(user)) {
104
                dao.update(group);
105
            }
106
        }
107
    }
108

    
109
    @Override
110
    @Transactional(readOnly=false)
111
    public void removeUserFromGroup(String username, String groupName) {
112
        Assert.hasText(username);
113
        Assert.hasText(groupName);
114

    
115
        Group group = dao.findGroupByName(groupName);
116
        User user = userDao.findUserByUsername(username);
117

    
118
        if(group != null && user != null){
119
            if(group.removeMember(user)){
120
                dao.update(group);
121
            }
122
        }
123
    }
124

    
125
    @Override
126
    public List<GrantedAuthority> findGroupAuthorities(String groupName) {
127
        Assert.hasText(groupName);
128
        Group group = dao.findGroupByName(groupName);
129

    
130
        if (group != null){
131
            return new ArrayList<>(group.getGrantedAuthorities());
132
        }
133

    
134
        return new ArrayList<>();
135
    }
136

    
137
    @Override
138
    @Transactional(readOnly=false)
139
    public void addGroupAuthority(String groupName, GrantedAuthority authority) {
140
        Assert.hasText(groupName);
141
        Assert.notNull(authority);
142

    
143
        Group group = dao.findGroupByName(groupName);
144

    
145
        if (group != null){
146
            if(group.getGrantedAuthorities().add(authority)){
147
                dao.update(group);
148
            }
149
        }
150
    }
151

    
152
    @Override
153
    @Transactional(readOnly=false)
154
    public void removeGroupAuthority(String groupName,
155
            GrantedAuthority authority) {
156
        Assert.hasText(groupName);
157
        Assert.notNull(authority);
158

    
159
        Group group = dao.findGroupByName(groupName);
160

    
161
        if(group != null){
162
            if(group.getGrantedAuthorities().remove(authority)) {
163
                dao.update(group);
164
            }
165
        }
166
    }
167

    
168
    @Override
169
    @Autowired
170
    protected void setDao(IGroupDao dao) {
171
        this.dao = dao;
172
    }
173

    
174
    @Autowired
175
    public void setUserDao(IUserDao userDao){
176
        this.userDao = userDao;
177
    }
178

    
179
    @Autowired
180
    public void setGrantedAuthorityDao(IGrantedAuthorityDao grantedAuthorityDao){
181
        this.grantedAuthorityDao = grantedAuthorityDao;
182
    }
183

    
184

    
185
    @Override
186
    @Transactional(readOnly = true)
187
    public List<Group> listByName(String queryString,MatchMode matchmode, List<Criterion> criteria, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {
188
         long numberOfResults = dao.countByName(queryString, matchmode, criteria);
189

    
190
         List<Group> results = new ArrayList<>();
191
         if(numberOfResults > 0) {
192
                results = dao.findByName(queryString, matchmode, criteria, pageSize, pageNumber, orderHints, propertyPaths);
193
         }
194
         return results;
195
    }
196

    
197
    @Override
198
    @Transactional(readOnly=false)
199
    public void createGroup(String groupName, List<GrantedAuthority> authorities) {
200
        Assert.hasText(groupName);
201
        Assert.notNull(authorities);
202

    
203
        Group newGroup = Group.NewInstance(groupName);
204
        for (GrantedAuthority grantedAuthority: authorities){
205
            newGroup.addGrantedAuthority(grantedAuthority);
206
        }
207
        saveGroup(newGroup);
208
    }
209

    
210
    @Override
211
    @Transactional(readOnly=false)
212
    @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_USER_MANAGER')")
213
    public UUID saveGroup(Group group) {
214
        return dao.save(group).getUuid();
215
    }
216

    
217
    @Override
218
    @Transactional(readOnly=false)
219
    public DeleteResult delete(UUID groupUUID ){
220

    
221
       String groupUUIDString = groupUUID.toString();
222
       Group group = dao.findByUuid(groupUUID);
223
       //org.springframework.security.provisioning.GroupManager#deleteGroup needs a string argument
224
       this.deleteGroup(groupUUIDString);
225
       DeleteResult result = new DeleteResult();
226
       result.addDeletedObject(group);
227
        //there is no feedback from the deleteGroup method...
228
        return result;
229
    }
230

    
231
    @Override
232
    @Transactional(readOnly = false)
233
    public MergeResult<Group> merge(Group newInstance, boolean returnTransientEntity) {
234

    
235
        Set<GrantedAuthority> newAuthorities = newInstance.getGrantedAuthorities();
236
        Map<GrantedAuthority, GrantedAuthority> mapOfAlreadyExistingAuthorities = new HashMap<GrantedAuthority, GrantedAuthority>();
237
        GrantedAuthorityImpl alreadyInDB;
238
        for (GrantedAuthority authority: newAuthorities){
239
            if (authority instanceof GrantedAuthorityImpl){
240
                alreadyInDB = grantedAuthorityDao.findAuthorityString(authority.getAuthority());
241
                if (alreadyInDB != null){
242
                    if (alreadyInDB.getId() != ((GrantedAuthorityImpl)authority).getId()){
243
                        mapOfAlreadyExistingAuthorities.put(authority,alreadyInDB);
244
                    }
245
                }
246
            }
247
        }
248
        for (GrantedAuthority authority : mapOfAlreadyExistingAuthorities.keySet()){
249
            newInstance.removeGrantedAuthority(authority);
250
            newInstance.addGrantedAuthority(mapOfAlreadyExistingAuthorities.get(authority));
251
        }
252

    
253
        return dao.merge(newInstance, returnTransientEntity);
254
    }
255
}
(26-26/105)