Search in sources :

Example 66 with Principal

use of org.structr.core.entity.Principal in project structr by structr.

the class AdvancedPagingTest method test01PagingWithDeletedNodes.

/* moved from AccessControlTest to improve performance */
@Test
public void test01PagingWithDeletedNodes() {
    final Class testUserType = createTestUserType();
    final PropertyKey<String> passwordKey = StructrApp.key(testUserType, "password");
    final PropertyKey<Boolean> isAdminKey = StructrApp.key(testUserType, "isAdmin");
    List<TestOne> testOnes = null;
    // Create two User and ten TestOne nodes
    try (final Tx tx = StructrApp.getInstance().tx()) {
        createEntityAsSuperUser("/resource_access", "{'signature': 'TestOne', 'flags': 4095}");
        List<Principal> users = createTestNodes(testUserType, 2);
        users.get(0).setProperty(Principal.name, "user1");
        users.get(0).setProperty(passwordKey, "user1");
        users.get(1).setProperty(Principal.name, "user2");
        users.get(1).setProperty(passwordKey, "user2");
        users.get(1).setProperty(isAdminKey, true);
        testOnes = createTestNodes(TestOne.class, 3);
        int i = 0;
        // First test user is owner
        for (TestOne t : testOnes) {
            i++;
            t.setProperty(TestOne.name, "t-one-" + i);
            t.setProperty(TestOne.owner, users.get(0));
            t.setProperty(TestOne.visibleToAuthenticatedUsers, true);
        }
        tx.success();
    } catch (FrameworkException ex) {
        logger.warn("", ex);
        fail(ex.getMessage());
    }
    // Check as user1 with pageSize=1
    RestAssured.given().contentType("application/json; charset=UTF-8").filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(200)).header("X-User", "user1").header("X-Password", "user1").expect().statusCode(200).body("result", hasSize(1)).body("result_count", equalTo(3)).when().get("/test_ones?pageSize=1&page=1");
    // Check as user2 with pageSize=1
    RestAssured.given().contentType("application/json; charset=UTF-8").filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(200)).header("X-User", "user2").header("X-Password", "user2").expect().statusCode(200).body("result", hasSize(1)).body("result_count", equalTo(3)).when().get("/test_ones?pageSize=1&page=1");
    try (final Tx tx = StructrApp.getInstance().tx()) {
        // "soft delete" first node
        testOnes.get(0).setProperty(TestOne.name, "deleted");
        testOnes.get(0).setProperty(TestOne.deleted, true);
        // testOnes.get(0).setProperty(TestOne.hidden, true);
        tx.success();
    } catch (FrameworkException ex) {
        logger.warn("", ex);
        fail(ex.getMessage());
    }
    // Check as user1 with pageSize=1
    RestAssured.given().contentType("application/json; charset=UTF-8").filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(200)).header("X-User", "user2").header("X-Password", "user2").expect().statusCode(200).body("result", hasSize(1)).body("result_count", equalTo(3)).when().get("/test_ones?sort=name&pageSize=1&page=1");
}
Also used : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) TestOne(org.structr.rest.entity.TestOne) Principal(org.structr.core.entity.Principal) Test(org.junit.Test) StructrRestTest(org.structr.rest.common.StructrRestTest)

Example 67 with Principal

use of org.structr.core.entity.Principal in project structr by structr.

the class AdvancedPagingTest method test02PagingWithSoftDeletedNodes.

/**
 * Paging with soft-deleted nodes
 */
@Test
public void test02PagingWithSoftDeletedNodes() {
    final Class testUserType = createTestUserType();
    final PropertyKey<String> passwordKey = StructrApp.key(testUserType, "password");
    final PropertyKey<Boolean> isAdminKey = StructrApp.key(testUserType, "isAdmin");
    List<TestOne> testOnes = null;
    // Create two User and ten TestOne nodes
    try (final Tx tx = StructrApp.getInstance().tx()) {
        createEntityAsSuperUser("/resource_access", "{'signature': 'TestOne', 'flags': 4095}");
        List<Principal> users = createTestNodes(testUserType, 2);
        users.get(0).setProperty(Principal.name, "user1");
        users.get(0).setProperty(passwordKey, "user1");
        users.get(1).setProperty(Principal.name, "user2");
        users.get(1).setProperty(passwordKey, "user2");
        users.get(1).setProperty(isAdminKey, true);
        testOnes = createTestNodes(TestOne.class, 3);
        int i = 0;
        // First test user is owner
        for (TestOne t : testOnes) {
            i++;
            t.setProperty(TestOne.name, "t-one-" + i);
            t.setProperty(TestOne.owner, users.get(0));
            t.setProperty(TestOne.visibleToAuthenticatedUsers, true);
        }
        // "soft delete" first node
        testOnes.get(0).setProperty(TestOne.name, "deleted");
        testOnes.get(0).setProperty(TestOne.deleted, true);
        // testOnes.get(0).setProperty(TestOne.hidden, true);
        tx.success();
    } catch (FrameworkException ex) {
        logger.warn("", ex);
        fail(ex.getMessage());
    }
    // Check as user1 with pageSize=1
    RestAssured.given().contentType("application/json; charset=UTF-8").filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(200)).header("X-User", "user2").header("X-Password", "user2").expect().statusCode(200).body("result", hasSize(1)).body("result_count", equalTo(3)).when().get("/test_ones?sort=name&pageSize=1&page=1");
    // Check as user1 with pageSize=1
    RestAssured.given().contentType("application/json; charset=UTF-8").filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(200)).header("X-User", "user2").header("X-Password", "user2").expect().statusCode(200).body("result", hasSize(1)).body("result_count", equalTo(2)).when().get("/test_ones?deleted=false&sort=name&pageSize=1&page=1");
}
Also used : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) TestOne(org.structr.rest.entity.TestOne) Principal(org.structr.core.entity.Principal) Test(org.junit.Test) StructrRestTest(org.structr.rest.common.StructrRestTest)

