use of org.structr.core.entity.AbstractNode in project structr by structr.
the class CreateRelationshipFunction method apply.
@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
try {
if (!arrayHasLengthAndAllElementsNotNull(sources, 3)) {
return "";
}
final Object source = sources[0];
final Object target = sources[1];
final String relType = (String) sources[2];
AbstractNode sourceNode = null;
AbstractNode targetNode = null;
if (source instanceof AbstractNode && target instanceof AbstractNode) {
sourceNode = (AbstractNode) source;
targetNode = (AbstractNode) target;
} else {
logger.warn("Error: entities are not nodes. Parameters: {}", getParametersAsString(sources));
return "Error: entities are not nodes.";
}
final Class relClass = StructrApp.getConfiguration().getRelationClassForCombinedType(sourceNode.getType(), relType, targetNode.getType());
if (relClass != null) {
return StructrApp.getInstance(sourceNode.getSecurityContext()).create(sourceNode, targetNode, relClass);
} else {
logger.warn("Error: Unknown relationship type. Parameters: {}", getParametersAsString(sources));
return "Error: Unknown relationship type";
}
} catch (final IllegalArgumentException e) {
logParameterError(caller, sources, ctx.isJavaScriptContext());
return usage(ctx.isJavaScriptContext());
}
}
use of org.structr.core.entity.AbstractNode in project structr by structr.
the class GetOutgoingRelationshipsFunction method apply.
@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
final List<AbstractRelationship> list = new ArrayList<>();
if (arrayHasMinLengthAndMaxLengthAndAllElementsNotNull(sources, 2, 3)) {
final Object source = sources[0];
final Object target = sources[1];
AbstractNode sourceNode = null;
AbstractNode targetNode = null;
if (source instanceof AbstractNode && target instanceof AbstractNode) {
sourceNode = (AbstractNode) source;
targetNode = (AbstractNode) target;
} else {
logger.warn("Error: entities are not nodes. Parameters: {}", getParametersAsString(sources));
return "Error: entities are not nodes.";
}
if (sources.length == 2) {
for (final AbstractRelationship rel : sourceNode.getOutgoingRelationships()) {
final NodeInterface s = rel.getSourceNode();
final NodeInterface t = rel.getTargetNode();
// We need to check if current user can see source and target node which is often not the case for OWNS or SECURITY rels
if (s != null && t != null && s.equals(sourceNode) && t.equals(targetNode)) {
list.add(rel);
}
}
} else if (sources.length == 3) {
// dont try to create the relClass because we would need to do that both ways!!! otherwise it just fails if the nodes are in the "wrong" order (see tests:890f)
final String relType = (String) sources[2];
for (final AbstractRelationship rel : sourceNode.getOutgoingRelationships()) {
final NodeInterface s = rel.getSourceNode();
final NodeInterface t = rel.getTargetNode();
// We need to check if current user can see source and target node which is often not the case for OWNS or SECURITY rels
if (s != null && t != null && rel.getRelType().name().equals(relType) && s.equals(sourceNode) && t.equals(targetNode)) {
list.add(rel);
}
}
}
} else {
logParameterError(caller, sources, ctx.isJavaScriptContext());
}
return list;
}
use of org.structr.core.entity.AbstractNode in project structr by structr.
the class HasRelationshipFunction method apply.
@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
if (arrayHasMinLengthAndMaxLengthAndAllElementsNotNull(sources, 2, 3)) {
final Object source = sources[0];
final Object target = sources[1];
AbstractNode sourceNode = null;
AbstractNode targetNode = null;
if (source instanceof AbstractNode && target instanceof AbstractNode) {
sourceNode = (AbstractNode) source;
targetNode = (AbstractNode) target;
} else {
logger.warn("Error: entities are not nodes. Parameters: {}", getParametersAsString(sources));
return "Error: entities are not nodes.";
}
if (sources.length == 2) {
for (final AbstractRelationship rel : sourceNode.getRelationships()) {
final NodeInterface s = rel.getSourceNode();
final NodeInterface t = rel.getTargetNode();
// We need to check if current user can see source and target node which is often not the case for OWNS or SECURITY rels
if (s != null & t != null && ((s.equals(sourceNode) && t.equals(targetNode)) || (s.equals(targetNode) && t.equals(sourceNode)))) {
return true;
}
}
} else if (sources.length == 3) {
// dont try to create the relClass because we would need to do that both ways!!! otherwise it just fails if the nodes are in the "wrong" order (see tests:890f)
final String relType = (String) sources[2];
for (final AbstractRelationship rel : sourceNode.getRelationships()) {
final NodeInterface s = rel.getSourceNode();
final NodeInterface t = rel.getTargetNode();
// We need to check if current user can see source and target node which is often not the case for OWNS or SECURITY rels
if (s != null & t != null && rel.getRelType().name().equals(relType) && ((s.equals(sourceNode) && t.equals(targetNode)) || (s.equals(targetNode) && t.equals(sourceNode)))) {
return true;
}
}
}
} else {
logParameterError(caller, sources, ctx.isJavaScriptContext());
}
return false;
}
use of org.structr.core.entity.AbstractNode 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.AbstractNode in project structr by structr.
the class SearchAndSortingTest method test01SearchSingleNodeByName.
@Test
public void test01SearchSingleNodeByName() {
try {
PropertyMap props = new PropertyMap();
final PropertyKey key = AbstractNode.name;
final String name = "89w3hkl sdfghsdkljth";
props.put(key, name);
final AbstractNode node = createTestNode(TestOne.class, props);
Result result = null;
try (final Tx tx = app.tx()) {
result = app.nodeQuery(TestOne.class).andName(name).includeDeletedAndHidden().getResult();
assertTrue(result.size() == 1);
assertTrue(result.get(0).equals(node));
}
// Change name attribute and search again
final String name2 = "klppptzoehi gösoiu tzüw0e9hg";
try (final Tx tx = app.tx()) {
node.setProperty(key, name2);
tx.success();
}
try (final Tx tx = app.tx()) {
result = app.nodeQuery(TestOne.class).andName(name2).includeDeletedAndHidden().getResult();
assertTrue(result.size() == 1);
assertTrue(result.get(0).equals(node));
}
} catch (FrameworkException ex) {
logger.warn("", ex);
logger.error(ex.toString());
fail("Unexpected exception");
}
}
Aggregations