Search in sources :

Example 1 with Permissions

use of nl.uva.cs.lobcder.auth.Permissions in project lobcder by skoulouzis.

the class JDBCatalogue method copyFolder.

public void copyFolder(LogicalData toCopy, LogicalData newParent, String newName, MyPrincipal principal, Connection connection) throws SQLException {
    try {
        Permissions toCopyPerm = getPermissions(toCopy.getUid(), toCopy.getOwner(), connection);
        Permissions newParentPerm = getPermissions(newParent.getUid(), newParent.getOwner(), connection);
        Permissions permissionsForNew = new Permissions(principal, newParentPerm);
        if (toCopy.isFolder() && principal.canWrite(newParentPerm)) {
            // ignore folder if there is a problem
            LogicalData newFolderEntry = toCopy.clone();
            newFolderEntry.setName(newName);
            newFolderEntry.setParentRef(newParent.getUid());
            newFolderEntry.setType(Constants.LOGICAL_FOLDER);
            newFolderEntry.setCreateDate(System.currentTimeMillis());
            newFolderEntry.setModifiedDate(System.currentTimeMillis());
            newFolderEntry.setOwner(principal.getUserId());
            newFolderEntry = registerDirLogicalData(newFolderEntry, connection);
            setPermissions(newFolderEntry.getUid(), permissionsForNew, connection);
            if (principal.canRead(toCopyPerm)) {
                try (CallableStatement cs = connection.prepareCall("{CALL copyFolderContentProc(?, ?, ?, ?, ?, ?)}")) {
                    cs.setString(1, principal.getUserId());
                    cs.setString(2, principal.getRolesStr());
                    cs.setString(3, permissionsForNew.getReadStr());
                    cs.setString(4, permissionsForNew.getWriteStr());
                    cs.setLong(5, toCopy.getUid());
                    cs.setLong(6, newFolderEntry.getUid());
                    cs.execute();
                    try (PreparedStatement ps1 = connection.prepareStatement("SELECT uid, ownerId, ldName FROM ldata_table WHERE datatype='logical.folder' AND parentRef = ?")) {
                        ps1.setLong(1, toCopy.getUid());
                        ResultSet rs = ps1.executeQuery();
                        while (rs.next()) {
                            LogicalData element = new LogicalData();
                            element.setUid(rs.getLong(1));
                            element.setOwner(rs.getString(2));
                            element.setType("logical.folder");
                            element.setName(rs.getString(3));
                            element.setParentRef(toCopy.getUid());
                            copyFolder(element, newFolderEntry, element.getName(), principal, connection);
                        }
                    }
                }
            }
        }
    } catch (CloneNotSupportedException e) {
        throw new RuntimeException(e);
    }
}
Also used : LogicalData(nl.uva.cs.lobcder.resources.LogicalData) Permissions(nl.uva.cs.lobcder.auth.Permissions)

Example 2 with Permissions

use of nl.uva.cs.lobcder.auth.Permissions in project lobcder by skoulouzis.

the class JDBCatalogue method removeFolderContent.

public boolean removeFolderContent(LogicalData toRemove, MyPrincipal principal, Connection connection) throws SQLException {
    boolean flag = true;
    Permissions toRemovePerm = getPermissions(toRemove.getUid(), toRemove.getOwner(), connection);
    if (principal.canRead(toRemovePerm) && principal.canWrite(toRemovePerm)) {
        try (PreparedStatement ps1 = connection.prepareStatement("DELETE FROM ldata_table WHERE datatype  = 'logical.file' AND parentRef = ?")) {
            ps1.setLong(1, toRemove.getUid());
            ps1.executeUpdate();
            try (PreparedStatement ps2 = connection.prepareStatement("SELECT uid, ownerId FROM ldata_table WHERE datatype  = 'logical.folder' AND parentRef = ?", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE)) {
                ps2.setLong(1, toRemove.getUid());
                ResultSet rs = ps2.executeQuery();
                while (rs.next()) {
                    LogicalData element = new LogicalData();
                    element.setUid(rs.getLong(1));
                    element.setParentRef(toRemove.getUid());
                    element.setType(Constants.LOGICAL_FOLDER);
                    element.setOwner(rs.getString(2));
                    if (removeFolderContent(element, principal, connection)) {
                        rs.deleteRow();
                    } else {
                        flag = false;
                    }
                }
            }
        }
    } else {
        return false;
    }
    return flag;
}
Also used : LogicalData(nl.uva.cs.lobcder.resources.LogicalData) Permissions(nl.uva.cs.lobcder.auth.Permissions)

Example 3 with Permissions

use of nl.uva.cs.lobcder.auth.Permissions in project lobcder by skoulouzis.

the class JDBCatalogue method copyFile.

