Search in sources :

Example 31 with RepositoryFileAcl

use of org.pentaho.platform.api.repository2.unified.RepositoryFileAcl in project pentaho-platform by pentaho.

the class JcrRepositoryFileDao method internalCopyOrMove.

private void internalCopyOrMove(final Serializable fileId, final String destRelPath, final String versionMessage, final boolean copy) {
    if (isKioskEnabled()) {
        // $NON-NLS-1$
        throw new RuntimeException(Messages.getInstance().getString("JcrRepositoryFileDao.ERROR_0006_ACCESS_DENIED"));
    }
    Assert.notNull(fileId);
    jcrTemplate.execute(new JcrCallback() {

        @Override
        public Object doInJcr(final Session session) throws RepositoryException, IOException {
            // if we're moving the file,
            // check that user has permissions to remove the file from it's current location
            RepositoryFile file = getFileById(fileId);
            if (!copy) {
                RepositoryFileAcl acl = aclDao.getAcl(fileId);
                if (!accessVoterManager.hasAccess(file, RepositoryFilePermission.WRITE, acl, PentahoSessionHolder.getSession())) {
                    throw new AccessDeniedException(Messages.getInstance().getString("JcrRepositoryFileDao.ERROR_0006_ACCESS_DENIED_DELETE", fileId));
                }
            }
            // check that user has permissions to write to the destination folder
            RepositoryFile destFolder = getFile(destRelPath);
            if (destFolder != null) {
                RepositoryFileAcl destFolderAcl = aclDao.getAcl(destFolder.getId());
                if (!accessVoterManager.hasAccess(destFolder, RepositoryFilePermission.WRITE, destFolderAcl, PentahoSessionHolder.getSession())) {
                    throw new AccessDeniedException(Messages.getInstance().getString("JcrRepositoryFileDao.ERROR_0006_ACCESS_DENIED_CREATE", destFolder.getId()));
                }
            }
            PentahoJcrConstants pentahoJcrConstants = new PentahoJcrConstants(session);
            String destAbsPath = pathConversionHelper.relToAbs(destRelPath);
            String cleanDestAbsPath = destAbsPath;
            if (cleanDestAbsPath.endsWith(RepositoryFile.SEPARATOR)) {
                cleanDestAbsPath.substring(0, cleanDestAbsPath.length() - 1);
            }
            Node srcFileNode = session.getNodeByIdentifier(fileId.toString());
            Serializable srcParentFolderId = JcrRepositoryFileUtils.getParentId(session, fileId);
            boolean appendFileName = false;
            boolean destExists = true;
            Node destFileNode = null;
            Node destParentFolderNode = null;
            try {
                destFileNode = (Node) session.getItem(JcrStringHelper.pathEncode(cleanDestAbsPath));
            } catch (PathNotFoundException e) {
                destExists = false;
            }
            if (destExists) {
                // make sure it's a file or folder
                Assert.isTrue(JcrRepositoryFileUtils.isSupportedNodeType(pentahoJcrConstants, destFileNode));
                // existing item; make sure src is not a folder if dest is a file
                Assert.isTrue(!(JcrRepositoryFileUtils.isPentahoFolder(pentahoJcrConstants, srcFileNode) && JcrRepositoryFileUtils.isPentahoFile(pentahoJcrConstants, destFileNode)), Messages.getInstance().getString(// $NON-NLS-1$
                "JcrRepositoryFileDao.ERROR_0002_CANNOT_OVERWRITE_FILE_WITH_FOLDER"));
                if (JcrRepositoryFileUtils.isPentahoFolder(pentahoJcrConstants, destFileNode)) {
                    // existing item; caller is not renaming file, only moving it
                    appendFileName = true;
                    destParentFolderNode = destFileNode;
                } else {
                    // get parent of existing dest item
                    int lastSlashIndex = cleanDestAbsPath.lastIndexOf(RepositoryFile.SEPARATOR);
                    Assert.isTrue(lastSlashIndex > 1, Messages.getInstance().getString(// $NON-NLS-1$
                    "JcrRepositoryFileDao.ERROR_0003_ILLEGAL_DEST_PATH"));
                    String absPathToDestParentFolder = cleanDestAbsPath.substring(0, lastSlashIndex);
                    destParentFolderNode = (Node) session.getItem(JcrStringHelper.pathEncode(absPathToDestParentFolder));
                }
            } else {
                // destination doesn't exist; go up one level to a folder that does exist
                int lastSlashIndex = cleanDestAbsPath.lastIndexOf(RepositoryFile.SEPARATOR);
                Assert.isTrue(lastSlashIndex > 1, Messages.getInstance().getString(// $NON-NLS-1$
                "JcrRepositoryFileDao.ERROR_0003_ILLEGAL_DEST_PATH"));
                String absPathToDestParentFolder = cleanDestAbsPath.substring(0, lastSlashIndex);
                // JcrRepositoryFileUtils.checkName( cleanDestAbsPath.substring( lastSlashIndex + 1 ) );
                try {
                    destParentFolderNode = (Node) session.getItem(JcrStringHelper.pathEncode(absPathToDestParentFolder));
                } catch (PathNotFoundException e1) {
                    Assert.isTrue(false, Messages.getInstance().getString(// $NON-NLS-1$
                    "JcrRepositoryFileDao.ERROR_0004_PARENT_MUST_EXIST"));
                }
                Assert.isTrue(JcrRepositoryFileUtils.isPentahoFolder(pentahoJcrConstants, destParentFolderNode), Messages.getInstance().getString(// $NON-NLS-1$
                "JcrRepositoryFileDao.ERROR_0005_PARENT_MUST_BE_FOLDER"));
            }
            if (!copy) {
                JcrRepositoryFileUtils.checkoutNearestVersionableFileIfNecessary(session, pentahoJcrConstants, srcParentFolderId);
            }
            JcrRepositoryFileUtils.checkoutNearestVersionableNodeIfNecessary(session, pentahoJcrConstants, destParentFolderNode);
            String finalEncodedSrcAbsPath = srcFileNode.getPath();
            String finalEncodedDestAbsPath = null;
            if (appendFileName) {
                final String fileName = srcFileNode.getName();
                if (JcrStringHelper.isEncoded(fileName)) {
                    finalEncodedDestAbsPath = JcrStringHelper.pathEncode(cleanDestAbsPath) + RepositoryFile.SEPARATOR + fileName;
                } else {
                    finalEncodedDestAbsPath = JcrStringHelper.pathEncode(cleanDestAbsPath + RepositoryFile.SEPARATOR + fileName);
                }
            } else {
                finalEncodedDestAbsPath = JcrStringHelper.pathEncode(cleanDestAbsPath);
            }
            try {
                if (copy) {
                    session.getWorkspace().copy(finalEncodedSrcAbsPath, finalEncodedDestAbsPath);
                } else {
                    session.getWorkspace().move(finalEncodedSrcAbsPath, finalEncodedDestAbsPath);
                }
            } catch (ItemExistsException iae) {
                throw new UnifiedRepositoryException((file.isFolder() ? "Folder " : "File ") + "with path [" + cleanDestAbsPath + "] already exists in the repository");
            }
            JcrRepositoryFileUtils.checkinNearestVersionableNodeIfNecessary(session, pentahoJcrConstants, destParentFolderNode, versionMessage);
            // if it's a move within the same folder, then the next checkin is unnecessary
            if (!copy && !destParentFolderNode.getIdentifier().equals(srcParentFolderId.toString())) {
                JcrRepositoryFileUtils.checkinNearestVersionableFileIfNecessary(session, pentahoJcrConstants, srcParentFolderId, versionMessage);
            }
            session.save();
            return null;
        }
    });
}
Also used : AccessDeniedException(javax.jcr.AccessDeniedException) Serializable(java.io.Serializable) DataNode(org.pentaho.platform.api.repository2.unified.data.node.DataNode) Node(javax.jcr.Node) UnifiedRepositoryException(org.pentaho.platform.api.repository2.unified.UnifiedRepositoryException) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException) JcrCallback(org.springframework.extensions.jcr.JcrCallback) ItemExistsException(javax.jcr.ItemExistsException) UnifiedRepositoryException(org.pentaho.platform.api.repository2.unified.UnifiedRepositoryException) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) PathNotFoundException(javax.jcr.PathNotFoundException) RepositoryFileAcl(org.pentaho.platform.api.repository2.unified.RepositoryFileAcl) Session(javax.jcr.Session)

