Search in sources :

Example 11 with UserInfo

use of org.usermanagement.model.UserInfo in project open-kilda by telstra.

the class BaseController method validateAndRedirect.

/**
 * Validate request.
 * <ul>
 * <li>If user is logged in and role is user then redirected to Home.</li>
 * <li>If user is logged in and role is not user then redirected to view
 * passed as an argument.</li>
 * <li>If user is not logged in then redirected to login page.</li>
 * </ul>
 *
 * @param request HttpServletRequest to check user log in status.
 * @param viewName on which user has to redirect if logged in and not of type user.
 * @return ModelAndView information containing view name on which user is going to be redirected.
 */
public ModelAndView validateAndRedirect(final HttpServletRequest request, final String viewName) {
    ModelAndView modelAndView;
    if (isUserLoggedIn()) {
        UserInfo userInfo = getLoggedInUser(request);
        LOGGER.info("Logged in user. view name: " + viewName + ", User name: " + userInfo.getName());
        modelAndView = new ModelAndView(IConstants.View.REDIRECT_HOME);
    } else {
        LOGGER.warn("User is not logged in, redirected to login page. Requested view name: " + viewName);
        modelAndView = new ModelAndView("login");
        modelAndView.addObject("idps", samlService.getAllActiveIdp());
    }
    return modelAndView;
}
Also used : ModelAndView(org.springframework.web.servlet.ModelAndView) UserInfo(org.usermanagement.model.UserInfo)

Example 12 with UserInfo

use of org.usermanagement.model.UserInfo in project open-kilda by telstra.

the class RequestInterceptor method preHandle.

@Override
public boolean preHandle(final HttpServletRequest request, final HttpServletResponse response, final Object handler) throws AccessDeniedException {
    String correlationId = request.getParameter(CORRELATION_ID);
    correlationId = correlationId == null ? UUID.randomUUID().toString() : correlationId;
    HttpSession session = request.getSession();
    UserInfo userInfo = null;
    if (IConstants.SessionTimeout.TIME_IN_MINUTE == null) {
        IConstants.SessionTimeout.TIME_IN_MINUTE = Integer.valueOf(applicationSettingService.getApplicationSettings().get(ApplicationSetting.SESSION_TIMEOUT.name()));
    }
    session.setMaxInactiveInterval(IConstants.SessionTimeout.TIME_IN_MINUTE * 60);
    userInfo = (UserInfo) session.getAttribute(IConstants.SESSION_OBJECT);
    if (userInfo != null) {
        validateUser(userInfo);
        if (handler instanceof HandlerMethod) {
            HandlerMethod handlerMethod = (HandlerMethod) handler;
            Permissions permissions = handlerMethod.getMethod().getAnnotation(Permissions.class);
            if (permissions != null) {
                validateAndPopulatePermisssion(userInfo, permissions);
            }
        }
        updateRequestContext(correlationId, request, userInfo);
    } else {
        RequestContext requestContext = serverContext.getRequestContext();
        requestContext.setCorrelationId(correlationId);
    }
    return true;
}
Also used : HttpSession(javax.servlet.http.HttpSession) Permissions(org.openkilda.auth.model.Permissions) UserInfo(org.usermanagement.model.UserInfo) RequestContext(org.openkilda.auth.model.RequestContext) HandlerMethod(org.springframework.web.method.HandlerMethod)

Example 13 with UserInfo

use of org.usermanagement.model.UserInfo in project open-kilda by telstra.

the class FlowService method getFlowById.

/**
 * Flow by flow id.
 *
 * @param flowId
 *            the flow id
 * @return the flow by id
 * @throws AccessDeniedException the access denied exception
 */
