| 21561 |
ashik.ali |
1 |
package com.spice.profitmandi.web.controller;
|
| 21555 |
kshitij.so |
2 |
|
| 22111 |
ashik.ali |
3 |
import java.util.HashSet;
|
|
|
4 |
import java.util.List;
|
|
|
5 |
import java.util.Set;
|
| 22069 |
ashik.ali |
6 |
|
| 21577 |
ashik.ali |
7 |
import javax.servlet.http.Cookie;
|
| 21561 |
ashik.ali |
8 |
import javax.servlet.http.HttpServletRequest;
|
| 21577 |
ashik.ali |
9 |
import javax.servlet.http.HttpServletResponse;
|
| 21561 |
ashik.ali |
10 |
|
| 21568 |
ashik.ali |
11 |
import org.slf4j.Logger;
|
|
|
12 |
import org.slf4j.LoggerFactory;
|
| 21561 |
ashik.ali |
13 |
import org.springframework.beans.factory.annotation.Autowired;
|
| 22079 |
amit.gupta |
14 |
import org.springframework.beans.factory.annotation.Value;
|
| 21555 |
kshitij.so |
15 |
import org.springframework.stereotype.Controller;
|
| 21987 |
kshitij.so |
16 |
import org.springframework.transaction.annotation.Transactional;
|
| 21615 |
kshitij.so |
17 |
import org.springframework.ui.Model;
|
| 21555 |
kshitij.so |
18 |
import org.springframework.ui.ModelMap;
|
|
|
19 |
import org.springframework.web.bind.annotation.ModelAttribute;
|
|
|
20 |
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
21 |
import org.springframework.web.bind.annotation.RequestMethod;
|
| 21561 |
ashik.ali |
22 |
import org.springframework.web.bind.annotation.RequestParam;
|
| 21555 |
kshitij.so |
23 |
|
| 21561 |
ashik.ali |
24 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
|
|
25 |
import com.spice.profitmandi.common.model.ProfitMandiConstants;
|
| 22111 |
ashik.ali |
26 |
import com.spice.profitmandi.dao.entity.dtr.Retailer;
|
|
|
27 |
import com.spice.profitmandi.dao.entity.dtr.User;
|
|
|
28 |
import com.spice.profitmandi.dao.entity.dtr.UserAccounts;
|
|
|
29 |
import com.spice.profitmandi.dao.entity.dtr.UserRole;
|
|
|
30 |
import com.spice.profitmandi.dao.enumuration.dtr.AccountType;
|
|
|
31 |
import com.spice.profitmandi.dao.enumuration.dtr.RoleType;
|
|
|
32 |
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
|
|
|
33 |
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
|
|
|
34 |
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
|
|
|
35 |
import com.spice.profitmandi.dao.repository.dtr.UserRoleRepository;
|
| 21561 |
ashik.ali |
36 |
import com.spice.profitmandi.web.model.FofoDetails;
|
| 22069 |
ashik.ali |
37 |
import com.spice.profitmandi.web.util.CookiesProcessor;
|
| 22111 |
ashik.ali |
38 |
import com.spice.profitmandi.web.util.GoogleTokenUtil;
|
| 21574 |
ashik.ali |
39 |
import com.spice.profitmandi.web.util.MVCResponseSender;
|
| 21561 |
ashik.ali |
40 |
|
| 21555 |
kshitij.so |
41 |
@Controller
|
| 22037 |
amit.gupta |
42 |
@Transactional(rollbackFor=Throwable.class)
|
| 21555 |
kshitij.so |
43 |
public class LoginController {
|
|
|
44 |
|
| 21568 |
ashik.ali |
45 |
private static final Logger LOGGER = LoggerFactory.getLogger(LoginController.class);
|
|
|
46 |
|
| 21561 |
ashik.ali |
47 |
@Autowired
|
| 22111 |
ashik.ali |
48 |
GoogleTokenUtil googleTokenUtil;
|
| 21561 |
ashik.ali |
49 |
|
| 21574 |
ashik.ali |
50 |
@Autowired
|
| 22111 |
ashik.ali |
51 |
RetailerRepository retailerRepository;
|
|
|
52 |
|
|
|
53 |
@Autowired
|
|
|
54 |
UserRepository userRepository;
|
|
|
55 |
|
|
|
56 |
@Autowired
|
|
|
57 |
UserAccountRepository userAccountRepository;
|
|
|
58 |
|
|
|
59 |
@Autowired
|
|
|
60 |
UserRoleRepository userRoleRepository;
|
|
|
61 |
|
|
|
62 |
@Autowired
|
| 21574 |
ashik.ali |
63 |
MVCResponseSender mvcResponseSender;
|
|
|
64 |
|
| 21578 |
ashik.ali |
65 |
@Autowired
|
| 22069 |
ashik.ali |
66 |
CookiesProcessor cookiesProcessor;
|
| 22079 |
amit.gupta |
67 |
|
|
|
68 |
@Value("${google.api.key}")
|
|
|
69 |
private String googleApiKey;
|
|
|
70 |
|
| 21555 |
kshitij.so |
71 |
@RequestMapping(value = "/login", method = RequestMethod.GET)
|
| 22072 |
ashik.ali |
72 |
public String loginPage(HttpServletRequest request, Model model) throws Exception{
|
| 22088 |
amit.gupta |
73 |
LOGGER.info("Context Path is {}", request.getContextPath());
|
| 21578 |
ashik.ali |
74 |
try{
|
| 22069 |
ashik.ali |
75 |
cookiesProcessor.getCookiesObject(request);
|
| 21578 |
ashik.ali |
76 |
LOGGER.info("Request session is already exist, should be redirect to /dashboard");
|
| 22084 |
amit.gupta |
77 |
return "redirect:/dashboard";
|
| 21615 |
kshitij.so |
78 |
}catch(Exception | ProfitMandiBusinessException profitMandiBusinessException){
|
| 22079 |
amit.gupta |
79 |
model.addAttribute("googleApiKey", googleApiKey);
|
| 22086 |
amit.gupta |
80 |
model.addAttribute("appContextPath", request.getContextPath());
|
| 21577 |
ashik.ali |
81 |
return "login";
|
| 21574 |
ashik.ali |
82 |
}
|
| 21555 |
kshitij.so |
83 |
}
|
|
|
84 |
|
|
|
85 |
@RequestMapping(value = "/login", method = RequestMethod.POST)
|
| 21615 |
kshitij.so |
86 |
public String login(HttpServletRequest request, HttpServletResponse response, @RequestParam(name = ProfitMandiConstants.TOKEN) String token, Model model) throws Exception{
|
| 21561 |
ashik.ali |
87 |
try{
|
| 22111 |
ashik.ali |
88 |
String emailId = googleTokenUtil.getEmailId(token);
|
|
|
89 |
User user = null;
|
|
|
90 |
try{
|
|
|
91 |
user = userRepository.selectByEmailId(emailId);
|
|
|
92 |
}catch(ProfitMandiBusinessException profitMandiBusinessException){
|
|
|
93 |
LOGGER.error("User not found with given emailId", profitMandiBusinessException);
|
|
|
94 |
model.addAttribute("loginResponse", mvcResponseSender.createResponseString("RTLR_OK_1002", true, request.getContextPath() + "/register"));
|
|
|
95 |
return "response";
|
|
|
96 |
}
|
|
|
97 |
UserAccounts userAccounts = userAccountRepository.getUserAccountByType(user.getId(), AccountType.saholic);
|
|
|
98 |
Retailer retailer = retailerRepository.selectById(Integer.parseInt(userAccounts.getAccount_key()));
|
|
|
99 |
if(!retailer.isFofo()){
|
|
|
100 |
LOGGER.error("Retailer is not fofo, should be registered");
|
|
|
101 |
model.addAttribute("loginResponse", mvcResponseSender.createResponseString("RTLR_OK_1002", true, request.getContextPath() + "/register"));
|
|
|
102 |
return "response";
|
|
|
103 |
}
|
|
|
104 |
List<UserRole> userRoles = userRoleRepository.selectByUserId(user.getId());
|
|
|
105 |
Set<RoleType> roleTypes = new HashSet<>();
|
|
|
106 |
StringBuilder roleNames = new StringBuilder();
|
|
|
107 |
for(int index = 0; index < userRoles.size(); index++){
|
|
|
108 |
roleTypes.add(userRoles.get(index).getRoleType());
|
|
|
109 |
roleNames.append(userRoles.get(index).getRoleType().toString());
|
|
|
110 |
if(index + 1 != userRoles.size()){
|
|
|
111 |
roleNames.append(",");
|
|
|
112 |
}
|
|
|
113 |
}
|
|
|
114 |
FofoDetails fofoDetails = new FofoDetails();
|
|
|
115 |
fofoDetails.setFofoId(retailer.getId());
|
|
|
116 |
fofoDetails.setEmailId(emailId);
|
|
|
117 |
fofoDetails.setRoleTypes(roleTypes);
|
|
|
118 |
//FofoDetails fofoDetails = googleTokenUtil.getFofoDetail(token);
|
| 21577 |
ashik.ali |
119 |
Cookie cookieFofoId = new Cookie(ProfitMandiConstants.FOFO_ID, String.valueOf(fofoDetails.getFofoId()));
|
| 22069 |
ashik.ali |
120 |
cookieFofoId.setDomain(request.getServerName());
|
| 22087 |
amit.gupta |
121 |
cookieFofoId.setPath(request.getContextPath());
|
| 21578 |
ashik.ali |
122 |
Cookie cookieEmailId = new Cookie(ProfitMandiConstants.EMAIL_ID, fofoDetails.getEmailId());
|
| 22069 |
ashik.ali |
123 |
cookieEmailId.setDomain(request.getServerName());
|
| 22087 |
amit.gupta |
124 |
cookieEmailId.setPath(request.getContextPath());
|
| 22111 |
ashik.ali |
125 |
Cookie cookieRoleNames = new Cookie(ProfitMandiConstants.ROLE_NAMES, roleNames.toString());
|
| 21577 |
ashik.ali |
126 |
response.addCookie(cookieFofoId);
|
|
|
127 |
response.addCookie(cookieEmailId);
|
| 22111 |
ashik.ali |
128 |
response.addCookie(cookieRoleNames);
|
| 21574 |
ashik.ali |
129 |
LOGGER.info("Requested token email_id is valid, user login to system, shoud be redirect to /dashboard");
|
| 22085 |
amit.gupta |
130 |
model.addAttribute("loginResponse", mvcResponseSender.createResponseString("RTLR_OK_1002", true, request.getContextPath() + "/dashboard"));
|
| 21578 |
ashik.ali |
131 |
return "response";
|
| 21615 |
kshitij.so |
132 |
// return mvcResponseSender.createResponseString("RTLR_OK_1002", true, "/profitmandi-fofo/dashboard");
|
| 21561 |
ashik.ali |
133 |
}catch(ProfitMandiBusinessException profitMandiBusinessException){
|
| 21568 |
ashik.ali |
134 |
LOGGER.error("Error : ", profitMandiBusinessException);
|
| 21578 |
ashik.ali |
135 |
model.addAttribute("loginResponse", mvcResponseSender.createResponseString(profitMandiBusinessException.getCode(), false, "/error"));
|
|
|
136 |
return "response";
|
| 21561 |
ashik.ali |
137 |
}
|
| 21555 |
kshitij.so |
138 |
}
|
| 21574 |
ashik.ali |
139 |
|
| 22069 |
ashik.ali |
140 |
@RequestMapping(value = "/logout", method = RequestMethod.GET)
|
|
|
141 |
public String logout(HttpServletRequest request, @ModelAttribute("model") ModelMap model, HttpServletResponse response) throws Exception{
|
|
|
142 |
try{
|
|
|
143 |
cookiesProcessor.removeCookies(request, response);
|
|
|
144 |
LOGGER.info("Logout is successfull, should be redirect to /login");
|
| 22085 |
amit.gupta |
145 |
return "redirect:/login";
|
| 22069 |
ashik.ali |
146 |
}catch(Exception | ProfitMandiBusinessException profitMandiBusinessException){
|
|
|
147 |
LOGGER.info("Error occured while removing requested cookies, should be redirect to /login");
|
| 22085 |
amit.gupta |
148 |
return "redirect:/login";
|
| 22069 |
ashik.ali |
149 |
}
|
|
|
150 |
}
|
|
|
151 |
|
|
|
152 |
|
|
|
153 |
|
| 21555 |
kshitij.so |
154 |
}
|