Subversion Repositories SmartDukaan

Rev

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

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