Example 32 with RepositoryFileAcl

use of org.pentaho.platform.api.repository2.unified.RepositoryFileAcl in project pentaho-platform by pentaho.

the class JcrRepositoryFileDao method internalGetFileById.

private RepositoryFile internalGetFileById(final Serializable fileId, final boolean loadMaps, final IPentahoLocale locale) {
    Assert.notNull(fileId);
    return (RepositoryFile) jcrTemplate.execute(new JcrCallback() {

        @Override
        public Object doInJcr(final Session session) throws RepositoryException, IOException {
            PentahoJcrConstants pentahoJcrConstants = new PentahoJcrConstants(session);
            Node fileNode;
            try {
                fileNode = session.getNodeByIdentifier(fileId.toString());
            } catch (ItemNotFoundException e) {
                logger.info("Couldn't find file by id: " + fileId);
                fileNode = null;
            }
            RepositoryFile file = fileNode != null ? JcrRepositoryFileUtils.nodeToFile(session, pentahoJcrConstants, pathConversionHelper, lockHelper, fileNode, loadMaps, locale) : null;
            if (file != null) {
                RepositoryFileAcl acl = aclDao.getAcl(file.getId());
                // Invoke accessVoterManager to see if we have access to perform this operation
                if (!accessVoterManager.hasAccess(file, RepositoryFilePermission.READ, acl, PentahoSessionHolder.getSession())) {
                    return null;
                }
            }
            return file;
        }
    });
}
Also used : DataNode(org.pentaho.platform.api.repository2.unified.data.node.DataNode) Node(javax.jcr.Node) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) JcrCallback(org.springframework.extensions.jcr.JcrCallback) RepositoryFileAcl(org.pentaho.platform.api.repository2.unified.RepositoryFileAcl) Session(javax.jcr.Session) ItemNotFoundException(javax.jcr.ItemNotFoundException)

