Search in sources :

Example 1 with RoleEntity

use of org.usermanagement.dao.entity.RoleEntity in project open-kilda by telstra.

the class SamlController method samlAuthenticate.

/**
 * Saml Authenticate.
 *
 * @param request the request
 * @return the model and view
 */
@RequestMapping(value = "/authenticate")
public ModelAndView samlAuthenticate(final HttpServletRequest request, RedirectAttributes redir) {
    ModelAndView modelAndView = null;
    String error = null;
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (null != authentication) {
        boolean isValid = (authentication.isAuthenticated() && !(authentication instanceof AnonymousAuthenticationToken));
        if (isValid) {
            SAMLCredential saml = (SAMLCredential) authentication.getCredentials();
            SamlConfig samlConfig = samlService.getConfigByEntityId(saml.getRemoteEntityID());
            NameID nameId = (NameID) authentication.getPrincipal();
            String username = nameId.getValue();
            UserInfo userInfo = userService.getUserInfoByUsername(username);
            if (userInfo != null && userInfo.getStatus().equalsIgnoreCase(Status.ACTIVE.name())) {
                userService.populateUserInfo(userInfo, username);
                request.getSession().setAttribute(IConstants.SESSION_OBJECT, userInfo);
                userService.updateLoginDetail(username);
                modelAndView = new ModelAndView(IConstants.View.REDIRECT_HOME);
            } else if (userInfo != null && userInfo.getStatus().equalsIgnoreCase(Status.INACTIVE.name())) {
                error = messageUtil.getAttributeUserInactive();
                request.getSession(false);
                modelAndView = new ModelAndView(IConstants.View.REDIRECT_LOGIN);
            } else if (userInfo == null && samlConfig.isUserCreation()) {
                Set<RoleEntity> roleEntities = roleService.getRoleByIds(samlConfig.getRoles());
                userService.createSamlUser(nameId.getValue(), roleEntities);
                UserInfo userInfo1 = getLoggedInUser(request);
                userService.populateUserInfo(userInfo1, username);
                userService.updateLoginDetail(username);
                modelAndView = new ModelAndView(IConstants.View.REDIRECT_HOME);
            } else {
                error = messageUtil.getAttributeUserDoesNotExist();
                LOGGER.warn("User is not logged in, redirected to login page. Requested view name: ");
                request.getSession(false);
                modelAndView = new ModelAndView(IConstants.View.REDIRECT_LOGIN);
            }
        }
    } else {
        error = messageUtil.getAttributeAuthenticationFailure();
        LOGGER.warn("User is not logged in, redirected to login page. Requested view name: ");
        modelAndView = new ModelAndView(IConstants.View.LOGIN);
    }
    if (error != null) {
        redir.addFlashAttribute("error", error);
    }
    return modelAndView;
}
Also used : RoleEntity(org.usermanagement.dao.entity.RoleEntity) SAMLCredential(org.springframework.security.saml.SAMLCredential) NameID(org.opensaml.saml2.core.NameID) Authentication(org.springframework.security.core.Authentication) ModelAndView(org.springframework.web.servlet.ModelAndView) UserInfo(org.usermanagement.model.UserInfo) AnonymousAuthenticationToken(org.springframework.security.authentication.AnonymousAuthenticationToken) SamlConfig(org.openkilda.saml.model.SamlConfig) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with RoleEntity

use of org.usermanagement.dao.entity.RoleEntity in project open-kilda by telstra.

the class SamlService method create.

/**
 * Creates the provider.
 *
 * @param file the metadata file
 * @param url the metadata url
 * @param name the provider name
 * @param entityId the entityId
 * @param status the provider status
 * @param attribute the attribute
 * @param userCreation the userCreation
 * @param roleIds the role Ids
 * @return the SamlConfig
 */
public SamlConfig create(MultipartFile file, String name, String url, String entityId, boolean status, boolean userCreation, List<Long> roleIds, String attribute) {
    samlValidator.validateCreateProvider(file, name, entityId, url, userCreation, roleIds);
    Set<RoleEntity> roleEntities = roleService.getRolesById(roleIds);
    SamlConfigEntity samlConfigEntity = SamlConversionUtil.toSamlConfigEntity(file, name, url, entityId, status, attribute, userCreation, roleEntities);
    samlRepository.save(samlConfigEntity);
    try {
        metadataManager.loadProviderMetadata(samlConfigEntity.getUuid(), samlConfigEntity.getType().name());
    } catch (MetadataProviderException e) {
        LOGGER.error("Error occurred while loading provider" + e);
    }
    return SamlConversionUtil.toSamlConfig(samlConfigEntity);
}
Also used : RoleEntity(org.usermanagement.dao.entity.RoleEntity) SamlConfigEntity(org.openkilda.saml.dao.entity.SamlConfigEntity) MetadataProviderException(org.opensaml.saml2.metadata.provider.MetadataProviderException)

Example 3 with RoleEntity

use of org.usermanagement.dao.entity.RoleEntity in project open-kilda by telstra.

the class SamlService method update.

/**
 * Updates the provider.
 *
 * @param uuid the uuid
 * @param file the metadata file
 * @param name the provider name
 * @param url the metadata url
 * @param entityId the entityId
 * @param status the provider status
 * @param attribute the attribute
 * @param userCreation the userCreation
 * @param roleIds the role Ids
 * @return the SamlConfig
 */