public FlowInfo getFlowById(String flowId, boolean controller) throws AccessDeniedException {
    FlowInfo flowInfo = new FlowInfo();
    FlowV2 flow = null;
    try {
        flow = flowsIntegrationService.getFlowById(flowId);
    } catch (InvalidResponseException ex) {
        LOGGER.error("Error occurred while retrieving flows from controller", ex);
        if (controller) {
            throw new InvalidResponseException(ex.getCode(), ex.getResponse());
        }
    }
    Map<String, String> csNames = switchIntegrationService.getSwitchNames();
    if (flow != null) {
        flowInfo = flowConverter.toFlowV2Info(flow, csNames);
    }
    UserInfo userInfo = userService.getLoggedInUserInfo();
    try {
        if (!controller && userInfo.getPermissions().contains(IConstants.Permission.FW_FLOW_INVENTORY)) {
            if (storeService.getLinkStoreConfig().getUrls().size() > 0) {
                InventoryFlow inventoryFlow = flowStoreService.getFlowById(flowId);
                if (flow == null && inventoryFlow.getId() == null) {
                    throw new RequestValidationException("Can not get flow: Flow " + flowId + " not found");
                } else if (flow != null && inventoryFlow.getId() != null) {
                    flowInfo.setState(inventoryFlow.getState());
                    FlowDiscrepancy discrepancy = new FlowDiscrepancy();
                    discrepancy.setControllerDiscrepancy(false);
                    if (flowInfo.getMaximumBandwidth() != (inventoryFlow.getMaximumBandwidth() == null ? 0 : inventoryFlow.getMaximumBandwidth())) {
                        discrepancy.setBandwidth(true);
                        FlowBandwidth flowBandwidth = new FlowBandwidth();
                        flowBandwidth.setControllerBandwidth(flow.getMaximumBandwidth());
                        flowBandwidth.setInventoryBandwidth(inventoryFlow.getMaximumBandwidth());
                        discrepancy.setBandwidthValue(flowBandwidth);
                    }
                    if (("UP".equalsIgnoreCase(flowInfo.getStatus()) && !"ACTIVE".equalsIgnoreCase(inventoryFlow.getState())) || ("DOWN".equalsIgnoreCase(flowInfo.getStatus()) && "ACTIVE".equalsIgnoreCase(inventoryFlow.getState()))) {
                        discrepancy.setStatus(true);
                        FlowState flowState = new FlowState();
                        flowState.setControllerState(flow.getStatus());
                        flowState.setInventoryState(inventoryFlow.getState());
                        discrepancy.setStatusValue(flowState);
                    }
                    flowInfo.setInventoryFlow(true);
                    flowInfo.setDiscrepancy(discrepancy);
                } else if (inventoryFlow.getId() == null && flow != null) {
                    FlowDiscrepancy discrepancy = new FlowDiscrepancy();
                    discrepancy.setInventoryDiscrepancy(true);
                    discrepancy.setControllerDiscrepancy(false);
                    discrepancy.setStatus(true);
                    discrepancy.setBandwidth(true);
                    FlowBandwidth flowBandwidth = new FlowBandwidth();
                    flowBandwidth.setControllerBandwidth(flow.getMaximumBandwidth());
                    flowBandwidth.setInventoryBandwidth(0);
                    discrepancy.setBandwidthValue(flowBandwidth);
                    FlowState flowState = new FlowState();
                    flowState.setControllerState(flow.getStatus());
                    flowState.setInventoryState(null);
                    discrepancy.setStatusValue(flowState);
                    flowInfo.setDiscrepancy(discrepancy);
                } else {
                    flowConverter.toFlowInfo(flowInfo, inventoryFlow, csNames);
                }
            }
        }
        if (flow == null) {
            throw new RequestValidationException("Can not get flow: Flow " + flowId + " not found");
        }
    } catch (Exception ex) {
        LOGGER.error("Error occurred while retrieving flows from store", ex);
        throw new RequestValidationException(ex.getMessage());
    }
    return flowInfo;
}
Also used : FlowInfo(org.openkilda.model.FlowInfo) FlowDiscrepancy(org.openkilda.model.FlowDiscrepancy) FlowState(org.openkilda.model.FlowState) FlowV2(org.openkilda.integration.model.FlowV2) InventoryFlow(org.openkilda.integration.source.store.dto.InventoryFlow) UserInfo(org.usermanagement.model.UserInfo) FlowBandwidth(org.openkilda.model.FlowBandwidth) InvalidResponseException(org.openkilda.integration.exception.InvalidResponseException) RequestValidationException(org.usermanagement.exception.RequestValidationException) InvalidResponseException(org.openkilda.integration.exception.InvalidResponseException) IntegrationException(org.openkilda.integration.exception.IntegrationException) RequestValidationException(org.usermanagement.exception.RequestValidationException) AccessDeniedException(java.nio.file.AccessDeniedException)

Example 14 with UserInfo

use of org.usermanagement.model.UserInfo in project open-kilda by telstra.

the class UserConversionUtil method toUserInfo.

/**
 * To user info.
 *
 * @param userEntity the user entity
 * @return the user info
 */