Example 33 with RepositoryFileAcl

use of org.pentaho.platform.api.repository2.unified.RepositoryFileAcl in project pentaho-platform by pentaho.

the class JcrRepositoryFileDao method getData.

/**
 * {@inheritDoc}
 */
@Override
@SuppressWarnings("unchecked")
public <T extends IRepositoryFileData> T getData(final Serializable fileId, final Serializable versionId, final Class<T> contentClass) {
    Assert.notNull(fileId);
    return (T) jcrTemplate.execute(new JcrCallback() {

        @Override
        public Object doInJcr(final Session session) throws RepositoryException, IOException {
            PentahoJcrConstants pentahoJcrConstants = new PentahoJcrConstants(session);
            IRepositoryFileData data = JcrRepositoryFileUtils.getContent(session, pentahoJcrConstants, fileId, versionId, findTransformerForRead(JcrRepositoryFileUtils.getFileContentType(session, pentahoJcrConstants, fileId, versionId), contentClass));
            if (fileId != null) {
                RepositoryFile file = internalGetFileById(fileId, false, null);
                if (file != null) {
                    RepositoryFileAcl acl = aclDao.getAcl(fileId);
                    // Invoke accessVoterManager to see if we have access to perform this operation
                    if (!accessVoterManager.hasAccess(file, RepositoryFilePermission.READ, acl, PentahoSessionHolder.getSession())) {
                        return null;
                    }
                }
            }
            return data;
        }
    });
}
Also used : IRepositoryFileData(org.pentaho.platform.api.repository2.unified.IRepositoryFileData) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) JcrCallback(org.springframework.extensions.jcr.JcrCallback) RepositoryFileAcl(org.pentaho.platform.api.repository2.unified.RepositoryFileAcl) Session(javax.jcr.Session)

Example 34 with RepositoryFileAcl

use of org.pentaho.platform.api.repository2.unified.RepositoryFileAcl in project pentaho-platform by pentaho.

the class JcrRepositoryFileDao method internalCreateFile.

