| 21248 |
ashik.ali |
1 |
package com.spice.profitmandi.web.controller;
|
|
|
2 |
|
|
|
3 |
import java.time.LocalDateTime;
|
| 21428 |
amit.gupta |
4 |
import java.util.HashMap;
|
| 21277 |
ashik.ali |
5 |
import java.util.Map;
|
| 21428 |
amit.gupta |
6 |
import java.util.Set;
|
| 21248 |
ashik.ali |
7 |
|
|
|
8 |
import javax.servlet.http.HttpServletRequest;
|
|
|
9 |
|
|
|
10 |
import org.slf4j.Logger;
|
|
|
11 |
import org.slf4j.LoggerFactory;
|
| 21278 |
ashik.ali |
12 |
import org.springframework.beans.factory.annotation.Autowired;
|
| 21414 |
kshitij.so |
13 |
import org.springframework.beans.factory.annotation.Value;
|
| 21248 |
ashik.ali |
14 |
import org.springframework.http.HttpStatus;
|
|
|
15 |
import org.springframework.http.ResponseEntity;
|
|
|
16 |
import org.springframework.stereotype.Controller;
|
| 21366 |
kshitij.so |
17 |
import org.springframework.web.bind.annotation.RequestBody;
|
| 21248 |
ashik.ali |
18 |
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
19 |
import org.springframework.web.bind.annotation.RequestMethod;
|
|
|
20 |
import org.springframework.web.bind.annotation.RequestParam;
|
|
|
21 |
|
|
|
22 |
import com.spice.profitmandi.common.ResponseCodeHolder;
|
|
|
23 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
|
|
24 |
import com.spice.profitmandi.common.model.ProfitMandiConstants;
|
| 21282 |
ashik.ali |
25 |
import com.spice.profitmandi.common.util.JWTUtil;
|
| 21426 |
ashik.ali |
26 |
import com.spice.profitmandi.dao.entity.Permission;
|
| 21414 |
kshitij.so |
27 |
import com.spice.profitmandi.dao.entity.Role;
|
| 21278 |
ashik.ali |
28 |
import com.spice.profitmandi.dao.entity.User;
|
| 21426 |
ashik.ali |
29 |
import com.spice.profitmandi.dao.entity.UserRole;
|
|
|
30 |
import com.spice.profitmandi.dao.repository.PermissionRepository;
|
|
|
31 |
import com.spice.profitmandi.dao.repository.RoleRepository;
|
| 21248 |
ashik.ali |
32 |
import com.spice.profitmandi.dao.repository.UserRepository;
|
| 21426 |
ashik.ali |
33 |
import com.spice.profitmandi.dao.repository.UserRoleRepository;
|
| 21248 |
ashik.ali |
34 |
import com.spice.profitmandi.web.model.ProfitMandiResponse;
|
|
|
35 |
import com.spice.profitmandi.web.model.Response;
|
|
|
36 |
import com.spice.profitmandi.web.model.ResponseStatus;
|
| 21277 |
ashik.ali |
37 |
import com.spice.profitmandi.web.processor.GoogleLoginProcessor;
|
| 21426 |
ashik.ali |
38 |
import com.spice.profitmandi.web.req.UserAddRoleRequest;
|
| 21366 |
kshitij.so |
39 |
import com.spice.profitmandi.web.req.UserRequest;
|
| 21448 |
ashik.ali |
40 |
import com.spice.profitmandi.web.util.ResponseSender;
|
| 21248 |
ashik.ali |
41 |
|
|
|
42 |
/**
|
|
|
43 |
* @author ashikali
|
|
|
44 |
*
|
|
|
45 |
*/
|
|
|
46 |
@Controller
|
|
|
47 |
public class UserController {
|
| 21426 |
ashik.ali |
48 |
|
| 21448 |
ashik.ali |
49 |
@Autowired
|
|
|
50 |
ResponseSender<?> responseSender;
|
|
|
51 |
|
| 21248 |
ashik.ali |
52 |
private static final Logger LOGGER=LoggerFactory.getLogger(UserController.class);
|
|
|
53 |
|
| 21414 |
kshitij.so |
54 |
@Value("${admin.token}")
|
|
|
55 |
private String validAdminToken;
|
| 21426 |
ashik.ali |
56 |
|
| 21278 |
ashik.ali |
57 |
@Autowired
|
|
|
58 |
UserRepository userRepository;
|
| 21426 |
ashik.ali |
59 |
|
| 21278 |
ashik.ali |
60 |
@Autowired
|
| 21426 |
ashik.ali |
61 |
RoleRepository roleRepository;
|
|
|
62 |
|
|
|
63 |
@Autowired
|
|
|
64 |
UserRoleRepository userRoleRepository;
|
|
|
65 |
|
|
|
66 |
@Autowired
|
|
|
67 |
PermissionRepository permissionRepository;
|
|
|
68 |
|
|
|
69 |
@Autowired
|
| 21278 |
ashik.ali |
70 |
GoogleLoginProcessor googleLoginProcessor;
|
| 21426 |
ashik.ali |
71 |
|
| 21277 |
ashik.ali |
72 |
@SuppressWarnings("unchecked")
|
|
|
73 |
@RequestMapping(value = ProfitMandiConstants.URL_USER_GOOGLE_LOGIN,method=RequestMethod.POST)
|
|
|
74 |
public ResponseEntity<?> googleLogin(HttpServletRequest request){
|
|
|
75 |
LOGGER.info("requested url : "+request.getRequestURL().toString());
|
|
|
76 |
final Map<String, Object> googleLoginMap = (Map<String, Object>)request.getAttribute(ProfitMandiConstants.GOOGLE_LOGIN_MAP);
|
|
|
77 |
request.removeAttribute(ProfitMandiConstants.GOOGLE_LOGIN_MAP);
|
|
|
78 |
try {
|
| 21448 |
ashik.ali |
79 |
return responseSender.ok(googleLoginProcessor.process(googleLoginMap));
|
| 21440 |
ashik.ali |
80 |
}catch (ProfitMandiBusinessException profitMandiBusinessException) {
|
|
|
81 |
LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
|
| 21448 |
ashik.ali |
82 |
return responseSender.badRequest(profitMandiBusinessException);
|
| 21277 |
ashik.ali |
83 |
}
|
|
|
84 |
}
|
| 21426 |
ashik.ali |
85 |
|
| 21282 |
ashik.ali |
86 |
@RequestMapping(value = ProfitMandiConstants.URL_USER_TOKEN_IS_EXPIRED, method=RequestMethod.GET)
|
|
|
87 |
public ResponseEntity<?> tokenIsExpired(HttpServletRequest request, @RequestParam(name = "token") String token){
|
|
|
88 |
LOGGER.info("requested url : "+request.getRequestURL().toString());
|
|
|
89 |
try {
|
| 21448 |
ashik.ali |
90 |
return responseSender.ok(JWTUtil.isExpired(token));
|
|
|
91 |
|
| 21440 |
ashik.ali |
92 |
}catch (ProfitMandiBusinessException profitMandiBusinessException) {
|
|
|
93 |
LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
|
| 21448 |
ashik.ali |
94 |
return responseSender.badRequest(profitMandiBusinessException);
|
| 21282 |
ashik.ali |
95 |
}
|
|
|
96 |
}
|
| 21426 |
ashik.ali |
97 |
|
|
|
98 |
|
| 21278 |
ashik.ali |
99 |
@RequestMapping(value = ProfitMandiConstants.URL_USER, method=RequestMethod.POST)
|
| 21366 |
kshitij.so |
100 |
public ResponseEntity<?> createUser(HttpServletRequest request, @RequestBody UserRequest userRequest){
|
| 21278 |
ashik.ali |
101 |
LOGGER.info("requested url : "+request.getRequestURL().toString());
|
| 21368 |
kshitij.so |
102 |
User user = new User();
|
|
|
103 |
user.setFirstName(userRequest.getFirstName());
|
|
|
104 |
user.setLastName(userRequest.getLastName());
|
|
|
105 |
user.setCity(userRequest.getCity());
|
|
|
106 |
user.setPinCode(Integer.valueOf(userRequest.getPinCode()));
|
|
|
107 |
user.setEmailId(userRequest.getEmailId());
|
|
|
108 |
user.setUsername("");
|
|
|
109 |
user.setPassword("");
|
|
|
110 |
user.setMobile_verified(false);
|
|
|
111 |
user.setReferral_url("");
|
|
|
112 |
user.setGroup_id(1);
|
|
|
113 |
user.setStatus(0);
|
|
|
114 |
user.setActivated(false);
|
| 21414 |
kshitij.so |
115 |
|
| 21278 |
ashik.ali |
116 |
try {
|
|
|
117 |
user.setCreateTimestamp(LocalDateTime.now());
|
|
|
118 |
user.setUpdateTimestamp(LocalDateTime.now());
|
|
|
119 |
userRepository.persist(user);
|
| 21448 |
ashik.ali |
120 |
return responseSender.ok(ResponseCodeHolder.getMessage("USR_OK_1000"));
|
|
|
121 |
|
| 21440 |
ashik.ali |
122 |
}catch (ProfitMandiBusinessException profitMandiBusinessException) {
|
|
|
123 |
LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
|
| 21448 |
ashik.ali |
124 |
return responseSender.badRequest(profitMandiBusinessException);
|
| 21278 |
ashik.ali |
125 |
}
|
|
|
126 |
}
|
| 21426 |
ashik.ali |
127 |
|
| 21248 |
ashik.ali |
128 |
@RequestMapping(value = ProfitMandiConstants.URL_USER_ALL,method=RequestMethod.GET)
|
|
|
129 |
public ResponseEntity<?> getAll(HttpServletRequest request){
|
|
|
130 |
LOGGER.info("requested url : "+request.getRequestURL().toString());
|
| 21448 |
ashik.ali |
131 |
return responseSender.ok(userRepository.selectAll());
|
| 21248 |
ashik.ali |
132 |
}
|
| 21426 |
ashik.ali |
133 |
|
| 21248 |
ashik.ali |
134 |
@RequestMapping(value = ProfitMandiConstants.URL_USER_ID,method=RequestMethod.GET)
|
| 21432 |
ashik.ali |
135 |
public ResponseEntity<?> getById(HttpServletRequest request, @RequestParam(name = "id") int id){
|
| 21248 |
ashik.ali |
136 |
LOGGER.info("requested url : "+request.getRequestURL().toString());
|
|
|
137 |
try {
|
| 21448 |
ashik.ali |
138 |
return responseSender.ok(userRepository.selectById(id));
|
| 21440 |
ashik.ali |
139 |
}catch (ProfitMandiBusinessException profitMandiBusinessException) {
|
|
|
140 |
LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
|
| 21448 |
ashik.ali |
141 |
return responseSender.badRequest(profitMandiBusinessException);
|
| 21248 |
ashik.ali |
142 |
}
|
|
|
143 |
}
|
| 21426 |
ashik.ali |
144 |
|
| 21248 |
ashik.ali |
145 |
@RequestMapping(value = ProfitMandiConstants.URL_USER_MOBILE_NUMBER,method=RequestMethod.GET)
|
|
|
146 |
public ResponseEntity<?> getByMobileNumber(HttpServletRequest request, @RequestParam(name = "mobileNumber") String mobileNumber){
|
|
|
147 |
LOGGER.info("requested url : "+request.getRequestURL().toString());
|
|
|
148 |
try {
|
| 21448 |
ashik.ali |
149 |
return responseSender.ok(userRepository.selectByMobileNumber(mobileNumber));
|
| 21440 |
ashik.ali |
150 |
}catch (ProfitMandiBusinessException profitMandiBusinessException) {
|
|
|
151 |
LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
|
| 21448 |
ashik.ali |
152 |
return responseSender.badRequest(profitMandiBusinessException);
|
| 21248 |
ashik.ali |
153 |
}
|
|
|
154 |
}
|
| 21426 |
ashik.ali |
155 |
|
|
|
156 |
@RequestMapping(value = ProfitMandiConstants.URL_USER_IS_EXIST_MOBILE_NUMBER, method = RequestMethod.GET)
|
|
|
157 |
public ResponseEntity<?> isMobileNumberExist(HttpServletRequest request, @RequestParam(name = "mobileNumber") String mobileNumber){
|
|
|
158 |
LOGGER.info("requested url : "+request.getRequestURL().toString());
|
| 21448 |
ashik.ali |
159 |
return responseSender.ok(userRepository.isExistByMobileNumber(mobileNumber));
|
| 21426 |
ashik.ali |
160 |
}
|
|
|
161 |
|
|
|
162 |
|
| 21248 |
ashik.ali |
163 |
@RequestMapping(value = ProfitMandiConstants.URL_USER_EMAIL_ID,method=RequestMethod.GET)
|
|
|
164 |
public ResponseEntity<?> getByEmailId(HttpServletRequest request, @RequestParam(name = "emailId") String emailId){
|
|
|
165 |
LOGGER.info("requested url : "+request.getRequestURL().toString());
|
|
|
166 |
try {
|
| 21448 |
ashik.ali |
167 |
return responseSender.ok(userRepository.selectByEmailId(emailId));
|
| 21440 |
ashik.ali |
168 |
}catch (ProfitMandiBusinessException profitMandiBusinessException) {
|
|
|
169 |
LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
|
| 21448 |
ashik.ali |
170 |
return responseSender.badRequest(profitMandiBusinessException);
|
| 21248 |
ashik.ali |
171 |
}
|
|
|
172 |
}
|
| 21426 |
ashik.ali |
173 |
|
|
|
174 |
@RequestMapping(value = ProfitMandiConstants.URL_USER_ROLE_ADD, method=RequestMethod.POST)
|
|
|
175 |
public ResponseEntity<?> addRole(HttpServletRequest request, @RequestBody UserAddRoleRequest userAddRoleRequest){
|
| 21248 |
ashik.ali |
176 |
LOGGER.info("requested url : "+request.getRequestURL().toString());
|
|
|
177 |
try {
|
| 21426 |
ashik.ali |
178 |
User user = userRepository.selectById(userAddRoleRequest.getUserId());
|
|
|
179 |
|
|
|
180 |
Role role = null;
|
|
|
181 |
try{
|
|
|
182 |
role = roleRepository.selectByNameAndType(userAddRoleRequest.getRole().getName(), userAddRoleRequest.getRole().getType());
|
|
|
183 |
}catch(ProfitMandiBusinessException profitMandiBusinessException){
|
|
|
184 |
role = new Role();
|
|
|
185 |
role.setName(userAddRoleRequest.getRole().getName());
|
|
|
186 |
role.setType(userAddRoleRequest.getRole().getType());
|
|
|
187 |
roleRepository.persist(role);
|
|
|
188 |
}
|
|
|
189 |
Permission permission = new Permission();
|
|
|
190 |
permission.setType(userAddRoleRequest.getRole().getPermissionType());
|
|
|
191 |
permission.setRoleId(role.getId());
|
|
|
192 |
permissionRepository.persist(permission);
|
|
|
193 |
UserRole userRole = new UserRole();
|
|
|
194 |
userRole.setRoleId(role.getId());
|
|
|
195 |
userRole.setUserId(user.getId());
|
|
|
196 |
userRoleRepository.persist(userRole);
|
| 21448 |
ashik.ali |
197 |
return responseSender.ok("");
|
| 21440 |
ashik.ali |
198 |
}catch (ProfitMandiBusinessException profitMandiBusinessException) {
|
|
|
199 |
LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
|
| 21448 |
ashik.ali |
200 |
return responseSender.badRequest(profitMandiBusinessException);
|
| 21248 |
ashik.ali |
201 |
}
|
|
|
202 |
}
|
| 21426 |
ashik.ali |
203 |
|
|
|
204 |
@RequestMapping(value = ProfitMandiConstants.URL_USER_ROLE_REMOVE,method=RequestMethod.DELETE)
|
| 21432 |
ashik.ali |
205 |
public ResponseEntity<?> removeRole(HttpServletRequest request, @RequestParam(name = "roleId") int roleId, @RequestParam(name = "userId") int userId){
|
| 21248 |
ashik.ali |
206 |
LOGGER.info("requested url : "+request.getRequestURL().toString());
|
|
|
207 |
try {
|
| 21426 |
ashik.ali |
208 |
roleRepository.selectById(roleId);
|
|
|
209 |
userRepository.selectById(userId);
|
|
|
210 |
userRoleRepository.deleteByUserAndRoleId(userId, roleId);
|
|
|
211 |
permissionRepository.deleteByRoleId(roleId);
|
| 21448 |
ashik.ali |
212 |
return responseSender.ok("");
|
| 21440 |
ashik.ali |
213 |
}catch (ProfitMandiBusinessException profitMandiBusinessException) {
|
|
|
214 |
LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
|
| 21448 |
ashik.ali |
215 |
return responseSender.badRequest(profitMandiBusinessException);
|
| 21248 |
ashik.ali |
216 |
}
|
|
|
217 |
}
|
| 21426 |
ashik.ali |
218 |
|
|
|
219 |
|
|
|
220 |
@RequestMapping(value = ProfitMandiConstants.URL_USER_ROLE_ALL,method=RequestMethod.GET)
|
| 21432 |
ashik.ali |
221 |
public ResponseEntity<?> getAllRoles(HttpServletRequest request, @RequestParam(name = "id") int id){
|
| 21248 |
ashik.ali |
222 |
LOGGER.info("requested url : "+request.getRequestURL().toString());
|
| 21448 |
ashik.ali |
223 |
return responseSender.ok(userRoleRepository.selectRolesByUserId(id));
|
| 21248 |
ashik.ali |
224 |
}
|
| 21414 |
kshitij.so |
225 |
|
|
|
226 |
@RequestMapping(value = ProfitMandiConstants.URL_ADMIN_TOKEN, method = RequestMethod.POST)
|
|
|
227 |
public ResponseEntity<?> getAdminToken(HttpServletRequest request, @RequestParam(name = "adminToken") String adminToken, @RequestParam(name = "emailId") String emailId){
|
|
|
228 |
LOGGER.info("requested url : "+request.getRequestURL().toString());
|
|
|
229 |
if (!adminToken.equals(validAdminToken)){
|
|
|
230 |
final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.FORBIDDEN.toString(), HttpStatus.FORBIDDEN, ResponseStatus.FAILURE, null);
|
|
|
231 |
return new ResponseEntity<>(profitMandiResponse, HttpStatus.FORBIDDEN);
|
|
|
232 |
}
|
|
|
233 |
|
|
|
234 |
|
|
|
235 |
Map<String, Object> responseMap = new HashMap<>(2);
|
|
|
236 |
try{
|
|
|
237 |
User user = userRepository.selectByEmailId(emailId);
|
|
|
238 |
Set<Role> roles = user.getRoles();
|
|
|
239 |
String[] roleTypes = new String[roles.size()];
|
|
|
240 |
int index = 0;
|
|
|
241 |
for(Role role : roles){
|
|
|
242 |
roleTypes[index++] = role.getType().toString();
|
|
|
243 |
}
|
|
|
244 |
responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(user.getId(), roleTypes));
|
|
|
245 |
responseMap.put(ProfitMandiConstants.REGISTERED, true);
|
| 21440 |
ashik.ali |
246 |
}catch (ProfitMandiBusinessException profitMandiBusinessException) {
|
| 21414 |
kshitij.so |
247 |
responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create());
|
|
|
248 |
responseMap.put(ProfitMandiConstants.REGISTERED, false);
|
|
|
249 |
}
|
| 21448 |
ashik.ali |
250 |
return responseSender.ok(responseMap);
|
|
|
251 |
|
| 21414 |
kshitij.so |
252 |
}
|
| 21248 |
ashik.ali |
253 |
}
|