Completed
Push — dev ( b1caef...970466 )
by Konstantin
05:15 queued 53s
created

passgenerator

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
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.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.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
        if (!this.sessionService.isUser()) {
44
            return false;
45
        }
46
        return this.sessionService.getUserModel().getIsAdmin();
47
    }
48
49
    /**
50
     * list
51
     */
52
53
    @GetMapping("")
54
    public List<User> list() throws ForbiddenException {
55
        if (!this.isAdmin()) {
56
            throw new ForbiddenException();
57
        }
58
        final List<UserModelInterface> usersModels = this.usersService.findAll();
59
60
        return usersModels
61
                .stream()
62
                .map(model -> this.usersMapper.map(model, User.class))
63
                .collect(Collectors.toList());
64
    }
65
66
    /**
67
     * create
68
     */
69
70
    @PostMapping("")
71
    @ResponseStatus(HttpStatus.CREATED)
72
    public Identity create(@RequestBody User user) throws BadRequestException, ForbiddenException {
73
        if (!this.isAdmin()) {
74
            throw new ForbiddenException();
75
        }
76
        if (user.getId() != null) {
77
            throw new IdentifiedModelException();
78
        }
79
        if (this.usersService.findByEmail(user.getEmail()) != null) {
80
            throw new BadRequestException("This email already exist.");
81
        }
82
83
        final UserModelInterface userModel = this.usersMapper.map(user, UserModel.class);
84
85
        userModel.setPassword(passgenerator(6));
86
87
        this.usersService.save(userModel);
88
89
        return this.usersMapper.map(userModel, Identity.class);
90
    }
91
92
    /**
93
     * update
94
     */
95
    @GetMapping("/{userId}")
96
    public User show(@PathVariable Integer userId) throws NotFoundException, ForbiddenException {
97
        final UserModelInterface userModel = this.usersService.find(userId);
98
99
        if (!this.isAdmin()) {
100
            throw new ForbiddenException();
101
        }
102
103
        if (userModel == null) {
104
            throw new NotFoundException();
105
        }
106
107
        return this.usersMapper.map(userModel, User.class);
108
    }
109
110
    @DeleteMapping("/{userId}")
111
    public void delete(@PathVariable Integer userId) throws NotFoundException, ForbiddenException {
112
        if (!this.isAdmin()) {
113
            throw new ForbiddenException();
114
        }
115
116
        final UsersOptionsInterface usersOptions = this.usersOptionsBuilder.forDelete();
117
        final UserModelInterface userModel = this.usersService.find(userId, usersOptions);
118
119
        if (userModel == null) {
120
            throw new NotFoundException();
121
        }
122
123
        this.usersService.delete(userModel, usersOptions);
124
    }
125
126
    @GetMapping("/me")
127
    public User showme() throws ForbiddenException {
128
129
        if (!this.sessionService.isUser()) {
130
            throw new ForbiddenException();
131
        }
132
        final UserModelInterface userModel = this.sessionService.getUserModel();
133
        return this.usersMapper.map(userModel, User.class);
134
    }
135
136
    private String passgenerator(int n) {
137
        final String dict = "qwertyuiopasdfghjklzxcvbnm1234567890QWERTYUIOPASDFGHJKLZXCVBNM";
138
        String pass = "";
139
        for (int i = 0; i < n; i++) {
140
            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...
141
        }
142
        return pass;
143
    }
144
}
145