Passed
Push — dev ( fafe9b...b1caef )
by Konstantin
03:58
created

isAdmin()   A

Complexity

Conditions 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 5
rs 10
cc 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.Identity;
6
import easytests.api.v1.models.User;
7
import easytests.auth.services.SessionServiceInterface;
8
import easytests.core.models.UserModel;
9
import easytests.core.models.UserModelInterface;
10
import easytests.core.options.builder.UsersOptionsBuilderInterface;
11
import easytests.core.services.UsersServiceInterface;
12
import java.util.List;
13
import java.util.stream.Collectors;
14
import org.springframework.beans.factory.annotation.Autowired;
15
import org.springframework.beans.factory.annotation.Qualifier;
16
import org.springframework.http.HttpStatus;
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
        if (!this.sessionService.isUser()) {
43
            return false;
44
        }
45
        return this.sessionService.getUserModel().getIsAdmin();
46
    }
47
48
    /**
49
     * list
50
     */
51
52
    @GetMapping("")
53
    public List<User> list() throws ForbiddenException {
54
        if (!this.isAdmin()) {
55
            throw new ForbiddenException();
56
        }
57
        final List<UserModelInterface> usersModels = this.usersService.findAll();
58
59
        return usersModels
60
                .stream()
61
                .map(model -> this.usersMapper.map(model, User.class))
62
                .collect(Collectors.toList());
63
    }
64
65
    /**
66
     * create
67
     */
68
69
    @PostMapping("")
70
    @ResponseStatus(HttpStatus.CREATED)
71
    public Identity create(@RequestBody User user) throws BadRequestException, ForbiddenException {
72
        if (!this.isAdmin()) {
73
            throw new ForbiddenException();
74
        }
75
        if (user.getId() != null) {
76
            throw new IdentifiedModelException();
77
        }
78
        if (this.usersService.findByEmail(user.getEmail()) != null) {
79
            throw new BadRequestException("This email already exist.");
80
        }
81
82
        final UserModelInterface userModel = this.usersMapper.map(user, UserModel.class);
83
84
        userModel.setPassword(passgenerator(6));
85
86
        this.usersService.save(userModel);
87
88
        return this.usersMapper.map(userModel, Identity.class);
89
    }
90
91
    /**
92
     * update
93
     */
94
    @GetMapping("/{userId}")
95
    public User show(@PathVariable Integer userId) throws NotFoundException, ForbiddenException {
96
        final UserModelInterface userModel = this.usersService.find(userId);
97
98
        if (!this.isAdmin()) {
99
            throw new ForbiddenException();
100
        }
101
102
        if (userModel == null) {
103
            throw new NotFoundException();
104
        }
105
106
        return this.usersMapper.map(userModel, User.class);
107
    }
108
    /**
109
     * delete(userId)
110
     */
111
    /**
112
     * showMe
113
     */
114
115
    @GetMapping("/me")
116
    public User showme() throws ForbiddenException {
117
118
        if (!this.sessionService.isUser()) {
119
            throw new ForbiddenException();
120
        }
121
        final UserModelInterface userModel = this.sessionService.getUserModel();
122
        return this.usersMapper.map(userModel, User.class);
123
    }
124
125
    private String passgenerator(int n) {
126
        final String dict = "qwertyuiopasdfghjklzxcvbnm1234567890QWERTYUIOPASDFGHJKLZXCVBNM";
127
        String pass = "";
128
        for (int i = 0; i < n; i++) {
129
            pass = pass + (dict.charAt(0 + (int) (Math.random() * dict.length())));
0 ignored issues
show
Performance introduced by
String concatenation with + is inefficient. Doing so in a loop may incur a significant performance penalty. Consider using a StringBuilder instead
Loading history...
introduced by
Use "java.util.Random.nextInt()" instead.
Loading history...
130
        }
131
        return pass;
132
    }
133
}
134