Search in sources :

Example 26 with MetadataRepositoryException

use of com.thinkbiganalytics.metadata.modeshape.MetadataRepositoryException in project kylo by Teradata.

the class JcrProjectProvider method createProject.

/**
 * Creates a new user with the specified name.
 *
 * @param name   the name of the Project
 * @param ensure {@code true} to return the Project if it already exists, or {@code false} to throw an exception
 * @return the Project
 * @throws MetadataRepositoryException if the user could not be created
 */
@Nonnull
private Project createProject(@Nonnull final String name, final boolean ensure) {
    final Session session = getSession();
    final String projPath = ProjectPaths.projectPath(name).toString();
    logger.debug("workspace= {}", session.getWorkspace().getName());
    try {
        Node projNode = session.getRootNode().getNode(ProjectPaths.PROJECTS.toString());
        if (session.getRootNode().hasNode(projPath)) {
            if (ensure) {
                return JcrUtil.getJcrObject(projNode, name, JcrProject.class);
            } else {
                // TODO specialize me..
                throw new RuntimeException(projPath);
            }
        } else {
            // project does not yet exist
            JcrProject newProject = JcrUtil.getOrCreateNode(projNode, name, JcrProject.NODE_TYPE, JcrProject.class);
            // grant (or deny) current user access to the project he is creating
            if (this.accessController.isEntityAccessControlled()) {
                List<SecurityRole> roles = this.roleProvider.getEntityRoles(SecurityRole.PROJECT);
                this.actionsProvider.getAvailableActions(AllowedActions.PROJECTS).ifPresent(actions -> newProject.enableAccessControl((JcrAllowedActions) actions, JcrMetadataAccess.getActiveUser(), roles));
            } else {
                this.actionsProvider.getAvailableActions(AllowedActions.PROJECTS).ifPresent(actions -> newProject.disableAccessControl((JcrAllowedActions) actions, JcrMetadataAccess.getActiveUser()));
            }
            return newProject;
        }
    } catch (RepositoryException e) {
        throw new MetadataRepositoryException("Failed attempting to create a new Project with name: " + name, e);
    }
}
Also used : JcrProject(com.thinkbiganalytics.metadata.modeshape.project.JcrProject) MetadataRepositoryException(com.thinkbiganalytics.metadata.modeshape.MetadataRepositoryException) SecurityRole(com.thinkbiganalytics.security.role.SecurityRole) JcrAllowedActions(com.thinkbiganalytics.metadata.modeshape.security.action.JcrAllowedActions) Node(javax.jcr.Node) MetadataRepositoryException(com.thinkbiganalytics.metadata.modeshape.MetadataRepositoryException) RepositoryException(javax.jcr.RepositoryException) Session(javax.jcr.Session) Nonnull(javax.annotation.Nonnull)

Example 27 with MetadataRepositoryException

use of com.thinkbiganalytics.metadata.modeshape.MetadataRepositoryException in project kylo by Teradata.

the class JcrAccessControlUtil method removeHierarchyPermissions.

public static boolean removeHierarchyPermissions(Node node, Principal principal, Node toNode, String... privilegeNames) {
    try {
        Node current = node;
        Node rootNode = toNode.getSession().getRootNode();
        boolean removed = false;
        while (!current.equals(toNode) && !current.equals(rootNode)) {
            removed |= removePermissions(node.getSession(), current.getPath(), principal, privilegeNames);
            current = current.getParent();
        }
        if (current.equals(rootNode) && !toNode.equals(rootNode)) {
            throw new IllegalArgumentException("removeHierarchyPermissions: The \"toNode\" argument is not in the \"node\" argument's hierarchy: " + toNode);
        } else {
            removed |= removePermissions(node.getSession(), current.getPath(), principal, privilegeNames);
        }
        return removed;
    } catch (AccessDeniedException e) {
        throw new AccessControlException(e.getMessage());
    } catch (RepositoryException e) {
        throw new MetadataRepositoryException("Failed to remove permission(s) from hierarch from node " + node + " up to " + toNode, e);
    }
}
Also used : MetadataRepositoryException(com.thinkbiganalytics.metadata.modeshape.MetadataRepositoryException) AccessDeniedException(javax.jcr.AccessDeniedException) Node(javax.jcr.Node) AccessControlException(java.security.AccessControlException) MetadataRepositoryException(com.thinkbiganalytics.metadata.modeshape.MetadataRepositoryException) RepositoryException(javax.jcr.RepositoryException)

Example 28 with MetadataRepositoryException

use of com.thinkbiganalytics.metadata.modeshape.MetadataRepositoryException in project kylo by Teradata.

the class JcrAccessControlUtil method clearPermissions.

public static boolean clearPermissions(Session session, String path) {
    try {
        AccessControlManager acm = session.getAccessControlManager();
        AccessControlPolicy[] acls = acm.getPolicies(path);
        if (acls.length > 0) {
            for (AccessControlPolicy policy : acm.getPolicies(path)) {
                acm.removePolicy(path, policy);
            }
            return true;
        } else {
            return false;
        }
    } catch (AccessDeniedException e) {
        throw new AccessControlException(e.getMessage());
    } catch (RepositoryException e) {
        throw new MetadataRepositoryException("Failed to remove all permission(s) from node " + path, e);
    }
}
Also used : AccessControlManager(javax.jcr.security.AccessControlManager) MetadataRepositoryException(com.thinkbiganalytics.metadata.modeshape.MetadataRepositoryException) AccessControlPolicy(javax.jcr.security.AccessControlPolicy) AccessDeniedException(javax.jcr.AccessDeniedException) AccessControlException(java.security.AccessControlException) MetadataRepositoryException(com.thinkbiganalytics.metadata.modeshape.MetadataRepositoryException) RepositoryException(javax.jcr.RepositoryException)