private RepositoryFile internalCreateFile(final Serializable parentFolderId, final RepositoryFile file, final IRepositoryFileData content, final RepositoryFileAcl acl, final String versionMessage) {
    if (isKioskEnabled()) {
        // $NON-NLS-1$
        throw new RuntimeException(Messages.getInstance().getString("JcrRepositoryFileDao.ERROR_0006_ACCESS_DENIED"));
    }
    /*
     * PPP-3049: Changed the Assert.notNull(content) to code that creates a file with a single blank when the assert
     * WOULD have been triggered.
     */
    Assert.notNull(file);
    Assert.isTrue(!file.isFolder());
    // Get repository file info and acl info of parent
    if (parentFolderId != null) {
        RepositoryFile parentRepositoryFile = getFileById(parentFolderId);
        if (parentRepositoryFile != null) {
            RepositoryFileAcl parentAcl = aclDao.getAcl(parentRepositoryFile.getId());
            // Invoke accessVoterManager to see if we have access to perform this operation
            if (!accessVoterManager.hasAccess(parentRepositoryFile, RepositoryFilePermission.WRITE, parentAcl, PentahoSessionHolder.getSession())) {
                return null;
            }
        }
    }
    // Assert.notNull(content);
    DataNode emptyDataNode = new DataNode(file.getName());
    // $NON-NLS-1$ //$NON-NLS-2$
    emptyDataNode.setProperty(" ", "content");
    final IRepositoryFileData emptyContent = new NodeRepositoryFileData(emptyDataNode);
    return (RepositoryFile) jcrTemplate.execute(new JcrCallback() {

        @Override
        public Object doInJcr(final Session session) throws RepositoryException, IOException {
            PentahoJcrConstants pentahoJcrConstants = new PentahoJcrConstants(session);
            JcrRepositoryFileUtils.checkoutNearestVersionableFileIfNecessary(session, pentahoJcrConstants, parentFolderId);
            Node fileNode = JcrRepositoryFileUtils.createFileNode(session, pentahoJcrConstants, parentFolderId, file, content == null ? emptyContent : content, findTransformerForWrite(content == null ? emptyContent.getClass() : content.getClass()));
            // create a tmp file with correct path for default acl creation purposes.
            String path = JcrRepositoryFileUtils.getAbsolutePath(session, pentahoJcrConstants, fileNode);
            RepositoryFile tmpFile = new RepositoryFile.Builder(file).path(path).build();
            // we must create the acl during checkout
            aclDao.createAcl(fileNode.getIdentifier(), acl == null ? defaultAclHandler.createDefaultAcl(tmpFile) : acl);
            session.save();
            if (file.isVersioned()) {
                JcrRepositoryFileUtils.checkinNearestVersionableNodeIfNecessary(session, pentahoJcrConstants, fileNode, versionMessage, file.getCreatedDate(), false);
            }
            JcrRepositoryFileUtils.checkinNearestVersionableFileIfNecessary(session, pentahoJcrConstants, parentFolderId, Messages.getInstance().getString("JcrRepositoryFileDao.USER_0002_VER_COMMENT_ADD_FILE", file.getName(), // $NON-NLS-1$ //$NON-NLS-2$
            (parentFolderId == null ? "root" : parentFolderId.toString())));
            return JcrRepositoryFileUtils.nodeToFile(session, pentahoJcrConstants, pathConversionHelper, lockHelper, fileNode);
        }
    });
}
Also used : IRepositoryFileData(org.pentaho.platform.api.repository2.unified.IRepositoryFileData) DataNode(org.pentaho.platform.api.repository2.unified.data.node.DataNode) NodeRepositoryFileData(org.pentaho.platform.api.repository2.unified.data.node.NodeRepositoryFileData) DataNode(org.pentaho.platform.api.repository2.unified.data.node.DataNode) Node(javax.jcr.Node) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) JcrCallback(org.springframework.extensions.jcr.JcrCallback) RepositoryFileAcl(org.pentaho.platform.api.repository2.unified.RepositoryFileAcl) Session(javax.jcr.Session)

Example 35 with RepositoryFileAcl

use of org.pentaho.platform.api.repository2.unified.RepositoryFileAcl in project pentaho-platform by pentaho.

the class JcrRepositoryFileDao method undeleteFile.

/**
 * {@inheritDoc}
 */
