Search in sources :

Example 1 with RoleType

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;
}
Also used : RoleType(com.orion.ops.consts.user.RoleType) UserDTO(com.orion.ops.entity.dto.UserDTO) RequireRole(com.orion.ops.annotation.RequireRole) HandlerMethod(org.springframework.web.method.HandlerMethod)

Aggregations

RequireRole (com.orion.ops.annotation.RequireRole)1 RoleType (com.orion.ops.consts.user.RoleType)1 UserDTO (com.orion.ops.entity.dto.UserDTO)1 HandlerMethod (org.springframework.web.method.HandlerMethod)1