Search in sources :

Example 11 with MyPrincipal

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

the class PrincipalCacheTest method testCache.

@Test
public void testCache() throws InterruptedException {
    System.out.println("testCache");
    String token = "token";
    PrincipalCache instance = new PrincipalCache();
    MyPrincipal principal = getPrincipal("user1");
    instance.putPrincipal(token, principal, new Date().getTime() + 1000);
    Thread.sleep(500);
    MyPrincipal result = instance.getPrincipal(token);
    assertNotNull(result);
}
Also used : MyPrincipal(nl.uva.cs.lobcder.auth.MyPrincipal) Date(java.util.Date) PrincipalCache(nl.uva.cs.lobcder.auth.PrincipalCache) Test(org.junit.Test)

Example 12 with MyPrincipal

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

the class PrincipalCacheTest method testCacheTimeout.

@Test
public void testCacheTimeout() throws InterruptedException {
    System.out.println("testCacheTimeout");
    String token = "token";
    PrincipalCache instance = new PrincipalCache();
    MyPrincipal principal = getPrincipal(token);
    instance.putPrincipal(token, principal, new Date().getTime() + 1000);
    Thread.sleep(1500);
    MyPrincipal result = instance.getPrincipal(token);
    assertNull(result);
}
Also used : MyPrincipal(nl.uva.cs.lobcder.auth.MyPrincipal) Date(java.util.Date) PrincipalCache(nl.uva.cs.lobcder.auth.PrincipalCache) Test(org.junit.Test)

Example 13 with MyPrincipal

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

the class PrincipalCacheTest method getPrincipal.

public MyPrincipal getPrincipal(String token) {
    String[] roles = { "role1", "role2", "role3", "role4" };
    MyPrincipal mp = new MyPrincipal("id", new HashSet<>(Arrays.asList(roles)), token);
    return mp;
}
Also used : MyPrincipal(nl.uva.cs.lobcder.auth.MyPrincipal)

Example 14 with MyPrincipal

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

the class PermissionsResource method addPermissionsRecursive.

@Path("recursive/{uid}/")
@POST
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public UIDS addPermissionsRecursive(@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());
                    while (rs.next()) {
                        String permType = rs.getString(1);
                        String roleName = rs.getString(2);
                        if (permType.equals("read")) {
                            read.remove(roleName);
                        } else if (permType.equals("write")) {
                            write.remove(roleName);
                        }
                    }
                    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 || !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 15 with MyPrincipal

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

Aggregations

MyPrincipal (nl.uva.cs.lobcder.auth.MyPrincipal)37 Connection (java.sql.Connection)23 SQLException (java.sql.SQLException)22 Permissions (nl.uva.cs.lobcder.auth.Permissions)20 LogicalData (nl.uva.cs.lobcder.resources.LogicalData)20 Path (javax.ws.rs.Path)11 ArrayList (java.util.ArrayList)8 GET (javax.ws.rs.GET)8 Produces (javax.ws.rs.Produces)7 WebApplicationException (javax.ws.rs.WebApplicationException)5 PreparedStatement (java.sql.PreparedStatement)4 Stack (java.util.Stack)4 ResultSet (java.sql.ResultSet)3 Date (java.util.Date)3 HashSet (java.util.HashSet)3 Consumes (javax.ws.rs.Consumes)3 PUT (javax.ws.rs.PUT)3 JAXBElement (javax.xml.bind.JAXBElement)3 QName (javax.xml.namespace.QName)3 PDRIDescr (nl.uva.cs.lobcder.resources.PDRIDescr)3