Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
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;
21469 amit.gupta 7
import java.util.function.Predicate;
21248 ashik.ali 8
 
9
import javax.servlet.http.HttpServletRequest;
10
 
11
import org.slf4j.Logger;
12
import org.slf4j.LoggerFactory;
21278 ashik.ali 13
import org.springframework.beans.factory.annotation.Autowired;
21414 kshitij.so 14
import org.springframework.beans.factory.annotation.Value;
21248 ashik.ali 15
import org.springframework.http.HttpStatus;
16
import org.springframework.http.ResponseEntity;
17
import org.springframework.stereotype.Controller;
21366 kshitij.so 18
import org.springframework.web.bind.annotation.RequestBody;
21248 ashik.ali 19
import org.springframework.web.bind.annotation.RequestMapping;
20
import org.springframework.web.bind.annotation.RequestMethod;
21
import org.springframework.web.bind.annotation.RequestParam;
22
 
21483 amit.gupta 23
import com.auth0.jwt.JWT;
21248 ashik.ali 24
import com.spice.profitmandi.common.ResponseCodeHolder;
25
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
26
import com.spice.profitmandi.common.model.ProfitMandiConstants;
21469 amit.gupta 27
import com.spice.profitmandi.common.model.UserInfo;
21282 ashik.ali 28
import com.spice.profitmandi.common.util.JWTUtil;
21426 ashik.ali 29
import com.spice.profitmandi.dao.entity.Permission;
21414 kshitij.so 30
import com.spice.profitmandi.dao.entity.Role;
21278 ashik.ali 31
import com.spice.profitmandi.dao.entity.User;
21426 ashik.ali 32
import com.spice.profitmandi.dao.entity.UserRole;
21469 amit.gupta 33
import com.spice.profitmandi.dao.enumuration.RoleType;
21426 ashik.ali 34
import com.spice.profitmandi.dao.repository.PermissionRepository;
35
import com.spice.profitmandi.dao.repository.RoleRepository;
21248 ashik.ali 36
import com.spice.profitmandi.dao.repository.UserRepository;
21426 ashik.ali 37
import com.spice.profitmandi.dao.repository.UserRoleRepository;
21469 amit.gupta 38
import com.spice.profitmandi.web.enumuration.UserStatus;
21248 ashik.ali 39
import com.spice.profitmandi.web.model.ProfitMandiResponse;
40
import com.spice.profitmandi.web.model.ResponseStatus;
21277 ashik.ali 41
import com.spice.profitmandi.web.processor.GoogleLoginProcessor;
21426 ashik.ali 42
import com.spice.profitmandi.web.req.UserAddRoleRequest;
21366 kshitij.so 43
import com.spice.profitmandi.web.req.UserRequest;
21448 ashik.ali 44
import com.spice.profitmandi.web.util.ResponseSender;
21248 ashik.ali 45
 
21469 amit.gupta 46
import io.swagger.annotations.ApiImplicitParam;
47
import io.swagger.annotations.ApiImplicitParams;
48
 
21248 ashik.ali 49
/**
50
 * @author ashikali
51
 *
52
 */
