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