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