53
@Controller
54
public class UserController {
21469 amit.gupta 55
 
21448 ashik.ali 56
	@Autowired
57
	ResponseSender<?> responseSender;
21469 amit.gupta 58
 
59
	private static final Logger LOGGER = LoggerFactory.getLogger(UserController.class);
60
 
21414 kshitij.so 61
	@Value("${admin.token}")
62
	private String validAdminToken;
21469 amit.gupta 63
 
21278 ashik.ali 64
	@Autowired
65
	UserRepository userRepository;
21469 amit.gupta 66
 
21278 ashik.ali 67
	@Autowired
21426 ashik.ali 68
	RoleRepository roleRepository;
21469 amit.gupta 69
 
21426 ashik.ali 70
	@Autowired
71
	UserRoleRepository userRoleRepository;
21469 amit.gupta 72
 
21426 ashik.ali 73
	@Autowired
74
	PermissionRepository permissionRepository;
21469 amit.gupta 75
 
21426 ashik.ali 76
	@Autowired
21278 ashik.ali 77
	GoogleLoginProcessor googleLoginProcessor;
21469 amit.gupta 78
 
21277 ashik.ali 79
	@SuppressWarnings("unchecked")
21469 amit.gupta 80
	@RequestMapping(value = ProfitMandiConstants.URL_USER_GOOGLE_LOGIN, method = RequestMethod.POST)
81
	public ResponseEntity<?> googleLogin(HttpServletRequest request) {
82
		LOGGER.info("requested url : " + request.getRequestURL().toString());
83
		final Map<String, Object> googleLoginMap = (Map<String, Object>) request
84
				.getAttribute(ProfitMandiConstants.GOOGLE_LOGIN_MAP);
21277 ashik.ali 85
		request.removeAttribute(ProfitMandiConstants.GOOGLE_LOGIN_MAP);
86
		try {
21448 ashik.ali 87
			return responseSender.ok(googleLoginProcessor.process(googleLoginMap));
21469 amit.gupta 88
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
89
			LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
21448 ashik.ali 90
			return responseSender.badRequest(profitMandiBusinessException);
21277 ashik.ali 91
		}
92
	}
21469 amit.gupta 93
 
94
	@RequestMapping(value = ProfitMandiConstants.URL_USER_TOKEN_IS_EXPIRED, method = RequestMethod.GET)
95
	public ResponseEntity<?> tokenIsExpired(HttpServletRequest request, @RequestParam(name = "token") String token) {
96
		LOGGER.info("requested url : " + request.getRequestURL().toString());
21282 ashik.ali 97
		try {
21448 ashik.ali 98
			return responseSender.ok(JWTUtil.isExpired(token));
21469 amit.gupta 99
 
100
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
101
			LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
21448 ashik.ali 102
			return responseSender.badRequest(profitMandiBusinessException);
21282 ashik.ali 103
		}
104
	}
21469 amit.gupta 105
 
106
	@RequestMapping(value = ProfitMandiConstants.URL_USER_TOKEN_INFO, method = RequestMethod.GET)
107
	@ApiImplicitParams({
108
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
109
	public ResponseEntity<?> tokenInfo(HttpServletRequest request) throws Throwable {
110
		Map<String, Object> responseMap = new HashMap<>();
111
		LOGGER.info("requested url : " + request.getRequestURL().toString());
112
		UserInfo userInfo = (UserInfo) request.getAttribute("userInfo");
21483 amit.gupta 113
		User user = null;
114
		if(userInfo.getUserId()>-1){
115
			user = userRepository.selectById(userInfo.getUserId());
116
		} else {
117
			try {
118
				user = userRepository.selectByEmailId(userInfo.getEmail());
119
			} catch (ProfitMandiBusinessException e1) {
120
				LOGGER.info("Uneregistered user");
121
			}
122
		}
123
		if (user != null) {
124
 
125
			Set<Role> roles = user.getRoles();
126
			//generate new token if roles size is different
127
			if(userInfo.getRoleNames() == null || roles.size() != userInfo.getRoleNames().size()) {
128
				String[] roleTypes = new String[roles.size()];
129
				int index = 0;
130
				for (Role role : roles) {
131
					roleTypes[index++] = role.getType().toString();
132
				}
133
				String newToken = JWTUtil.create(user.getId(), roleTypes);
134
				responseMap.put("newAuthToken", newToken);
135
			}
136
 
21469 amit.gupta 137
			// if user is retailer
138
			if (user.getRoles().stream().anyMatch(new Predicate<Role>() {
139
				@Override
140
				public boolean test(Role t) {
141
					return t.getType().equals(RoleType.RETAILER);
142
				}
143
			})) {
144
				// TODO: This should be from retailer Table
145
				// if retailer is activated and migrated is 0 then verified
146
				// retailer
147
				// if retailer is not activated and and migrated is 0 then not
148
				// verified retailer
149
				// if retailer is activated and migrated is 1 then retailer is
150
				// retailer.
151
				if (user.isActivated()) {
152
					responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.VERIFIED_RETAILER.getValue());
153
				} else {
21483 amit.gupta 154
					responseMap.put(ProfitMandiConstants.NEW_AUTH_TOKEN, UserStatus.NOT_VERIFIED_RETAILER.getValue());
21469 amit.gupta 155
				}
156
			} else if (user.getRoles().stream().anyMatch(new Predicate<Role>() {
157
				@Override
158
				public boolean test(Role t) {
159
					return t.getType().equals(RoleType.USER);
160
				}
161
			})) {
162
				responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.REGISTERED.getValue());
21479 amit.gupta 163
			} else {
164
				responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.RETAILER.getValue());
21469 amit.gupta 165
			}
166
		} else {
167
			responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.NOT_REGISTERED.getValue());
168
			responseMap.put(ProfitMandiConstants.EMAIL_ID, userInfo.getEmail());
169
		}
170
 
171
		return responseSender.ok(responseMap);
172
 
173
	}
174
 
175
	@RequestMapping(value = ProfitMandiConstants.URL_USER, method = RequestMethod.POST)
