| Line 1... |
Line 1... |
| 1 |
package com.spice.profitmandi.web.controller;
|
1 |
package com.spice.profitmandi.web.controller;
|
| 2 |
|
2 |
|
| 3 |
import java.time.LocalDateTime;
|
3 |
import java.time.LocalDateTime;
|
| 4 |
import java.util.ArrayList;
|
4 |
import java.util.ArrayList;
|
| 5 |
import java.util.HashMap;
|
5 |
import java.util.HashMap;
|
| - |
|
6 |
import java.util.HashSet;
|
| 6 |
import java.util.List;
|
7 |
import java.util.List;
|
| 7 |
import java.util.Map;
|
8 |
import java.util.Map;
|
| - |
|
9 |
import java.util.Set;
|
| 8 |
|
10 |
|
| 9 |
import javax.servlet.http.HttpServletRequest;
|
11 |
import javax.servlet.http.HttpServletRequest;
|
| 10 |
|
12 |
|
| 11 |
import org.apache.http.conn.HttpHostConnectException;
|
13 |
import org.apache.http.conn.HttpHostConnectException;
|
| 12 |
import org.apache.logging.log4j.Logger;
|
- |
|
| 13 |
import org.apache.logging.log4j.LogManager;
|
14 |
import org.apache.logging.log4j.LogManager;
|
| - |
|
15 |
import org.apache.logging.log4j.Logger;
|
| 14 |
import org.springframework.beans.factory.annotation.Autowired;
|
16 |
import org.springframework.beans.factory.annotation.Autowired;
|
| 15 |
import org.springframework.beans.factory.annotation.Value;
|
17 |
import org.springframework.beans.factory.annotation.Value;
|
| 16 |
import org.springframework.http.ResponseEntity;
|
18 |
import org.springframework.http.ResponseEntity;
|
| 17 |
import org.springframework.stereotype.Controller;
|
19 |
import org.springframework.stereotype.Controller;
|
| 18 |
import org.springframework.transaction.annotation.Transactional;
|
20 |
import org.springframework.transaction.annotation.Transactional;
|
| Line 43... |
Line 45... |
| 43 |
import com.spice.profitmandi.dao.entity.user.Address;
|
45 |
import com.spice.profitmandi.dao.entity.user.Address;
|
| 44 |
import com.spice.profitmandi.dao.enumuration.dtr.RoleType;
|
46 |
import com.spice.profitmandi.dao.enumuration.dtr.RoleType;
|
| 45 |
import com.spice.profitmandi.dao.model.UserCart;
|
47 |
import com.spice.profitmandi.dao.model.UserCart;
|
| 46 |
import com.spice.profitmandi.dao.repository.dtr.PermissionRepository;
|
48 |
import com.spice.profitmandi.dao.repository.dtr.PermissionRepository;
|
| 47 |
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
|
49 |
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
|
| - |
|
50 |
import com.spice.profitmandi.dao.repository.dtr.RoleRepository;
|
| 48 |
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
|
51 |
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
|
| 49 |
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
|
52 |
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
|
| 50 |
import com.spice.profitmandi.dao.repository.dtr.UserRoleRepository;
|
53 |
import com.spice.profitmandi.dao.repository.dtr.UserRoleRepository;
|
| 51 |
import com.spice.profitmandi.dao.repository.user.AddressRepository;
|
54 |
import com.spice.profitmandi.dao.repository.user.AddressRepository;
|
| - |
|
55 |
import com.spice.profitmandi.service.RoleManagerService;
|
| 52 |
import com.spice.profitmandi.service.UserService;
|
56 |
import com.spice.profitmandi.service.UserService;
|
| 53 |
import com.spice.profitmandi.web.enumuration.UserStatus;
|
57 |
import com.spice.profitmandi.web.enumuration.UserStatus;
|
| 54 |
import com.spice.profitmandi.web.processor.GoogleLoginProcessor;
|
58 |
import com.spice.profitmandi.web.processor.GoogleLoginProcessor;
|
| 55 |
import com.spice.profitmandi.web.req.UserAddRoleRequest;
|
59 |
import com.spice.profitmandi.web.req.UserAddRoleRequest;
|
| 56 |
import com.spice.profitmandi.web.req.UserRequest;
|
60 |
import com.spice.profitmandi.web.req.UserRequest;
|
| Line 105... |
Line 109... |
| 105 |
@Autowired
|
109 |
@Autowired
|
| 106 |
private GoogleLoginProcessor googleLoginProcessor;
|
110 |
private GoogleLoginProcessor googleLoginProcessor;
|
| 107 |
|
111 |
|
| 108 |
@Autowired
|
112 |
@Autowired
|
| 109 |
private UserService userService;
|
113 |
private UserService userService;
|
| - |
|
114 |
|
| - |
|
115 |
@Autowired
|
| - |
|
116 |
private RoleManagerService roleManagerService;
|
| - |
|
117 |
|
| - |
|
118 |
@Autowired
|
| - |
|
119 |
private RoleRepository roleRepository;
|
| 110 |
|
120 |
|
| 111 |
@RequestMapping(value = ProfitMandiConstants.URL_USER_GOOGLE_LOGIN, method = RequestMethod.POST)
|
121 |
@RequestMapping(value = ProfitMandiConstants.URL_USER_GOOGLE_LOGIN, method = RequestMethod.POST)
|
| 112 |
public ResponseEntity<?> googleLogin(HttpServletRequest request, @RequestBody GoogleLoginRequest googleLoginRequest) throws ProfitMandiBusinessException{
|
122 |
public ResponseEntity<?> googleLogin(HttpServletRequest request, @RequestBody GoogleLoginRequest googleLoginRequest) throws ProfitMandiBusinessException{
|
| 113 |
LOGGER.info("requested url : " + request.getRequestURL().toString());
|
123 |
LOGGER.info("requested url : " + request.getRequestURL().toString());
|
| 114 |
return responseSender.ok(googleLoginProcessor.process(googleLoginRequest));
|
124 |
return responseSender.ok(googleLoginProcessor.process(googleLoginRequest));
|
| Line 144... |
Line 154... |
| 144 |
}
|
154 |
}
|
| 145 |
if (user != null) {
|
155 |
if (user != null) {
|
| 146 |
responseMap.put(ProfitMandiConstants.EMAIL_ID, user.getEmailId());
|
156 |
responseMap.put(ProfitMandiConstants.EMAIL_ID, user.getEmailId());
|
| 147 |
responseMap.put(ProfitMandiConstants.USER_ID, user.getId());
|
157 |
responseMap.put(ProfitMandiConstants.USER_ID, user.getId());
|
| 148 |
responseMap.put(ProfitMandiConstants.USER_NAME, user.getFirstName() + " " + user.getLastName());
|
158 |
responseMap.put(ProfitMandiConstants.USER_NAME, user.getFirstName() + " " + user.getLastName());
|
| 149 |
List<RoleType> roleTypes = userRoleRepository.selectRoleTypesByUserId(user.getId());
|
159 |
List<Integer> roleIds = userRoleRepository.selectRoleIdsByUserId(user.getId());
|
| 150 |
//LOGGER.info("userRoles {} ", userRoles);
|
160 |
//LOGGER.info("userRoles {} ", userRoles);
|
| 151 |
|
161 |
|
| 152 |
// generate new token if roles have been updated
|
162 |
// generate new token if roles have been updated
|
| 153 |
if (userInfo.getRoleNames() == null || roleTypes.size() != userInfo.getRoleNames().size()) {
|
163 |
if (userInfo.getRoleIds() == null || roleIds.size() != userInfo.getRoleIds().size()) {
|
| 154 |
String[] roleType = new String[roleTypes.size()];
|
164 |
String[] roleIdArray = new String[roleIds.size()];
|
| 155 |
int index = 0;
|
165 |
int index = 0;
|
| 156 |
for (RoleType userRole : roleTypes) {
|
166 |
for (int roleId : roleIds) {
|
| 157 |
roleType[index++] = userRole.toString();
|
167 |
roleIdArray[index++] = String.valueOf(roleId);
|
| 158 |
}
|
168 |
}
|
| 159 |
String newToken = JWTUtil.create(user.getId(), roleType);
|
169 |
String newToken = JWTUtil.create(user.getId(), roleIdArray);
|
| 160 |
responseMap.put("newAuthToken", newToken);
|
170 |
responseMap.put("newAuthToken", newToken);
|
| 161 |
}
|
171 |
}
|
| 162 |
|
172 |
|
| 163 |
// if user is retailer
|
173 |
Set<Integer> roleIdsSet = new HashSet<Integer>(roleIds);
|
| 164 |
if (roleTypes.contains(RoleType.RETAILER)) {
|
174 |
if (roleManagerService.isRetailer(new HashSet<Integer>(roleIds))) {
|
| 165 |
UserCart uc = userAccountRepository.getUserCart(userInfo.getUserId());
|
175 |
UserCart uc = userAccountRepository.getUserCart(userInfo.getUserId());
|
| 166 |
Retailer retailer = retailerRepository.selectById(uc.getUserId());
|
176 |
Retailer retailer = retailerRepository.selectById(uc.getUserId());
|
| 167 |
com.spice.profitmandi.dao.entity.user.User saholicUser = userUserRepository.selectById(uc.getUserId());
|
177 |
com.spice.profitmandi.dao.entity.user.User saholicUser = userUserRepository.selectById(uc.getUserId());
|
| 168 |
if(saholicUser.getAddressId() != null){
|
178 |
if(saholicUser.getAddressId() != null){
|
| 169 |
Address address = addressRepository.selectById(saholicUser.getAddressId());
|
179 |
Address address = addressRepository.selectById(saholicUser.getAddressId());
|
| Line 172... |
Line 182... |
| 172 |
// if retailer is activated 1 then verified retailer
|
182 |
// if retailer is activated 1 then verified retailer
|
| 173 |
// else if migrated is 1 then old retailer
|
183 |
// else if migrated is 1 then old retailer
|
| 174 |
// also lets incoporte old process i.e is user is activated then also retailer is verified retailer
|
184 |
// also lets incoporte old process i.e is user is activated then also retailer is verified retailer
|
| 175 |
// else retailer is not verifed
|
185 |
// else retailer is not verifed
|
| 176 |
if (retailer.isActive() || user.isActivated()) {
|
186 |
if (retailer.isActive() || user.isActivated()) {
|
| 177 |
if (roleTypes.contains(RoleType.FOFO)) {
|
187 |
if (roleManagerService.isPartner(roleIdsSet)) {
|
| 178 |
responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.FOFO.getValue());
|
188 |
responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.FOFO.getValue());
|
| 179 |
} else {
|
189 |
} else {
|
| 180 |
responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.VERIFIED_RETAILER.getValue());
|
190 |
responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.VERIFIED_RETAILER.getValue());
|
| 181 |
}
|
191 |
}
|
| 182 |
} else if (retailer.isMigrated()) {
|
192 |
} else if (retailer.isMigrated()) {
|
| 183 |
responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.RETAILER.getValue());
|
193 |
responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.RETAILER.getValue());
|
| 184 |
} else {
|
194 |
} else {
|
| 185 |
responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.NOT_VERIFIED_RETAILER.getValue());
|
195 |
responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.NOT_VERIFIED_RETAILER.getValue());
|
| 186 |
}
|
196 |
}
|
| 187 |
} else if (roleTypes.contains(RoleType.USER)) {
|
197 |
} else if (roleManagerService.isUser(roleIdsSet)) {
|
| 188 |
responseMap.put("userInfo", getRegisteredUserInfo(user));
|
198 |
responseMap.put("userInfo", getRegisteredUserInfo(user));
|
| 189 |
responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.REGISTERED.getValue());
|
199 |
responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.REGISTERED.getValue());
|
| 190 |
}
|
200 |
}
|
| 191 |
} else {
|
201 |
} else {
|
| 192 |
responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.NOT_REGISTERED.getValue());
|
202 |
responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.NOT_REGISTERED.getValue());
|
| Line 230... |
Line 240... |
| 230 |
user.setActivated(false);
|
240 |
user.setActivated(false);
|
| 231 |
user.setCreateTimestamp(LocalDateTime.now());
|
241 |
user.setCreateTimestamp(LocalDateTime.now());
|
| 232 |
user.setUpdateTimestamp(LocalDateTime.now());
|
242 |
user.setUpdateTimestamp(LocalDateTime.now());
|
| 233 |
userRepository.persist(user);
|
243 |
userRepository.persist(user);
|
| 234 |
UserRole userRole = new UserRole();
|
244 |
UserRole userRole = new UserRole();
|
| 235 |
userRole.setRoleType(RoleType.USER);
|
245 |
userRole.setRoleId(roleRepository.selectByName(RoleType.USER.name()).getId());
|
| 236 |
userRole.setUserId(user.getId());
|
246 |
userRole.setUserId(user.getId());
|
| 237 |
userRoleRepository.persist(userRole);
|
247 |
userRoleRepository.persist(userRole);
|
| 238 |
return responseSender.ok(ResponseCodeHolder.getMessage("USR_OK_1000"));
|
248 |
return responseSender.ok(ResponseCodeHolder.getMessage("USR_OK_1000"));
|
| 239 |
|
249 |
|
| 240 |
}
|
250 |
}
|
| Line 331... |
Line 341... |
| 331 |
LOGGER.info("requested url : " + request.getRequestURL().toString());
|
341 |
LOGGER.info("requested url : " + request.getRequestURL().toString());
|
| 332 |
User user = userRepository.selectById(userAddRoleRequest.getUserId());
|
342 |
User user = userRepository.selectById(userAddRoleRequest.getUserId());
|
| 333 |
|
343 |
|
| 334 |
Permission permission = new Permission();
|
344 |
Permission permission = new Permission();
|
| 335 |
permission.setType(userAddRoleRequest.getPermissionType());
|
345 |
permission.setType(userAddRoleRequest.getPermissionType());
|
| 336 |
permission.setRoleType(userAddRoleRequest.getRoleType());
|
346 |
permission.setRoleType(RoleType.FOFO);;
|
| 337 |
permissionRepository.persist(permission);
|
347 |
permissionRepository.persist(permission);
|
| 338 |
UserRole userRole = new UserRole();
|
348 |
UserRole userRole = new UserRole();
|
| 339 |
userRole.setRoleType(userAddRoleRequest.getRoleType());
|
349 |
userRole.setRoleId(userAddRoleRequest.getRoleId());
|
| 340 |
userRole.setUserId(user.getId());
|
350 |
userRole.setUserId(user.getId());
|
| 341 |
userRoleRepository.persist(userRole);
|
351 |
userRoleRepository.persist(userRole);
|
| 342 |
return responseSender.ok("");
|
352 |
return responseSender.ok("");
|
| 343 |
}
|
353 |
}
|
| 344 |
|
354 |
|
| 345 |
@RequestMapping(value = ProfitMandiConstants.URL_USER_ROLE_REMOVE, method = RequestMethod.DELETE)
|
355 |
@RequestMapping(value = ProfitMandiConstants.URL_USER_ROLE_REMOVE, method = RequestMethod.DELETE)
|
| 346 |
public ResponseEntity<?> removeRole(HttpServletRequest request, @RequestParam(name = "roleType") RoleType roleType,
|
356 |
public ResponseEntity<?> removeRole(HttpServletRequest request, @RequestParam int roleId,
|
| 347 |
@RequestParam(name = "userId") int userId) throws ProfitMandiBusinessException{
|
357 |
@RequestParam(name = "userId") int userId) throws ProfitMandiBusinessException{
|
| 348 |
LOGGER.info("requested url : " + request.getRequestURL().toString());
|
358 |
LOGGER.info("requested url : " + request.getRequestURL().toString());
|
| 349 |
userRepository.selectById(userId);
|
359 |
userRepository.selectById(userId);
|
| 350 |
userRoleRepository.deleteByUserAndRoleType(userId, roleType);
|
360 |
/*userRoleRepository.deleteByUserAndRoleId(userId, roleId);
|
| 351 |
permissionRepository.deleteByRoleType(roleType);
|
361 |
permissionRepository.deleteByRoleType(roleRepository.selectById(id));*/
|
| 352 |
return responseSender.ok("");
|
362 |
return responseSender.ok("");
|
| 353 |
}
|
363 |
}
|
| 354 |
|
364 |
|
| 355 |
@RequestMapping(value = ProfitMandiConstants.URL_USER_ROLE_ALL, method = RequestMethod.GET)
|
365 |
@RequestMapping(value = ProfitMandiConstants.URL_USER_ROLE_ALL, method = RequestMethod.GET)
|
| 356 |
public ResponseEntity<?> getAllRoles(HttpServletRequest request, @RequestParam(name = "id") int id) {
|
366 |
public ResponseEntity<?> getAllRoles(HttpServletRequest request, @RequestParam(name = "id") int id) {
|
| Line 402... |
Line 412... |
| 402 |
|
412 |
|
| 403 |
}
|
413 |
}
|
| 404 |
}
|
414 |
}
|
| 405 |
if(user != null){
|
415 |
if(user != null){
|
| 406 |
List<UserRole> userRoles = userRoleRepository.selectByUserId(user.getId());
|
416 |
List<UserRole> userRoles = userRoleRepository.selectByUserId(user.getId());
|
| 407 |
String[] roleTypes = new String[userRoles.size()];
|
417 |
String[] roleIds = new String[userRoles.size()];
|
| 408 |
int index = 0;
|
418 |
int index = 0;
|
| 409 |
for (UserRole userRole : userRoles) {
|
419 |
for (UserRole userRole : userRoles) {
|
| 410 |
roleTypes[index++] = userRole.getRoleType().toString();
|
420 |
roleIds[index++] = String.valueOf(userRole.getRoleId());
|
| 411 |
}
|
421 |
}
|
| 412 |
responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(user.getId(), roleTypes));
|
422 |
responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(user.getId(), roleIds));
|
| 413 |
responseMap.put(ProfitMandiConstants.REGISTERED, true);
|
423 |
responseMap.put(ProfitMandiConstants.REGISTERED, true);
|
| 414 |
}else{
|
424 |
}else{
|
| 415 |
responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(emailId));
|
425 |
responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(emailId));
|
| 416 |
responseMap.put(ProfitMandiConstants.REGISTERED, false);
|
426 |
responseMap.put(ProfitMandiConstants.REGISTERED, false);
|
| 417 |
}
|
427 |
}
|