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