Project

General

Profile

Download (8.22 KB) Statistics
| Branch: | Tag: | Revision:
1
package eu.etaxonomy.cdm.api.service;
2

    
3
import java.util.List;
4

    
5
import org.hibernate.NonUniqueResultException;
6
import org.springframework.beans.factory.annotation.Autowired;
7
import org.springframework.dao.DataAccessException;
8
import org.springframework.dao.IncorrectResultSizeDataAccessException;
9
import org.springframework.security.AccessDeniedException;
10
import org.springframework.security.Authentication;
11
import org.springframework.security.AuthenticationManager;
12
import org.springframework.security.GrantedAuthority;
13
import org.springframework.security.context.SecurityContextHolder;
14
import org.springframework.security.providers.UsernamePasswordAuthenticationToken;
15
import org.springframework.security.providers.dao.SaltSource;
16
import org.springframework.security.providers.dao.UserCache;
17
import org.springframework.security.providers.dao.cache.NullUserCache;
18
import org.springframework.security.providers.encoding.PasswordEncoder;
19
import org.springframework.security.userdetails.GroupManager;
20
import org.springframework.security.userdetails.UserDetails;
21
import org.springframework.security.userdetails.UserDetailsManager;
22
import org.springframework.security.userdetails.UsernameNotFoundException;
23
import org.springframework.stereotype.Service;
24
import org.springframework.transaction.annotation.Transactional;
25
import org.springframework.util.Assert;
26

    
27
import eu.etaxonomy.cdm.model.common.Group;
28
import eu.etaxonomy.cdm.model.common.User;
29
import eu.etaxonomy.cdm.persistence.dao.common.IGroupDao;
30
import eu.etaxonomy.cdm.persistence.dao.common.IUserDao;
31

    
32
@Service
33
@Transactional
34
public class UserService implements UserDetailsManager, GroupManager {
35

    
36
	protected IUserDao userDao;
37
	
38
	protected IGroupDao groupDao;
39
	
40
	private SaltSource saltSource;
41
	
42
	private PasswordEncoder passwordEncoder;
43
	
44
	private AuthenticationManager authenticationManager;
45
	
46
	private UserCache userCache = new NullUserCache();
47
	
48
	@Autowired(required = false)
49
	public void setUserCache(UserCache userCache) {
50
		Assert.notNull(userCache, "userCache cannot be null");
51
		this.userCache = userCache;
52
	}
53
	
54
	//@Autowired
55
	public void setPasswordEncoder(PasswordEncoder passwordEncoder) {
56
		this.passwordEncoder = passwordEncoder;
57
	}
58

    
59
	//@Autowired
60
	public void setSaltSource(SaltSource saltSource) {
61
		this.saltSource = saltSource;
62
	}
63
	
64
	//@Autowired
65
	public void setAuthenticationManager(AuthenticationManager authenticationManager) {
66
		this.authenticationManager = authenticationManager;
67
	}
68
	
69
	//@Autowired
70
	public void setUserDao(IUserDao userDao) {
71
		this.userDao = userDao;
72
	}
73
	
74
	//@Autowired
75
	public void setGroupDao(IGroupDao groupDao) {
76
		this.groupDao = groupDao;
77
	}
78
	
79
	protected Authentication createNewAuthentication(Authentication currentAuth, String newPassword) {
80
		UserDetails user = loadUserByUsername(currentAuth.getName());
81
			
82
		UsernamePasswordAuthenticationToken newAuthentication = new UsernamePasswordAuthenticationToken(user, user.getPassword(), user.getAuthorities());
83
		newAuthentication.setDetails(currentAuth.getDetails());
84
			
85
		return newAuthentication;
86
	}
87
	
88
	public void changePassword(String oldPassword, String newPassword) {
89
		Assert.hasText(oldPassword);
90
		Assert.hasText(newPassword);
91
		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
92
		if(authentication != null && authentication.getPrincipal() != null && authentication.getPrincipal() instanceof User) {
93
			User user = (User)authentication.getPrincipal();
94
			
95
			authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(user.getUsername(), oldPassword));
96
			
97
			Object salt = this.saltSource.getSalt(user);
98
			
99
			String password = passwordEncoder.encodePassword(newPassword, salt);
100
			((User)user).setPassword(password);
101
			
102
			userDao.update((User)user);
103
			SecurityContextHolder.getContext().setAuthentication(createNewAuthentication(authentication, newPassword));
104
			userCache.removeUserFromCache(user.getUsername());
105
		} else {
106
			throw new AccessDeniedException("Can't change password as no Authentication object found in context for current user.");
107
		}		
108
	}
109

    
110
	public void createUser(UserDetails user) {
111
		Assert.isInstanceOf(User.class, user);
112
		
113
		String rawPassword = user.getPassword();
114
		Object salt = this.saltSource.getSalt(user);
115
		
116
		String password = passwordEncoder.encodePassword(rawPassword, salt);
117
		((User)user).setPassword(password);
118
		
119
		userDao.save((User)user);
120
	}
