easytests.auth.services.AuthUsersService   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
c 2
b 0
f 0
loc 29
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A loadUserByUsername(String) 0 14 3
A getAuthorities(UserModelInterface) 0 8 2
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)
0 ignored issues
show
Comprehensibility introduced by
When accessing a static member, you should reference it statically. This makes it clear to any reader of cour code that a static method is being accessed.
Loading history...
49
        );
50
    }
51
}
52