| 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;
|
| 23271 |
ashik.ali |
29 |
import com.spice.profitmandi.dao.entity.dtr.UserAccount;
|
| 22111 |
ashik.ali |
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
|
| 22927 |
ashik.ali |
50 |
private GoogleTokenUtil googleTokenUtil;
|
| 21561 |
ashik.ali |
51 |
|
| 21574 |
ashik.ali |
52 |
@Autowired
|
| 22927 |
ashik.ali |
53 |
private RetailerRepository retailerRepository;
|
| 22111 |
ashik.ali |
54 |
|
|
|
55 |
@Autowired
|
| 22927 |
ashik.ali |
56 |
private UserRepository userRepository;
|
| 22111 |
ashik.ali |
57 |
|
|
|
58 |
@Autowired
|
| 22927 |
ashik.ali |
59 |
private UserAccountRepository userAccountRepository;
|
| 22111 |
ashik.ali |
60 |
|
|
|
61 |
@Autowired
|
| 22927 |
ashik.ali |
62 |
private UserRoleRepository userRoleRepository;
|
| 22111 |
ashik.ali |
63 |
|
|
|
64 |
@Autowired
|
| 22927 |
ashik.ali |
65 |
private MVCResponseSender mvcResponseSender;
|
| 21574 |
ashik.ali |
66 |
|
| 21578 |
ashik.ali |
67 |
@Autowired
|
| 22927 |
ashik.ali |
68 |
private 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);
|
| 22927 |
ashik.ali |
80 |
}catch(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(){
|
| 22860 |
ashik.ali |
88 |
return "redirect:/login";
|
| 22155 |
amit.gupta |
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);
|
| 23203 |
ashik.ali |
107 |
}catch(ProfitMandiBusinessException profitMandiBusinessException){
|
|
|
108 |
LOGGER.error("User not found with given emailId", profitMandiBusinessException);
|
|
|
109 |
}
|
|
|
110 |
if(user == null){
|
|
|
111 |
try{
|
|
|
112 |
user = userRepository.selectBySecondryEmailId(emailId);
|
|
|
113 |
}catch(ProfitMandiBusinessException profitMandiBusinessException){
|
|
|
114 |
LOGGER.error("User not found with given emailId", profitMandiBusinessException);
|
|
|
115 |
}
|
|
|
116 |
}
|
|
|
117 |
if(user != null){
|
| 22139 |
amit.gupta |
118 |
fofoDetails.setFofoId(user.getId());
|
|
|
119 |
try {
|
|
|
120 |
List<UserRole> userRoles = userRoleRepository.selectByUserId(user.getId());
|
|
|
121 |
for(int index = 0; index < userRoles.size(); index++){
|
|
|
122 |
roleTypes.add(userRoles.get(index).getRoleType());
|
|
|
123 |
}
|
| 22166 |
amit.gupta |
124 |
if(roleTypes.contains(RoleType.RETAILER)) {
|
| 23271 |
ashik.ali |
125 |
UserAccount userAccounts = userAccountRepository.selectByUserIdType(user.getId(), AccountType.saholic);
|
|
|
126 |
Retailer retailer = retailerRepository.selectById(userAccounts.getAccountKey());
|
| 22166 |
amit.gupta |
127 |
fofoDetails.setFofoId(retailer.getId());
|
| 22533 |
ashik.ali |
128 |
//fofoDetails.setFofo(retailer.isFofo());
|
| 22166 |
amit.gupta |
129 |
}
|
| 22139 |
amit.gupta |
130 |
} catch(ProfitMandiBusinessException pmbe) {
|
|
|
131 |
LOGGER.error("Data Inconsistent", pmbe);
|
|
|
132 |
}
|
| 22111 |
ashik.ali |
133 |
}
|
| 22139 |
amit.gupta |
134 |
String redirectUrl = Utils.getRedictUrlFromLogin(fofoDetails);
|
| 23173 |
ashik.ali |
135 |
if(!redirectUrl.equals("/login")){
|
|
|
136 |
this.addCookiesToResponse(fofoDetails, request, response);
|
|
|
137 |
LOGGER.info("Requested token email_id is valid, user login to system, shoud be redirect to {}", redirectUrl);
|
|
|
138 |
model.addAttribute("loginResponse", mvcResponseSender.createResponseString("RTLR_OK_1002", true, request.getContextPath() + redirectUrl));
|
|
|
139 |
}else{
|
|
|
140 |
LOGGER.error("Requested token email_id is not valid, please try to login");
|
|
|
141 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.EMAIL_ID, fofoDetails.getEmailId(), "RTLR_1000");
|
|
|
142 |
}
|
| 21578 |
ashik.ali |
143 |
return "response";
|
| 22139 |
amit.gupta |
144 |
} catch(ProfitMandiBusinessException profitMandiBusinessException){
|
| 21568 |
ashik.ali |
145 |
LOGGER.error("Error : ", profitMandiBusinessException);
|
| 21578 |
ashik.ali |
146 |
model.addAttribute("loginResponse", mvcResponseSender.createResponseString(profitMandiBusinessException.getCode(), false, "/error"));
|
|
|
147 |
return "response";
|
| 21561 |
ashik.ali |
148 |
}
|
| 21555 |
kshitij.so |
149 |
}
|
| 21574 |
ashik.ali |
150 |
|
| 22139 |
amit.gupta |
151 |
private void addCookiesToResponse(LoginDetails fofoDetails, HttpServletRequest request, HttpServletResponse response) {
|
|
|
152 |
List<String> roleNames = new ArrayList<>();
|
|
|
153 |
|
|
|
154 |
for(RoleType roleType : fofoDetails.getRoleTypes()) {
|
|
|
155 |
roleNames.add(roleType.toString());
|
|
|
156 |
}
|
|
|
157 |
Cookie cookieRoleNames = new Cookie(ProfitMandiConstants.ROLE_NAMES, String.join(",", roleNames));
|
|
|
158 |
cookieRoleNames.setDomain(request.getServerName());
|
| 22160 |
amit.gupta |
159 |
cookieRoleNames.setPath(request.getContextPath());
|
| 22139 |
amit.gupta |
160 |
|
|
|
161 |
Cookie cookieFofoId = new Cookie(ProfitMandiConstants.FOFO_ID, String.valueOf(fofoDetails.getFofoId()));
|
|
|
162 |
cookieFofoId.setDomain(request.getServerName());
|
| 22160 |
amit.gupta |
163 |
cookieFofoId.setPath(request.getContextPath());
|
| 22139 |
amit.gupta |
164 |
|
|
|
165 |
Cookie cookieEmailId = new Cookie(ProfitMandiConstants.EMAIL_ID, fofoDetails.getEmailId());
|
|
|
166 |
cookieEmailId.setDomain(request.getServerName());
|
| 22160 |
amit.gupta |
167 |
cookieEmailId.setPath(request.getContextPath());
|
| 22139 |
amit.gupta |
168 |
|
|
|
169 |
response.addCookie(cookieFofoId);
|
|
|
170 |
response.addCookie(cookieEmailId);
|
|
|
171 |
response.addCookie(cookieRoleNames);
|
|
|
172 |
}
|
|
|
173 |
|
| 22069 |
ashik.ali |
174 |
@RequestMapping(value = "/logout", method = RequestMethod.GET)
|
|
|
175 |
public String logout(HttpServletRequest request, @ModelAttribute("model") ModelMap model, HttpServletResponse response) throws Exception{
|
|
|
176 |
try{
|
|
|
177 |
cookiesProcessor.removeCookies(request, response);
|
|
|
178 |
LOGGER.info("Logout is successfull, should be redirect to /login");
|
| 22085 |
amit.gupta |
179 |
return "redirect:/login";
|
| 22927 |
ashik.ali |
180 |
}catch(ProfitMandiBusinessException profitMandiBusinessException){
|
| 22069 |
ashik.ali |
181 |
LOGGER.info("Error occured while removing requested cookies, should be redirect to /login");
|
| 22085 |
amit.gupta |
182 |
return "redirect:/login";
|
| 22069 |
ashik.ali |
183 |
}
|
|
|
184 |
}
|
|
|
185 |
|
|
|
186 |
|
|
|
187 |
|
| 21555 |
kshitij.so |
188 |
}
|