176
	public ResponseEntity<?> createUser(HttpServletRequest request, @RequestBody UserRequest userRequest) {
177
		LOGGER.info("requested url : " + request.getRequestURL().toString());
21368 kshitij.so 178
		User user = new User();
179
		user.setFirstName(userRequest.getFirstName());
180
		user.setLastName(userRequest.getLastName());
181
		user.setCity(userRequest.getCity());
182
		user.setPinCode(Integer.valueOf(userRequest.getPinCode()));
183
		user.setEmailId(userRequest.getEmailId());
184
		user.setUsername("");
185
		user.setPassword("");
186
		user.setMobile_verified(false);
187
		user.setReferral_url("");
188
		user.setGroup_id(1);
189
		user.setStatus(0);
190
		user.setActivated(false);
21414 kshitij.so 191
 
21278 ashik.ali 192
		try {
193
			user.setCreateTimestamp(LocalDateTime.now());
194
			user.setUpdateTimestamp(LocalDateTime.now());
195
			userRepository.persist(user);
21448 ashik.ali 196
			return responseSender.ok(ResponseCodeHolder.getMessage("USR_OK_1000"));
21469 amit.gupta 197
 
198
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
199
			LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
21448 ashik.ali 200
			return responseSender.badRequest(profitMandiBusinessException);
21278 ashik.ali 201
		}
202
	}
21469 amit.gupta 203
 
204
	@RequestMapping(value = ProfitMandiConstants.URL_USER_ALL, method = RequestMethod.GET)
205
	public ResponseEntity<?> getAll(HttpServletRequest request) {
206
		LOGGER.info("requested url : " + request.getRequestURL().toString());
21448 ashik.ali 207
		return responseSender.ok(userRepository.selectAll());
21248 ashik.ali 208
	}
21469 amit.gupta 209
 
210
	@RequestMapping(value = ProfitMandiConstants.URL_USER_ID, method = RequestMethod.GET)
211
	public ResponseEntity<?> getById(HttpServletRequest request, @RequestParam(name = "id") int id) {
212
		LOGGER.info("requested url : " + request.getRequestURL().toString());
21248 ashik.ali 213
		try {
21448 ashik.ali 214
			return responseSender.ok(userRepository.selectById(id));
21469 amit.gupta 215
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
216
			LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
21448 ashik.ali 217
			return responseSender.badRequest(profitMandiBusinessException);
21248 ashik.ali 218
		}
219
	}
21469 amit.gupta 220
 
221
	@RequestMapping(value = ProfitMandiConstants.URL_USER_MOBILE_NUMBER, method = RequestMethod.GET)
222
	public ResponseEntity<?> getByMobileNumber(HttpServletRequest request,
223
			@RequestParam(name = "mobileNumber") String mobileNumber) {
224
		LOGGER.info("requested url : " + request.getRequestURL().toString());
21248 ashik.ali 225
		try {
21448 ashik.ali 226
			return responseSender.ok(userRepository.selectByMobileNumber(mobileNumber));
21469 amit.gupta 227
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
228
			LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
21448 ashik.ali 229
			return responseSender.badRequest(profitMandiBusinessException);
21248 ashik.ali 230
		}
231
	}
21469 amit.gupta 232
 
21426 ashik.ali 233
	@RequestMapping(value = ProfitMandiConstants.URL_USER_IS_EXIST_MOBILE_NUMBER, method = RequestMethod.GET)
21469 amit.gupta 234
	public ResponseEntity<?> isMobileNumberExist(HttpServletRequest request,
235
			@RequestParam(name = "mobileNumber") String mobileNumber) {
236
		LOGGER.info("requested url : " + request.getRequestURL().toString());
21448 ashik.ali 237
		return responseSender.ok(userRepository.isExistByMobileNumber(mobileNumber));
21426 ashik.ali 238
	}
21469 amit.gupta 239
 
240
	@RequestMapping(value = ProfitMandiConstants.URL_USER_EMAIL_ID, method = RequestMethod.GET)
241
	public ResponseEntity<?> getByEmailId(HttpServletRequest request, @RequestParam(name = "emailId") String emailId) {
242
		LOGGER.info("requested url : " + request.getRequestURL().toString());
21248 ashik.ali 243
		try {
21448 ashik.ali 244
			return responseSender.ok(userRepository.selectByEmailId(emailId));
21469 amit.gupta 245
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
246
			LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
21448 ashik.ali 247
			return responseSender.badRequest(profitMandiBusinessException);
21248 ashik.ali 248
		}
249
	}
21469 amit.gupta 250
 
251
	@RequestMapping(value = ProfitMandiConstants.URL_USER_ROLE_ADD, method = RequestMethod.POST)
