|
@@ -1,194 +0,0 @@
|
1
|
|
-package rocks.zipcode.io.web.rest;
|
2
|
|
-
|
3
|
|
-import com.codahale.metrics.annotation.Timed;
|
4
|
|
-
|
5
|
|
-import rocks.zipcode.io.domain.User;
|
6
|
|
-import rocks.zipcode.io.repository.UserRepository;
|
7
|
|
-import rocks.zipcode.io.security.SecurityUtils;
|
8
|
|
-import rocks.zipcode.io.service.MailService;
|
9
|
|
-import rocks.zipcode.io.service.UserService;
|
10
|
|
-import rocks.zipcode.io.service.dto.PasswordChangeDTO;
|
11
|
|
-import rocks.zipcode.io.service.dto.UserDTO;
|
12
|
|
-import rocks.zipcode.io.web.rest.errors.*;
|
13
|
|
-import rocks.zipcode.io.web.rest.vm.KeyAndPasswordVM;
|
14
|
|
-import rocks.zipcode.io.web.rest.vm.ManagedUserVM;
|
15
|
|
-
|
16
|
|
-import org.apache.commons.lang3.StringUtils;
|
17
|
|
-import org.slf4j.Logger;
|
18
|
|
-import org.slf4j.LoggerFactory;
|
19
|
|
-import org.springframework.http.HttpStatus;
|
20
|
|
-import org.springframework.web.bind.annotation.*;
|
21
|
|
-
|
22
|
|
-import javax.servlet.http.HttpServletRequest;
|
23
|
|
-import javax.validation.Valid;
|
24
|
|
-import java.util.*;
|
25
|
|
-
|
26
|
|
-
|
27
|
|
-/**
|
28
|
|
- * REST controller for managing the current user's account.
|
29
|
|
- */
|
30
|
|
-@RestController
|
31
|
|
-@RequestMapping("/api")
|
32
|
|
-public class AccountResource {
|
33
|
|
-
|
34
|
|
- private final Logger log = LoggerFactory.getLogger(AccountResource.class);
|
35
|
|
-
|
36
|
|
- private final UserRepository userRepository;
|
37
|
|
-
|
38
|
|
- private final UserService userService;
|
39
|
|
-
|
40
|
|
- private final MailService mailService;
|
41
|
|
-
|
42
|
|
- public AccountResource(UserRepository userRepository, UserService userService, MailService mailService) {
|
43
|
|
-
|
44
|
|
- this.userRepository = userRepository;
|
45
|
|
- this.userService = userService;
|
46
|
|
- this.mailService = mailService;
|
47
|
|
- }
|
48
|
|
-
|
49
|
|
- /**
|
50
|
|
- * POST /register : register the user.
|
51
|
|
- *
|
52
|
|
- * @param managedUserVM the managed user View Model
|
53
|
|
- * @throws InvalidPasswordException 400 (Bad Request) if the password is incorrect
|
54
|
|
- * @throws EmailAlreadyUsedException 400 (Bad Request) if the email is already used
|
55
|
|
- * @throws LoginAlreadyUsedException 400 (Bad Request) if the login is already used
|
56
|
|
- */
|
57
|
|
- @PostMapping("/register")
|
58
|
|
- @Timed
|
59
|
|
- @ResponseStatus(HttpStatus.CREATED)
|
60
|
|
- public void registerAccount(@Valid @RequestBody ManagedUserVM managedUserVM) {
|
61
|
|
- if (!checkPasswordLength(managedUserVM.getPassword())) {
|
62
|
|
- throw new InvalidPasswordException();
|
63
|
|
- }
|
64
|
|
- User user = userService.registerUser(managedUserVM, managedUserVM.getPassword());
|
65
|
|
- mailService.sendActivationEmail(user);
|
66
|
|
- System.out.println("1");
|
67
|
|
- }
|
68
|
|
-
|
69
|
|
- /**
|
70
|
|
- * GET /activate : activate the registered user.
|
71
|
|
- *
|
72
|
|
- * @param key the activation key
|
73
|
|
- * @throws RuntimeException 500 (Internal Server Error) if the user couldn't be activated
|
74
|
|
- */
|
75
|
|
- @GetMapping("/activate")
|
76
|
|
- @Timed
|
77
|
|
- public void activateAccount(@RequestParam(value = "key") String key) {
|
78
|
|
- Optional<User> user = userService.activateRegistration(key);
|
79
|
|
- if (!user.isPresent()) {
|
80
|
|
- throw new InternalServerErrorException("No user was found for this activation key");
|
81
|
|
- }
|
82
|
|
- System.out.println("2");
|
83
|
|
- }
|
84
|
|
-
|
85
|
|
- /**
|
86
|
|
- * GET /authenticate : check if the user is authenticated, and return its login.
|
87
|
|
- *
|
88
|
|
- * @param request the HTTP request
|
89
|
|
- * @return the login if the user is authenticated
|
90
|
|
- */
|
91
|
|
- @GetMapping("/authenticate")
|
92
|
|
- @Timed
|
93
|
|
- public String isAuthenticated(HttpServletRequest request) {
|
94
|
|
- log.debug("REST request to check if the current user is authenticated");
|
95
|
|
- System.out.println("3");
|
96
|
|
- return request.getRemoteUser();
|
97
|
|
- }
|
98
|
|
-
|
99
|
|
- /**
|
100
|
|
- * GET /account : get the current user.
|
101
|
|
- *
|
102
|
|
- * @return the current user
|
103
|
|
- * @throws RuntimeException 500 (Internal Server Error) if the user couldn't be returned
|
104
|
|
- */
|
105
|
|
- @GetMapping("/account")
|
106
|
|
- @Timed
|
107
|
|
- public UserDTO getAccount() {
|
108
|
|
- System.out.println("4");
|
109
|
|
- return userService.getUserWithAuthorities()
|
110
|
|
- .map(UserDTO::new)
|
111
|
|
- .orElseThrow(() -> new InternalServerErrorException("User could not be found"));
|
112
|
|
- }
|
113
|
|
-
|
114
|
|
- /**
|
115
|
|
- * POST /account : update the current user information.
|
116
|
|
- *
|
117
|
|
- * @param userDTO the current user information
|
118
|
|
- * @throws EmailAlreadyUsedException 400 (Bad Request) if the email is already used
|
119
|
|
- * @throws RuntimeException 500 (Internal Server Error) if the user login wasn't found
|
120
|
|
- */
|
121
|
|
- @PostMapping("/account")
|
122
|
|
- @Timed
|
123
|
|
- public void saveAccount(@Valid @RequestBody UserDTO userDTO) {
|
124
|
|
- final String userLogin = SecurityUtils.getCurrentUserLogin().orElseThrow(() -> new InternalServerErrorException("Current user login not found"));
|
125
|
|
- Optional<User> existingUser = userRepository.findOneByEmailIgnoreCase(userDTO.getEmail());
|
126
|
|
- if (existingUser.isPresent() && (!existingUser.get().getLogin().equalsIgnoreCase(userLogin))) {
|
127
|
|
- throw new EmailAlreadyUsedException();
|
128
|
|
- }
|
129
|
|
- Optional<User> user = userRepository.findOneByLogin(userLogin);
|
130
|
|
- if (!user.isPresent()) {
|
131
|
|
- throw new InternalServerErrorException("User could not be found");
|
132
|
|
- }
|
133
|
|
- userService.updateUser(userDTO.getFirstName(), userDTO.getLastName(), userDTO.getEmail(),
|
134
|
|
- userDTO.getLangKey(), userDTO.getImageUrl());
|
135
|
|
- System.out.println("5");
|
136
|
|
- }
|
137
|
|
-
|
138
|
|
- /**
|
139
|
|
- * POST /account/change-password : changes the current user's password
|
140
|
|
- *
|
141
|
|
- * @param passwordChangeDto current and new password
|
142
|
|
- * @throws InvalidPasswordException 400 (Bad Request) if the new password is incorrect
|
143
|
|
- */
|
144
|
|
- @PostMapping(path = "/account/change-password")
|
145
|
|
- @Timed
|
146
|
|
- public void changePassword(@RequestBody PasswordChangeDTO passwordChangeDto) {
|
147
|
|
- if (!checkPasswordLength(passwordChangeDto.getNewPassword())) {
|
148
|
|
- throw new InvalidPasswordException();
|
149
|
|
- }
|
150
|
|
- userService.changePassword(passwordChangeDto.getCurrentPassword(), passwordChangeDto.getNewPassword());
|
151
|
|
- }
|
152
|
|
-
|
153
|
|
- /**
|
154
|
|
- * POST /account/reset-password/init : Send an email to reset the password of the user
|
155
|
|
- *
|
156
|
|
- * @param mail the mail of the user
|
157
|
|
- * @throws EmailNotFoundException 400 (Bad Request) if the email address is not registered
|
158
|
|
- */
|
159
|
|
- @PostMapping(path = "/account/reset-password/init")
|
160
|
|
- @Timed
|
161
|
|
- public void requestPasswordReset(@RequestBody String mail) {
|
162
|
|
- mailService.sendPasswordResetMail(
|
163
|
|
- userService.requestPasswordReset(mail)
|
164
|
|
- .orElseThrow(EmailNotFoundException::new)
|
165
|
|
- );
|
166
|
|
- }
|
167
|
|
-
|
168
|
|
- /**
|
169
|
|
- * POST /account/reset-password/finish : Finish to reset the password of the user
|
170
|
|
- *
|
171
|
|
- * @param keyAndPassword the generated key and the new password
|
172
|
|
- * @throws InvalidPasswordException 400 (Bad Request) if the password is incorrect
|
173
|
|
- * @throws RuntimeException 500 (Internal Server Error) if the password could not be reset
|
174
|
|
- */
|
175
|
|
- @PostMapping(path = "/account/reset-password/finish")
|
176
|
|
- @Timed
|
177
|
|
- public void finishPasswordReset(@RequestBody KeyAndPasswordVM keyAndPassword) {
|
178
|
|
- if (!checkPasswordLength(keyAndPassword.getNewPassword())) {
|
179
|
|
- throw new InvalidPasswordException();
|
180
|
|
- }
|
181
|
|
- Optional<User> user =
|
182
|
|
- userService.completePasswordReset(keyAndPassword.getNewPassword(), keyAndPassword.getKey());
|
183
|
|
-
|
184
|
|
- if (!user.isPresent()) {
|
185
|
|
- throw new InternalServerErrorException("No user was found for this reset key");
|
186
|
|
- }
|
187
|
|
- }
|
188
|
|
-
|
189
|
|
- private static boolean checkPasswordLength(String password) {
|
190
|
|
- return !StringUtils.isEmpty(password) &&
|
191
|
|
- password.length() >= ManagedUserVM.PASSWORD_MIN_LENGTH &&
|
192
|
|
- password.length() <= ManagedUserVM.PASSWORD_MAX_LENGTH;
|
193
|
|
- }
|
194
|
|
-}
|