public SamlConfig update(String uuid, MultipartFile file, String name, String url, String entityId, boolean status, String attribute, boolean userCreation, List<Long> roleIds) {
    SamlConfigEntity samlConfigEntity = samlValidator.validateUpdateProvider(uuid, file, name, entityId, url, userCreation, roleIds);
    Set<RoleEntity> roleEntities = roleService.getRolesById(roleIds);
    boolean requireManagerUpdate = SamlConversionUtil.toUpdateSamlConfigEntity(samlConfigEntity, roleEntities, file, name, url, entityId, status, userCreation, attribute);
    samlRepository.save(samlConfigEntity);
    if (requireManagerUpdate) {
        metadataManager.updateProviderToMetadataManager(samlConfigEntity.getUuid(), samlConfigEntity.getType().name());
    }
    return SamlConversionUtil.toSamlConfig(samlConfigEntity);
}
Also used : RoleEntity(org.usermanagement.dao.entity.RoleEntity) SamlConfigEntity(org.openkilda.saml.dao.entity.SamlConfigEntity)

Example 4 with RoleEntity

use of org.usermanagement.dao.entity.RoleEntity in project open-kilda by telstra.

the class SamlConversionUtil method toSamlConfig.

/**
 * To saml config.
 *
 * @param samlConfigEntity the saml config entity
 * @return the saml config
 */
public static SamlConfig toSamlConfig(SamlConfigEntity samlConfigEntity) {
    SamlConfig samlConfig = new SamlConfig();
    samlConfig.setName(samlConfigEntity.getName());
    samlConfig.setUrl(samlConfigEntity.getUrl());
    samlConfig.setEntityId(samlConfigEntity.getEntityId());
    samlConfig.setUuid(samlConfigEntity.getUuid());
    samlConfig.setUserCreation(samlConfigEntity.isUserCreation());
    samlConfig.setStatus(samlConfigEntity.isStatus());
    samlConfig.setType(samlConfigEntity.getType());
    samlConfig.setAttribute(samlConfigEntity.getAttribute());
    Set<Long> roles = new HashSet<>();
    if (samlConfigEntity.getRoles() != null) {
        for (RoleEntity roleEntity : samlConfigEntity.getRoles()) {
            roles.add(roleEntity.getRoleId());
        }
        samlConfig.setRoles(roles);
    }
    return samlConfig;
}
Also used : RoleEntity(org.usermanagement.dao.entity.RoleEntity) SamlConfig(org.openkilda.saml.model.SamlConfig) HashSet(java.util.HashSet)

Example 5 with RoleEntity

use of org.usermanagement.dao.entity.RoleEntity in project open-kilda by telstra.

the class RoleService method assignRoleByPermissionId.

/**
 * Assign role by permission id.
 *
 * @param permissionId the permission id
 * @param request the request
 * @return the permission
 */
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public Permission assignRoleByPermissionId(final Long permissionId, final Permission request) {
    PermissionEntity permissionEntity = permissionRepository.findByPermissionId(permissionId);
    if (ValidatorUtil.isNull(permissionEntity)) {
        LOGGER.warn("Permission with permissionId '" + permissionId + "' not found. Error: " + messageUtil.getAttributeInvalid("permissionId", permissionId + ""));
        throw new RequestValidationException(messageUtil.getAttributeInvalid("permissionId", permissionId + ""));
    }
    permissionEntity.getRoles().clear();
    if (request.getRoles() != null) {
        for (Role role : request.getRoles()) {
            RoleEntity roleEntity = roleRepository.findByRoleId(role.getRoleId());
            permissionEntity.getRoles().add(roleEntity);
        }
    }
    permissionRepository.save(permissionEntity);
    activityLogger.log(ActivityType.ASSIGN_ROLES_TO_PERMISSION, permissionEntity.getName());
    LOGGER.info("Roles assigned with permission successfully (permissionId: " + permissionId + ")");
    return RoleConversionUtil.toPermissionByRole(permissionEntity.getRoles(), permissionEntity);
}
Also used : Role(org.usermanagement.model.Role) RoleEntity(org.usermanagement.dao.entity.RoleEntity) PermissionEntity(org.usermanagement.dao.entity.PermissionEntity) RequestValidationException(org.usermanagement.exception.RequestValidationException) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

RoleEntity (org.usermanagement.dao.entity.RoleEntity)21 Transactional (org.springframework.transaction.annotation.Transactional)12 RequestValidationException (org.usermanagement.exception.RequestValidationException)10 PermissionEntity (org.usermanagement.dao.entity.PermissionEntity)7 HashSet (java.util.HashSet)5 UserEntity (org.usermanagement.dao.entity.UserEntity)5 Role (org.usermanagement.model.Role)4 ArrayList (java.util.ArrayList)3 UserInfo (org.usermanagement.model.UserInfo)3 SamlConfigEntity (org.openkilda.saml.dao.entity.SamlConfigEntity)2 SamlConfig (org.openkilda.saml.model.SamlConfig)2 Permission (org.usermanagement.model.Permission)2 AccessDeniedException (java.nio.file.AccessDeniedException)1 HashMap (java.util.HashMap)1 InvalidOtpException (org.openkilda.exception.InvalidOtpException)1 OtpRequiredException (org.openkilda.exception.OtpRequiredException)1 TwoFaKeyNotSetException (org.openkilda.exception.TwoFaKeyNotSetException)1 NameID (org.opensaml.saml2.core.NameID)1 MetadataProviderException (org.opensaml.saml2.metadata.provider.MetadataProviderException)1 AnonymousAuthenticationToken (org.springframework.security.authentication.AnonymousAuthenticationToken)1