Subversion Repositories SmartDukaan

Rev

Rev 34300 | Rev 34815 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 34300 Rev 34785
Line 17... Line 17...
17
import com.spice.profitmandi.dao.repository.catalog.DeviceRepository;
17
import com.spice.profitmandi.dao.repository.catalog.DeviceRepository;
18
import com.spice.profitmandi.dao.repository.dtr.*;
18
import com.spice.profitmandi.dao.repository.dtr.*;
19
import com.spice.profitmandi.service.AuthService;
19
import com.spice.profitmandi.service.AuthService;
20
import com.spice.profitmandi.service.authentication.RoleManager;
20
import com.spice.profitmandi.service.authentication.RoleManager;
21
import com.spice.profitmandi.service.user.RetailerService;
21
import com.spice.profitmandi.service.user.RetailerService;
-
 
22
import com.spice.profitmandi.service.user.UserService;
22
import com.spice.profitmandi.web.model.LoginDetails;
23
import com.spice.profitmandi.web.model.LoginDetails;
23
import com.spice.profitmandi.web.util.CookiesProcessor;
24
import com.spice.profitmandi.web.util.CookiesProcessor;
24
import com.spice.profitmandi.web.util.GoogleTokenUtil;
25
import com.spice.profitmandi.web.util.GoogleTokenUtil;
25
import com.spice.profitmandi.web.util.MVCResponseSender;
26
import com.spice.profitmandi.web.util.MVCResponseSender;
26
import org.apache.commons.lang.StringUtils;
27
import org.apache.commons.lang.StringUtils;
Line 104... Line 105...
104
 
105
 
105
    @Autowired
106
    @Autowired
106
    private AuthService authService;
107
    private AuthService authService;
107
 
108
 
108
    @Autowired
109
    @Autowired
-
 
110
    private UserService userService;
-
 
111
 
-
 
112
    @Autowired
109
    private AuthRepository authRepository;
113
    private AuthRepository authRepository;
110
 
114
 
111
    @RequestMapping(value = "/login", method = RequestMethod.GET)
115
    @RequestMapping(value = "/login", method = RequestMethod.GET)
