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