Example 29 with MetadataRepositoryException

use of com.thinkbiganalytics.metadata.modeshape.MetadataRepositoryException in project kylo by Teradata.

the class JcrAccessControlUtil method addHierarchyPermissions.

/**
 * Adds the specified privilege to the node hierarchy starting at a child node and proceeding through its parents until
 * the destination node is reached.
 *
 * @param node           the starting node on which the privilege is assigned
 * @param principal      the principal being given the privilege
 * @param toNode         the ending parent node
 * @param privilegeNames the privilege being assigned
 * @return true if any of the nodes had their privilege change for the principle (i.e. the privilege had not already existed)
 */
public static boolean addHierarchyPermissions(Node node, Principal principal, Node toNode, Collection<String> privilegeNames) {
    try {
        Node current = node;
        Node rootNode = toNode.getSession().getRootNode();
        AtomicBoolean added = new AtomicBoolean(false);
        Deque<Node> stack = new ArrayDeque<>();
        while (!current.equals(toNode) && !current.equals(rootNode)) {
            stack.push(current);
            current = current.getParent();
        }
        if (current.equals(rootNode) && !toNode.equals(rootNode)) {
            throw new IllegalArgumentException("addHierarchyPermissions: The \"toNode\" argument is not in the \"node\" argument's hierarchy: " + toNode);
        } else {
            stack.push(current);
        }
        stack.stream().forEach((n) -> added.compareAndSet(false, addPermissions(n, principal, privilegeNames)));
        return added.get();
    } catch (AccessDeniedException e) {
        throw new AccessControlException(e.getMessage());
    } catch (RepositoryException e) {
        throw new MetadataRepositoryException("Failed to add permission(s) to hierarchy from node " + node + " up to " + toNode, e);
    }
}
Also used : MetadataRepositoryException(com.thinkbiganalytics.metadata.modeshape.MetadataRepositoryException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AccessDeniedException(javax.jcr.AccessDeniedException) Node(javax.jcr.Node) AccessControlException(java.security.AccessControlException) MetadataRepositoryException(com.thinkbiganalytics.metadata.modeshape.MetadataRepositoryException) RepositoryException(javax.jcr.RepositoryException) ArrayDeque(java.util.ArrayDeque)

Example 30 with MetadataRepositoryException

use of com.thinkbiganalytics.metadata.modeshape.MetadataRepositoryException in project kylo by Teradata.

the class JcrAccessControlUtil method updatePermissions.

private static boolean updatePermissions(Session session, String path, Principal principal, boolean replace, Privilege... privileges) {
    try {
        AccessControlManager acm = session.getAccessControlManager();
        AccessControlList acl = getAccessControlList(path, acm);
        boolean changed = false;
        if (replace) {
            changed |= removeEntry(acl, principal);
        }
        if (privileges.length > 0) {
            changed |= addEntry(session, acl, principal, privileges);
        }
        acm.setPolicy(path, acl);
        return changed;
    } catch (AccessDeniedException e) {
        throw new AccessControlException(e.getMessage());
    } catch (RepositoryException e) {
        throw new MetadataRepositoryException("Failed to add permission(s) to node " + path + ": " + Arrays.toString(privileges), e);
    }
}
Also used : AccessControlManager(javax.jcr.security.AccessControlManager) AccessControlList(javax.jcr.security.AccessControlList) MetadataRepositoryException(com.thinkbiganalytics.metadata.modeshape.MetadataRepositoryException) AccessDeniedException(javax.jcr.AccessDeniedException) AccessControlException(java.security.AccessControlException) MetadataRepositoryException(com.thinkbiganalytics.metadata.modeshape.MetadataRepositoryException) RepositoryException(javax.jcr.RepositoryException)

Aggregations

MetadataRepositoryException (com.thinkbiganalytics.metadata.modeshape.MetadataRepositoryException)83 RepositoryException (javax.jcr.RepositoryException)79 Node (javax.jcr.Node)54 AccessDeniedException (javax.jcr.AccessDeniedException)29 AccessControlException (java.security.AccessControlException)28 Session (javax.jcr.Session)25 ArrayList (java.util.ArrayList)16 HashMap (java.util.HashMap)14 HashSet (java.util.HashSet)12 NodeIterator (javax.jcr.NodeIterator)12 Nonnull (javax.annotation.Nonnull)10 Value (javax.jcr.Value)10 Map (java.util.Map)9 Property (javax.jcr.Property)8 ItemNotFoundException (javax.jcr.ItemNotFoundException)7 QueryResult (javax.jcr.query.QueryResult)7 JcrObject (com.thinkbiganalytics.metadata.modeshape.common.JcrObject)6 AccessControlManager (javax.jcr.security.AccessControlManager)6 UserFieldDescriptor (com.thinkbiganalytics.metadata.api.extension.UserFieldDescriptor)5 List (java.util.List)5