| 22107 |
ashik.ali |
1 |
package com.spice.profitmandi.web.interceptor;
|
|
|
2 |
|
|
|
3 |
import java.util.HashSet;
|
|
|
4 |
import java.util.List;
|
|
|
5 |
import java.util.Set;
|
|
|
6 |
import java.util.function.Predicate;
|
|
|
7 |
|
|
|
8 |
import javax.servlet.http.HttpServletRequest;
|
|
|
9 |
import javax.servlet.http.HttpServletResponse;
|
|
|
10 |
|
|
|
11 |
import org.slf4j.Logger;
|
|
|
12 |
import org.slf4j.LoggerFactory;
|
|
|
13 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
14 |
import org.springframework.http.HttpStatus;
|
|
|
15 |
import org.springframework.http.MediaType;
|
|
|
16 |
import org.springframework.stereotype.Component;
|
|
|
17 |
import org.springframework.web.servlet.HandlerInterceptor;
|
|
|
18 |
import org.springframework.web.servlet.ModelAndView;
|
|
|
19 |
|
|
|
20 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
|
|
21 |
import com.spice.profitmandi.dao.entity.dtr.User;
|
|
|
22 |
import com.spice.profitmandi.dao.entity.dtr.UserRole;
|
|
|
23 |
import com.spice.profitmandi.dao.enumuration.dtr.RoleType;
|
|
|
24 |
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
|
|
|
25 |
import com.spice.profitmandi.dao.repository.dtr.UserRoleRepository;
|
|
|
26 |
import com.spice.profitmandi.web.model.FofoDetails;
|
|
|
27 |
import com.spice.profitmandi.web.util.CookiesProcessor;
|
|
|
28 |
import com.spice.profitmandi.web.util.MVCResponseSender;
|
|
|
29 |
|
|
|
30 |
@Component
|
|
|
31 |
public class RoleInterceptor implements HandlerInterceptor {
|
|
|
32 |
|
|
|
33 |
private static final Logger LOGGER = LoggerFactory.getLogger(RoleInterceptor.class);
|
|
|
34 |
|
|
|
35 |
private static final Set<String> REQUESTED_URI_PATTERNS = new HashSet<>();
|
|
|
36 |
static{
|
|
|
37 |
REQUESTED_URI_PATTERNS.add("/fofo");
|
|
|
38 |
REQUESTED_URI_PATTERNS.add("/fofo/[1-9]{1, 6}/edit");
|
|
|
39 |
REQUESTED_URI_PATTERNS.add("/file-display/[1-9]{1,6}");
|
|
|
40 |
}
|
|
|
41 |
@Autowired
|
|
|
42 |
UserRepository userRepository;
|
|
|
43 |
|
|
|
44 |
@Autowired
|
|
|
45 |
UserRoleRepository userRoleRepository;
|
|
|
46 |
|
|
|
47 |
@Autowired
|
|
|
48 |
MVCResponseSender mvcResponseSender;
|
|
|
49 |
|
|
|
50 |
@Autowired
|
|
|
51 |
CookiesProcessor cookiesProcessor;
|
|
|
52 |
|
|
|
53 |
@Override
|
|
|
54 |
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object arg2, Exception arg3)
|
|
|
55 |
throws Exception {
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
@Override
|
|
|
59 |
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object arg2, ModelAndView arg3)
|
|
|
60 |
throws Exception {
|
|
|
61 |
LOGGER.info("request is received after : "+request.getRequestURL().toString());
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
@Override
|
|
|
65 |
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) throws Exception {
|
|
|
66 |
LOGGER.info("request is received before : "+request.getRequestURL().toString());
|
|
|
67 |
LOGGER.info("Request method {}",request.getMethod());
|
|
|
68 |
try {
|
|
|
69 |
FofoDetails fofoDetails = cookiesProcessor.getCookiesObject(request);
|
|
|
70 |
User user = userRepository.selectByEmailId(fofoDetails.getEmailId());
|
|
|
71 |
List<UserRole> userRoles = userRoleRepository.selectByUserId(user.getId());
|
|
|
72 |
if(
|
|
|
73 |
// condition start
|
|
|
74 |
// first condition start
|
|
|
75 |
REQUESTED_URI_PATTERNS.stream().anyMatch(new Predicate<String>() {
|
|
|
76 |
@Override
|
|
|
77 |
public boolean test(String regexUriPattern) {
|
|
|
78 |
return request.getRequestURI().matches(regexUriPattern);
|
|
|
79 |
};})
|
|
|
80 |
// first condition end
|
|
|
81 |
&&
|
|
|
82 |
// second condition start
|
|
|
83 |
userRoles.stream().anyMatch(new Predicate<UserRole>() {
|
|
|
84 |
@Override
|
|
|
85 |
public boolean test(UserRole userRole) {
|
|
|
86 |
return userRole.getRoleType() == RoleType.FOFO_ADMIN;
|
|
|
87 |
}
|
|
|
88 |
}))
|
|
|
89 |
// second condition end
|
|
|
90 |
// condition end
|
|
|
91 |
{
|
|
|
92 |
response.setStatus(HttpStatus.FORBIDDEN.value());
|
|
|
93 |
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
|
|
|
94 |
response.setCharacterEncoding("UTF-8");
|
|
|
95 |
response.getWriter().write(mvcResponseSender.createResponseString("GE_1004", false, "/error"));
|
|
|
96 |
response.getWriter().flush();
|
|
|
97 |
}
|
|
|
98 |
return true;
|
|
|
99 |
} catch (ProfitMandiBusinessException e) {
|
|
|
100 |
LOGGER.error("Requested session is expired", e);
|
|
|
101 |
return false;
|
|
|
102 |
}
|
|
|
103 |
}
|
|
|
104 |
}
|