| 21812 |
amit.gupta |
1 |
package com.spice.profitmandi.web.controller;
|
|
|
2 |
|
|
|
3 |
import java.util.Arrays;
|
|
|
4 |
import java.util.HashMap;
|
|
|
5 |
import java.util.List;
|
|
|
6 |
import java.util.Map;
|
|
|
7 |
|
|
|
8 |
import javax.servlet.http.HttpServletRequest;
|
|
|
9 |
|
|
|
10 |
import org.slf4j.Logger;
|
|
|
11 |
import org.slf4j.LoggerFactory;
|
|
|
12 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
13 |
import org.springframework.http.ResponseEntity;
|
|
|
14 |
import org.springframework.stereotype.Controller;
|
|
|
15 |
import org.springframework.web.bind.annotation.RequestAttribute;
|
|
|
16 |
import org.springframework.web.bind.annotation.RequestBody;
|
|
|
17 |
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
18 |
import org.springframework.web.bind.annotation.RequestMethod;
|
|
|
19 |
import org.springframework.web.bind.annotation.RequestParam;
|
|
|
20 |
|
|
|
21 |
import com.spice.profitmandi.admin.util.JWTUtil;
|
|
|
22 |
import com.spice.profitmandi.common.model.ProfitMandiConstants;
|
|
|
23 |
import com.spice.profitmandi.common.model.UserInfo;
|
|
|
24 |
import com.spice.profitmandi.common.services.Department;
|
|
|
25 |
import com.spice.profitmandi.common.services.HelperServiceUtils;
|
|
|
26 |
import com.spice.profitmandi.common.web.util.ResponseSender;
|
|
|
27 |
import com.spice.profitmandi.web.request.EmailPassword;
|
|
|
28 |
|
|
|
29 |
import in.shop2020.utils.Agent;
|
|
|
30 |
import io.swagger.annotations.ApiImplicitParam;
|
|
|
31 |
import io.swagger.annotations.ApiImplicitParams;
|
|
|
32 |
|
|
|
33 |
@Controller
|
|
|
34 |
public class AuthController {
|
|
|
35 |
|
|
|
36 |
private static final Logger LOGGER = LoggerFactory.getLogger(AuthController.class);
|
|
|
37 |
|
|
|
38 |
|
|
|
39 |
@Autowired
|
|
|
40 |
ResponseSender<?> responseSender;
|
|
|
41 |
|
|
|
42 |
|
|
|
43 |
@RequestMapping(value = "/login", method = RequestMethod.POST)
|
|
|
44 |
public ResponseEntity<?> login(HttpServletRequest request, @RequestBody EmailPassword emailPassword) throws Throwable {
|
|
|
45 |
|
|
|
46 |
Map<String, Object> responseMap = new HashMap<>();
|
|
|
47 |
|
|
|
48 |
if(HelperServiceUtils.authenticate(emailPassword.getEmail(), emailPassword.getPassword())){
|
|
|
49 |
List<String> roles = HelperServiceUtils.getRolesByAgent(emailPassword.getEmail());
|
|
|
50 |
Agent agent = HelperServiceUtils.getAgent(emailPassword.getEmail());
|
|
|
51 |
responseMap.put(ProfitMandiConstants.ACCESS_TOKEN, JWTUtil.create(agent.getId(), roles.toArray(new String[(roles.size())])));
|
|
|
52 |
responseMap.put(ProfitMandiConstants.AUTHENTICATED, true);
|
|
|
53 |
} else {
|
|
|
54 |
responseMap.put(ProfitMandiConstants.AUTHENTICATED, false);
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
return responseSender.ok(responseMap);
|
|
|
58 |
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
@RequestMapping(value = "/departments", method = RequestMethod.POST)
|
|
|
62 |
@ApiImplicitParams({
|
|
|
63 |
@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
|
|
|
64 |
public ResponseEntity<?> getRoles(@RequestAttribute("userInfo") UserInfo userInfo) throws Throwable {
|
|
|
65 |
return responseSender.ok(Arrays.asList(Department.values()));
|
|
|
66 |
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
}
|