getCurrentUserModel()   A
last analyzed

Complexity

Conditions 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
dl 0
loc 10
rs 9.9
c 0
b 0
f 0
1
package easytests.common.controllers;
2
3
import easytests.core.models.UserModelInterface;
4
import easytests.core.services.UsersService;
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.Controller;
10
import org.springframework.web.bind.annotation.ModelAttribute;
11
12
13
/**
14
 * @author malinink
15
 */
16
@Controller
17
public class AbstractPersonalController {
18
    @Autowired
19
    protected UsersService usersService;
20
21
    private UserModelInterface userModel;
22
23
    private Boolean userModelFetched = false;
24
25
    @ModelAttribute("currentUserModel")
26
    public UserModelInterface getCurrentUserModel() {
27
        if (!this.userModelFetched) {
28
            final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
29
            if (!(authentication instanceof AnonymousAuthenticationToken)) {
30
                this.userModel = usersService.findByEmail(authentication.getName());
31
            }
32
            this.userModelFetched = true;
33
        }
34
        return this.userModel;
35
    }
36
}
37