121

    
122
	public void deleteUser(String username) {
123
		Assert.hasLength(username);
124
		
125
		User user = userDao.findUserByUsername(username); 
126
        if(user != null) {		
127
		    userDao.delete((User)user);
128
        }
129
        
130
        userCache.removeUserFromCache(username);
131
	}
132

    
133
	public void updateUser(UserDetails user) {
134
		Assert.isInstanceOf(User.class, user);
135
		
136
		userDao.update((User)user);
137
		userCache.removeUserFromCache(user.getUsername());
138
	}
139

    
140
	public boolean userExists(String username) {
141
		Assert.hasText(username);
142
		
143
		User user = userDao.findUserByUsername(username);
144
		return user != null;
145
	}
146

    
147
	public UserDetails loadUserByUsername(String username)
148
			throws UsernameNotFoundException, DataAccessException {
149
		Assert.hasText(username);
150
		try {
151
		    User user = userDao.findUserByUsername(username);
152
		    if(user == null) {
153
				throw new UsernameNotFoundException(username);
154
			}
155
		    return user;
156
		} catch(NonUniqueResultException nure) {
157
			throw new IncorrectResultSizeDataAccessException("More than one user found with name '" + username + "'", 1);
158
		}
159
	}
160

    
161
	public void addGroupAuthority(String groupName, GrantedAuthority authority) {
162
		Assert.hasText(groupName);
163
		Assert.notNull(authority);
164
		
165
		Group group = groupDao.findGroupByName(groupName);
166
		if(group.getGrantedAuthorities().add(authority)) {
167
			groupDao.update(group);
168
		}
169
	}
170

    
171
	public void addUserToGroup(String username, String groupName) {
172
		Assert.hasText(username);
173
		Assert.hasText(groupName);
174
		
175
		Group group = groupDao.findGroupByName(groupName);
176
		User user = userDao.findUserByUsername(username);
177
		
178
		if(group.addMember(user)) {
179
			groupDao.update(group);
180
			userCache.removeUserFromCache(user.getUsername());
181
		}		
182
	}
183

    
184
	public void createGroup(String groupName, GrantedAuthority[] authorities) {
185
		Assert.hasText(groupName);
186
		Assert.notNull(authorities);
187
		
188
		Group group = new Group();
189
		group.setName(groupName);
190
		
191
		for(GrantedAuthority authority : authorities) {
192
			group.getGrantedAuthorities().add(authority);
193
		}
194
		
195
		groupDao.save(group);
196
	}
197

    
198
	public void deleteGroup(String groupName) {
199
		Assert.hasText(groupName);
200
		
201
		Group group = groupDao.findGroupByName(groupName);
202
		groupDao.delete(group);
203
	}
204

    
205
	public String[] findAllGroups() {
206
		List<String> names = groupDao.listNames(null,null);
207
		return names.toArray(new String[names.size()]);
208
	}
209

    
210
	public GrantedAuthority[] findGroupAuthorities(String groupName) {
211
		Assert.hasText(groupName);
212
		Group group = groupDao.findGroupByName(groupName);
213
		
214
		return group.getGrantedAuthorities().toArray(new GrantedAuthority[group.getGrantedAuthorities().size()]);
215
	}
216

    
217
	public String[] findUsersInGroup(String groupName) {
218
		Assert.hasText(groupName);
219
		Group group = groupDao.findGroupByName(groupName);
220
		
221
		List<String> users = groupDao.listMembers(group, null, null);
222
		
223
		return users.toArray(new String[users.size()]);
224
	}
225

    
226
	public void removeGroupAuthority(String groupName,	GrantedAuthority authority) {
227
		Assert.hasText(groupName);
228
		Assert.notNull(authority);
229
		
230
		Group group = groupDao.findGroupByName(groupName);
231
		
232
		if(group.getGrantedAuthorities().remove(authority)) {
233
			groupDao.update(group);
234
		}
235
		
236
	}
237

    
238
	public void removeUserFromGroup(String username, String groupName) {
239
		Assert.hasText(username);
240
		Assert.hasText(groupName);
241
		
242
		Group group = groupDao.findGroupByName(groupName);
243
		User user = userDao.findUserByUsername(username);
244
		
245
		if(group.removeMember(user)) {
246
			groupDao.update(group);
247
			userCache.removeUserFromCache(user.getUsername());
248
		}
249
	}
250

    
251
	public void renameGroup(String oldName, String newName) {
252
		Assert.hasText(oldName);
253
		Assert.hasText(newName);
254
		
255
		Group group = groupDao.findGroupByName(oldName);
256
		
257
		group.setName(newName);
258
		groupDao.update(group);
259
	}
260

    
261
}
(28-28/28)