Search in sources :

Example 1 with NamespacePermissionEnum

use of org.finra.herd.model.api.xml.NamespacePermissionEnum in project herd by FINRAOS.

the class NamespaceSecurityHelper method checkPermission.

/**
 * Checks the current user's permissions against the given namespace.
 *
 * @param namespace The namespace
 * @param permissions The permissions the current user must have for the given namespace
 */
public void checkPermission(String namespace, NamespacePermissionEnum[] permissions) {
    // Skip the permission check if there is no authentication or namespace is not specified.
    if (!isAuthenticated() || StringUtils.isBlank(namespace)) {
        return;
    }
    // Trim the namespace.
    String namespaceTrimmed = namespace.trim();
    // Check if the current user is authorized to the given namespace and has the given permissions.
    ApplicationUser applicationUser = getApplicationUser();
    if (!isAuthorized(applicationUser, namespaceTrimmed, permissions)) {
        String permissionsString = Arrays.asList(permissions).stream().map(n -> n.toString()).collect(Collectors.joining(" OR "));
        permissionsString = "[" + permissionsString + "]";
        // The current user is not authorized to access the given namespace, so log a warning and throw an exception.
        LOGGER.warn(String.format("User does not have permission(s) to the namespace. %s namespace=\"%s\" permissions=\"%s\"", applicationUser, namespaceTrimmed, permissionsString));
        if (applicationUser != null) {
            throw new AccessDeniedException(String.format("User \"%s\" does not have \"%s\" permission(s) to the namespace \"%s\"", applicationUser.getUserId(), permissionsString, namespaceTrimmed));
        } else {
            throw new AccessDeniedException(String.format("Current user does not have \"%s\" permission(s) to the namespace \"%s\"", permissionsString, namespaceTrimmed));
        }
    }
}
Also used : ApplicationUser(org.finra.herd.model.dto.ApplicationUser) Arrays(java.util.Arrays) Logger(org.slf4j.Logger) NamespacePermissionEnum(org.finra.herd.model.api.xml.NamespacePermissionEnum) NamespaceAuthorization(org.finra.herd.model.api.xml.NamespaceAuthorization) SecurityUserWrapper(org.finra.herd.model.dto.SecurityUserWrapper) Collection(java.util.Collection) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) ApplicationUser(org.finra.herd.model.dto.ApplicationUser) AccessDeniedException(org.springframework.security.access.AccessDeniedException) Collectors(java.util.stream.Collectors) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) List(java.util.List) Component(org.springframework.stereotype.Component) SecurityContextHolder(org.springframework.security.core.context.SecurityContextHolder) Collections(java.util.Collections) AccessDeniedException(org.springframework.security.access.AccessDeniedException)

Example 2 with NamespacePermissionEnum

use of org.finra.herd.model.api.xml.NamespacePermissionEnum in project herd by FINRAOS.

the class UserNamespaceAuthorizationServiceImpl method validateNamespacePermissions.

/**
 * Validates a list of namespace permissions.
 *
 * @param namespacePermissions the list of namespace permissions
 *
 * @throws IllegalArgumentException if any validation errors were found
 */
public void validateNamespacePermissions(List<NamespacePermissionEnum> namespacePermissions) throws IllegalArgumentException {
    Assert.isTrue(!CollectionUtils.isEmpty(namespacePermissions), "Namespace permissions must be specified.");
    // Ensure permission isn't a duplicate by using a hash set with uppercase permission values for case insensitivity.
    Set<NamespacePermissionEnum> validatedNamespacePermissions = new HashSet<>();
    // Validate the permissions.
    for (NamespacePermissionEnum namespacePermission : namespacePermissions) {
        // Fail if duplicate permission value is detected.
        if (validatedNamespacePermissions.contains(namespacePermission)) {
            throw new IllegalArgumentException(String.format("Duplicate namespace permission \"%s\" is found.", namespacePermission.value()));
        }
        validatedNamespacePermissions.add(namespacePermission);
    }
}
Also used : NamespacePermissionEnum(org.finra.herd.model.api.xml.NamespacePermissionEnum) HashSet(java.util.HashSet)

Aggregations

HashSet (java.util.HashSet)2 NamespacePermissionEnum (org.finra.herd.model.api.xml.NamespacePermissionEnum)2 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 List (java.util.List)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 StringUtils (org.apache.commons.lang3.StringUtils)1 NamespaceAuthorization (org.finra.herd.model.api.xml.NamespaceAuthorization)1 ApplicationUser (org.finra.herd.model.dto.ApplicationUser)1 SecurityUserWrapper (org.finra.herd.model.dto.SecurityUserWrapper)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1 AccessDeniedException (org.springframework.security.access.AccessDeniedException)1 SecurityContextHolder (org.springframework.security.core.context.SecurityContextHolder)1 Component (org.springframework.stereotype.Component)1