| Line 1... |
Line 1... |
| 1 |
package com.spice.profitmandi.web.controller;
|
1 |
package com.spice.profitmandi.web.controller;
|
| 2 |
|
2 |
|
| - |
|
3 |
import java.util.ArrayList;
|
| 3 |
import java.util.HashSet;
|
4 |
import java.util.HashSet;
|
| 4 |
import java.util.List;
|
5 |
import java.util.List;
|
| 5 |
import java.util.Set;
|
6 |
import java.util.Set;
|
| 6 |
|
7 |
|
| 7 |
import javax.servlet.http.Cookie;
|
8 |
import javax.servlet.http.Cookie;
|
| Line 31... |
Line 32... |
| 31 |
import com.spice.profitmandi.dao.enumuration.dtr.RoleType;
|
32 |
import com.spice.profitmandi.dao.enumuration.dtr.RoleType;
|
| 32 |
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
|
33 |
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
|
| 33 |
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
|
34 |
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
|
| 34 |
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
|
35 |
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
|
| 35 |
import com.spice.profitmandi.dao.repository.dtr.UserRoleRepository;
|
36 |
import com.spice.profitmandi.dao.repository.dtr.UserRoleRepository;
|
| 36 |
import com.spice.profitmandi.web.model.FofoDetails;
|
37 |
import com.spice.profitmandi.web.model.LoginDetails;
|
| 37 |
import com.spice.profitmandi.web.util.CookiesProcessor;
|
38 |
import com.spice.profitmandi.web.util.CookiesProcessor;
|
| 38 |
import com.spice.profitmandi.web.util.GoogleTokenUtil;
|
39 |
import com.spice.profitmandi.web.util.GoogleTokenUtil;
|
| 39 |
import com.spice.profitmandi.web.util.MVCResponseSender;
|
40 |
import com.spice.profitmandi.web.util.MVCResponseSender;
|
| - |
|
41 |
import com.spice.profitmandi.web.util.Utils;
|
| 40 |
|
42 |
|
| 41 |
@Controller
|
43 |
@Controller
|
| 42 |
@Transactional(rollbackFor=Throwable.class)
|
44 |
@Transactional(rollbackFor=Throwable.class)
|
| 43 |
public class LoginController {
|
45 |
public class LoginController {
|
| 44 |
|
46 |
|
| Line 70... |
Line 72... |
| 70 |
|
72 |
|
| 71 |
@RequestMapping(value = "/login", method = RequestMethod.GET)
|
73 |
@RequestMapping(value = "/login", method = RequestMethod.GET)
|
| 72 |
public String loginPage(HttpServletRequest request, Model model) throws Exception{
|
74 |
public String loginPage(HttpServletRequest request, Model model) throws Exception{
|
| 73 |
LOGGER.info("Context Path is {}", request.getContextPath());
|
75 |
LOGGER.info("Context Path is {}", request.getContextPath());
|
| 74 |
try{
|
76 |
try{
|
| 75 |
cookiesProcessor.getCookiesObject(request);
|
77 |
LoginDetails details = cookiesProcessor.getCookiesObject(request);
|
| 76 |
LOGGER.info("Request session is already exist, should be redirect to /dashboard");
|
78 |
LOGGER.info("Request session is already exist, should be redirect to as per roles assigned");
|
| 77 |
return "redirect:/dashboard";
|
79 |
return "redirect:" + Utils.getRedictUrlFromLogin(details);
|
| 78 |
}catch(Exception | ProfitMandiBusinessException profitMandiBusinessException){
|
80 |
}catch(Exception | ProfitMandiBusinessException profitMandiBusinessException){
|
| 79 |
model.addAttribute("googleApiKey", googleApiKey);
|
81 |
model.addAttribute("googleApiKey", googleApiKey);
|
| 80 |
model.addAttribute("appContextPath", request.getContextPath());
|
82 |
model.addAttribute("appContextPath", request.getContextPath());
|
| 81 |
return "login";
|
83 |
return "login";
|
| 82 |
}
|
84 |
}
|
| 83 |
}
|
85 |
}
|
| 84 |
|
86 |
|
| 85 |
@RequestMapping(value = "/login", method = RequestMethod.POST)
|
87 |
@RequestMapping(value = "/login", method = RequestMethod.POST)
|
| 86 |
public String login(HttpServletRequest request, HttpServletResponse response, @RequestParam(name = ProfitMandiConstants.TOKEN) String token, Model model) throws Exception{
|
88 |
public String login(HttpServletRequest request, HttpServletResponse response, @RequestParam(name = ProfitMandiConstants.TOKEN) String token, Model model) throws Exception{
|
| - |
|
89 |
LoginDetails fofoDetails = new LoginDetails();
|
| - |
|
90 |
Set<RoleType> roleTypes = new HashSet<>();
|
| - |
|
91 |
fofoDetails.setRoleTypes(roleTypes);
|
| - |
|
92 |
fofoDetails.setFofo(false);
|
| - |
|
93 |
|
| 87 |
try{
|
94 |
try{
|
| - |
|
95 |
//if role is retailer then FOFO_ID is retailerId else it is userid as normal user's wont have retailer id.
|
| 88 |
String emailId = googleTokenUtil.getEmailId(token);
|
96 |
String emailId = googleTokenUtil.getEmailId(token);
|
| - |
|
97 |
fofoDetails.setEmailId(emailId);
|
| - |
|
98 |
fofoDetails.setFofoId(-1);
|
| 89 |
User user = null;
|
99 |
User user = null;
|
| 90 |
try{
|
100 |
try{
|
| 91 |
user = userRepository.selectByEmailId(emailId);
|
101 |
user = userRepository.selectByEmailId(emailId);
|
| - |
|
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 |
}
|
| 92 |
}catch(ProfitMandiBusinessException profitMandiBusinessException){
|
115 |
}catch(ProfitMandiBusinessException profitMandiBusinessException){
|
| 93 |
LOGGER.error("User not found with given emailId", profitMandiBusinessException);
|
116 |
LOGGER.error("User not found with given emailId", profitMandiBusinessException);
|
| 94 |
model.addAttribute("loginResponse", mvcResponseSender.createResponseString("RTLR_OK_1002", true, request.getContextPath() + "/register"));
|
- |
|
| 95 |
return "response";
|
- |
|
| 96 |
}
|
- |
|
| 97 |
UserAccounts userAccounts = userAccountRepository.getUserAccountByType(user.getId(), AccountType.saholic);
|
- |
|
| 98 |
Retailer retailer = retailerRepository.selectById(Integer.parseInt(userAccounts.getAccount_key()));
|
- |
|
| 99 |
if(!retailer.isFofo()){
|
- |
|
| 100 |
LOGGER.error("Retailer is not fofo, should be registered");
|
- |
|
| 101 |
model.addAttribute("loginResponse", mvcResponseSender.createResponseString("RTLR_OK_1002", true, request.getContextPath() + "/register"));
|
- |
|
| 102 |
return "response";
|
- |
|
| 103 |
}
|
- |
|
| 104 |
List<UserRole> userRoles = userRoleRepository.selectByUserId(user.getId());
|
- |
|
| 105 |
Set<RoleType> roleTypes = new HashSet<>();
|
- |
|
| 106 |
StringBuilder roleNames = new StringBuilder();
|
- |
|
| 107 |
for(int index = 0; index < userRoles.size(); index++){
|
- |
|
| 108 |
roleTypes.add(userRoles.get(index).getRoleType());
|
- |
|
| 109 |
roleNames.append(userRoles.get(index).getRoleType().toString());
|
- |
|
| 110 |
if(index + 1 != userRoles.size()){
|
- |
|
| 111 |
roleNames.append(",");
|
- |
|
| 112 |
}
|
- |
|
| 113 |
}
|
117 |
}
|
| 114 |
FofoDetails fofoDetails = new FofoDetails();
|
118 |
addCookiesToResponse(fofoDetails, request, response);
|
| 115 |
fofoDetails.setFofoId(retailer.getId());
|
- |
|
| 116 |
fofoDetails.setEmailId(emailId);
|
- |
|
| 117 |
fofoDetails.setRoleTypes(roleTypes);
|
- |
|
| 118 |
//FofoDetails fofoDetails = googleTokenUtil.getFofoDetail(token);
|
119 |
String redirectUrl = Utils.getRedictUrlFromLogin(fofoDetails);
|
| 119 |
Cookie cookieFofoId = new Cookie(ProfitMandiConstants.FOFO_ID, String.valueOf(fofoDetails.getFofoId()));
|
- |
|
| 120 |
cookieFofoId.setDomain(request.getServerName());
|
- |
|
| 121 |
cookieFofoId.setPath(request.getContextPath());
|
- |
|
| 122 |
Cookie cookieEmailId = new Cookie(ProfitMandiConstants.EMAIL_ID, fofoDetails.getEmailId());
|
- |
|
| 123 |
cookieEmailId.setDomain(request.getServerName());
|
- |
|
| 124 |
cookieEmailId.setPath(request.getContextPath());
|
- |
|
| 125 |
Cookie cookieRoleNames = new Cookie(ProfitMandiConstants.ROLE_NAMES, roleNames.toString());
|
- |
|
| 126 |
response.addCookie(cookieFofoId);
|
- |
|
| 127 |
response.addCookie(cookieEmailId);
|
- |
|
| 128 |
response.addCookie(cookieRoleNames);
|
- |
|
| 129 |
LOGGER.info("Requested token email_id is valid, user login to system, shoud be redirect to /dashboard");
|
120 |
LOGGER.info("Requested token email_id is valid, user login to system, shoud be redirect to {}", redirectUrl);
|
| - |
|
121 |
|
| 130 |
model.addAttribute("loginResponse", mvcResponseSender.createResponseString("RTLR_OK_1002", true, request.getContextPath() + "/dashboard"));
|
122 |
model.addAttribute("loginResponse", mvcResponseSender.createResponseString("RTLR_OK_1002", true, request.getContextPath() + redirectUrl));
|
| 131 |
return "response";
|
123 |
return "response";
|
| 132 |
// return mvcResponseSender.createResponseString("RTLR_OK_1002", true, "/profitmandi-fofo/dashboard");
|
- |
|
| 133 |
}catch(ProfitMandiBusinessException profitMandiBusinessException){
|
124 |
} catch(ProfitMandiBusinessException profitMandiBusinessException){
|
| 134 |
LOGGER.error("Error : ", profitMandiBusinessException);
|
125 |
LOGGER.error("Error : ", profitMandiBusinessException);
|
| 135 |
model.addAttribute("loginResponse", mvcResponseSender.createResponseString(profitMandiBusinessException.getCode(), false, "/error"));
|
126 |
model.addAttribute("loginResponse", mvcResponseSender.createResponseString(profitMandiBusinessException.getCode(), false, "/error"));
|
| 136 |
return "response";
|
127 |
return "response";
|
| 137 |
}
|
128 |
}
|
| 138 |
}
|
129 |
}
|
| 139 |
|
130 |
|
| - |
|
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 |
|
| 140 |
@RequestMapping(value = "/logout", method = RequestMethod.GET)
|
159 |
@RequestMapping(value = "/logout", method = RequestMethod.GET)
|
| 141 |
public String logout(HttpServletRequest request, @ModelAttribute("model") ModelMap model, HttpServletResponse response) throws Exception{
|
160 |
public String logout(HttpServletRequest request, @ModelAttribute("model") ModelMap model, HttpServletResponse response) throws Exception{
|
| 142 |
try{
|
161 |
try{
|
| 143 |
cookiesProcessor.removeCookies(request, response);
|
162 |
cookiesProcessor.removeCookies(request, response);
|
| 144 |
LOGGER.info("Logout is successfull, should be redirect to /login");
|
163 |
LOGGER.info("Logout is successfull, should be redirect to /login");
|