Subversion Repositories SmartDukaan

Rev

Rev 22139 | Rev 22155 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
21561 ashik.ali 1
package com.spice.profitmandi.web.controller;
21555 kshitij.so 2
 
22139 amit.gupta 3
import java.util.ArrayList;
22111 ashik.ali 4
import java.util.HashSet;
5
import java.util.List;
6
import java.util.Set;
22069 ashik.ali 7
 
21577 ashik.ali 8
import javax.servlet.http.Cookie;
21561 ashik.ali 9
import javax.servlet.http.HttpServletRequest;
21577 ashik.ali 10
import javax.servlet.http.HttpServletResponse;
21561 ashik.ali 11
 
21568 ashik.ali 12
import org.slf4j.Logger;
13
import org.slf4j.LoggerFactory;
21561 ashik.ali 14
import org.springframework.beans.factory.annotation.Autowired;
22079 amit.gupta 15
import org.springframework.beans.factory.annotation.Value;
21555 kshitij.so 16
import org.springframework.stereotype.Controller;
21987 kshitij.so 17
import org.springframework.transaction.annotation.Transactional;
21615 kshitij.so 18
import org.springframework.ui.Model;
21555 kshitij.so 19
import org.springframework.ui.ModelMap;
20
import org.springframework.web.bind.annotation.ModelAttribute;
21
import org.springframework.web.bind.annotation.RequestMapping;
22
import org.springframework.web.bind.annotation.RequestMethod;
21561 ashik.ali 23
import org.springframework.web.bind.annotation.RequestParam;
21555 kshitij.so 24
 
21561 ashik.ali 25
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
26
import com.spice.profitmandi.common.model.ProfitMandiConstants;
22111 ashik.ali 27
import com.spice.profitmandi.dao.entity.dtr.Retailer;
28
import com.spice.profitmandi.dao.entity.dtr.User;
29
import com.spice.profitmandi.dao.entity.dtr.UserAccounts;
30
import com.spice.profitmandi.dao.entity.dtr.UserRole;
31
import com.spice.profitmandi.dao.enumuration.dtr.AccountType;
32
import com.spice.profitmandi.dao.enumuration.dtr.RoleType;
33
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
34
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
35
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
36
import com.spice.profitmandi.dao.repository.dtr.UserRoleRepository;
22139 amit.gupta 37
import com.spice.profitmandi.web.model.LoginDetails;
22069 ashik.ali 38
import com.spice.profitmandi.web.util.CookiesProcessor;
22111 ashik.ali 39
import com.spice.profitmandi.web.util.GoogleTokenUtil;
21574 ashik.ali 40
import com.spice.profitmandi.web.util.MVCResponseSender;
22139 amit.gupta 41
import com.spice.profitmandi.web.util.Utils;
21561 ashik.ali 42
 