public static UserInfo toUserInfo(final UserEntity userEntity) {
    UserInfo userInfo = null;
    if (userEntity != null) {
        userInfo = new UserInfo();
        userInfo.setName(userEntity.getName());
        userInfo.setEmail(userEntity.getEmail().toLowerCase());
        userInfo.setUsername(userEntity.getUsername().toLowerCase());
        userInfo.setIs2FaEnabled(userEntity.getIs2FaEnabled());
        userInfo.setStatus(userEntity.getStatusEntity().getStatus());
        userInfo.setUserId(userEntity.getUserId());
        Set<String> roles = new HashSet<>();
        if (!ValidatorUtil.isNull(userEntity.getRoles())) {
            for (RoleEntity roleEntity : userEntity.getRoles()) {
                roles.add(roleEntity.getName());
            }
            userInfo.setRoles(roles);
        }
    }
    return userInfo;
}
Also used : RoleEntity(org.usermanagement.dao.entity.RoleEntity) UserInfo(org.usermanagement.model.UserInfo) HashSet(java.util.HashSet)

Example 15 with UserInfo

use of org.usermanagement.model.UserInfo in project open-kilda by telstra.

the class RoleConversionUtil method toRole.

/**
 * To role.
 *
 * @param roleEntity the role entity
 * @param withPermissions the with permissions
 * @param withUsers the with users
 * @return the role
 */
public static Role toRole(final RoleEntity roleEntity, final boolean withPermissions, final boolean withUsers) {
    Role role = new Role();
    role.setName(roleEntity.getName());
    role.setRoleId(roleEntity.getRoleId());
    role.setStatus(roleEntity.getStatusEntity().getStatus());
    role.setDescription(roleEntity.getDescription());
    if (withPermissions) {
        List<Permission> permissionList = new ArrayList<Permission>();
        if (!ValidatorUtil.isNull(roleEntity.getPermissions())) {
            for (PermissionEntity permissionEntity : roleEntity.getPermissions()) {
                permissionList.add(PermissionConversionUtil.toPermission(permissionEntity, null));
            }
            role.setPermissions(permissionList);
        }
    }
    if (withUsers) {
        List<UserInfo> userInfoList = new ArrayList<>();
        for (UserEntity userEntity : roleEntity.getUsers()) {
            if (userEntity.getUserId() != 1) {
                UserInfo userInfo = new UserInfo();
                userInfo.setUserId(userEntity.getUserId());
                userInfo.setName(userEntity.getName());
                userInfoList.add(userInfo);
            }
        }
        role.setUserInfo(userInfoList);
    }
    return role;
}
Also used : Role(org.usermanagement.model.Role) Permission(org.usermanagement.model.Permission) ArrayList(java.util.ArrayList) UserInfo(org.usermanagement.model.UserInfo) PermissionEntity(org.usermanagement.dao.entity.PermissionEntity) UserEntity(org.usermanagement.dao.entity.UserEntity)

Aggregations

UserInfo (org.usermanagement.model.UserInfo)17 AccessDeniedException (java.nio.file.AccessDeniedException)6 RequestValidationException (org.usermanagement.exception.RequestValidationException)6 IntegrationException (org.openkilda.integration.exception.IntegrationException)4 InvalidResponseException (org.openkilda.integration.exception.InvalidResponseException)4 UserEntity (org.usermanagement.dao.entity.UserEntity)4 ArrayList (java.util.ArrayList)3 HttpSession (javax.servlet.http.HttpSession)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 ModelAndView (org.springframework.web.servlet.ModelAndView)3 RoleEntity (org.usermanagement.dao.entity.RoleEntity)3 RequestContext (org.openkilda.auth.model.RequestContext)2 InvalidOtpException (org.openkilda.exception.InvalidOtpException)2 OtpRequiredException (org.openkilda.exception.OtpRequiredException)2 TwoFaKeyNotSetException (org.openkilda.exception.TwoFaKeyNotSetException)2 StoreIntegrationException (org.openkilda.integration.exception.StoreIntegrationException)2 InventoryFlow (org.openkilda.integration.source.store.dto.InventoryFlow)2 InventorySwitch (org.openkilda.integration.source.store.dto.InventorySwitch)2 FlowInfo (org.openkilda.model.FlowInfo)2 SwitchInfo (org.openkilda.model.SwitchInfo)2