Example 68 with Principal

use of org.structr.core.entity.Principal in project structr by structr.

the class IsAllowedFunction method apply.

@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
    try {
        if (!arrayHasLengthAndAllElementsNotNull(sources, 3)) {
            return false;
        }
        if (sources[0] instanceof Principal) {
            final Principal principal = (Principal) sources[0];
            if (sources[1] instanceof AbstractNode) {
                final AbstractNode node = (AbstractNode) sources[1];
                if (sources[2] instanceof String) {
                    final String[] parts = ((String) sources[2]).split("[,]+");
                    boolean allowed = true;
                    for (final String part : parts) {
                        final String trimmedPart = part.trim();
                        if (trimmedPart.length() > 0) {
                            final Permission permission = Permissions.valueOf(trimmedPart);
                            if (permission != null) {
                                allowed &= node.isGranted(permission, SecurityContext.getInstance(principal, AccessMode.Backend));
                            } else {
                                logger.warn("Error: unknown permission \"{}\". Parameters: {}", new Object[] { trimmedPart, getParametersAsString(sources) });
                                return "Error: unknown permission " + trimmedPart;
                            }
                        }
                    }
                    return allowed;
                } else {
                    logger.warn("Error: third argument is not a string. Parameters: {}", getParametersAsString(sources));
                    return "Error: third argument is not a string.";
                }
            } else {
                logger.warn("Error: second argument is not a node. Parameters: {}", getParametersAsString(sources));
                return "Error: second argument is not a node.";
            }
        } else {
            logger.warn("Error: first argument is not of type Principal. Parameters: {}", getParametersAsString(sources));
            return "Error: first argument is not of type Principal.";
        }
    } catch (final IllegalArgumentException e) {
        logParameterError(caller, sources, ctx.isJavaScriptContext());
        return usage(ctx.isJavaScriptContext());
    }
}
Also used : AbstractNode(org.structr.core.entity.AbstractNode) Permission(org.structr.common.Permission) Principal(org.structr.core.entity.Principal)

Example 69 with Principal

use of org.structr.core.entity.Principal in project structr by structr.

the class IsInGroupFunction method apply.

@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
    try {
        if (!arrayHasLengthAndAllElementsNotNull(sources, 2)) {
            return "";
        }
        if (!(sources[0] instanceof Group)) {
            logger.warn("Error: first argument is not a Group. Parameters: {}", getParametersAsString(sources));
            return "Error: first argument is not a Group.";
        }
        if (!(sources[1] instanceof Principal)) {
            logger.warn("Error: second argument is not a Principal. Parameters: {}", getParametersAsString(sources));
            return "Error: second argument is not a Principal.";
        }
        final RelationshipType type = StructrApp.getInstance().getDatabaseService().forName(RelationshipType.class, "CONTAINS");
        final Group group = (Group) sources[0];
        final Principal user = (Principal) sources[1];
        return group.hasRelationshipTo(type, user);
    } catch (final IllegalArgumentException e) {
        logParameterError(caller, sources, ctx.isJavaScriptContext());
        return usage(ctx.isJavaScriptContext());
    }
}
Also used : Group(org.structr.core.entity.Group) RelationshipType(org.structr.api.graph.RelationshipType) Principal(org.structr.core.entity.Principal)

Example 70 with Principal

use of org.structr.core.entity.Principal in project structr by structr.

the class CopyPermissionsFunction method apply.

@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
    if (arrayHasLengthAndAllElementsNotNull(sources, 2)) {
        final Object source = sources[0];
        final Object target = sources[1];
        if (source instanceof NodeInterface && target instanceof NodeInterface) {
            final NodeInterface sourceNode = (NodeInterface) source;
            final NodeInterface targetNode = (NodeInterface) target;
            for (final Security security : sourceNode.getIncomingRelationships(Security.class)) {
                final Principal principal = security.getSourceNode();
                for (final String perm : security.getPermissions()) {
                    targetNode.grant(Permissions.valueOf(perm), principal);
                }
            }
        } else {
            logParameterError(caller, sources, ctx.isJavaScriptContext());
        }
    } else {
        return usage(ctx.isJavaScriptContext());
    }
    return null;
}
Also used : Security(org.structr.core.entity.Security) NodeInterface(org.structr.core.graph.NodeInterface) Principal(org.structr.core.entity.Principal)

Aggregations

Principal (org.structr.core.entity.Principal)112 FrameworkException (org.structr.common.error.FrameworkException)68 Tx (org.structr.core.graph.Tx)65 Test (org.junit.Test)41 App (org.structr.core.app.App)31 StructrApp (org.structr.core.app.StructrApp)31 TestOne (org.structr.core.entity.TestOne)16 Group (org.structr.core.entity.Group)14 NodeAttribute (org.structr.core.graph.NodeAttribute)13 PropertyMap (org.structr.core.property.PropertyMap)13 SecurityContext (org.structr.common.SecurityContext)10 LinkedList (java.util.LinkedList)9 Result (org.structr.core.Result)8 User (org.structr.web.entity.User)8 AbstractNode (org.structr.core.entity.AbstractNode)7 SuperUser (org.structr.core.entity.SuperUser)7 StructrUiTest (org.structr.web.StructrUiTest)7 Page (org.structr.web.entity.dom.Page)7 IOException (java.io.IOException)6 List (java.util.List)6