Project

General

Profile

Download (7.97 KB) Statistics
| Branch: | Tag: | Revision:
1 655481e5 n.hoffmann
/**
2
* Copyright (C) 2007 EDIT
3 f512ae07 Andreas Kohlbecker
* European Distributed Institute of Taxonomy
4 655481e5 n.hoffmann
* http://www.e-taxonomy.eu
5 f512ae07 Andreas Kohlbecker
*
6 655481e5 n.hoffmann
* 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 0e5acce8 Katja Luther
import java.util.HashMap;
14 a93dc16d Andreas Müller
import java.util.Iterator;
15 655481e5 n.hoffmann
import java.util.List;
16 0e5acce8 Katja Luther
import java.util.Map;
17
import java.util.Set;
18 d0a75c57 Katja Luther
import java.util.UUID;
19 655481e5 n.hoffmann
20 001915e1 Andreas Müller
import org.hibernate.criterion.Criterion;
21 655481e5 n.hoffmann
import org.springframework.beans.factory.annotation.Autowired;
22 f512ae07 Andreas Kohlbecker
import org.springframework.security.access.prepost.PreAuthorize;
23 655481e5 n.hoffmann
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 0e5acce8 Katja Luther
import eu.etaxonomy.cdm.model.common.GrantedAuthorityImpl;
29 655481e5 n.hoffmann
import eu.etaxonomy.cdm.model.common.Group;
30
import eu.etaxonomy.cdm.model.common.User;
31 0e5acce8 Katja Luther
import eu.etaxonomy.cdm.persistence.dao.common.IGrantedAuthorityDao;
32 655481e5 n.hoffmann
import eu.etaxonomy.cdm.persistence.dao.common.IGroupDao;
33
import eu.etaxonomy.cdm.persistence.dao.common.IUserDao;
34 0e5acce8 Katja Luther
import eu.etaxonomy.cdm.persistence.dto.MergeResult;
35 001915e1 Andreas Müller
import eu.etaxonomy.cdm.persistence.query.MatchMode;
36
import eu.etaxonomy.cdm.persistence.query.OrderHint;
37 655481e5 n.hoffmann
38
/**
39
 * @author n.hoffmann
40 a88578ce Andreas Müller
 * @since Mar 9, 2011
41 655481e5 n.hoffmann
 */
42
@Service
43 77b4a247 Andreas Kohlbecker
@Transactional(readOnly = true)
44 f512ae07 Andreas Kohlbecker
@PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_USER_MANAGER')")
45 9e424f23 n.hoffmann
public class GroupServiceImpl extends ServiceBase<Group,IGroupDao> implements IGroupService {
46 655481e5 n.hoffmann
47 f512ae07 Andreas Kohlbecker
    protected IUserDao userDao;
48
49 0e5acce8 Katja Luther
    protected IGrantedAuthorityDao grantedAuthorityDao;
50
51 f512ae07 Andreas Kohlbecker
    @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 815a9015 Katja Luther
67 f512ae07 Andreas Kohlbecker
    @Override
68
    @Transactional(readOnly=false)
69 1995f802 Katja Luther
    public void deleteGroup(String groupUUID) {
70
        Assert.notNull(groupUUID);
71 f512ae07 Andreas Kohlbecker
72 1995f802 Katja Luther
        Group group = dao.findByUuid(UUID.fromString(groupUUID));
73 a93dc16d Andreas Müller
        Iterator<User> it = group.getMembers().iterator();
74 78e3bcbc Katja Luther
        group.getMembers().clear();
75
//        while (it.hasNext()){
76
//            it.remove();
77
//        }
78 a93dc16d Andreas Müller
        dao.delete(group);
79 3dba21ef Andreas Kohlbecker
80 f512ae07 Andreas Kohlbecker
    }
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 a93dc16d Andreas Müller
        if(group != null && user != null){
104 f512ae07 Andreas Kohlbecker
            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 7b04ab8a Andreas Müller
        if(group != null && user != null){
120 f512ae07 Andreas Kohlbecker
            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 a93dc16d Andreas Müller
            return new ArrayList<>(group.getGrantedAuthorities());
133 f512ae07 Andreas Kohlbecker
        }
134
135 a93dc16d Andreas Müller
        return new ArrayList<>();
136 f512ae07 Andreas Kohlbecker
    }
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 0e5acce8 Katja Luther
    @Autowired
181
    public void setGrantedAuthorityDao(IGrantedAuthorityDao grantedAuthorityDao){
182
        this.grantedAuthorityDao = grantedAuthorityDao;
183
    }
184
185
186 3dba21ef Andreas Kohlbecker
    @Override
187 f512ae07 Andreas Kohlbecker
    @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 f08f6894 Andreas Müller
         long numberOfResults = dao.countByName(queryString, matchmode, criteria);
190 f512ae07 Andreas Kohlbecker
191 a93dc16d Andreas Müller
         List<Group> results = new ArrayList<>();
192 f512ae07 Andreas Kohlbecker
         if(numberOfResults > 0) {
193
                results = dao.findByName(queryString, matchmode, criteria, pageSize, pageNumber, orderHints, propertyPaths);
194
         }
195
         return results;
196
    }
197
198 d0a75c57 Katja Luther
    @Override
199
    @Transactional(readOnly=false)
200 3dba21ef Andreas Kohlbecker
    public void createGroup(String groupName, List<GrantedAuthority> authorities) {
201
        Assert.hasText(groupName);
202 d0a75c57 Katja Luther
        Assert.notNull(authorities);
203 3dba21ef Andreas Kohlbecker
204
        Group newGroup = Group.NewInstance(groupName);
205
        for (GrantedAuthority grantedAuthority: authorities){
206
            newGroup.addGrantedAuthority(grantedAuthority);
207
        }
208
        saveGroup(newGroup);
209
    }
210
211 d0a75c57 Katja Luther
    @Override
212
    @Transactional(readOnly=false)
213
    @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_USER_MANAGER')")
214
    public UUID saveGroup(Group group) {
215 26b857a9 Cherian Mathew
        return dao.save(group).getUuid();
216 d0a75c57 Katja Luther
    }
217
218 1995f802 Katja Luther
    @Override
219 ddf0581b Cherian Mathew
    @Transactional(readOnly=false)
220 0a32c706 Katja Luther
    public DeleteResult delete(UUID groupUUID ){
221 ddf0581b Cherian Mathew
222 0a32c706 Katja Luther
       String groupUUIDString = groupUUID.toString();
223 815a9015 Katja Luther
       Group group = dao.findByUuid(groupUUID);
224 1995f802 Katja Luther
       //org.springframework.security.provisioning.GroupManager#deleteGroup needs a string argument
225 815a9015 Katja Luther
       this.deleteGroup(groupUUIDString);
226
       DeleteResult result = new DeleteResult();
227
       result.addDeletedObject(group);
228 08d62c6f Katja Luther
        //there is no feedback from the deleteGroup method...
229 815a9015 Katja Luther
        return result;
230 1995f802 Katja Luther
    }
231 3dba21ef Andreas Kohlbecker
232 0e5acce8 Katja Luther
    @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 655481e5 n.hoffmann
}