@Override
public void undeleteFile(final Serializable fileId, final String versionMessage) {
    if (isKioskEnabled()) {
        // $NON-NLS-1$
        throw new RuntimeException(Messages.getInstance().getString("JcrRepositoryFileDao.ERROR_0006_ACCESS_DENIED"));
    }
    Assert.notNull(fileId);
    jcrTemplate.execute(new JcrCallback() {

        @Override
        public Object doInJcr(final Session session) throws RepositoryException, IOException {
            PentahoJcrConstants pentahoJcrConstants = new PentahoJcrConstants(session);
            String absOrigParentFolderPath = deleteHelper.getOriginalParentFolderPath(session, pentahoJcrConstants, fileId);
            Serializable origParentFolderId = null;
            RepositoryFile file = getFileById(fileId);
            RepositoryFileAcl acl = aclDao.getAcl(fileId);
            if (!accessVoterManager.hasAccess(file, RepositoryFilePermission.WRITE, acl, PentahoSessionHolder.getSession())) {
                return null;
            }
            // original parent folder path may no longer exist!
            if (session.itemExists(JcrStringHelper.pathEncode(absOrigParentFolderPath))) {
                origParentFolderId = ((Node) session.getItem(JcrStringHelper.pathEncode(absOrigParentFolderPath))).getIdentifier();
            } else {
                // go through each of the segments of the original parent folder path, creating as necessary
                String[] segments = pathConversionHelper.absToRel(absOrigParentFolderPath).split(RepositoryFile.SEPARATOR);
                RepositoryFile lastParentFolder = internalGetFile(session, ServerRepositoryPaths.getTenantRootFolderPath(), false, null);
                for (String segment : segments) {
                    if (StringUtils.hasLength(segment)) {
                        RepositoryFile tmp = internalGetFile(session, pathConversionHelper.relToAbs((lastParentFolder.getPath().equals(RepositoryFile.SEPARATOR) ? "" : lastParentFolder.getPath()) + RepositoryFile.SEPARATOR + segment), false, // $NON-NLS-1$
                        null);
                        if (tmp == null) {
                            lastParentFolder = internalCreateFolder(session, lastParentFolder.getId(), new RepositoryFile.Builder(segment).folder(true).build(), defaultAclHandler.createDefaultAcl(lastParentFolder), null);
                        } else {
                            lastParentFolder = tmp;
                        }
                    }
                }
                origParentFolderId = lastParentFolder.getId();
            }
            JcrRepositoryFileUtils.checkoutNearestVersionableFileIfNecessary(session, pentahoJcrConstants, origParentFolderId);
            deleteHelper.undeleteFile(session, pentahoJcrConstants, fileId);
            session.save();
            JcrRepositoryFileUtils.checkinNearestVersionableFileIfNecessary(session, pentahoJcrConstants, origParentFolderId, versionMessage);
            return null;
        }
    });
}
Also used : Serializable(java.io.Serializable) DataNode(org.pentaho.platform.api.repository2.unified.data.node.DataNode) Node(javax.jcr.Node) UnifiedRepositoryException(org.pentaho.platform.api.repository2.unified.UnifiedRepositoryException) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException) JcrCallback(org.springframework.extensions.jcr.JcrCallback) RepositoryFile(org.pentaho.platform.api.repository2.unified.RepositoryFile) RepositoryFileAcl(org.pentaho.platform.api.repository2.unified.RepositoryFileAcl) Session(javax.jcr.Session)

Aggregations

RepositoryFileAcl (org.pentaho.platform.api.repository2.unified.RepositoryFileAcl)99 RepositoryFile (org.pentaho.platform.api.repository2.unified.RepositoryFile)73 Test (org.junit.Test)50 ITenant (org.pentaho.platform.api.mt.ITenant)25 RepositoryFileSid (org.pentaho.platform.api.repository2.unified.RepositoryFileSid)23 RepositoryFileAce (org.pentaho.platform.api.repository2.unified.RepositoryFileAce)15 Node (javax.jcr.Node)13 Matchers.anyString (org.mockito.Matchers.anyString)13 UnifiedRepositoryException (org.pentaho.platform.api.repository2.unified.UnifiedRepositoryException)13 Serializable (java.io.Serializable)12 Session (javax.jcr.Session)12 JcrCallback (org.springframework.extensions.jcr.JcrCallback)12 ArrayList (java.util.ArrayList)11 RepositoryException (javax.jcr.RepositoryException)10 ByteArrayInputStream (java.io.ByteArrayInputStream)9 IOException (java.io.IOException)9 DataNode (org.pentaho.platform.api.repository2.unified.data.node.DataNode)9 IPentahoSession (org.pentaho.platform.api.engine.IPentahoSession)8 RepositoryFilePermission (org.pentaho.platform.api.repository2.unified.RepositoryFilePermission)8 InputStream (java.io.InputStream)7