21555 kshitij.so 43
@Controller
22148 amit.gupta 44
@Transactional
21555 kshitij.so 45
public class LoginController {
46
 
21568 ashik.ali 47
	private static final Logger LOGGER = LoggerFactory.getLogger(LoginController.class);
48
 
21561 ashik.ali 49
	@Autowired
22111 ashik.ali 50
	GoogleTokenUtil googleTokenUtil;
21561 ashik.ali 51
 
21574 ashik.ali 52
	@Autowired
22111 ashik.ali 53
	RetailerRepository retailerRepository;
54
 
55
	@Autowired
56
	UserRepository userRepository;
57
 
58
	@Autowired
59
	UserAccountRepository userAccountRepository;
60
 
61
	@Autowired
62
	UserRoleRepository userRoleRepository;
63
 
64
	@Autowired
21574 ashik.ali 65
	MVCResponseSender mvcResponseSender;
66
 
21578 ashik.ali 67
	@Autowired
22069 ashik.ali 68
	CookiesProcessor cookiesProcessor;
22079 amit.gupta 69
 
70
	@Value("${google.api.key}")
71
	private String googleApiKey;
72
 
21555 kshitij.so 73
	@RequestMapping(value = "/login", method = RequestMethod.GET)
22072 ashik.ali 74
	public String loginPage(HttpServletRequest request, Model model) throws Exception{
22088 amit.gupta 75
		LOGGER.info("Context Path is {}", request.getContextPath());
21578 ashik.ali 76
		try{
22139 amit.gupta 77
			LoginDetails details = cookiesProcessor.getCookiesObject(request);
78
			LOGGER.info("Request session is already exist, should be redirect to as per roles assigned");
79
			return "redirect:" + Utils.getRedictUrlFromLogin(details);
21615 kshitij.so 80
		}catch(Exception | ProfitMandiBusinessException profitMandiBusinessException){
22079 amit.gupta 81
			model.addAttribute("googleApiKey", googleApiKey);
22086 amit.gupta 82
			model.addAttribute("appContextPath", request.getContextPath());
21577 ashik.ali 83
			return "login";
21574 ashik.ali 84
		}
21555 kshitij.so 85
	}
86
 
87
	@RequestMapping(value = "/login", method = RequestMethod.POST)
21615 kshitij.so 88
	public String login(HttpServletRequest request, HttpServletResponse response, @RequestParam(name = ProfitMandiConstants.TOKEN) String token, Model model) throws Exception{
22139 amit.gupta 89
		LoginDetails fofoDetails = new LoginDetails();
90
		Set<RoleType> roleTypes = new HashSet<>();
91
		fofoDetails.setRoleTypes(roleTypes);
92
		fofoDetails.setFofo(false);
93
 
21561 ashik.ali 94
		try{
22139 amit.gupta 95
			//if role is retailer then FOFO_ID is retailerId else it is userid as normal user's wont have retailer id. 
22111 ashik.ali 96
			String emailId = googleTokenUtil.getEmailId(token);
22139 amit.gupta 97
			fofoDetails.setEmailId(emailId);
98
			fofoDetails.setFofoId(-1);
22111 ashik.ali 99
			User user = null;
100
			try{
101
				user = userRepository.selectByEmailId(emailId);
22139 amit.gupta 102
				fofoDetails.setFofoId(user.getId());
103
				try {
104
					UserAccounts userAccounts = userAccountRepository.getUserAccountByType(user.getId(), AccountType.saholic);
105
					Retailer retailer = retailerRepository.selectById(Integer.parseInt(userAccounts.getAccount_key()));
106
					fofoDetails.setFofoId(retailer.getId());
107
					List<UserRole> userRoles = userRoleRepository.selectByUserId(user.getId());
108
					for(int index = 0; index < userRoles.size(); index++){
109
						roleTypes.add(userRoles.get(index).getRoleType());
110
					}
111
					fofoDetails.setFofo(retailer.isFofo());
112
				} catch(ProfitMandiBusinessException pmbe) {
113
					LOGGER.error("Data Inconsistent", pmbe);
114
				}
22111 ashik.ali 115
			}catch(ProfitMandiBusinessException profitMandiBusinessException){
116
				LOGGER.error("User not found with given emailId", profitMandiBusinessException);
117
			}
22139 amit.gupta 118
			addCookiesToResponse(fofoDetails, request, response);
119
			String redirectUrl = Utils.getRedictUrlFromLogin(fofoDetails);
120
			LOGGER.info("Requested token email_id is valid, user login to system, shoud be redirect to {}", redirectUrl);
121
 
122
			model.addAttribute("loginResponse", mvcResponseSender.createResponseString("RTLR_OK_1002", true, request.getContextPath() + redirectUrl));
21578 ashik.ali 123
			return "response";
22139 amit.gupta 124
		} catch(ProfitMandiBusinessException profitMandiBusinessException){
21568 ashik.ali 125
			LOGGER.error("Error : ", profitMandiBusinessException);
21578 ashik.ali 126
			model.addAttribute("loginResponse", mvcResponseSender.createResponseString(profitMandiBusinessException.getCode(), false, "/error"));
127
			return "response";
21561 ashik.ali 128
		}
21555 kshitij.so 129
	}
21574 ashik.ali 130
 
22139 amit.gupta 131
	private void addCookiesToResponse(LoginDetails fofoDetails, HttpServletRequest request, HttpServletResponse response) {
132
		List<String> roleNames = new ArrayList<>();
133
 
134
		for(RoleType roleType : fofoDetails.getRoleTypes()) {
135
			roleNames.add(roleType.toString());
136
		}
137
		Cookie cookieRoleNames = new Cookie(ProfitMandiConstants.ROLE_NAMES, String.join(",", roleNames));
138
		cookieRoleNames.setDomain(request.getServerName());
139
		cookieRoleNames.setPath("/");
140
 
141
		Cookie cookieFofoId = new Cookie(ProfitMandiConstants.FOFO_ID, String.valueOf(fofoDetails.getFofoId()));
142
		cookieFofoId.setDomain(request.getServerName());
143
		cookieFofoId.setPath("/");
144
 
145
		Cookie cookieEmailId = new Cookie(ProfitMandiConstants.EMAIL_ID, fofoDetails.getEmailId());
146
		cookieEmailId.setDomain(request.getServerName());
147
		cookieEmailId.setPath("/");
148
 
149
		Cookie fofoFlagCookie = new Cookie(ProfitMandiConstants.FOFO_FLAG, Boolean.toString(fofoDetails.isFofo()));
150
		fofoFlagCookie.setDomain(request.getServerName());
151
		fofoFlagCookie.setPath("/");
152
 
153
		response.addCookie(cookieFofoId);
154
		response.addCookie(cookieEmailId);
155
		response.addCookie(cookieRoleNames);
156
		response.addCookie(fofoFlagCookie);
157
	}
158
 
22069 ashik.ali 159
	@RequestMapping(value = "/logout", method = RequestMethod.GET)
160
	public String logout(HttpServletRequest request, @ModelAttribute("model") ModelMap model, HttpServletResponse response) throws Exception{
161
		try{
162
			cookiesProcessor.removeCookies(request, response);
163
			LOGGER.info("Logout is successfull, should be redirect to /login");
22085 amit.gupta 164
			return "redirect:/login";
22069 ashik.ali 165
		}catch(Exception | ProfitMandiBusinessException profitMandiBusinessException){
166
			LOGGER.info("Error occured while removing requested cookies, should be redirect to /login");
22085 amit.gupta 167
			return "redirect:/login";
22069 ashik.ali 168
		}
169
	}
170
 
171
 
172
 
21555 kshitij.so 173
}