Passed
Pull Request — dev (#338)
by Konstantin
04:48
created

easytests.auth.services.SessionService   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 28
rs 10
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
getUserModel 0 10 ?
isUser 0 3 ?
A isUser(UserModelInterface) 0 2 1
A isUser() 0 3 1
A getUserModel() 0 10 3
1
package easytests.auth.services;
2
3
import easytests.core.models.UserModelInterface;
4
import easytests.core.services.UsersServiceInterface;
5
import org.springframework.beans.factory.annotation.Autowired;
6
import org.springframework.security.authentication.AnonymousAuthenticationToken;
7
import org.springframework.security.core.Authentication;
8
import org.springframework.security.core.context.SecurityContextHolder;
9
import org.springframework.stereotype.Service;
10
11
12
/**
13
 * @author malinink
14
 */
15
@Service
16
public class SessionService implements SessionServiceInterface {
17
    @Autowired
18
    protected UsersServiceInterface usersService;
19
20
    private Boolean userModelFetched = false;
21
22
    private UserModelInterface userModel;
23
24
    @Override
25
    public Boolean isUser() {
26
        return this.isUser(this.getUserModel());
27
    }
28
29
    private Boolean isUser(UserModelInterface userModel) {
30
        return userModel != null;
31
    }
32
33
    @Override
34
    public UserModelInterface getUserModel() {
35
        if (!this.userModelFetched) {
36
            final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
37
            if (!(authentication instanceof AnonymousAuthenticationToken)) {
38
                this.userModel = usersService.findByEmail(authentication.getName());
39
            }
40
            this.userModelFetched = true;
41
        }
42
        return this.userModel;
43
    }
44
}
45