Search in sources :

Example 26 with Permissions

use of nl.uva.cs.lobcder.auth.Permissions 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 27 with Permissions

use of nl.uva.cs.lobcder.auth.Permissions 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)

Example 28 with Permissions

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

the class SetBulkPermissionsResource method setPermissions2.

/**
 * Sets permissions for folder and subtree
 *
 * @param path the folder's path
 * @param jbPermissions the permissions: owner, read, write
 */
@PUT
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public void setPermissions2(@QueryParam("path") String path, JAXBElement<Permissions> jbPermissions) throws UnsupportedEncodingException {
    try (Connection connection = catalogue.getConnection()) {
        try {
            Permissions permissions = jbPermissions.getValue();
            MyPrincipal principal = (MyPrincipal) request.getAttribute("myprincipal");
            LogicalData ld = catalogue.getLogicalDataByPath(io.milton.common.Path.path(path), 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 (!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 (!entry_owner.equals(permissions.getOwner())) {
                                    changeOwner.add(entry_uid);
                                }
                            }
                        }
                    }
                }
            }
            final int batchSize = 100;
            int count = 0;
            try (PreparedStatement psDel = connection.prepareStatement("DELETE FROM permission_table WHERE permission_table.ldUidRef = ?");
                PreparedStatement psIns = connection.prepareStatement("INSERT INTO permission_table (permType, ldUidRef, roleName) VALUES (?, ?, ?)")) {
                for (Long uid : elements) {
                    psDel.setLong(1, uid);
                    psDel.addBatch();
                    for (String cr : permissions.getRead()) {
                        psIns.setString(1, "read");
                        psIns.setLong(2, uid);
                        psIns.setString(3, cr);
                        psIns.addBatch();
                    }
                    for (String cw : permissions.getWrite()) {
                        psIns.setString(1, "write");
                        psIns.setLong(2, uid);
                        psIns.setString(3, cw);
                        psIns.addBatch();
                    }
                    count++;
                    if (count % batchSize == 0) {
                        psDel.executeBatch();
                        psIns.executeBatch();
                    }
                }
                psDel.executeBatch();
                psIns.executeBatch();
            }
            try (PreparedStatement ps = connection.prepareStatement("UPDATE ldata_table SET ownerId = ? WHERE uid = ?")) {
                count = 0;
                ps.setString(1, permissions.getOwner());
                for (Long uid : changeOwner) {
                    ps.setLong(2, uid);
                    ps.addBatch();
                    count++;
                    if (count % batchSize == 0) {
                        ps.executeBatch();
                    }
                }
                ps.executeBatch();
            }
            connection.commit();
        } catch (SQLException ex) {
            Logger.getLogger(SetBulkPermissionsResource.class.getName()).log(Level.SEVERE, null, ex);
            connection.rollback();
            throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
        }
    } catch (SQLException ex) {
        Logger.getLogger(SetBulkPermissionsResource.class.getName()).log(Level.SEVERE, null, ex);
        throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
    }
}
Also used : ArrayList(java.util.ArrayList) 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)

Example 29 with Permissions

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

the class SetBulkPermissionsResource method setPermissions.

private void setPermissions(Long uid, MyPrincipal principal, CallableStatement cs, PreparedStatement ps, Connection cn) throws SQLException {
    ps.setLong(1, uid);
    ArrayList<Long> folders = new ArrayList<>();
    try (ResultSet resultSet = ps.executeQuery()) {
        while (resultSet.next()) {
            Long myUid = resultSet.getLong(1);
            String myOwner = resultSet.getString(2);
            Permissions p = catalogue.getPermissions(myUid, myOwner, cn);
            if (principal.canRead(p) && myUid != 1) {
                folders.add(uid);
            }
        }
    }
    cs.setLong(6, uid);
    cs.execute();
    cn.commit();
    for (Long _uid : folders) {
        setPermissions(_uid, principal, cs, ps, cn);
    }
}
Also used : ArrayList(java.util.ArrayList) Permissions(nl.uva.cs.lobcder.auth.Permissions)

Example 30 with Permissions

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

the class PathReservationService method request.

@Path("{commID}/request/")
@GET
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public ReservationInfo request(@PathParam("commID") String communicationID) throws MalformedURLException, IOException {
    // rest/reservation/5455/request/?dataPath=/sbuiifv/dsudsuds&storageSiteHost=sps1&storageSiteHost=sps2&storageSiteHost=sps3
    MyPrincipal mp = (MyPrincipal) request.getAttribute("myprincipal");
    MultivaluedMap<String, String> queryParameters = info.getQueryParameters();
    if (mp.getRoles().contains("planner") || mp.isAdmin() && queryParameters != null && !queryParameters.isEmpty()) {
        String dataName = queryParameters.getFirst("dataName");
        if (dataName != null && dataName.length() > 0) {
            List<String> storageList = queryParameters.get("storageSiteHost");
            String storageSiteHost = null;
            int index = -1;
            if (storageList != null && storageList.size() > 0) {
                storageSiteHost = getStorageSiteHost(storageList);
                index = storageList.indexOf(storageSiteHost);
            } else {
            }
            LogicalData ld;
            Permissions p = null;
            try (Connection cn = getCatalogue().getConnection()) {
                // -----------------THIS IS TEMPORARY IT'S ONLY FOR THE DEMO!!!!!!!!!!
                String fileNameWithOutExt = FilenameUtils.removeExtension(dataName);
                fileNameWithOutExt += ".webm";
                List<LogicalData> ldList = getCatalogue().getLogicalDataByName(io.milton.common.Path.path(fileNameWithOutExt), cn);
                if (ldList == null || ldList.isEmpty()) {
                    ldList = getCatalogue().getLogicalDataByName(io.milton.common.Path.path(dataName), cn);
                }
                // --------------------------------------------------------------
                if (ldList == null || ldList.isEmpty()) {
                    Response.status(Response.Status.NOT_FOUND);
                    return null;
                }
                // Should be only one
                ld = ldList.get(0);
                if (ld != null) {
                    p = getCatalogue().getPermissions(ld.getUid(), ld.getOwner(), cn);
                }
            } catch (SQLException ex) {
                log.log(Level.SEVERE, null, ex);
                throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
            }
            // Integer alocationStrategy = Integer.valueOf(queryParameters.getFirst("allocationStrategy"));
            ReservationInfo info = new ReservationInfo();
            if (p != null && mp.canRead(p)) {
                info.setCommunicationID(communicationID);
                String workerURL = scheduleWorker(storageSiteHost, ld);
                info.setCommunicationID(communicationID);
                storageSiteHost = Network.replaceIP(storageSiteHost);
                info.setStorageHost(storageSiteHost);
                info.setStorageHostIndex(index);
                workerURL = Network.replaceIP(workerURL);
                info.setWorkerDataAccessURL(workerURL);
            }
            return info;
        }
    }
    return null;
}
Also used : LogicalData(nl.uva.cs.lobcder.resources.LogicalData) MyPrincipal(nl.uva.cs.lobcder.auth.MyPrincipal) WebApplicationException(javax.ws.rs.WebApplicationException) SQLException(java.sql.SQLException) ReservationInfo(nl.uva.cs.lobcder.rest.wrappers.ReservationInfo) Permissions(nl.uva.cs.lobcder.auth.Permissions) Connection(java.sql.Connection) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

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