public void copyFile(LogicalData toCopy, LogicalData newParent, String newName, MyPrincipal principal, Connection connection) throws SQLException {
    try {
        Permissions toCopyPerm = getPermissions(toCopy.getUid(), toCopy.getOwner(), connection);
        Permissions newParentPerm = getPermissions(newParent.getUid(), newParent.getOwner(), connection);
        Permissions permissionsForNew = new Permissions(principal, newParentPerm);
        if (!toCopy.isFolder() && principal.canRead(toCopyPerm) && principal.canWrite(newParentPerm)) {
            LogicalData newFileEntry = toCopy.clone();
            newFileEntry.setUid(Long.valueOf(0));
            newFileEntry.setOwner(principal.getUserId());
            newFileEntry.setName(newName);
            newFileEntry.setParentRef(newParent.getUid());
            newFileEntry.setCreateDate(System.currentTimeMillis());
            newFileEntry.setModifiedDate(System.currentTimeMillis());
            newFileEntry = registerLogicalData(newFileEntry, connection);
            setPermissions(newFileEntry.getUid(), permissionsForNew, connection);
            try (Statement s = connection.createStatement()) {
                s.executeUpdate("UPDATE pdrigroup_table SET refCount=refCount+1 WHERE pdriGroupId = " + newFileEntry.getPdriGroupId());
            }
        }
    } catch (CloneNotSupportedException cns) {
        throw new RuntimeException(cns);
    }
}
Also used : LogicalData(nl.uva.cs.lobcder.resources.LogicalData) Permissions(nl.uva.cs.lobcder.auth.Permissions)

Example 4 with Permissions

use of nl.uva.cs.lobcder.auth.Permissions in project lobcder by skoulouzis.

the class JDBCatalogue method getPermissions.

public Permissions getPermissions(Long UID, @Nonnull String owner, @Nonnull Connection connection) throws SQLException {
    Permissions p = new Permissions();
    try (Statement s = connection.createStatement()) {
        ResultSet rs = s.executeQuery("SELECT permType, roleName FROM permission_table " + "WHERE permission_table.ldUidRef = " + UID);
        Set<String> canRead = new HashSet<>();
        Set<String> canWrite = new HashSet<>();
        while (rs.next()) {
            if (rs.getString(1).equals("read")) {
                canRead.add(rs.getString(2));
            } else {
                canWrite.add(rs.getString(2));
            }
        }
        p.setRead(canRead);
        p.setWrite(canWrite);
        p.setOwner(owner);
        p.setLocalId(UID);
        return p;
    }
}
Also used : Permissions(nl.uva.cs.lobcder.auth.Permissions)

Example 5 with Permissions

use of nl.uva.cs.lobcder.auth.Permissions in project lobcder by skoulouzis.

the class ItemBean method main.

// / test
public static void main(String[] args) {
    try {
        DatatypeFactory df = DatatypeFactory.newInstance();
        GregorianCalendar gregorianCalendar = new GregorianCalendar();
        ItemBean ldb = new ItemBean();
        ldb.setUid(123L);
        ldb.setCreateDate(df.newXMLGregorianCalendar(gregorianCalendar));
        ldb.setType(Type.FILE);
        ldb.setName("Name");
        ldb.setParentRef(45L);
        Permissions p = new Permissions();
        p.setOwner("Dima");
        p.getRead().add("group_read1");
        p.getRead().add("group_read2");
        p.getWrite().add("group_write1");
        ldb.setPermissions(p);
        ldb.setParentRef(46L);
        ldb.addContentType("application/xml");
        ldb.setPath("/qwa/qwe");
        List<PDRIDescr> pris = new ArrayList<>();
        // pdril.getPdriList().add(new PdriBean("name", "url", "username", "password", false, null, false));
        // pdril.getPdriList().add(new PdriBean("name2","url", "username", "password", false, null, false));
        // ldb.setPdriList(pdril);
        LockInfo lockInfo = new LockInfo(LockInfo.LockScope.EXCLUSIVE, LockInfo.LockType.WRITE, "user", LockInfo.LockDepth.INFINITY);
        // LockTokenBean lockToken = new LockTokenBean();
        // lockToken.setLocktoken("token-name");
        // lockToken.setLockInfo(lockInfo);
        // lockToken.setLockedUntil(df.newXMLGregorianCalendar(gregorianCalendar));
        // ldb.setLock(lockToken);
        DriBean dri = new DriBean();
        dri.setStatus(DriBean.Status.unavailable);
        ldb.setDri(dri);
        JAXBContext context = JAXBContext.newInstance(ItemBean.class);
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        m.marshal(ldb, System.err);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Marshaller(javax.xml.bind.Marshaller) DatatypeFactory(javax.xml.datatype.DatatypeFactory) PDRIDescr(nl.uva.cs.lobcder.resources.PDRIDescr) XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) Permissions(nl.uva.cs.lobcder.auth.Permissions) LockInfo(io.milton.http.LockInfo) JAXBContext(javax.xml.bind.JAXBContext)

Aggregations

Permissions (nl.uva.cs.lobcder.auth.Permissions)40 LogicalData (nl.uva.cs.lobcder.resources.LogicalData)29 SQLException (java.sql.SQLException)23 Connection (java.sql.Connection)22 MyPrincipal (nl.uva.cs.lobcder.auth.MyPrincipal)21 ArrayList (java.util.ArrayList)7 BadRequestException (io.milton.http.exceptions.BadRequestException)6 NotAuthorizedException (io.milton.http.exceptions.NotAuthorizedException)6 PDRIDescr (nl.uva.cs.lobcder.resources.PDRIDescr)5 PreparedStatement (java.sql.PreparedStatement)4 ResultSet (java.sql.ResultSet)4 Stack (java.util.Stack)4 Path (io.milton.common.Path)3 Path (javax.ws.rs.Path)3 JAXBElement (javax.xml.bind.JAXBElement)3 QName (javax.xml.namespace.QName)3 LogicalDataWrapped (nl.uva.cs.lobcder.rest.wrappers.LogicalDataWrapped)3 ConflictException (io.milton.http.exceptions.ConflictException)2 IOException (java.io.IOException)2 URISyntaxException (java.net.URISyntaxException)2