Completed
Pull Request — dev (#338)
by Konstantin
04:20
created

easytests.auth.services.SessionService.isUser()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
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
18
    @Autowired
19
    protected UsersServiceInterface usersService;
20
21
    private Boolean userModelFetched = false;
22
23
    private UserModelInterface userModel;
24
25
    @Override
26
    public Boolean isUser() {
27
        return this.isUser(this.getUserModel());
28
    }
29
30
    private Boolean isUser(UserModelInterface userModel) {
31
        return userModel != null;
32
    }
33
34
    @Override
35
    public UserModelInterface getUserModel() {
36
        if (!this.userModelFetched) {
37
            final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
38
            if (!(authentication instanceof AnonymousAuthenticationToken)) {
39
                this.userModel = usersService.findByEmail(authentication.getName());
40
            }
41
            this.userModelFetched = true;
42
        }
43
        return this.userModel;
44
    }
45
}
46