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