112
    public String loginPage(HttpServletRequest request, HttpServletResponse response, Model model) throws Exception {
116
    public String loginPage(HttpServletRequest request, HttpServletResponse response, Model model) throws Exception {
113
        LOGGER.info("Context Path is {}", request.getContextPath());
117
        LOGGER.info("Context Path is {}", request.getContextPath());
Line 250... Line 254...
250
        LoginDetails fofoDetails = new LoginDetails();
254
        LoginDetails fofoDetails = new LoginDetails();
251
        Set<Integer> roleIds = new HashSet<>();
255
        Set<Integer> roleIds = new HashSet<>();
252
        fofoDetails.setRoleIds(roleIds);
256
        fofoDetails.setRoleIds(roleIds);
253
        String emailId = null;
257
        String emailId = null;
254
        String name = null;
258
        String name = null;
-
 
259
        User user = null;
255
        try {
260
        try {
256
            // if role is retailer then FOFO_ID is retailerId else it is userid as normal
261
            // if role is retailer then FOFO_ID is retailerId else it is userid as normal
257
            // user's wont have retailer id.
262
            // user's wont have retailer id.
258
            if (StringUtils.isEmpty(token)) {
263
            if (StringUtils.isEmpty(token)) {
259
                if (authService.authenticate(emailIdOrMobileNumber, password)) {
264
                if (authService.authenticate(emailIdOrMobileNumber, password)) {
260
                    AuthUser authUser = authRepository.selectByEmailOrMobile(emailIdOrMobileNumber);
265
                    AuthUser authUser = authRepository.selectByEmailOrMobile(emailIdOrMobileNumber);
261
                    if (authUser == null) {
266
                    if (authUser == null) {
262
                        throw new ProfitMandiBusinessException("Authentication", "Email or Mobile",
267
                        throw new ProfitMandiBusinessException("Authentication", "Email or Mobile", "Invalid Email Or Mobile");
263
                                "Invalid Email Or Mobile");
-
 
264
                    }
268
                    }
265
                    emailId = authUser.getEmailId();
269
                    emailId = authUser.getEmailId();
266
                    name = authUser.getFirstName() + " " + authUser.getLastName();
270
                    name = authUser.getFirstName() + " " + authUser.getLastName();
267
                    authUser.setLastLoginTimestamp(LocalDateTime.now());
271
                    authUser.setLastLoginTimestamp(LocalDateTime.now());
-
 
272
                } else {
-
 
273
                    // TODO: Add login with email and password
-
 
274
                    user = userService.authenticate(emailIdOrMobileNumber, password);
-
 
275
                    if (user != null) {
-
 
276
                        emailId = user.getEmailId();
-
 
277
                    } else {
-
 
278
                        LOGGER.error("User not found with given emailIdOrMobileNumber [{}]", emailIdOrMobileNumber);
-
 
279
                        model.addAttribute("response1", mvcResponseSender.createResponseString("RTLR_OK_1002", true,
-
 
280
                                request.getContextPath() + "/login", "Email"));
-
 
281
                        return "response";
-
 
282
                    }
268
                }
283
                }
269
 
-
 
270
            } else {
284
            } else {
271
                try {
285
                try {
272
                    emailId = googleTokenUtil.getEmailId(token);
286
                    emailId = googleTokenUtil.getEmailId(token);
273
                } catch (Exception e) {
287
                } catch (Exception e) {
274
                    // in case we cannot connect to google server.
288
                    // in case we cannot connect to google server.
275
                    emailId = emailIdOrMobileNumber;
289
                    emailId = emailIdOrMobileNumber;
276
                }
290
                }
277
            }
291
            }
278
            fofoDetails.setEmailId(emailId);
292
            fofoDetails.setEmailId(emailId);
279
            fofoDetails.setFofoId(-1);
293
            fofoDetails.setFofoId(-1);
280
            // fofoDetails.setFofo(false);
-
 
281
            User user = null;
-
 
282
            try {
-
 
283
                user = userRepository.selectByEmailId(emailId);
-
 
284
            } catch (ProfitMandiBusinessException profitMandiBusinessException) {
-
 
285
                LOGGER.error("User not found with given emailId [{}]", emailId);
-
 
286
            }
294
 
287
            if (user == null) {
295
            if (user == null) {
288
                try {
296
                try {
289
                    user = userRepository.selectBySecondryEmailId(emailId);
297
                    user = userRepository.selectByEmailId(emailId);
290
                } catch (ProfitMandiBusinessException profitMandiBusinessException) {
298
                } catch (ProfitMandiBusinessException profitMandiBusinessException) {
-
 
299
                    LOGGER.error("User not found with given emailId [{}]", emailId);
-
 
300
                    try {
-
 
301
                        user = userRepository.selectBySecondryEmailId(emailId);
-
 
302
                    } catch (ProfitMandiBusinessException exception) {
291
                    LOGGER.error("User not found with given emailId", profitMandiBusinessException);
303
                        LOGGER.error("User not found with given emailId", exception);
292
                    model.addAttribute("response1", mvcResponseSender.createResponseString("RTLR_OK_1002", true,
304
                        model.addAttribute("response1", mvcResponseSender.createResponseString("RTLR_OK_1002", true,
293
                            request.getContextPath() + "/login", "Email"));
305
                                request.getContextPath() + "/login", "Email"));
294
                    return "response";
306
                        return "response";
-
 
307
                    }
295
                }
308
                }
296
            }
309
            }
-
 
310
 
297
            if (user != null) {
311
            if (user != null) {
298
                fofoDetails.setFofoId(user.getId());
312
                fofoDetails.setFofoId(user.getId());
299
                try {
313
                try {
300
                    List<UserRole> userRoles = userRoleRepository.selectByUserId(user.getId());
314
                    List<UserRole> userRoles = userRoleRepository.selectByUserId(user.getId());
301
                    for (int index = 0; index < userRoles.size(); index++) {
315
                    for (UserRole userRole : userRoles) {
302
                        roleIds.add(userRoles.get(index).getRoleId());
316
                        roleIds.add(userRole.getRoleId());
303
                    }
317
                    }
304
                    List<Role> roles = roleRepository.selectByIds(roleIds);
318
                    List<Role> roles = roleRepository.selectByIds(roleIds);
305
                    for (Role role : roles) {
319
                    for (Role role : roles) {
306
                        if (role.getName().equals(RoleType.RETAILER.toString())) {
320
                        if (role.getName().equals(RoleType.RETAILER.toString())) {
307
                            UserAccount userAccounts = userAccountRepository.selectByUserIdType(user.getId(),
321
                            UserAccount userAccounts = userAccountRepository.selectByUserIdType(user.getId(), AccountType.saholic);
308
                                    AccountType.saholic);
-
 
309
                            Retailer retailer = retailerRepository.selectById(userAccounts.getAccountKey());
322
                            Retailer retailer = retailerRepository.selectById(userAccounts.getAccountKey());
310
                            fofoDetails.setFofoId(retailer.getId());
323
                            fofoDetails.setFofoId(retailer.getId());
311
                            // fofoDetails.setFofo(retailer.isFofo());
-
 
312
                        }
324
                        }
313
                    }
325
                    }
314
                } catch (ProfitMandiBusinessException pmbe) {
326
                } catch (ProfitMandiBusinessException pmbe) {
315
                    LOGGER.error("Data Inconsistent", pmbe);
327
                    LOGGER.error("Data Inconsistent", pmbe);
316
                }
328
                }
Line 320... Line 332...
320
            Role roleFofo = roleRepository.selectByName(RoleType.FOFO.toString());
332
            Role roleFofo = roleRepository.selectByName(RoleType.FOFO.toString());
321
            Role roleFofoAdmin = roleRepository.selectByName(RoleType.FOFO_ADMIN.toString());
333
            Role roleFofoAdmin = roleRepository.selectByName(RoleType.FOFO_ADMIN.toString());
322
 
334
 
323
            if (fofoDetails.getRoleIds().contains(roleFofo.getId())) {
335
            if (fofoDetails.getRoleIds().contains(roleFofo.getId())) {
324
                redirectUrl = "/dashboard";
336
                redirectUrl = "/dashboard";
325
                if (fofoDetails.getRoleIds().contains(roleFofo.getId())) {
-
 
326
 
-
 
327
                }
-
 
328
            } else {
337
            } else {
329
                redirectUrl = "/login";
338
                redirectUrl = "/login";
330
            }
339
            }
331
 
340
 
332
            if (redirectUrl.equals("/dashboard")) {
341
            if (redirectUrl.equals("/dashboard")) {
333
                user.setLoginTimestamp(LocalDateTime.now());
342
                user.setLoginTimestamp(LocalDateTime.now());
334
                this.addCookiesToResponse(fofoDetails, request, response);
343
                this.addCookiesToResponse(fofoDetails, request, response);
335
                LOGGER.info("Requested token email_id is valid, user login to system, shoud be redirect to {}",
344
                LOGGER.info("Requested token email_id is valid, user login to system, shoud be redirect to {}", redirectUrl);
336
                        redirectUrl);
-
 
337
                model.addAttribute("response1", mvcResponseSender.createResponseString("RTLR_OK_1002", true,
345
                model.addAttribute("response1", mvcResponseSender.createResponseString("RTLR_OK_1002", true,
338
                        request.getContextPath() + redirectUrl, name));
346
                        request.getContextPath() + redirectUrl, name));
339
            } else {
347
            } else {
340
                LOGGER.error("Requested token email_id is not valid, please try to login");
348
                LOGGER.error("Requested token email_id is not valid, please try to login");
341
                /**
349
                /**
342
                 * TODO:Amit This redirect should be handle.
350
                 * TODO:Amit This redirect should be handle.
343
                 */
351
                 */
344
                throw new ProfitMandiBusinessException(ProfitMandiConstants.EMAIL_ID, fofoDetails.getEmailId(),
352
                throw new ProfitMandiBusinessException(ProfitMandiConstants.EMAIL_ID, fofoDetails.getEmailId(), "RTLR_1000");
345
                        "RTLR_1000");
-
 
346
            }
353
            }
347
            return "response";
354
            return "response";
348
        } catch (ProfitMandiBusinessException profitMandiBusinessException) {
355
        } catch (ProfitMandiBusinessException profitMandiBusinessException) {
349
            LOGGER.error("Error : ", profitMandiBusinessException);
356
            LOGGER.error("Error : ", profitMandiBusinessException);
350
            model.addAttribute("response1",
357
            model.addAttribute("response1",
Line 359... Line 366...
359
            return "response";
366
            return "response";
360
        }
367
        }
361
    }
368
    }
362
 
369
 
363
    @PostMapping(value = "/forgetPassword")
370
    @PostMapping(value = "/forgetPassword")
-
 
371
    public String forgetPasswordPage(@RequestParam(name = ProfitMandiConstants.EMAIL_ID, defaultValue = "") String emailId, Model model) throws Exception {
-
 
372
        try {
364
    public String forgetPasswordPage(
373
            LOGGER.info("Forgetting password of: [{}]",emailId);
365
            @RequestParam(name = ProfitMandiConstants.EMAIL_ID, defaultValue = "") String emailId, Model model)
374
            authService.resetPassword(emailId);
366
            throws Exception {
375
        } catch (Exception exception){
367
        LOGGER.info(emailId);
376
            try {
368
        authService.resetPassword(emailId);
377
                userService.resetPassword(emailId);
-
 
378
            } catch (Exception e){
-
 
379
                throw new ProfitMandiBusinessException("Password Reset Email", emailId, "Could not send password reset mail. Password Could not be reset");
-
 
380
            }
-
 
381
        }
369
        model.addAttribute("response1", mvcResponseSender.createResponseString(true));
382
        model.addAttribute("response1", mvcResponseSender.createResponseString(true));
370
        LOGGER.info("completed");
383
        LOGGER.info("completed");
371
        return "response";
384
        return "response";
372
    }
385
    }
373
 
386
 
374
    private void addCookiesToResponse(LoginDetails fofoDetails, HttpServletRequest request,
387
    private void addCookiesToResponse(LoginDetails fofoDetails, HttpServletRequest request, HttpServletResponse response) {
375
                                      HttpServletResponse response) {
-
 
376
        List<String> roleIds = new ArrayList<>();
388
        List<String> roleIds = new ArrayList<>();
377
 
389
 
378
        for (int roleId : fofoDetails.getRoleIds()) {
390
        for (int roleId : fofoDetails.getRoleIds()) {
379
            roleIds.add(String.valueOf(roleId));
391
            roleIds.add(String.valueOf(roleId));
380
        }
392
        }