252
	public ResponseEntity<?> addRole(HttpServletRequest request, @RequestBody UserAddRoleRequest userAddRoleRequest) {
253
		LOGGER.info("requested url : " + request.getRequestURL().toString());
21248 ashik.ali 254
		try {
21426 ashik.ali 255
			User user = userRepository.selectById(userAddRoleRequest.getUserId());
21469 amit.gupta 256
 
21426 ashik.ali 257
			Role role = null;
21469 amit.gupta 258
			try {
259
				role = roleRepository.selectByNameAndType(userAddRoleRequest.getRole().getName(),
260
						userAddRoleRequest.getRole().getType());
261
			} catch (ProfitMandiBusinessException profitMandiBusinessException) {
21426 ashik.ali 262
				role = new Role();
263
				role.setName(userAddRoleRequest.getRole().getName());
264
				role.setType(userAddRoleRequest.getRole().getType());
265
				roleRepository.persist(role);
266
			}
267
			Permission permission = new Permission();
268
			permission.setType(userAddRoleRequest.getRole().getPermissionType());
269
			permission.setRoleId(role.getId());
270
			permissionRepository.persist(permission);
271
			UserRole userRole = new UserRole();
272
			userRole.setRoleId(role.getId());
273
			userRole.setUserId(user.getId());
274
			userRoleRepository.persist(userRole);
21448 ashik.ali 275
			return responseSender.ok("");
21469 amit.gupta 276
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
277
			LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
21448 ashik.ali 278
			return responseSender.badRequest(profitMandiBusinessException);
21248 ashik.ali 279
		}
280
	}
21469 amit.gupta 281
 
282
	@RequestMapping(value = ProfitMandiConstants.URL_USER_ROLE_REMOVE, method = RequestMethod.DELETE)
283
	public ResponseEntity<?> removeRole(HttpServletRequest request, @RequestParam(name = "roleId") int roleId,
284
			@RequestParam(name = "userId") int userId) {
285
		LOGGER.info("requested url : " + request.getRequestURL().toString());
21248 ashik.ali 286
		try {
21426 ashik.ali 287
			roleRepository.selectById(roleId);
288
			userRepository.selectById(userId);
289
			userRoleRepository.deleteByUserAndRoleId(userId, roleId);
290
			permissionRepository.deleteByRoleId(roleId);
21448 ashik.ali 291
			return responseSender.ok("");
21469 amit.gupta 292
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
293
			LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
21448 ashik.ali 294
			return responseSender.badRequest(profitMandiBusinessException);
21248 ashik.ali 295
		}
296
	}
21469 amit.gupta 297
 
298
	@RequestMapping(value = ProfitMandiConstants.URL_USER_ROLE_ALL, method = RequestMethod.GET)
299
	public ResponseEntity<?> getAllRoles(HttpServletRequest request, @RequestParam(name = "id") int id) {
300
		LOGGER.info("requested url : " + request.getRequestURL().toString());
21448 ashik.ali 301
		return responseSender.ok(userRoleRepository.selectRolesByUserId(id));
21248 ashik.ali 302
	}
21414 kshitij.so 303
 
304
	@RequestMapping(value = ProfitMandiConstants.URL_ADMIN_TOKEN, method = RequestMethod.POST)
21469 amit.gupta 305
	public ResponseEntity<?> getAdminToken(HttpServletRequest request,
306
			@RequestParam(name = "adminToken") String adminToken, @RequestParam(name = "emailId") String emailId) {
307
		LOGGER.info("requested url : " + request.getRequestURL().toString());
308
		if (!adminToken.equals(validAdminToken)) {
309
			final ProfitMandiResponse<?> profitMandiResponse = new ProfitMandiResponse<>(LocalDateTime.now(),
310
					request.getRequestURL().toString(), HttpStatus.FORBIDDEN.toString(), HttpStatus.FORBIDDEN,
311
					ResponseStatus.FAILURE, null);
21414 kshitij.so 312
			return new ResponseEntity<>(profitMandiResponse, HttpStatus.FORBIDDEN);
313
		}
21469 amit.gupta 314
 
21414 kshitij.so 315
		Map<String, Object> responseMap = new HashMap<>(2);
21469 amit.gupta 316
		try {
21414 kshitij.so 317
			User user = userRepository.selectByEmailId(emailId);
318
			Set<Role> roles = user.getRoles();
319
			String[] roleTypes = new String[roles.size()];
320
			int index = 0;
21469 amit.gupta 321
			for (Role role : roles) {
21414 kshitij.so 322
				roleTypes[index++] = role.getType().toString();
323
			}
324
			responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(user.getId(), roleTypes));
325
			responseMap.put(ProfitMandiConstants.REGISTERED, true);
21469 amit.gupta 326
		} catch (ProfitMandiBusinessException profitMandiBusinessException) {
327
			responseMap.put(ProfitMandiConstants.TOKEN, JWTUtil.create(emailId));
21414 kshitij.so 328
			responseMap.put(ProfitMandiConstants.REGISTERED, false);
329
		}
21448 ashik.ali 330
		return responseSender.ok(responseMap);
21469 amit.gupta 331
 
21414 kshitij.so 332
	}
21248 ashik.ali 333
}