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");
}
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");
}
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());
}
}
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());
}
}
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;
}
Aggregations