Search in sources :

Example 16 with Permissions

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

the class WebDataFileResource method copyTo.

@Override
public void copyTo(CollectionResource collectionResource, String name) throws NotAuthorizedException, BadRequestException, ConflictException {
    WebDataDirResource toWDDR = (WebDataDirResource) collectionResource;
    Logger.getLogger(WebDataFileResource.class.getName()).log(Level.FINEST, "copyTo(''{0}'', ''{1}'') for {2}", new Object[] { toWDDR.getPath(), name, getPath() });
    try (Connection connection = getCatalogue().getConnection()) {
        try {
            Permissions newParentPerm = getCatalogue().getPermissions(toWDDR.getLogicalData().getUid(), toWDDR.getLogicalData().getOwner(), connection);
            if (!getPrincipal().canWrite(newParentPerm)) {
                throw new NotAuthorizedException(this);
            }
            getCatalogue().copyFile(getLogicalData(), toWDDR.getLogicalData(), name, getPrincipal(), connection);
            connection.commit();
        } catch (SQLException | NotAuthorizedException e) {
            Logger.getLogger(WebDataFileResource.class.getName()).log(Level.SEVERE, null, e);
            connection.rollback();
            throw new BadRequestException(this, e.getMessage());
        }
    } catch (SQLException e) {
        Logger.getLogger(WebDataFileResource.class.getName()).log(Level.SEVERE, null, e);
        throw new BadRequestException(this, e.getMessage());
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) Permissions(nl.uva.cs.lobcder.auth.Permissions) BadRequestException(io.milton.http.exceptions.BadRequestException) NotAuthorizedException(io.milton.http.exceptions.NotAuthorizedException)

Example 17 with Permissions

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

the class WebDataFileResource method moveTo.

@Override
public void moveTo(CollectionResource collectionResource, String name) throws ConflictException, NotAuthorizedException, BadRequestException {
    WebDataDirResource toWDDR = (WebDataDirResource) collectionResource;
    Logger.getLogger(WebDataFileResource.class.getName()).log(Level.FINEST, "moveTo(''{0}'', ''{1}'') for {2}", new Object[] { toWDDR.getPath(), name, getPath() });
    try (Connection connection = getCatalogue().getConnection()) {
        try {
            Permissions destPerm = getCatalogue().getPermissions(toWDDR.getLogicalData().getUid(), toWDDR.getLogicalData().getOwner(), connection);
            LogicalData parentLD = getCatalogue().getLogicalDataByUid(getLogicalData().getParentRef());
            Permissions parentPerm = getCatalogue().getPermissions(parentLD.getUid(), parentLD.getOwner());
            if (!(getPrincipal().canWrite(destPerm) && getPrincipal().canWrite(parentPerm))) {
                throw new NotAuthorizedException(this);
            }
            getCatalogue().moveEntry(getLogicalData(), toWDDR.getLogicalData(), name, connection);
            connection.commit();
        } catch (Exception e) {
            Logger.getLogger(WebDataFileResource.class.getName()).log(Level.SEVERE, null, e);
            connection.rollback();
            throw new BadRequestException(this, e.getMessage());
        }
    } catch (SQLException e) {
        Logger.getLogger(WebDataFileResource.class.getName()).log(Level.SEVERE, null, e);
        throw new BadRequestException(this, e.getMessage());
    }
}
Also used : LogicalData(nl.uva.cs.lobcder.resources.LogicalData) SQLException(java.sql.SQLException) Connection(java.sql.Connection) Permissions(nl.uva.cs.lobcder.auth.Permissions) BadRequestException(io.milton.http.exceptions.BadRequestException) NotAuthorizedException(io.milton.http.exceptions.NotAuthorizedException) ConflictException(io.milton.http.exceptions.ConflictException) URISyntaxException(java.net.URISyntaxException) SQLException(java.sql.SQLException) BadRequestException(io.milton.http.exceptions.BadRequestException) IOException(java.io.IOException) NotAuthorizedException(io.milton.http.exceptions.NotAuthorizedException) UnknownHostException(java.net.UnknownHostException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NotFoundException(io.milton.http.exceptions.NotFoundException)

Example 18 with Permissions

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

the class WebDataResource method authorise.

@Override
public boolean authorise(Request request, Request.Method method, Auth auth) {
    try {
        if (auth == null) {
            return false;
        }
        fromAddress = request.getFromAddress();
        String msg = "From: " + fromAddress + " User: " + getPrincipal().getUserId() + " Method: " + method;
        Logger.getLogger(WebDataResource.class.getName()).log(Level.INFO, msg);
        LogicalData parentLD;
        Permissions p;
        switch(method) {
            case ACL:
                return getPrincipal().canWrite(getPermissions());
            case HEAD:
                return true;
            case PROPFIND:
                return getPrincipal().canRead(getPermissions());
            case PROPPATCH:
                return getPrincipal().canWrite(getPermissions());
            case MKCALENDAR:
                return false;
            case COPY:
                return getPrincipal().canRead(getPermissions());
            case MOVE:
                return true;
            case LOCK:
                return getPrincipal().canWrite(getPermissions());
            case UNLOCK:
                return getPrincipal().canWrite(getPermissions());
            case DELETE:
                parentLD = getCatalogue().getLogicalDataByUid(getLogicalData().getParentRef());
                p = getCatalogue().getPermissions(parentLD.getUid(), parentLD.getOwner());
                return getPrincipal().canWrite(p);
            case GET:
                return getPrincipal().canRead(getPermissions());
            case OPTIONS:
                return getPrincipal().canRead(getPermissions());
            case POST:
                return getPrincipal().canWrite(getPermissions());
            case PUT:
                return getPrincipal().canWrite(getPermissions());
            case TRACE:
                return false;
            case CONNECT:
                return false;
            case REPORT:
                return false;
            default:
                return true;
        }
    } catch (Throwable th) {
        Logger.getLogger(WebDataResource.class.getName()).log(Level.SEVERE, "Exception in authorize for a resource " + getPath(), th);
        return false;
    }
// return false;
}
Also used : Permissions(nl.uva.cs.lobcder.auth.Permissions)

Example 19 with Permissions

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

the class WebDataResource method createResouses.

private Map<Long, Pair<WebDataFileResource, Long>> createResouses(Request request) throws SQLException, UnsupportedEncodingException, NotAuthorizedException, NoSuchAlgorithmException, IOException {
    Map<Long, Pair<WebDataFileResource, Long>> resources = null;
    try (Connection connection = getCatalogue().getConnection()) {
        Map<String, FileItem> files = request.getFiles();
        Collection<FileItem> fileItems = files.values();
        resources = new HashMap<>();
        WebDataFileResource resource = null;
        for (FileItem fi : fileItems) {
            Long pdriGroupid;
            Path newPath = Path.path(getPath(), fi.getName());
            LogicalData fileLogicalData = getCatalogue().getLogicalDataByPath(newPath, connection);
            String contentType = mimeTypeMap.get(FilenameUtils.getExtension(fi.getName()));
            if (fileLogicalData != null) {
                Permissions p = getCatalogue().getPermissions(fileLogicalData.getUid(), fileLogicalData.getOwner(), connection);
                if (!getPrincipal().canWrite(p)) {
                    throw new NotAuthorizedException(this);
                }
                fileLogicalData.setLength(fi.getSize());
                fileLogicalData.setModifiedDate(System.currentTimeMillis());
                fileLogicalData.setLastAccessDate(fileLogicalData.getModifiedDate());
                fileLogicalData.addContentType(contentType);
                pdriGroupid = fileLogicalData.getPdriGroupId();
                resource = new WebDataFileResource(fileLogicalData, Path.path(getPath(), fi.getName()), getCatalogue(), authList);
            } else {
                fileLogicalData = new LogicalData();
                fileLogicalData.setName(fi.getName());
                fileLogicalData.setParentRef(getLogicalData().getUid());
                fileLogicalData.setType(Constants.LOGICAL_FILE);
                fileLogicalData.setOwner(getPrincipal().getUserId());
                fileLogicalData.setLength(fi.getSize());
                fileLogicalData.setCreateDate(System.currentTimeMillis());
                fileLogicalData.setModifiedDate(System.currentTimeMillis());
                fileLogicalData.setLastAccessDate(System.currentTimeMillis());
                fileLogicalData.setTtlSec(getLogicalData().getTtlSec());
                fileLogicalData.addContentType(contentType);
                pdriGroupid = getCatalogue().associateLogicalDataAndPdriGroup(fileLogicalData, connection);
                getCatalogue().setPreferencesOn(fileLogicalData.getUid(), getLogicalData().getUid(), connection);
                List<String> pref = getLogicalData().getDataLocationPreferences();
                fileLogicalData.setDataLocationPreferences(pref);
                resource = new WebDataFileResource(fileLogicalData, Path.path(getPath(), fi.getName()), getCatalogue(), authList);
            }
            MutablePair<WebDataFileResource, Long> pair = new MutablePair<>();
            pair.setRight(pdriGroupid);
            pair.setLeft(resource);
            resources.put(Long.valueOf(resource.getUniqueId()), pair);
        }
        connection.commit();
        connection.close();
    }
    return resources;
}
Also used : Path(io.milton.common.Path) NotAuthorizedException(io.milton.http.exceptions.NotAuthorizedException) MutablePair(org.apache.commons.lang3.tuple.MutablePair) Permissions(nl.uva.cs.lobcder.auth.Permissions) MutablePair(org.apache.commons.lang3.tuple.MutablePair) Pair(org.apache.commons.lang3.tuple.Pair)

Example 20 with Permissions

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

the class Items method queryLogicalData.

private List<LogicalDataWrapped> queryLogicalData(@Nonnull MyPrincipal mp, @Nonnull Connection cn) throws Exception {
    MultivaluedMap<String, String> queryParameters = info.getQueryParameters();
    boolean addFlag = true;
    String rootPath = (queryParameters.containsKey("path") && queryParameters.get("path").iterator().hasNext()) ? queryParameters.get("path").iterator().next() : "/";
    if (!rootPath.equals("/") && rootPath.endsWith("/")) {
        rootPath = rootPath.substring(0, rootPath.length() - 1);
    }
    int rowLimit;
    try {
        rowLimit = (queryParameters.containsKey("limit") && queryParameters.get("limit").iterator().hasNext()) ? Integer.valueOf(queryParameters.get("limit").iterator().next()).intValue() : defaultRowLimit;
    } catch (Throwable th) {
        rowLimit = defaultRowLimit;
    }
    LogicalData ld = getCatalogue().getLogicalDataByPath(io.milton.common.Path.path(rootPath), cn);
    List<LogicalDataWrapped> logicalDataWrappedList = new ArrayList<>();
    if (ld == null || rowLimit < 1) {
        return logicalDataWrappedList;
    }
    Permissions p = getCatalogue().getPermissions(ld.getUid(), ld.getOwner(), cn);
    if (mp.canRead(p)) {
        try (PreparedStatement ps1 = cn.prepareStatement("SELECT uid, parentRef, " + "ownerId, datatype, ldName, createDate, modifiedDate, ldLength, " + "contentTypesStr, pdriGroupRef, isSupervised, checksum, lastValidationDate, " + "lockTokenID, lockScope, lockType, lockedByUser, lockDepth, lockTimeout, " + "description, locationPreference, status " + "FROM ldata_table WHERE (parentRef = ?) " + "AND (? OR (isSupervised = ?)) " + "AND (? OR (createDate BETWEEN FROM_UNIXTIME(?) AND FROM_UNIXTIME(?))) " + "AND (? OR (createDate >= FROM_UNIXTIME(?))) " + "AND (? OR (createDate <= FROM_UNIXTIME(?))) " + "AND (? OR (modifiedDate BETWEEN FROM_UNIXTIME(?) AND FROM_UNIXTIME(?))) " + "AND (? OR (modifiedDate >= FROM_UNIXTIME(?))) " + "AND (? OR (modifiedDate <= FROM_UNIXTIME(?))) " + "AND (? OR (ldName LIKE CONCAT('%', ? , '%')))" + "LIMIT ?");
            PreparedStatement ps2 = cn.prepareStatement("SELECT uid, ownerId, " + "ldName FROM ldata_table WHERE parentRef = ? AND datatype = '" + Constants.LOGICAL_FOLDER + "'")) {
            {
                if (queryParameters.containsKey("name") && queryParameters.get("name").iterator().hasNext()) {
                    String name = queryParameters.get("name").iterator().next();
                    ps1.setBoolean(18, false);
                    ps1.setString(19, name);
                    addFlag &= ld.getName().contains(name);
                } else {
                    ps1.setBoolean(18, true);
                    ps1.setString(19, "");
                }
                if (queryParameters.containsKey("cStartDate") && queryParameters.get("cStartDate").iterator().hasNext() && queryParameters.containsKey("cEndDate") && queryParameters.get("cEndDate").iterator().hasNext()) {
                    long cStartDate = Long.valueOf(queryParameters.get("cStartDate").iterator().next());
                    long cEndDate = Long.valueOf(queryParameters.get("cEndDate").iterator().next());
                    ps1.setBoolean(4, false);
                    ps1.setBoolean(7, true);
                    ps1.setBoolean(9, true);
                    ps1.setLong(5, cStartDate);
                    ps1.setLong(6, cEndDate);
                    ps1.setLong(8, 0);
                    ps1.setLong(10, 0);
                    addFlag &= (ld.getCreateDate() >= cStartDate * 1000) && (ld.getCreateDate() <= cEndDate * 1000);
                } else if (queryParameters.containsKey("cStartDate") && queryParameters.get("cStartDate").iterator().hasNext()) {
                    long cStartDate = Long.valueOf(queryParameters.get("cStartDate").iterator().next());
                    ps1.setBoolean(4, true);
                    ps1.setBoolean(7, false);
                    ps1.setBoolean(9, true);
                    ps1.setLong(5, 0);
                    ps1.setLong(6, 0);
                    ps1.setLong(8, cStartDate);
                    ps1.setLong(10, 0);
                    addFlag &= (ld.getCreateDate() >= cStartDate * 1000);
                } else if (queryParameters.containsKey("cEndDate") && queryParameters.get("cEndDate").iterator().hasNext()) {
                    long cEndDate = Long.valueOf(queryParameters.get("cEndDate").iterator().next());
                    ps1.setBoolean(4, true);
                    ps1.setBoolean(7, true);
                    ps1.setBoolean(9, false);
                    ps1.setLong(5, 0);
                    ps1.setLong(6, 0);
                    ps1.setLong(8, 0);
                    ps1.setLong(10, cEndDate);
                    addFlag &= (ld.getCreateDate() <= cEndDate * 1000);
                } else {
                    ps1.setBoolean(4, true);
                    ps1.setBoolean(7, true);
                    ps1.setBoolean(9, true);
                    ps1.setLong(5, 0);
                    ps1.setLong(6, 0);
                    ps1.setLong(8, 0);
                    ps1.setLong(10, 0);
                }
                if (queryParameters.containsKey("mStartDate") && queryParameters.get("mStartDate").iterator().hasNext() && queryParameters.containsKey("mEndDate") && queryParameters.get("mEndDate").iterator().hasNext()) {
                    long mStartDate = Long.valueOf(queryParameters.get("mStartDate").iterator().next());
                    long mEndDate = Long.valueOf(queryParameters.get("mEndDate").iterator().next());
                    ps1.setBoolean(11, false);
                    ps1.setBoolean(14, true);
                    ps1.setBoolean(16, true);
                    ps1.setLong(12, mStartDate);
                    ps1.setLong(13, mEndDate);
                    ps1.setLong(15, 0);
                    ps1.setLong(17, 0);
                    addFlag &= (ld.getModifiedDate() >= mStartDate * 1000) && (ld.getModifiedDate() <= mEndDate * 1000);
                } else if (queryParameters.containsKey("mStartDate") && queryParameters.get("mStartDate").iterator().hasNext()) {
                    long mStartDate = Long.valueOf(queryParameters.get("mStartDate").iterator().next());
                    ps1.setBoolean(11, true);
                    ps1.setBoolean(14, false);
                    ps1.setBoolean(16, true);
                    ps1.setLong(12, 0);
                    ps1.setLong(13, 0);
                    ps1.setLong(15, mStartDate);
                    ps1.setLong(17, 0);
                    addFlag &= (ld.getModifiedDate() >= mStartDate * 1000);
                } else if (queryParameters.containsKey("mEndDate") && queryParameters.get("mEndDate").iterator().hasNext()) {
                    long mEndDate = Long.valueOf(queryParameters.get("mEndDate").iterator().next());
                    ps1.setBoolean(11, true);
                    ps1.setBoolean(14, true);
                    ps1.setBoolean(16, false);
                    ps1.setLong(12, 0);
                    ps1.setLong(13, 0);
                    ps1.setLong(15, 0);
                    ps1.setLong(17, mEndDate);
                    addFlag &= (ld.getModifiedDate() <= mEndDate * 1000);
                } else {
                    ps1.setBoolean(11, true);
                    ps1.setBoolean(14, true);
                    ps1.setBoolean(16, true);
                    ps1.setLong(12, 0);
                    ps1.setLong(13, 0);
                    ps1.setLong(15, 0);
                    ps1.setLong(17, 0);
                }
                if (queryParameters.containsKey("isSupervised") && queryParameters.get("isSupervised").iterator().hasNext()) {
                    boolean isSupervised = Boolean.valueOf(queryParameters.get("isSupervised").iterator().next());
                    ps1.setBoolean(2, false);
                    ps1.setBoolean(3, isSupervised);
                    addFlag &= (ld.getSupervised() == isSupervised);
                } else {
                    ps1.setBoolean(2, true);
                    ps1.setBoolean(3, true);
                }
                if (addFlag) {
                    LogicalDataWrapped ldw = new LogicalDataWrapped();
                    ldw.setGlobalID(getCatalogue().getGlobalID(ld.getUid(), cn));
                    ldw.setLogicalData(ld);
                    ldw.setPath(rootPath);
                    ldw.setPermissions(p);
                    ldw.setUid(ld.getUid());
                    List<PDRIDescr> pdriDescr = getCatalogue().getPdriDescrByGroupId(ld.getPdriGroupId(), cn);
                    if (mp.isAdmin() && pdriDescr != null) {
                        for (PDRIDescr pdri : pdriDescr) {
                            if (pdri.getResourceUrl().startsWith("lfc") || pdri.getResourceUrl().startsWith("srm") || pdri.getResourceUrl().startsWith("gftp")) {
                                pdriDescr.remove(pdri);
                                GridHelper.initGridProxy(pdri.getUsername(), pdri.getPassword(), null, false);
                                pdri.setPassword(GridHelper.getProxyAsBase64String());
                                pdriDescr.add(pdri);
                            }
                        }
                    } else if (pdriDescr != null) {
                        for (PDRIDescr pdri : pdriDescr) {
                            pdriDescr.remove(pdri);
                            pdri.setPassword(null);
                            pdri.setUsername(null);
                            pdri.setKey(null);
                            pdri.setId(null);
                            pdri.setPdriGroupRef(null);
                            pdri.setStorageSiteId(null);
                            pdriDescr.add(pdri);
                        }
                    }
                    ldw.setPdriList(pdriDescr);
                    logicalDataWrappedList.add(ldw);
                    rowLimit--;
                }
                if (rowLimit != 0) {
                    logicalDataWrappedList.addAll(queryLogicalData(new MyData(ld.getUid(), rootPath.equals("/") ? "" : rootPath), rowLimit, ps1, ps2, mp, cn));
                }
            }
        }
    }
    return logicalDataWrappedList;
}
Also used : PDRIDescr(nl.uva.cs.lobcder.resources.PDRIDescr) ArrayList(java.util.ArrayList) LogicalDataWrapped(nl.uva.cs.lobcder.rest.wrappers.LogicalDataWrapped) PreparedStatement(java.sql.PreparedStatement) LogicalData(nl.uva.cs.lobcder.resources.LogicalData) Permissions(nl.uva.cs.lobcder.auth.Permissions) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List)

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