Completed
Pull Request — dev (#393)
by
unknown
06:16 queued 01:03
created

isAdmin

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 2
1
package easytests.api.v1.controllers;
2
3
import easytests.api.v1.exceptions.*;
4
import easytests.api.v1.mappers.UsersMapper;
5
import easytests.api.v1.models.User;
6
import easytests.auth.services.SessionServiceInterface;
7
import easytests.core.models.UserModelInterface;
8
import easytests.core.options.SubjectsOptions;
0 ignored issues
show
Unused Code introduced by
Remove this unused import 'easytests.core.options.SubjectsOptions'.
Loading history...
9
import easytests.core.options.UsersOptions;
0 ignored issues
show
Unused Code introduced by
Remove this unused import 'easytests.core.options.UsersOptions'.
Loading history...
10
import easytests.core.options.UsersOptionsInterface;
11
import easytests.core.options.builder.UsersOptionsBuilderInterface;
12
import easytests.core.services.UsersServiceInterface;
13
import java.util.List;
14
import java.util.stream.Collectors;
15
import org.springframework.beans.factory.annotation.Autowired;
16
import org.springframework.beans.factory.annotation.Qualifier;
17
import org.springframework.web.bind.annotation.*;
18
19
20
/**
21
 * @author SvetlanaTselikova
22
 */
23
@RestController("UsersControllerV1")
24
@SuppressWarnings("checkstyle:MultipleStringLiterals")
25
@RequestMapping("/v1/users")
26
public class UsersController {
27
28
    @Autowired
29
    protected UsersServiceInterface usersService;
30
31
    @Autowired
32
    private UsersOptionsBuilderInterface usersOptionsBuilder;
33
34
    @Autowired
35
    private SessionServiceInterface sessionService;
36
37
    @Autowired
38
    @Qualifier("UsersMapperV1")
39
    private UsersMapper usersMapper;
40
41
    private Boolean isAdmin() {
42
        return this.sessionService.getUserModel().getIsAdmin();
43
    }
44
45
    @GetMapping("")
46
    public List<User> list() throws ForbiddenException {
47
        if (!this.isAdmin()) {
48
            throw new ForbiddenException();
49
        }
50
        final List<UserModelInterface> usersModels = this.usersService.findAll();
51
52
        return usersModels
53
                .stream()
54
                .map(model -> this.usersMapper.map(model, User.class))
55
                .collect(Collectors.toList());
56
    }
57
    /**
58
     * create
59
     */
60
    /**
61
     * update
62
     */
63
    /**
64
     * show(userId)
65
     */
66
    /**
67
     * delete(userId)
68
     */
69
    /**
70
     * showMe
71
     */
72
73
    @GetMapping("/me")
74
    public User showme() throws ForbiddenException {
75
        final UserModelInterface userModel = this.sessionService.getUserModel();
76
        if (!this.sessionService.isUser()) {
77
            throw new ForbiddenException();
78
        }
79
        return this.usersMapper.map(userModel, User.class);
80
    }
81
82
    private UserModelInterface getUserModel(Integer id, UsersOptionsInterface userOptions) throws NotFoundException {
83
        final UserModelInterface userModel = this.usersService.find(id, userOptions);
84
        if (userModel == null) {
85
            throw new NotFoundException();
86
        }
87
        return userModel;
88
    }
89
90
    private UserModelInterface getUserModel(Integer id) throws NotFoundException {
0 ignored issues
show
Unused Code introduced by
Remove this unused private "getUserModel" method.
Loading history...
91
        return this.getUserModel(id, this.usersOptionsBuilder.forAuth());
92
    }
93
94
}
95