| 21561 |
ashik.ali |
1 |
package com.spice.profitmandi.web.controller;
|
| 21555 |
kshitij.so |
2 |
|
| 25008 |
amit.gupta |
3 |
import java.lang.reflect.Type;
|
| 23419 |
ashik.ali |
4 |
import java.time.LocalDateTime;
|
| 22139 |
amit.gupta |
5 |
import java.util.ArrayList;
|
| 25303 |
amit.gupta |
6 |
import java.util.Arrays;
|
| 25008 |
amit.gupta |
7 |
import java.util.HashMap;
|
| 22111 |
ashik.ali |
8 |
import java.util.HashSet;
|
|
|
9 |
import java.util.List;
|
| 25008 |
amit.gupta |
10 |
import java.util.Map;
|
| 22111 |
ashik.ali |
11 |
import java.util.Set;
|
| 24976 |
amit.gupta |
12 |
import java.util.stream.Collectors;
|
| 22069 |
ashik.ali |
13 |
|
| 21577 |
ashik.ali |
14 |
import javax.servlet.http.Cookie;
|
| 21561 |
ashik.ali |
15 |
import javax.servlet.http.HttpServletRequest;
|
| 21577 |
ashik.ali |
16 |
import javax.servlet.http.HttpServletResponse;
|
| 25303 |
amit.gupta |
17 |
import javax.transaction.Transactional;
|
| 21561 |
ashik.ali |
18 |
|
| 25009 |
amit.gupta |
19 |
import org.apache.http.NameValuePair;
|
|
|
20 |
import org.apache.http.client.utils.URLEncodedUtils;
|
|
|
21 |
import org.apache.http.message.BasicNameValuePair;
|
| 23784 |
ashik.ali |
22 |
import org.apache.logging.log4j.LogManager;
|
| 23568 |
govind |
23 |
import org.apache.logging.log4j.Logger;
|
| 21561 |
ashik.ali |
24 |
import org.springframework.beans.factory.annotation.Autowired;
|
| 22079 |
amit.gupta |
25 |
import org.springframework.beans.factory.annotation.Value;
|
| 21555 |
kshitij.so |
26 |
import org.springframework.stereotype.Controller;
|
| 21615 |
kshitij.so |
27 |
import org.springframework.ui.Model;
|
| 21555 |
kshitij.so |
28 |
import org.springframework.ui.ModelMap;
|
|
|
29 |
import org.springframework.web.bind.annotation.ModelAttribute;
|
| 24383 |
amit.gupta |
30 |
import org.springframework.web.bind.annotation.PostMapping;
|
| 24016 |
amit.gupta |
31 |
import org.springframework.web.bind.annotation.RequestBody;
|
| 21555 |
kshitij.so |
32 |
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
33 |
import org.springframework.web.bind.annotation.RequestMethod;
|
| 21561 |
ashik.ali |
34 |
import org.springframework.web.bind.annotation.RequestParam;
|
| 21555 |
kshitij.so |
35 |
|
| 25008 |
amit.gupta |
36 |
import com.google.gson.Gson;
|
|
|
37 |
import com.google.gson.reflect.TypeToken;
|
| 21561 |
ashik.ali |
38 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
| 24016 |
amit.gupta |
39 |
import com.spice.profitmandi.common.model.Location;
|
| 21561 |
ashik.ali |
40 |
import com.spice.profitmandi.common.model.ProfitMandiConstants;
|
| 25011 |
amit.gupta |
41 |
import com.spice.profitmandi.common.model.ProfitMandiResponse;
|
| 25008 |
amit.gupta |
42 |
import com.spice.profitmandi.common.web.client.RestClient;
|
| 24383 |
amit.gupta |
43 |
import com.spice.profitmandi.dao.entity.auth.AuthUser;
|
| 22111 |
ashik.ali |
44 |
import com.spice.profitmandi.dao.entity.dtr.Retailer;
|
| 23784 |
ashik.ali |
45 |
import com.spice.profitmandi.dao.entity.dtr.Role;
|
| 22111 |
ashik.ali |
46 |
import com.spice.profitmandi.dao.entity.dtr.User;
|
| 23271 |
ashik.ali |
47 |
import com.spice.profitmandi.dao.entity.dtr.UserAccount;
|
| 22111 |
ashik.ali |
48 |
import com.spice.profitmandi.dao.entity.dtr.UserRole;
|
| 24016 |
amit.gupta |
49 |
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
|
| 25303 |
amit.gupta |
50 |
import com.spice.profitmandi.dao.entity.user.Device;
|
| 22111 |
ashik.ali |
51 |
import com.spice.profitmandi.dao.enumuration.dtr.AccountType;
|
|
|
52 |
import com.spice.profitmandi.dao.enumuration.dtr.RoleType;
|
| 24383 |
amit.gupta |
53 |
import com.spice.profitmandi.dao.repository.auth.AuthRepository;
|
| 25303 |
amit.gupta |
54 |
import com.spice.profitmandi.dao.repository.catalog.DeviceRepository;
|
| 24016 |
amit.gupta |
55 |
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
|
| 22111 |
ashik.ali |
56 |
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
|
| 23784 |
ashik.ali |
57 |
import com.spice.profitmandi.dao.repository.dtr.RoleRepository;
|
| 22111 |
ashik.ali |
58 |
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
|
|
|
59 |
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
|
|
|
60 |
import com.spice.profitmandi.dao.repository.dtr.UserRoleRepository;
|
| 24383 |
amit.gupta |
61 |
import com.spice.profitmandi.service.AuthService;
|
| 24976 |
amit.gupta |
62 |
import com.spice.profitmandi.service.authentication.RoleManager;
|
|
|
63 |
import com.spice.profitmandi.service.user.RetailerService;
|
| 22139 |
amit.gupta |
64 |
import com.spice.profitmandi.web.model.LoginDetails;
|
| 22069 |
ashik.ali |
65 |
import com.spice.profitmandi.web.util.CookiesProcessor;
|
| 22111 |
ashik.ali |
66 |
import com.spice.profitmandi.web.util.GoogleTokenUtil;
|
| 21574 |
ashik.ali |
67 |
import com.spice.profitmandi.web.util.MVCResponseSender;
|
| 21561 |
ashik.ali |
68 |
|
| 21555 |
kshitij.so |
69 |
@Controller
|
| 22148 |
amit.gupta |
70 |
@Transactional
|
| 21555 |
kshitij.so |
71 |
public class LoginController {
|
|
|
72 |
|
| 23568 |
govind |
73 |
private static final Logger LOGGER = LogManager.getLogger(LoginController.class);
|
| 24016 |
amit.gupta |
74 |
|
| 21561 |
ashik.ali |
75 |
@Autowired
|
| 22927 |
ashik.ali |
76 |
private GoogleTokenUtil googleTokenUtil;
|
| 24016 |
amit.gupta |
77 |
|
| 21574 |
ashik.ali |
78 |
@Autowired
|
| 24976 |
amit.gupta |
79 |
private RoleManager roleManager;
|
| 26449 |
amit.gupta |
80 |
|
| 25303 |
amit.gupta |
81 |
@Autowired
|
|
|
82 |
private DeviceRepository deviceRepository;
|
| 24984 |
amit.gupta |
83 |
|
| 24976 |
amit.gupta |
84 |
@Autowired
|
| 22927 |
ashik.ali |
85 |
private RetailerRepository retailerRepository;
|
| 24016 |
amit.gupta |
86 |
|
| 22111 |
ashik.ali |
87 |
@Autowired
|
| 24976 |
amit.gupta |
88 |
private RetailerService retailerService;
|
| 24984 |
amit.gupta |
89 |
|
| 24976 |
amit.gupta |
90 |
@Autowired
|
| 22927 |
ashik.ali |
91 |
private UserRepository userRepository;
|
| 24016 |
amit.gupta |
92 |
|
| 22111 |
ashik.ali |
93 |
@Autowired
|
| 22927 |
ashik.ali |
94 |
private UserAccountRepository userAccountRepository;
|
| 24016 |
amit.gupta |
95 |
|
| 22111 |
ashik.ali |
96 |
@Autowired
|
| 22927 |
ashik.ali |
97 |
private UserRoleRepository userRoleRepository;
|
| 24016 |
amit.gupta |
98 |
|
| 22111 |
ashik.ali |
99 |
@Autowired
|
| 23784 |
ashik.ali |
100 |
private RoleRepository roleRepository;
|
| 24016 |
amit.gupta |
101 |
|
| 23784 |
ashik.ali |
102 |
@Autowired
|
| 24016 |
amit.gupta |
103 |
private FofoStoreRepository fofoStoreRepository;
|
|
|
104 |
|
|
|
105 |
@Autowired
|
| 22927 |
ashik.ali |
106 |
private MVCResponseSender mvcResponseSender;
|
| 25243 |
amit.gupta |
107 |
|
| 25008 |
amit.gupta |
108 |
@Autowired
|
|
|
109 |
private RestClient restClient;
|
| 24016 |
amit.gupta |
110 |
|
| 21578 |
ashik.ali |
111 |
@Autowired
|
| 22927 |
ashik.ali |
112 |
private CookiesProcessor cookiesProcessor;
|
| 22079 |
amit.gupta |
113 |
|
|
|
114 |
@Value("${google.api.key}")
|
|
|
115 |
private String googleApiKey;
|
| 28246 |
amit.gupta |
116 |
|
|
|
117 |
@Value("${app.token.url}")
|
|
|
118 |
private String appTokenUrl;
|
|
|
119 |
|
|
|
120 |
@Value("${admin.token}")
|
|
|
121 |
private String adminToken;
|
| 24016 |
amit.gupta |
122 |
|
| 24383 |
amit.gupta |
123 |
@Autowired
|
|
|
124 |
private AuthService authService;
|
|
|
125 |
|
|
|
126 |
@Autowired
|
|
|
127 |
private AuthRepository authRepository;
|
|
|
128 |
|
| 21555 |
kshitij.so |
129 |
@RequestMapping(value = "/login", method = RequestMethod.GET)
|
| 25544 |
amit.gupta |
130 |
public String loginPage(HttpServletRequest request, HttpServletResponse response, Model model) throws Exception {
|
| 22088 |
amit.gupta |
131 |
LOGGER.info("Context Path is {}", request.getContextPath());
|
| 24016 |
amit.gupta |
132 |
try {
|
| 23784 |
ashik.ali |
133 |
LoginDetails fofoDetails = cookiesProcessor.getCookiesObject(request);
|
| 22139 |
amit.gupta |
134 |
LOGGER.info("Request session is already exist, should be redirect to as per roles assigned");
|
| 23784 |
ashik.ali |
135 |
String redirectUrl = null;
|
| 24016 |
amit.gupta |
136 |
|
| 23784 |
ashik.ali |
137 |
Role roleRetailer = roleRepository.selectByName(RoleType.RETAILER.toString());
|
| 26449 |
amit.gupta |
138 |
Role roleFofo = roleRepository.selectByName(RoleType.FOFO.toString());
|
|
|
139 |
Role roleFofoAdmin = roleRepository.selectByName(RoleType.FOFO_ADMIN.toString());
|
|
|
140 |
if (fofoDetails.getRoleIds().contains(roleFofoAdmin.getId())) {
|
| 23784 |
ashik.ali |
141 |
redirectUrl = "/dashboard";
|
| 26449 |
amit.gupta |
142 |
} else if (fofoDetails.getRoleIds().contains(roleRetailer.getId())
|
|
|
143 |
&& fofoDetails.getRoleIds().contains(roleFofo.getId())) {
|
|
|
144 |
FofoStore fofoStore = fofoStoreRepository.selectByRetailerId(fofoDetails.getFofoId());
|
|
|
145 |
if (!fofoStore.isActive()) {
|
|
|
146 |
cookiesProcessor.removeCookies(request, response);
|
|
|
147 |
throw new ProfitMandiBusinessException("", "", "");
|
|
|
148 |
}
|
| 26450 |
amit.gupta |
149 |
redirectUrl = "/dashboard";
|
| 26449 |
amit.gupta |
150 |
|
| 23784 |
ashik.ali |
151 |
} else {
|
|
|
152 |
redirectUrl = "/login";
|
|
|
153 |
}
|
|
|
154 |
return "redirect:" + redirectUrl;
|
| 24016 |
amit.gupta |
155 |
} catch (ProfitMandiBusinessException profitMandiBusinessException) {
|
| 22079 |
amit.gupta |
156 |
model.addAttribute("googleApiKey", googleApiKey);
|
| 22086 |
amit.gupta |
157 |
model.addAttribute("appContextPath", request.getContextPath());
|
| 21577 |
ashik.ali |
158 |
return "login";
|
| 21574 |
ashik.ali |
159 |
}
|
| 21555 |
kshitij.so |
160 |
}
|
| 24016 |
amit.gupta |
161 |
|
| 22155 |
amit.gupta |
162 |
@RequestMapping(value = "/", method = RequestMethod.GET)
|
| 24016 |
amit.gupta |
163 |
public String home() {
|
| 22860 |
ashik.ali |
164 |
return "redirect:/login";
|
| 22155 |
amit.gupta |
165 |
}
|
| 25243 |
amit.gupta |
166 |
|
| 25008 |
amit.gupta |
167 |
@RequestMapping(value = "/mobileapp", method = RequestMethod.GET)
|
| 25275 |
amit.gupta |
168 |
public String mobileApp(HttpServletRequest request, Model model, @RequestParam(defaultValue = "") String emailId)
|
|
|
169 |
throws Exception {
|
| 25008 |
amit.gupta |
170 |
LoginDetails fofoDetails = cookiesProcessor.getCookiesObject(request);
|
| 25243 |
amit.gupta |
171 |
if (roleManager.isAdmin(fofoDetails.getRoleIds())) {
|
| 25275 |
amit.gupta |
172 |
if (emailId.equals("")) {
|
|
|
173 |
model.addAttribute("authToken", getToken(fofoDetails.getEmailId()));
|
|
|
174 |
} else {
|
|
|
175 |
model.addAttribute("authToken", getToken(emailId));
|
| 25303 |
amit.gupta |
176 |
model.addAttribute("androidId", getAndroidId(emailId));
|
| 25275 |
amit.gupta |
177 |
}
|
| 26394 |
amit.gupta |
178 |
} else {
|
| 26395 |
amit.gupta |
179 |
emailId = fofoDetails.getEmailId();
|
|
|
180 |
model.addAttribute("authToken", getToken(emailId));
|
|
|
181 |
model.addAttribute("androidId", getAndroidId(emailId));
|
| 25008 |
amit.gupta |
182 |
}
|
|
|
183 |
return "mobileapp";
|
|
|
184 |
}
|
| 24016 |
amit.gupta |
185 |
|
| 25303 |
amit.gupta |
186 |
private String getAndroidId(String emailId) throws Exception {
|
|
|
187 |
User user = userRepository.selectByEmailId(emailId);
|
| 26449 |
amit.gupta |
188 |
List<Device> devices = deviceRepository.selectByUserIdAndModifiedTimestamp(Arrays.asList(user.getId()),
|
|
|
189 |
LocalDateTime.now().minusYears(2), LocalDateTime.now());
|
|
|
190 |
if (devices.size() > 0) {
|
| 26147 |
amit.gupta |
191 |
Device device = devices.get(0);
|
|
|
192 |
return device.getAndroidId();
|
|
|
193 |
} else {
|
|
|
194 |
return "";
|
|
|
195 |
}
|
| 25303 |
amit.gupta |
196 |
}
|
|
|
197 |
|
| 25008 |
amit.gupta |
198 |
private String getToken(String emailId) throws Exception {
|
| 25009 |
amit.gupta |
199 |
List<NameValuePair> nameValuePairs = new ArrayList<>();
|
| 28246 |
amit.gupta |
200 |
nameValuePairs.add(new BasicNameValuePair("adminToken", this.adminToken));
|
| 25011 |
amit.gupta |
201 |
nameValuePairs.add(new BasicNameValuePair("emailId", emailId));
|
| 25243 |
amit.gupta |
202 |
String queryString = URLEncodedUtils.format(nameValuePairs, "UTF-8");
|
| 25452 |
amit.gupta |
203 |
String response = restClient.post("http://app.smartdukaan.com:8080/profitmandi-web/user/admin?" + queryString,
|
| 25243 |
amit.gupta |
204 |
new HashMap<>(), new HashMap<>());
|
|
|
205 |
Type t = new TypeToken<ProfitMandiResponse<Map<String, Object>>>() {
|
|
|
206 |
}.getType();
|
|
|
207 |
ProfitMandiResponse<Map<String, Object>> apiResponse = new Gson().fromJson(response, t);
|
|
|
208 |
if (apiResponse.getStatusCode().equals("200")) {
|
|
|
209 |
return (new StringBuilder((String) apiResponse.getResponse().get("token")).reverse().toString());
|
| 25008 |
amit.gupta |
210 |
} else {
|
| 25009 |
amit.gupta |
211 |
throw new Exception("Unauthorised access");
|
| 25008 |
amit.gupta |
212 |
}
|
| 25243 |
amit.gupta |
213 |
|
| 25008 |
amit.gupta |
214 |
}
|
| 25243 |
amit.gupta |
215 |
|
| 24976 |
amit.gupta |
216 |
@RequestMapping(value = "/login-as-partner", method = RequestMethod.GET)
|
|
|
217 |
public String adminLogin(HttpServletRequest request, Model model, HttpServletResponse response,
|
|
|
218 |
@RequestParam int fofoId) throws Exception {
|
|
|
219 |
LoginDetails fofoDetails = cookiesProcessor.getCookiesObject(request);
|
| 24984 |
amit.gupta |
220 |
if (roleManager.isAdmin(fofoDetails.getRoleIds())) {
|
|
|
221 |
int userId = userAccountRepository.selectUserIdByRetailerId(fofoId);
|
|
|
222 |
User user = userRepository.selectById(userId);
|
| 25243 |
amit.gupta |
223 |
Set<Integer> roleIds = userRoleRepository.selectByUserId(userId).stream().map(x -> x.getRoleId())
|
|
|
224 |
.collect(Collectors.toSet());
|
| 24976 |
amit.gupta |
225 |
LoginDetails newFofoDetails = new LoginDetails();
|
|
|
226 |
newFofoDetails.setFofoId(fofoId);
|
|
|
227 |
newFofoDetails.setRoleIds(roleIds);
|
| 24984 |
amit.gupta |
228 |
newFofoDetails.setEmailId(user.getEmailId());
|
| 24981 |
amit.gupta |
229 |
model.addAttribute("response", true);
|
| 24983 |
amit.gupta |
230 |
this.addCookiesToResponse(newFofoDetails, request, response);
|
| 24976 |
amit.gupta |
231 |
} else {
|
|
|
232 |
throw new ProfitMandiBusinessException("", "", "");
|
|
|
233 |
}
|
| 24981 |
amit.gupta |
234 |
return "response";
|
| 24976 |
amit.gupta |
235 |
}
|
| 27229 |
amit.gupta |
236 |
|
|
|
237 |
@RequestMapping(value = "/login-as-partner-readonly", method = RequestMethod.GET)
|
|
|
238 |
public String readonlyLogin(HttpServletRequest request, Model model, HttpServletResponse response,
|
|
|
239 |
@RequestParam int fofoId) throws Exception {
|
|
|
240 |
LoginDetails fofoDetails = cookiesProcessor.getCookiesObject(request);
|
|
|
241 |
if (roleManager.isAdmin(fofoDetails.getRoleIds())) {
|
|
|
242 |
int userId = userAccountRepository.selectUserIdByRetailerId(fofoId);
|
|
|
243 |
User user = userRepository.selectById(userId);
|
|
|
244 |
Set<Integer> roleIds = userRoleRepository.selectByUserId(userId).stream().map(x -> x.getRoleId())
|
|
|
245 |
.collect(Collectors.toSet());
|
|
|
246 |
LoginDetails newFofoDetails = new LoginDetails();
|
|
|
247 |
newFofoDetails.setFofoId(fofoId);
|
|
|
248 |
newFofoDetails.setRoleIds(roleIds);
|
|
|
249 |
newFofoDetails.setEmailId(user.getEmailId());
|
|
|
250 |
newFofoDetails.setReadOnly(true);
|
|
|
251 |
model.addAttribute("response", true);
|
|
|
252 |
this.addCookiesToResponse(newFofoDetails, request, response);
|
|
|
253 |
} else {
|
|
|
254 |
throw new ProfitMandiBusinessException("", "", "");
|
|
|
255 |
}
|
|
|
256 |
return "response";
|
|
|
257 |
}
|
| 24976 |
amit.gupta |
258 |
|
| 21555 |
kshitij.so |
259 |
@RequestMapping(value = "/login", method = RequestMethod.POST)
|
| 24016 |
amit.gupta |
260 |
public String login(HttpServletRequest request, HttpServletResponse response,
|
| 24383 |
amit.gupta |
261 |
@RequestParam(name = ProfitMandiConstants.TOKEN) String token,
|
|
|
262 |
@RequestParam(name = ProfitMandiConstants.EMAIL_ID_OR_MOBILE_NUMBER, defaultValue = "") String emailIdOrMobileNumber,
|
|
|
263 |
@RequestParam(name = "password", defaultValue = "") String password, Model model) throws Exception {
|
|
|
264 |
|
| 22139 |
amit.gupta |
265 |
LoginDetails fofoDetails = new LoginDetails();
|
| 23784 |
ashik.ali |
266 |
Set<Integer> roleIds = new HashSet<>();
|
|
|
267 |
fofoDetails.setRoleIds(roleIds);
|
| 24383 |
amit.gupta |
268 |
String emailId = null;
|
| 24976 |
amit.gupta |
269 |
String name = null;
|
| 24016 |
amit.gupta |
270 |
try {
|
|
|
271 |
// if role is retailer then FOFO_ID is retailerId else it is userid as normal
|
|
|
272 |
// user's wont have retailer id.
|
| 24383 |
amit.gupta |
273 |
if (token == "") {
|
|
|
274 |
if (authService.authenticate(emailIdOrMobileNumber, password)) {
|
|
|
275 |
AuthUser authUser = authRepository.selectByEmailOrMobile(emailIdOrMobileNumber);
|
| 24976 |
amit.gupta |
276 |
if (authUser == null) {
|
|
|
277 |
throw new ProfitMandiBusinessException("Authentication", "Email or Mobile",
|
|
|
278 |
"Invalid Email Or Mobile");
|
| 24383 |
amit.gupta |
279 |
}
|
| 26026 |
amit.gupta |
280 |
emailId = authUser.getEmailId();
|
| 24976 |
amit.gupta |
281 |
name = authUser.getFirstName() + " " + authUser.getLastName();
|
| 24383 |
amit.gupta |
282 |
authUser.setLastLoginTimestamp(LocalDateTime.now());
|
|
|
283 |
authRepository.persist(authUser);
|
|
|
284 |
}
|
|
|
285 |
|
|
|
286 |
} else {
|
| 25243 |
amit.gupta |
287 |
try {
|
|
|
288 |
emailId = googleTokenUtil.getEmailId(token);
|
| 25275 |
amit.gupta |
289 |
} catch (Exception e) {
|
|
|
290 |
// in case we cannot connect to google server.
|
| 25243 |
amit.gupta |
291 |
emailId = emailIdOrMobileNumber;
|
|
|
292 |
}
|
| 24383 |
amit.gupta |
293 |
}
|
| 22139 |
amit.gupta |
294 |
fofoDetails.setEmailId(emailId);
|
|
|
295 |
fofoDetails.setFofoId(-1);
|
| 24016 |
amit.gupta |
296 |
// fofoDetails.setFofo(false);
|
| 22111 |
ashik.ali |
297 |
User user = null;
|
| 24016 |
amit.gupta |
298 |
try {
|
| 22111 |
ashik.ali |
299 |
user = userRepository.selectByEmailId(emailId);
|
| 24016 |
amit.gupta |
300 |
} catch (ProfitMandiBusinessException profitMandiBusinessException) {
|
| 23628 |
ashik.ali |
301 |
LOGGER.error("User not found with given emailId [{}]", emailId);
|
| 23203 |
ashik.ali |
302 |
}
|
| 24016 |
amit.gupta |
303 |
if (user == null) {
|
|
|
304 |
try {
|
| 23203 |
ashik.ali |
305 |
user = userRepository.selectBySecondryEmailId(emailId);
|
| 24016 |
amit.gupta |
306 |
} catch (ProfitMandiBusinessException profitMandiBusinessException) {
|
| 23203 |
ashik.ali |
307 |
LOGGER.error("User not found with given emailId", profitMandiBusinessException);
|
| 24527 |
amit.gupta |
308 |
model.addAttribute("response", mvcResponseSender.createResponseString("RTLR_OK_1002", true,
|
| 24976 |
amit.gupta |
309 |
request.getContextPath() + "/login", "Email"));
|
| 24527 |
amit.gupta |
310 |
return "response";
|
| 23203 |
ashik.ali |
311 |
}
|
|
|
312 |
}
|
| 24016 |
amit.gupta |
313 |
if (user != null) {
|
| 22139 |
amit.gupta |
314 |
fofoDetails.setFofoId(user.getId());
|
|
|
315 |
try {
|
|
|
316 |
List<UserRole> userRoles = userRoleRepository.selectByUserId(user.getId());
|
| 24016 |
amit.gupta |
317 |
for (int index = 0; index < userRoles.size(); index++) {
|
| 23784 |
ashik.ali |
318 |
roleIds.add(userRoles.get(index).getRoleId());
|
| 22139 |
amit.gupta |
319 |
}
|
| 23784 |
ashik.ali |
320 |
List<Role> roles = roleRepository.selectByIds(roleIds);
|
| 24016 |
amit.gupta |
321 |
for (Role role : roles) {
|
|
|
322 |
if (role.getName().equals(RoleType.RETAILER.toString())) {
|
|
|
323 |
UserAccount userAccounts = userAccountRepository.selectByUserIdType(user.getId(),
|
|
|
324 |
AccountType.saholic);
|
| 23784 |
ashik.ali |
325 |
Retailer retailer = retailerRepository.selectById(userAccounts.getAccountKey());
|
|
|
326 |
fofoDetails.setFofoId(retailer.getId());
|
| 24016 |
amit.gupta |
327 |
// fofoDetails.setFofo(retailer.isFofo());
|
| 23784 |
ashik.ali |
328 |
}
|
| 22166 |
amit.gupta |
329 |
}
|
| 24016 |
amit.gupta |
330 |
} catch (ProfitMandiBusinessException pmbe) {
|
| 22139 |
amit.gupta |
331 |
LOGGER.error("Data Inconsistent", pmbe);
|
|
|
332 |
}
|
| 22111 |
ashik.ali |
333 |
}
|
| 23784 |
ashik.ali |
334 |
String redirectUrl = null;
|
| 24016 |
amit.gupta |
335 |
|
| 25243 |
amit.gupta |
336 |
Role roleFofo = roleRepository.selectByName(RoleType.FOFO.toString());
|
|
|
337 |
Role roleFofoAdmin = roleRepository.selectByName(RoleType.FOFO_ADMIN.toString());
|
| 24016 |
amit.gupta |
338 |
|
| 25243 |
amit.gupta |
339 |
if (fofoDetails.getRoleIds().contains(roleFofo.getId())) {
|
| 23784 |
ashik.ali |
340 |
redirectUrl = "/dashboard";
|
| 25275 |
amit.gupta |
341 |
if (fofoDetails.getRoleIds().contains(roleFofo.getId())) {
|
|
|
342 |
|
| 25243 |
amit.gupta |
343 |
}
|
| 23784 |
ashik.ali |
344 |
} else {
|
|
|
345 |
redirectUrl = "/login";
|
|
|
346 |
}
|
| 24016 |
amit.gupta |
347 |
|
| 25243 |
amit.gupta |
348 |
if (redirectUrl.equals("/dashboard")) {
|
| 23419 |
ashik.ali |
349 |
user.setLoginTimestamp(LocalDateTime.now());
|
| 23173 |
ashik.ali |
350 |
this.addCookiesToResponse(fofoDetails, request, response);
|
| 24016 |
amit.gupta |
351 |
LOGGER.info("Requested token email_id is valid, user login to system, shoud be redirect to {}",
|
|
|
352 |
redirectUrl);
|
|
|
353 |
model.addAttribute("response", mvcResponseSender.createResponseString("RTLR_OK_1002", true,
|
| 24976 |
amit.gupta |
354 |
request.getContextPath() + redirectUrl, name));
|
| 24016 |
amit.gupta |
355 |
} else {
|
| 23173 |
ashik.ali |
356 |
LOGGER.error("Requested token email_id is not valid, please try to login");
|
| 25243 |
amit.gupta |
357 |
/**
|
| 25275 |
amit.gupta |
358 |
* TODO:Amit This redirect should be handle.
|
| 25243 |
amit.gupta |
359 |
*/
|
| 24016 |
amit.gupta |
360 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.EMAIL_ID, fofoDetails.getEmailId(),
|
|
|
361 |
"RTLR_1000");
|
| 23173 |
ashik.ali |
362 |
}
|
| 21578 |
ashik.ali |
363 |
return "response";
|
| 24016 |
amit.gupta |
364 |
} catch (ProfitMandiBusinessException profitMandiBusinessException) {
|
| 21568 |
ashik.ali |
365 |
LOGGER.error("Error : ", profitMandiBusinessException);
|
| 24016 |
amit.gupta |
366 |
model.addAttribute("response",
|
|
|
367 |
mvcResponseSender.createResponseString(profitMandiBusinessException.getCode(), false, "/error"));
|
| 21578 |
ashik.ali |
368 |
return "response";
|
| 26021 |
amit.gupta |
369 |
} catch (Exception e) {
|
| 26025 |
amit.gupta |
370 |
e.printStackTrace();
|
| 26021 |
amit.gupta |
371 |
model.addAttribute("response",
|
| 26449 |
amit.gupta |
372 |
mvcResponseSender.createResponseString(
|
|
|
373 |
"You are not authorised! " + "Please contact the team if you feel this is incorrect", false,
|
|
|
374 |
"/error"));
|
| 26021 |
amit.gupta |
375 |
return "response";
|
| 21561 |
ashik.ali |
376 |
}
|
| 21555 |
kshitij.so |
377 |
}
|
| 24976 |
amit.gupta |
378 |
|
| 24383 |
amit.gupta |
379 |
@PostMapping(value = "/forgetPassword")
|
| 24976 |
amit.gupta |
380 |
public String forgetPasswordPage(
|
|
|
381 |
@RequestParam(name = ProfitMandiConstants.EMAIL_ID, defaultValue = "") String emailId, Model model)
|
|
|
382 |
throws Exception {
|
| 24383 |
amit.gupta |
383 |
LOGGER.info(emailId);
|
|
|
384 |
authService.resetPassword(emailId);
|
|
|
385 |
model.addAttribute("response", mvcResponseSender.createResponseString(true));
|
|
|
386 |
LOGGER.info("completed");
|
|
|
387 |
return "response";
|
|
|
388 |
}
|
| 24016 |
amit.gupta |
389 |
|
|
|
390 |
private void addCookiesToResponse(LoginDetails fofoDetails, HttpServletRequest request,
|
|
|
391 |
HttpServletResponse response) {
|
| 23784 |
ashik.ali |
392 |
List<String> roleIds = new ArrayList<>();
|
| 24016 |
amit.gupta |
393 |
|
|
|
394 |
for (int roleId : fofoDetails.getRoleIds()) {
|
| 23784 |
ashik.ali |
395 |
roleIds.add(String.valueOf(roleId));
|
| 22139 |
amit.gupta |
396 |
}
|
| 26743 |
amit.gupta |
397 |
Cookie cookieRoleIds = new Cookie(ProfitMandiConstants.ROLE_IDS, String.join("-", roleIds));
|
| 23784 |
ashik.ali |
398 |
cookieRoleIds.setDomain(request.getServerName());
|
|
|
399 |
cookieRoleIds.setPath(request.getContextPath());
|
| 24016 |
amit.gupta |
400 |
|
| 22139 |
amit.gupta |
401 |
Cookie cookieFofoId = new Cookie(ProfitMandiConstants.FOFO_ID, String.valueOf(fofoDetails.getFofoId()));
|
|
|
402 |
cookieFofoId.setDomain(request.getServerName());
|
| 22160 |
amit.gupta |
403 |
cookieFofoId.setPath(request.getContextPath());
|
| 24016 |
amit.gupta |
404 |
|
| 22139 |
amit.gupta |
405 |
Cookie cookieEmailId = new Cookie(ProfitMandiConstants.EMAIL_ID, fofoDetails.getEmailId());
|
|
|
406 |
cookieEmailId.setDomain(request.getServerName());
|
| 22160 |
amit.gupta |
407 |
cookieEmailId.setPath(request.getContextPath());
|
| 27234 |
amit.gupta |
408 |
|
|
|
409 |
Cookie cookieReadOnly = new Cookie(ProfitMandiConstants.READONLY_KEY, fofoDetails.isReadOnly() + "");
|
|
|
410 |
cookieReadOnly.setDomain(request.getServerName());
|
|
|
411 |
cookieReadOnly.setPath(request.getContextPath());
|
| 24984 |
amit.gupta |
412 |
|
| 22139 |
amit.gupta |
413 |
response.addCookie(cookieFofoId);
|
|
|
414 |
response.addCookie(cookieEmailId);
|
| 23784 |
ashik.ali |
415 |
response.addCookie(cookieRoleIds);
|
| 27234 |
amit.gupta |
416 |
response.addCookie(cookieReadOnly);
|
| 22139 |
amit.gupta |
417 |
}
|
| 24016 |
amit.gupta |
418 |
|
| 22069 |
ashik.ali |
419 |
@RequestMapping(value = "/logout", method = RequestMethod.GET)
|
| 24016 |
amit.gupta |
420 |
public String logout(HttpServletRequest request, @ModelAttribute("model") ModelMap model,
|
|
|
421 |
HttpServletResponse response) throws Exception {
|
|
|
422 |
try {
|
| 23419 |
ashik.ali |
423 |
LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
|
|
|
424 |
User user = null;
|
| 24016 |
amit.gupta |
425 |
try {
|
| 23419 |
ashik.ali |
426 |
user = userRepository.selectByEmailId(loginDetails.getEmailId());
|
| 24016 |
amit.gupta |
427 |
} catch (ProfitMandiBusinessException profitMandiBusinessException) {
|
| 23419 |
ashik.ali |
428 |
LOGGER.error("User not found with given emailId", profitMandiBusinessException);
|
|
|
429 |
}
|
| 24016 |
amit.gupta |
430 |
if (user == null) {
|
| 23419 |
ashik.ali |
431 |
user = userRepository.selectBySecondryEmailId(loginDetails.getEmailId());
|
|
|
432 |
}
|
|
|
433 |
user.setLogoutTimestamp(LocalDateTime.now());
|
|
|
434 |
userRepository.persist(user);
|
| 22069 |
ashik.ali |
435 |
cookiesProcessor.removeCookies(request, response);
|
|
|
436 |
LOGGER.info("Logout is successfull, should be redirect to /login");
|
| 22085 |
amit.gupta |
437 |
return "redirect:/login";
|
| 24016 |
amit.gupta |
438 |
} catch (ProfitMandiBusinessException profitMandiBusinessException) {
|
| 22069 |
ashik.ali |
439 |
LOGGER.info("Error occured while removing requested cookies, should be redirect to /login");
|
| 22085 |
amit.gupta |
440 |
return "redirect:/login";
|
| 22069 |
ashik.ali |
441 |
}
|
|
|
442 |
}
|
| 24016 |
amit.gupta |
443 |
|
|
|
444 |
@RequestMapping(value = "/partner/location", method = RequestMethod.PUT)
|
|
|
445 |
public String setLocation(HttpServletRequest request, Model model, @RequestBody Location location)
|
|
|
446 |
throws Exception {
|
|
|
447 |
LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
|
|
|
448 |
boolean response = true;
|
| 24976 |
amit.gupta |
449 |
try {
|
|
|
450 |
FofoStore fs = fofoStoreRepository.selectByRetailerId(loginDetails.getFofoId());
|
|
|
451 |
if (fs.getLatitude() == null) {
|
|
|
452 |
fs.setLatitude(location.getLatitude());
|
|
|
453 |
fs.setLongitude(location.getLongitude());
|
|
|
454 |
fofoStoreRepository.persist(fs);
|
|
|
455 |
}
|
|
|
456 |
model.addAttribute("response", response);
|
|
|
457 |
} catch (Exception e) {
|
| 24383 |
amit.gupta |
458 |
LOGGER.error("FofoStore Code not found of fofoId {}", loginDetails.getFofoId());
|
|
|
459 |
}
|
| 24016 |
amit.gupta |
460 |
return "response";
|
|
|
461 |
}
|
| 21555 |
kshitij.so |
462 |
}
|