use of com.orion.ops.consts.user.RoleType in project orion-ops by lijiahangmax.
the class RoleInterceptor method preHandle.
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if (!(handler instanceof HandlerMethod)) {
return true;
}
RequireRole role = ((HandlerMethod) handler).getMethodAnnotation(RequireRole.class);
if (role == null) {
return true;
}
UserDTO user = UserHolder.get();
if (user == null) {
response.setContentType(StandardContentType.APPLICATION_JSON);
Servlets.transfer(response, HttpWrapper.of(ResultCode.UNAUTHORIZED).toJsonString().getBytes());
return false;
}
RoleType[] hasRoles = role.value();
if (Arrays1.isEmpty(hasRoles)) {
return true;
}
for (RoleType roleType : hasRoles) {
if (roleType.getType().equals(user.getRoleType())) {
return true;
}
}
response.setContentType(StandardContentType.APPLICATION_JSON);
Servlets.transfer(response, HttpWrapper.of(ResultCode.NO_PERMISSION).toJsonString().getBytes());
return false;
}
Aggregations