Search in sources :

Example 36 with LogicalData

use of nl.uva.cs.lobcder.resources.LogicalData in project lobcder by skoulouzis.

the class PermissionsResource method delPermissionsRecursive.

@Path("recursive/{uid}/")
@DELETE
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public UIDS delPermissionsRecursive(@PathParam("uid") Long uid_p, @DefaultValue("False") @QueryParam("getall") Boolean getall, JAXBElement<Permissions> jbPermissions) {
    UIDS result = new UIDS();
    try (Connection connection = catalogue.getConnection()) {
        try {
            Permissions permissions = jbPermissions.getValue();
            MyPrincipal principal = (MyPrincipal) request.getAttribute("myprincipal");
            LogicalData ld = catalogue.getLogicalDataByUid(uid_p, connection);
            Stack<Long> folders = new Stack<>();
            ArrayList<Long> elements = new ArrayList<>();
            Permissions p = catalogue.getPermissions(ld.getUid(), ld.getOwner(), connection);
            if (ld.isFolder() && principal.canRead(p)) {
                folders.add(ld.getUid());
            }
            if (principal.canWrite(p)) {
                elements.add(ld.getUid());
            }
            try (PreparedStatement ps = connection.prepareStatement("SELECT uid, ownerId, datatype FROM ldata_table WHERE parentRef = ?")) {
                while (!folders.isEmpty()) {
                    Long curUid = folders.pop();
                    ps.setLong(1, curUid);
                    try (ResultSet resultSet = ps.executeQuery()) {
                        while (resultSet.next()) {
                            Long entry_uid = resultSet.getLong(1);
                            String entry_owner = resultSet.getString(2);
                            String entry_datatype = resultSet.getString(3);
                            Permissions entry_p = catalogue.getPermissions(entry_uid, entry_owner, connection);
                            if (entry_datatype.equals(Constants.LOGICAL_FOLDER) && principal.canRead(entry_p)) {
                                folders.push(entry_uid);
                            }
                            if (principal.canWrite(entry_p)) {
                                elements.add(entry_uid);
                            }
                        }
                    }
                }
            }
            try (PreparedStatement ps = connection.prepareStatement("DELETE FROM permission_table WHERE permType = ? AND ldUidRef = ? AND roleName=?")) {
                for (Long uid : elements) {
                    for (String cr : permissions.getRead()) {
                        ps.setString(1, "read");
                        ps.setLong(2, uid);
                        ps.setString(3, cr);
                        ps.addBatch();
                    }
                    for (String cw : permissions.getWrite()) {
                        ps.setString(1, "write");
                        ps.setLong(2, uid);
                        ps.setString(3, cw);
                        ps.addBatch();
                    }
                    for (int i : ps.executeBatch()) {
                        if (getall || (i > 0)) {
                            String myuid = catalogue.getGlobalID(uid, connection);
                            if (myuid != null) {
                                result.uids.add(myuid);
                            }
                            break;
                        }
                    }
                }
            }
            connection.commit();
            return result;
        } catch (SQLException ex) {
            Logger.getLogger(PermissionsResource.class.getName()).log(Level.SEVERE, null, ex);
            connection.rollback();
            throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
        }
    } catch (SQLException ex) {
        Logger.getLogger(PermissionsResource.class.getName()).log(Level.SEVERE, null, ex);
        throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) Stack(java.util.Stack) LogicalData(nl.uva.cs.lobcder.resources.LogicalData) MyPrincipal(nl.uva.cs.lobcder.auth.MyPrincipal) Permissions(nl.uva.cs.lobcder.auth.Permissions) ResultSet(java.sql.ResultSet)

Example 37 with LogicalData

use of nl.uva.cs.lobcder.resources.LogicalData in project lobcder by skoulouzis.

the class PermissionsResource method getPermissions.

/**
 * Gets the resource's permissions: owner, read, write
 *
 * @param uid the id of the resource
 * @return the resource's permissions: owner, read, write
 */
@Path("{uid}/")
@GET
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Permissions getPermissions(@PathParam("uid") Long uid) {
    try (Connection cn = catalogue.getConnection()) {
        LogicalData res = catalogue.getLogicalDataByUid(uid, cn);
        if (res == null) {
            throw new WebApplicationException(Response.Status.NOT_FOUND);
        }
        MyPrincipal mp = (MyPrincipal) request.getAttribute("myprincipal");
        Permissions p = catalogue.getPermissions(uid, res.getOwner(), cn);
        if (!mp.canRead(p)) {
            throw new WebApplicationException(Response.Status.UNAUTHORIZED);
        }
        return p;
    } catch (SQLException ex) {
        Logger.getLogger(PermissionsResource.class.getName()).log(Level.SEVERE, null, ex);
        throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
    }
}
Also used : LogicalData(nl.uva.cs.lobcder.resources.LogicalData) MyPrincipal(nl.uva.cs.lobcder.auth.MyPrincipal) SQLException(java.sql.SQLException) Connection(java.sql.Connection) Permissions(nl.uva.cs.lobcder.auth.Permissions)

Example 38 with LogicalData

use of nl.uva.cs.lobcder.resources.LogicalData in project lobcder by skoulouzis.

the class PermissionsResource method setPermissions.

/**
 * Sets the resource's permissions: owner, read, write
 *
 * @param uid the id of the resource
 * @param jbPermissions the permissions: owner, read, write
 */
@Path("{uid}/")
@PUT
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public void setPermissions(@PathParam("uid") Long uid, JAXBElement<Permissions> jbPermissions) {
    try (Connection cn = catalogue.getConnection()) {
        try {
            LogicalData res = catalogue.getLogicalDataByUid(uid, cn);
            if (res == null) {
                throw new WebApplicationException(Response.Status.NOT_FOUND);
            }
            MyPrincipal mp = (MyPrincipal) request.getAttribute("myprincipal");
            Permissions p = catalogue.getPermissions(uid, res.getOwner(), cn);
            if (!mp.canWrite(p)) {
                throw new WebApplicationException(Response.Status.UNAUTHORIZED);
            }
            Permissions permissions = jbPermissions.getValue();
            catalogue.updateOwner(uid, permissions.getOwner(), cn);
            catalogue.setPermissions(uid, permissions, cn);
            cn.commit();
        } catch (SQLException ex) {
            Logger.getLogger(PermissionsResource.class.getName()).log(Level.SEVERE, null, ex);
            cn.rollback();
            throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
        }
    } catch (SQLException ex) {
        Logger.getLogger(PermissionsResource.class.getName()).log(Level.SEVERE, null, ex);
        throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
    }
}
Also used : LogicalData(nl.uva.cs.lobcder.resources.LogicalData) MyPrincipal(nl.uva.cs.lobcder.auth.MyPrincipal) SQLException(java.sql.SQLException) Connection(java.sql.Connection) Permissions(nl.uva.cs.lobcder.auth.Permissions)

Example 39 with LogicalData

use of nl.uva.cs.lobcder.resources.LogicalData in project lobcder by skoulouzis.

the class PermissionsResource method setPermissionsRecursive.

@Path("recursive/{uid}/")
@PUT
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public UIDS setPermissionsRecursive(@PathParam("uid") Long uid_p, @DefaultValue("False") @QueryParam("getall") Boolean getall, JAXBElement<Permissions> jbPermissions) {
    UIDS result = new UIDS();
    try (Connection connection = catalogue.getConnection()) {
        try {
            Permissions permissions = jbPermissions.getValue();
            MyPrincipal principal = (MyPrincipal) request.getAttribute("myprincipal");
            LogicalData ld = catalogue.getLogicalDataByUid(uid_p, connection);
            Stack<Long> folders = new Stack<>();
            ArrayList<Long> elements = new ArrayList<>();
            ArrayList<Long> changeOwner = new ArrayList<>();
            Permissions p = catalogue.getPermissions(ld.getUid(), ld.getOwner(), connection);
            if (ld.isFolder() && principal.canRead(p)) {
                folders.add(ld.getUid());
            }
            if (principal.canWrite(p)) {
                elements.add(ld.getUid());
                if (permissions.getOwner() != null && !ld.getOwner().equals(permissions.getOwner())) {
                    changeOwner.add(ld.getUid());
                }
            }
            try (PreparedStatement ps = connection.prepareStatement("SELECT uid, ownerId, datatype FROM ldata_table WHERE parentRef = ?")) {
                while (!folders.isEmpty()) {
                    Long curUid = folders.pop();
                    ps.setLong(1, curUid);
                    try (ResultSet resultSet = ps.executeQuery()) {
                        while (resultSet.next()) {
                            Long entry_uid = resultSet.getLong(1);
                            String entry_owner = resultSet.getString(2);
                            String entry_datatype = resultSet.getString(3);
                            Permissions entry_p = catalogue.getPermissions(entry_uid, entry_owner, connection);
                            if (entry_datatype.equals(Constants.LOGICAL_FOLDER) && principal.canRead(entry_p)) {
                                folders.push(entry_uid);
                            }
                            if (principal.canWrite(entry_p)) {
                                elements.add(entry_uid);
                                if (permissions.getOwner() != null && !entry_owner.equals(permissions.getOwner())) {
                                    changeOwner.add(entry_uid);
                                }
                            }
                        }
                    }
                }
            }
            try (PreparedStatement ps = connection.prepareStatement("SELECT permType, roleName, ldUidRef, id  FROM permission_table WHERE permission_table.ldUidRef = ?", java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_UPDATABLE)) {
                for (Long uid : elements) {
                    ps.setLong(1, uid);
                    ResultSet rs = ps.executeQuery();
                    Set<String> read = new HashSet<>(permissions.getRead());
                    Set<String> write = new HashSet<>(permissions.getWrite());
                    boolean updateFlag = false;
                    while (rs.next()) {
                        String permType = rs.getString(1);
                        String roleName = rs.getString(2);
                        if (permType.equals("read")) {
                            if (!read.remove(roleName)) {
                                rs.deleteRow();
                                updateFlag = true;
                            }
                        } else if (permType.equals("write")) {
                            if (!write.remove(roleName)) {
                                rs.deleteRow();
                                updateFlag = true;
                            }
                        }
                    }
                    for (String role : read) {
                        rs.moveToInsertRow();
                        rs.updateString(1, "read");
                        rs.updateString(2, role);
                        rs.updateLong(3, uid);
                        rs.insertRow();
                    }
                    for (String role : write) {
                        rs.moveToInsertRow();
                        rs.updateString(1, "write");
                        rs.updateString(2, role);
                        rs.updateLong(3, uid);
                        rs.insertRow();
                    }
                    if (getall || updateFlag || !read.isEmpty() || !write.isEmpty()) {
                        String myuid = catalogue.getGlobalID(uid, connection);
                        if (myuid != null) {
                            result.uids.add(myuid);
                        }
                    }
                }
            }
            if (permissions.getOwner() != null && !permissions.getOwner().isEmpty()) {
                try (PreparedStatement ps = connection.prepareStatement("SELECT ownerId, uid from ldata_table WHERE uid = ?", java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_UPDATABLE)) {
                    for (Long uid : changeOwner) {
                        ps.setLong(1, uid);
                        ResultSet rs = ps.executeQuery();
                        if (rs.next()) {
                            rs.updateString(1, permissions.getOwner());
                            rs.updateRow();
                            if (!getall) {
                                result.uids.add(catalogue.getGlobalID(uid, connection));
                            }
                        }
                    }
                }
            }
            connection.commit();
            return result;
        } catch (SQLException ex) {
            Logger.getLogger(PermissionsResource.class.getName()).log(Level.SEVERE, null, ex);
            connection.rollback();
            throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
        }
    } catch (SQLException ex) {
        Logger.getLogger(PermissionsResource.class.getName()).log(Level.SEVERE, null, ex);
        throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) Stack(java.util.Stack) LogicalData(nl.uva.cs.lobcder.resources.LogicalData) MyPrincipal(nl.uva.cs.lobcder.auth.MyPrincipal) Permissions(nl.uva.cs.lobcder.auth.Permissions) ResultSet(java.sql.ResultSet) HashSet(java.util.HashSet)

Example 40 with LogicalData

use of nl.uva.cs.lobcder.resources.LogicalData in project lobcder by skoulouzis.

the class SetBulkPermissionsResource method setPermissions.

private void setPermissions(String rootPath, Permissions perm, MyPrincipal principal, @Nonnull Connection connection) throws SQLException, UnsupportedEncodingException {
    LogicalData ld = catalogue.getLogicalDataByPath(io.milton.common.Path.path(rootPath), connection);
    Permissions p = catalogue.getPermissions(ld.getUid(), ld.getOwner(), connection);
    if (ld.isFolder() && principal.canRead(p)) {
        try (CallableStatement cs = connection.prepareCall("{CALL updatePermissionsDirProc(?, ?, ?, ?, ?, ?)}");
            PreparedStatement ps = connection.prepareStatement("SELECT uid, ownerId, ldName FROM ldata_table WHERE parentRef = ? AND datatype = '" + Constants.LOGICAL_FOLDER + "'")) {
            cs.setString(1, principal.getUserId());
            cs.setString(2, principal.getRolesStr());
            cs.setString(3, perm.getOwner());
            cs.setString(4, perm.getReadStr());
            cs.setString(5, perm.getWriteStr());
            setPermissions(ld.getUid(), principal, cs, ps, connection);
        }
    }
    if (principal.canWrite(p)) {
        catalogue.updateOwner(ld.getUid(), perm.getOwner(), connection);
        catalogue.setPermissions(ld.getUid(), perm, connection);
    }
}
Also used : LogicalData(nl.uva.cs.lobcder.resources.LogicalData) Permissions(nl.uva.cs.lobcder.auth.Permissions)

Aggregations

LogicalData (nl.uva.cs.lobcder.resources.LogicalData)71 Connection (java.sql.Connection)29 SQLException (java.sql.SQLException)29 Permissions (nl.uva.cs.lobcder.auth.Permissions)29 MyPrincipal (nl.uva.cs.lobcder.auth.MyPrincipal)20 PreparedStatement (java.sql.PreparedStatement)11 ResultSet (java.sql.ResultSet)10 Path (io.milton.common.Path)7 ArrayList (java.util.ArrayList)7 BadRequestException (io.milton.http.exceptions.BadRequestException)6 NotAuthorizedException (io.milton.http.exceptions.NotAuthorizedException)5 PDRIDescr (nl.uva.cs.lobcder.resources.PDRIDescr)5 ConflictException (io.milton.http.exceptions.ConflictException)4 URISyntaxException (java.net.URISyntaxException)4 Stack (java.util.Stack)4 Path (javax.ws.rs.Path)4 LogicalDataWrapped (nl.uva.cs.lobcder.rest.wrappers.LogicalDataWrapped)4 VRL (nl.uva.vlet.vrl.VRL)4 PreConditionFailedException (io.milton.http.exceptions.PreConditionFailedException)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3