Passed
Pull Request — dev (#375)
by
unknown
05:10
created

passgenerator(int)   A

Complexity

Conditions 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 7
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.core.models.UserModel;
8
import easytests.auth.services.SessionServiceInterface;
9
10
import easytests.core.models.UserModelInterface;
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.http.HttpStatus;
18
import org.springframework.web.bind.annotation.*;
19
20
21
/**
22
 * @author SvetlanaTselikova
23
 */
24
@RestController("UsersControllerV1")
25
@SuppressWarnings("checkstyle:MultipleStringLiterals")
26
@RequestMapping("/v1/users")
27
public class UsersController {
28
29
    @Autowired
30
    protected UsersServiceInterface usersService;
31
32
    @Autowired
33
    private UsersOptionsBuilderInterface usersOptionsBuilder;
34
35
    @Autowired
36
    private SessionServiceInterface sessionService;
37
38
    @Autowired
39
    @Qualifier("UsersMapperV1")
40
    private UsersMapper usersMapper;
41
  
42
    private Boolean isAdmin() {
43
        return this.sessionService.getUserModel().getIsAdmin();
44
    }
45
46
    @GetMapping("")
47
    public List<User> list() throws ForbiddenException {
48
        if (!this.isAdmin()) {
49
            throw new ForbiddenException();
50
        }
51
        final List<UserModelInterface> usersModels = this.usersService.findAll();
52
53
        return usersModels
54
                .stream()
55
                .map(model -> this.usersMapper.map(model, User.class))
56
                .collect(Collectors.toList());
57
    }
58
  
59
   @PostMapping("")
60
    @ResponseStatus(HttpStatus.CREATED)
61
    public Identity create(@RequestBody User user) throws BadRequestException {
62
        if (user.getId() != null) {
63
            throw new IdentifiedModelException();
64
        }
65
        if (this.usersService.findByEmail(user.getEmail()) != null) {
66
            throw new BadRequestException("This email already exist.");
67
        }
68
69
        final UserModelInterface userModel = this.usersMapper.map(user, UserModel.class);
70
71
        userModel.setPassword(passgenerator(6));
72
73
        this.usersService.save(userModel);
74
75
        return this.usersMapper.map(userModel, Identity.class);
76
    }
77
 
78
    /**
79
     * update
80
     */
81
    /**
82
     * show(userId)
83
     */
84
    /**
85
     * delete(userId)
86
     */
87
    /**
88
     * showMe
89
     */
90
91
    private String passgenerator(int n) {
92
        final String dict = "qwertyuiopasdfghjklzxcvbnm1234567890QWERTYUIOPASDFGHJKLZXCVBNM";
93
        String pass = "";
94
        for (int i = 0; i < n; i++) {
95
            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...
96
        }
97
        return pass;
98
    }
99
}
100