| Line 1... |
Line 1... |
| 1 |
package com.spice.profitmandi.web.controller;
|
1 |
package com.spice.profitmandi.web.controller;
|
| 2 |
|
2 |
|
| 3 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
3 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
| 4 |
import com.spice.profitmandi.common.model.ProfitMandiConstants;
|
4 |
import com.spice.profitmandi.common.model.ProfitMandiConstants;
|
| - |
|
5 |
import com.spice.profitmandi.common.util.ExcelUtils;
|
| 5 |
import com.spice.profitmandi.dao.entity.auth.AuthUser;
|
6 |
import com.spice.profitmandi.dao.entity.auth.AuthUser;
|
| - |
|
7 |
import com.spice.profitmandi.dao.model.PasswordOverrideRow;
|
| 6 |
import com.spice.profitmandi.dao.entity.cs.PartnerPosition;
|
8 |
import com.spice.profitmandi.dao.entity.cs.PartnerPosition;
|
| 7 |
import com.spice.profitmandi.dao.entity.cs.Position;
|
9 |
import com.spice.profitmandi.dao.entity.cs.Position;
|
| 8 |
import com.spice.profitmandi.dao.entity.dtr.Role;
|
10 |
import com.spice.profitmandi.dao.entity.dtr.Role;
|
| 9 |
import com.spice.profitmandi.dao.entity.dtr.User;
|
11 |
import com.spice.profitmandi.dao.entity.dtr.User;
|
| 10 |
import com.spice.profitmandi.dao.entity.dtr.UserRole;
|
12 |
import com.spice.profitmandi.dao.entity.dtr.UserRole;
|
| Line 12... |
Line 14... |
| 12 |
import com.spice.profitmandi.dao.repository.cs.PartnerPositionRepository;
|
14 |
import com.spice.profitmandi.dao.repository.cs.PartnerPositionRepository;
|
| 13 |
import com.spice.profitmandi.dao.repository.cs.PositionRepository;
|
15 |
import com.spice.profitmandi.dao.repository.cs.PositionRepository;
|
| 14 |
import com.spice.profitmandi.dao.repository.dtr.RoleRepository;
|
16 |
import com.spice.profitmandi.dao.repository.dtr.RoleRepository;
|
| 15 |
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
|
17 |
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
|
| 16 |
import com.spice.profitmandi.dao.repository.dtr.UserRoleRepository;
|
18 |
import com.spice.profitmandi.dao.repository.dtr.UserRoleRepository;
|
| - |
|
19 |
import com.spice.profitmandi.dao.repository.devtools.PasswordOverrideRepository;
|
| 17 |
import com.spice.profitmandi.service.AuthService;
|
20 |
import com.spice.profitmandi.service.AuthService;
|
| 18 |
import com.spice.profitmandi.service.user.UserService;
|
21 |
import com.spice.profitmandi.service.user.UserService;
|
| 19 |
import com.spice.profitmandi.web.model.LoginDetails;
|
22 |
import com.spice.profitmandi.web.model.LoginDetails;
|
| 20 |
import com.spice.profitmandi.web.util.CookiesProcessor;
|
23 |
import com.spice.profitmandi.web.util.CookiesProcessor;
|
| 21 |
import com.spice.profitmandi.web.util.MVCResponseSender;
|
24 |
import com.spice.profitmandi.web.util.MVCResponseSender;
|
| 22 |
import org.apache.logging.log4j.LogManager;
|
25 |
import org.apache.logging.log4j.LogManager;
|
| 23 |
import org.apache.logging.log4j.Logger;
|
26 |
import org.apache.logging.log4j.Logger;
|
| 24 |
import org.springframework.beans.factory.annotation.Autowired;
|
27 |
import org.springframework.beans.factory.annotation.Autowired;
|
| 25 |
import org.springframework.beans.factory.annotation.Qualifier;
|
28 |
import org.springframework.beans.factory.annotation.Qualifier;
|
| 26 |
import org.springframework.beans.factory.annotation.Value;
|
29 |
import org.springframework.beans.factory.annotation.Value;
|
| - |
|
30 |
import org.springframework.core.io.InputStreamResource;
|
| - |
|
31 |
import org.springframework.http.HttpHeaders;
|
| - |
|
32 |
import org.springframework.http.HttpStatus;
|
| - |
|
33 |
import org.springframework.http.ResponseEntity;
|
| 27 |
import org.springframework.stereotype.Controller;
|
34 |
import org.springframework.stereotype.Controller;
|
| 28 |
import org.springframework.transaction.annotation.Transactional;
|
35 |
import org.springframework.transaction.annotation.Transactional;
|
| 29 |
import org.springframework.ui.Model;
|
36 |
import org.springframework.ui.Model;
|
| 30 |
import org.springframework.web.bind.annotation.GetMapping;
|
37 |
import org.springframework.web.bind.annotation.GetMapping;
|
| 31 |
import org.springframework.web.bind.annotation.PostMapping;
|
38 |
import org.springframework.web.bind.annotation.PostMapping;
|
| 32 |
import org.springframework.web.bind.annotation.RequestParam;
|
39 |
import org.springframework.web.bind.annotation.RequestParam;
|
| - |
|
40 |
import org.springframework.web.bind.annotation.RequestPart;
|
| - |
|
41 |
import org.springframework.web.multipart.MultipartFile;
|
| 33 |
|
42 |
|
| 34 |
import javax.servlet.http.HttpServletRequest;
|
43 |
import javax.servlet.http.HttpServletRequest;
|
| 35 |
import java.time.LocalDateTime;
|
44 |
import java.time.LocalDateTime;
|
| - |
|
45 |
import java.util.ArrayList;
|
| 36 |
import java.util.HashMap;
|
46 |
import java.util.HashMap;
|
| 37 |
import java.util.List;
|
47 |
import java.util.List;
|
| 38 |
import java.util.Map;
|
48 |
import java.util.Map;
|
| 39 |
import java.util.stream.Collectors;
|
49 |
import java.util.stream.Collectors;
|
| 40 |
|
50 |
|
| Line 48... |
Line 58... |
| 48 |
private AuthService authService;
|
58 |
private AuthService authService;
|
| 49 |
|
59 |
|
| 50 |
@Autowired
|
60 |
@Autowired
|
| 51 |
private UserService userService;
|
61 |
private UserService userService;
|
| 52 |
|
62 |
|
| - |
|
63 |
@Autowired
|
| - |
|
64 |
private PasswordOverrideRepository passwordOverrideRepository;
|
| - |
|
65 |
|
| 53 |
@Value("${prod}")
|
66 |
@Value("${prod}")
|
| 54 |
private boolean isProd;
|
67 |
private boolean isProd;
|
| 55 |
|
68 |
|
| 56 |
@Autowired
|
69 |
@Autowired
|
| 57 |
private AuthRepository authRepository;
|
70 |
private AuthRepository authRepository;
|
| Line 239... |
Line 252... |
| 239 |
throws Exception {
|
252 |
throws Exception {
|
| 240 |
if (isProd) {
|
253 |
if (isProd) {
|
| 241 |
throw new ProfitMandiBusinessException("Access Denied", "Environment",
|
254 |
throw new ProfitMandiBusinessException("Access Denied", "Environment",
|
| 242 |
"Password override is disabled in production");
|
255 |
"Password override is disabled in production");
|
| 243 |
}
|
256 |
}
|
| - |
|
257 |
LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
|
| - |
|
258 |
applyOverride(type, retailerId, identifier, newPassword, loginDetails.getEmailId());
|
| - |
|
259 |
model.addAttribute("response1", mvcResponseSender.createResponseString(true));
|
| - |
|
260 |
return "response";
|
| - |
|
261 |
}
|
| - |
|
262 |
|
| - |
|
263 |
// Reset one target's live password and record it in devtools.password_override.
|
| - |
|
264 |
private void applyOverride(String type, int retailerId, String identifier, String newPassword, String setBy)
|
| - |
|
265 |
throws ProfitMandiBusinessException {
|
| 244 |
if (newPassword.trim().isEmpty()) {
|
266 |
if (newPassword == null || newPassword.trim().isEmpty()) {
|
| 245 |
throw new ProfitMandiBusinessException("Password", "newPassword", "New password cannot be blank");
|
267 |
throw new ProfitMandiBusinessException("Password", "newPassword", "New password cannot be blank");
|
| 246 |
}
|
268 |
}
|
| 247 |
LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
|
- |
|
| 248 |
if ("PARTNER".equalsIgnoreCase(type)) {
|
269 |
if ("PARTNER".equalsIgnoreCase(type)) {
|
| 249 |
if (retailerId <= 0) {
|
270 |
if (retailerId <= 0) {
|
| 250 |
throw new ProfitMandiBusinessException("Partner", "retailerId", "Please select a partner");
|
271 |
throw new ProfitMandiBusinessException("Partner", "retailerId", "Please select a partner");
|
| 251 |
}
|
272 |
}
|
| 252 |
userService.resetPasswordById(retailerId, newPassword);
|
273 |
String stored = userService.resetPasswordById(retailerId, newPassword);
|
| - |
|
274 |
passwordOverrideRepository.upsert("PARTNER", 0, retailerId,
|
| - |
|
275 |
identifier == null ? "" : identifier.trim(), stored, newPassword, setBy);
|
| 253 |
LOGGER.info("Password override for partner retailerId={} by {}", retailerId, loginDetails.getEmailId());
|
276 |
LOGGER.info("Password override for partner retailerId={} by {}", retailerId, setBy);
|
| 254 |
} else {
|
277 |
} else {
|
| 255 |
if (identifier.trim().isEmpty()) {
|
278 |
if (identifier == null || identifier.trim().isEmpty()) {
|
| 256 |
throw new ProfitMandiBusinessException("Auth User", "identifier", "Please enter an email or mobile");
|
279 |
throw new ProfitMandiBusinessException("Auth User", "identifier", "Please enter an email or mobile");
|
| 257 |
}
|
280 |
}
|
| 258 |
authService.resetPassword(identifier.trim(), newPassword);
|
281 |
authService.resetPassword(identifier.trim(), newPassword);
|
| - |
|
282 |
AuthUser authUser = authRepository.selectByEmailOrMobile(identifier.trim());
|
| - |
|
283 |
passwordOverrideRepository.upsert("AUTH_USER", authUser.getId(), 0, identifier.trim(),
|
| - |
|
284 |
authUser.getPassword(), newPassword, setBy);
|
| 259 |
LOGGER.info("Password override for authUser={} by {}", identifier.trim(), loginDetails.getEmailId());
|
285 |
LOGGER.info("Password override for authUser={} by {}", identifier.trim(), setBy);
|
| 260 |
}
|
286 |
}
|
| - |
|
287 |
}
|
| - |
|
288 |
|
| - |
|
289 |
@GetMapping(value = "/passwordOverride/download")
|
| - |
|
290 |
public ResponseEntity<?> downloadPasswordOverrides(HttpServletRequest request) throws Exception {
|
| - |
|
291 |
if (isProd) {
|
| - |
|
292 |
throw new ProfitMandiBusinessException("Access Denied", "Environment",
|
| - |
|
293 |
"Password override is disabled in production");
|
| - |
|
294 |
}
|
| - |
|
295 |
java.io.ByteArrayOutputStream out = new java.io.ByteArrayOutputStream();
|
| - |
|
296 |
ExcelUtils.writePasswordOverrides(passwordOverrideRepository.selectAll(), out);
|
| - |
|
297 |
byte[] bytes = out.toByteArray();
|
| - |
|
298 |
HttpHeaders headers = new HttpHeaders();
|
| - |
|
299 |
headers.set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
|
| - |
|
300 |
headers.set("Content-disposition", "attachment; filename=password-overrides.xlsx");
|
| - |
|
301 |
headers.setContentLength(bytes.length);
|
| - |
|
302 |
return new ResponseEntity<>(new InputStreamResource(new java.io.ByteArrayInputStream(bytes)), headers,
|
| - |
|
303 |
HttpStatus.OK);
|
| - |
|
304 |
}
|
| - |
|
305 |
|
| - |
|
306 |
@PostMapping(value = "/passwordOverride/upload")
|
| - |
|
307 |
public String uploadPasswordOverrides(HttpServletRequest request,
|
| - |
|
308 |
@RequestPart("file") MultipartFile file, Model model) throws Exception {
|
| - |
|
309 |
if (isProd) {
|
| - |
|
310 |
throw new ProfitMandiBusinessException("Access Denied", "Environment",
|
| - |
|
311 |
"Password override is disabled in production");
|
| - |
|
312 |
}
|
| - |
|
313 |
LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
|
| - |
|
314 |
List<PasswordOverrideRow> rows = ExcelUtils.parsePasswordOverrides(file.getInputStream());
|
| - |
|
315 |
int applied = 0;
|
| - |
|
316 |
List<String> failures = new ArrayList<>();
|
| - |
|
317 |
for (int i = 0; i < rows.size(); i++) {
|
| - |
|
318 |
PasswordOverrideRow r = rows.get(i);
|
| - |
|
319 |
if (r.getCleartextPassword() == null || r.getCleartextPassword().trim().isEmpty()) {
|
| - |
|
320 |
continue; // blank password = leave this target unchanged
|
| - |
|
321 |
}
|
| - |
|
322 |
try {
|
| - |
|
323 |
int retailerId = "PARTNER".equalsIgnoreCase(r.getType()) ? r.getRetailerId() : 0;
|
| - |
|
324 |
applyOverride(r.getType(), retailerId, r.getIdentifier(), r.getCleartextPassword(),
|
| - |
|
325 |
loginDetails.getEmailId());
|
| - |
|
326 |
applied++;
|
| - |
|
327 |
} catch (Exception e) {
|
| - |
|
328 |
failures.add("Row " + (i + 2) + " (" + r.getType() + " "
|
| - |
|
329 |
+ (r.getIdentifier() == null ? r.getRetailerId() : r.getIdentifier()) + "): " + e.getMessage());
|
| - |
|
330 |
}
|
| - |
|
331 |
}
|
| - |
|
332 |
LOGGER.info("Bulk password override by {}: {} applied, {} failed", loginDetails.getEmailId(), applied,
|
| - |
|
333 |
failures.size());
|
| 261 |
model.addAttribute("response1", mvcResponseSender.createResponseString(true));
|
334 |
model.addAttribute("response1", mvcResponseSender.createResponseString(
|
| - |
|
335 |
"Applied " + applied + " of " + rows.size() + " row(s)."
|
| - |
|
336 |
+ (failures.isEmpty() ? "" : " Failures: " + String.join("; ", failures))));
|
| 262 |
return "response";
|
337 |
return "response";
|
| 263 |
}
|
338 |
}
|
| 264 |
|
339 |
|
| 265 |
@GetMapping(value = "/getPaginatedAuthUser")
|
340 |
@GetMapping(value = "/getPaginatedAuthUser")
|
| 266 |
public String getPaginatedAuthUser(HttpServletRequest request,
|
341 |
public String getPaginatedAuthUser(HttpServletRequest request,
|