|
1
|
|
|
package easytests.auth.services; |
|
2
|
|
|
|
|
3
|
|
|
import easytests.core.models.UserModelInterface; |
|
4
|
|
|
import easytests.core.services.UsersService; |
|
5
|
|
|
import java.util.ArrayList; |
|
6
|
|
|
import java.util.List; |
|
7
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
8
|
|
|
import org.springframework.security.core.GrantedAuthority; |
|
9
|
|
|
import org.springframework.security.core.authority.SimpleGrantedAuthority; |
|
10
|
|
|
import org.springframework.security.core.userdetails.User; |
|
11
|
|
|
import org.springframework.security.core.userdetails.UserDetails; |
|
12
|
|
|
import org.springframework.security.core.userdetails.UserDetailsService; |
|
13
|
|
|
import org.springframework.security.core.userdetails.UsernameNotFoundException; |
|
14
|
|
|
import org.springframework.stereotype.Service; |
|
15
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @author malinink |
|
19
|
|
|
*/ |
|
20
|
|
|
@Service |
|
21
|
|
|
public class AuthUsersService implements UserDetailsService { |
|
22
|
|
|
@Autowired |
|
23
|
|
|
private UsersService usersService; |
|
24
|
|
|
|
|
25
|
|
|
private static List<GrantedAuthority> getAuthorities(UserModelInterface userModel) { |
|
26
|
|
|
final List<GrantedAuthority> authorities = new ArrayList<>(); |
|
27
|
|
|
authorities.add(new SimpleGrantedAuthority("ROLE_TESTEE")); |
|
28
|
|
|
authorities.add(new SimpleGrantedAuthority("ROLE_USER")); |
|
29
|
|
|
if (userModel.getIsAdmin()) { |
|
30
|
|
|
authorities.add(new SimpleGrantedAuthority("ROLE_ADMIN")); |
|
31
|
|
|
} |
|
32
|
|
|
return authorities; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
@Override |
|
36
|
|
|
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException { |
|
37
|
|
|
final UserModelInterface userModel = usersService.findByEmail(email); |
|
38
|
|
|
if (userModel == null) { |
|
39
|
|
|
throw new UsernameNotFoundException("No user found with login: " + email); |
|
40
|
|
|
} |
|
41
|
|
|
return new User( |
|
42
|
|
|
userModel.getEmail(), |
|
43
|
|
|
userModel.getPassword(), |
|
44
|
|
|
userModel.getState() == 3 ? true : false, |
|
45
|
|
|
true, |
|
46
|
|
|
true, |
|
47
|
|
|
true, |
|
48
|
|
|
this.getAuthorities(userModel) |
|
|
|
|
|
|
49
|
|
|
); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|