Subversion Repositories SmartDukaan

Rev

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

Rev 21479 Rev 21483
Line 18... Line 18...
18
import org.springframework.web.bind.annotation.RequestBody;
18
import org.springframework.web.bind.annotation.RequestBody;
19
import org.springframework.web.bind.annotation.RequestMapping;
19
import org.springframework.web.bind.annotation.RequestMapping;
20
import org.springframework.web.bind.annotation.RequestMethod;
20
import org.springframework.web.bind.annotation.RequestMethod;
21
import org.springframework.web.bind.annotation.RequestParam;
21
import org.springframework.web.bind.annotation.RequestParam;
22
 
22
 
-
 
23
import com.auth0.jwt.JWT;
23
import com.spice.profitmandi.common.ResponseCodeHolder;
24
import com.spice.profitmandi.common.ResponseCodeHolder;
24
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
25
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
25
import com.spice.profitmandi.common.model.ProfitMandiConstants;
26
import com.spice.profitmandi.common.model.ProfitMandiConstants;
26
import com.spice.profitmandi.common.model.UserInfo;
27
import com.spice.profitmandi.common.model.UserInfo;
27
import com.spice.profitmandi.common.util.JWTUtil;
28
import com.spice.profitmandi.common.util.JWTUtil;
Line 107... Line 108...
107
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
108
			@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
108
	public ResponseEntity<?> tokenInfo(HttpServletRequest request) throws Throwable {
109
	public ResponseEntity<?> tokenInfo(HttpServletRequest request) throws Throwable {
109
		Map<String, Object> responseMap = new HashMap<>();
110
		Map<String, Object> responseMap = new HashMap<>();
110
		LOGGER.info("requested url : " + request.getRequestURL().toString());
111
		LOGGER.info("requested url : " + request.getRequestURL().toString());
111
		UserInfo userInfo = (UserInfo) request.getAttribute("userInfo");
112
		UserInfo userInfo = (UserInfo) request.getAttribute("userInfo");
-
 
113
		User user = null;
112
		if (userInfo.getUserId() > 0) {
114
		if(userInfo.getUserId()>-1){
113
			User user = userRepository.selectById(userInfo.getUserId());
115
			user = userRepository.selectById(userInfo.getUserId());
-
 
116
		} else {
-
 
117
			try {
114
			responseMap.put(ProfitMandiConstants.EMAIL_ID, user.getEmailId());
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);
115
			responseMap.put(ProfitMandiConstants.USER_ID, user.getId());
134
				responseMap.put("newAuthToken", newToken);
-
 
135
			}
-
 
136
			
116
			// if user is retailer
137
			// if user is retailer
117
			if (user.getRoles().stream().anyMatch(new Predicate<Role>() {
138
			if (user.getRoles().stream().anyMatch(new Predicate<Role>() {
118
				@Override
139
				@Override
119
				public boolean test(Role t) {
140
				public boolean test(Role t) {
120
					return t.getType().equals(RoleType.RETAILER);
141
					return t.getType().equals(RoleType.RETAILER);
Line 128... Line 149...
128
				// if retailer is activated and migrated is 1 then retailer is
149
				// if retailer is activated and migrated is 1 then retailer is
129
				// retailer.
150
				// retailer.
130
				if (user.isActivated()) {
151
				if (user.isActivated()) {
131
					responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.VERIFIED_RETAILER.getValue());
152
					responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.VERIFIED_RETAILER.getValue());
132
				} else {
153
				} else {
133
					responseMap.put(ProfitMandiConstants.USER_STATUS, UserStatus.NOT_VERIFIED_RETAILER.getValue());
154
					responseMap.put(ProfitMandiConstants.NEW_AUTH_TOKEN, UserStatus.NOT_VERIFIED_RETAILER.getValue());
134
				}
155
				}
135
			} else if (user.getRoles().stream().anyMatch(new Predicate<Role>() {
156
			} else if (user.getRoles().stream().anyMatch(new Predicate<Role>() {
136
				@Override
157
				@Override
137
				public boolean test(Role t) {
158
				public boolean test(Role t) {
138
					return t.getType().equals(RoleType.USER);
159
					return t.getType().equals(RoleType.USER);