use of org.structr.common.SecurityContext in project structr by structr.
the class DeploymentTest method test21ExportGrants.
@Test
public void test21ExportGrants() {
Principal user1 = null;
Principal user2 = null;
try (final Tx tx = app.tx()) {
user1 = createTestNode(User.class, new NodeAttribute<>(AbstractNode.name, "user1"));
user2 = createTestNode(User.class, new NodeAttribute<>(AbstractNode.name, "user2"));
tx.success();
} catch (FrameworkException ex) {
fail("Unexpected exception.");
}
Assert.assertNotNull("User was not created, test cannot continue", user1);
Assert.assertNotNull("User was not created, test cannot continue", user2);
// setup
final SecurityContext context1 = SecurityContext.getInstance(user1, AccessMode.Backend);
final App app1 = StructrApp.getInstance(context1);
try (final Tx tx = app1.tx()) {
final Page page = Page.createNewPage(context1, "test21");
final Html html = createElement(page, page, "html");
final Head head = createElement(page, html, "head");
createElement(page, head, "title", "test21");
final Body body = createElement(page, html, "body");
final Div div1 = createElement(page, body, "div");
final Content content = createContent(page, div1, "<b>Test</b>");
content.setProperty(StructrApp.key(Content.class, "contentType"), "text/html");
// create grants
page.grant(Permission.read, user2);
div1.grant(Permission.read, user2);
content.grant(Permission.read, user2);
tx.success();
} catch (FrameworkException fex) {
fail("Unexpected exception.");
}
// test
compare(calculateHash(), true, false);
}
use of org.structr.common.SecurityContext in project structr by structr.
the class DeploymentTest method test20ExportOwnership.
@Test
public void test20ExportOwnership() {
Principal user1 = null;
Principal user2 = null;
try (final Tx tx = app.tx()) {
user1 = createTestNode(User.class, new NodeAttribute<>(AbstractNode.name, "user1"));
user2 = createTestNode(User.class, new NodeAttribute<>(AbstractNode.name, "user2"));
tx.success();
} catch (FrameworkException ex) {
fail("Unexpected exception.");
}
Assert.assertNotNull("User was not created, test cannot continue", user1);
Assert.assertNotNull("User was not created, test cannot continue", user2);
// setup
final SecurityContext context1 = SecurityContext.getInstance(user1, AccessMode.Backend);
final App app1 = StructrApp.getInstance(context1);
try (final Tx tx = app1.tx()) {
final Page page = Page.createNewPage(context1, "test20");
final Html html = createElement(page, page, "html");
final Head head = createElement(page, html, "head");
createElement(page, head, "title", "test20");
final Body body = createElement(page, html, "body");
final Div div1 = createElement(page, body, "div");
final Content content = createContent(page, div1, "<b>Test</b>");
content.setProperty(StructrApp.key(Content.class, "contentType"), "text/html");
// set owner to different user
div1.setProperty(AbstractNode.owner, user2);
content.setProperty(AbstractNode.owner, user2);
tx.success();
} catch (FrameworkException fex) {
fail("Unexpected exception.");
}
// test
compare(calculateHash(), true, false);
}
use of org.structr.common.SecurityContext in project structr by structr.
the class Factory method page.
protected Result page(final QueryResult<S> input, final int offset, final int pageSize) throws FrameworkException {
final SecurityContext securityContext = factoryProfile.getSecurityContext();
final boolean dontCheckCount = securityContext.ignoreResultCount();
final List<T> nodes = new ArrayList<>();
int overallCount = 0;
int position = 0;
int count = 0;
try (final QueryResult<S> tmp = input) {
for (final S item : tmp) {
final T n = instantiate(item);
if (n != null) {
overallCount++;
position++;
if (disablePaging || (position > offset && position <= offset + pageSize)) {
nodes.add(n);
// stop if we got enough nodes
if (++count == pageSize && dontCheckCount && !disablePaging) {
break;
}
}
}
}
} catch (NetworkException nex) {
throw new FrameworkException(503, nex.getMessage());
}
// The overall count may be inaccurate
return new Result(nodes, overallCount, true, false);
}
use of org.structr.common.SecurityContext in project structr by structr.
the class BulkChangeNodePropertyKeyCommand method execute.
// ~--- methods --------------------------------------------------------
@Override
public void execute(final Map<String, Object> properties) throws FrameworkException {
final DatabaseService graphDb = (DatabaseService) arguments.get("graphDb");
final SecurityContext superUserContext = SecurityContext.getSuperUserInstance();
final NodeFactory nodeFactory = new NodeFactory(superUserContext);
String type = null;
final String oldKey = (String) properties.get("oldKey");
final String newKey = (String) properties.get("newKey");
if (graphDb != null && StringUtils.isNotBlank(oldKey) && StringUtils.isNotBlank(newKey)) {
Iterator<AbstractNode> nodeIterator = null;
if (properties.containsKey(AbstractNode.type.dbName())) {
type = (String) properties.get(AbstractNode.type.dbName());
nodeIterator = Iterables.map(nodeFactory, graphDb.getNodesByLabel(type)).iterator();
properties.remove(AbstractNode.type.dbName());
} else {
nodeIterator = Iterables.map(nodeFactory, graphDb.getAllNodes()).iterator();
}
final long count = bulkGraphOperation(securityContext, nodeIterator, 1000, "ChangeNodePropertyKey", new BulkGraphOperation<AbstractNode>() {
@Override
public void handleGraphObject(SecurityContext securityContext, AbstractNode node) {
for (Entry entry : properties.entrySet()) {
String key = (String) entry.getKey();
PropertyKey propertyKey = StructrApp.getConfiguration().getPropertyKeyForDatabaseName(node.getClass(), key);
if (propertyKey != null) {
Node dbNode = node.getNode();
if (dbNode.hasProperty(newKey)) {
logger.error("Node {} has already a property with key {}", new Object[] { node, newKey });
throw new IllegalStateException("Node has already a property of the new key");
}
if (dbNode.hasProperty(oldKey)) {
dbNode.setProperty(newKey, dbNode.getProperty(oldKey));
dbNode.removeProperty(oldKey);
}
node.updateInIndex();
}
}
}
@Override
public void handleThrowable(SecurityContext securityContext, Throwable t, AbstractNode node) {
logger.warn("Unable to set properties of node {}: {}", new Object[] { node.getUuid(), t.getMessage() });
}
@Override
public void handleTransactionFailure(SecurityContext securityContext, Throwable t) {
logger.warn("Unable to set node properties: {}", t.getMessage());
}
});
logger.info("Fixed {} nodes ...", count);
} else {
logger.info("No values for oldKey and/or newKey found, aborting.");
}
logger.info("Done");
}
use of org.structr.common.SecurityContext in project structr by structr.
the class BulkCopyRelationshipPropertyCommand method execute.
@Override
public void execute(final Map<String, Object> map) throws FrameworkException {
final DatabaseService graphDb = (DatabaseService) arguments.get("graphDb");
final RelationshipFactory relFactory = new RelationshipFactory(securityContext);
final String sourceKey = (String) map.get("sourceKey");
final String destKey = (String) map.get("destKey");
if (sourceKey == null || destKey == null) {
throw new IllegalArgumentException("This command requires one argument of type Map. Map must contain values for 'sourceKey' and 'destKey'.");
}
if (graphDb != null) {
final Iterator<AbstractRelationship> relIterator = Iterables.map(relFactory, graphDb.getAllRelationships()).iterator();
final long count = bulkGraphOperation(securityContext, relIterator, 1000, "CopyRelationshipProperties", new BulkGraphOperation<AbstractRelationship>() {
@Override
public void handleGraphObject(SecurityContext securityContext, AbstractRelationship rel) {
// Treat only "our" rels
if (rel.getProperty(GraphObject.id) != null) {
Class type = rel.getClass();
PropertyKey destPropertyKey = StructrApp.getConfiguration().getPropertyKeyForDatabaseName(type, destKey);
PropertyKey sourcePropertyKey = StructrApp.getConfiguration().getPropertyKeyForDatabaseName(type, sourceKey);
try {
// copy properties
rel.setProperty(destPropertyKey, rel.getProperty(sourcePropertyKey));
} catch (FrameworkException fex) {
logger.warn("Unable to copy relationship property {} of relationship {} to {}: {}", new Object[] { sourcePropertyKey, rel.getUuid(), destPropertyKey, fex.getMessage() });
}
}
}
@Override
public void handleThrowable(SecurityContext securityContext, Throwable t, AbstractRelationship rel) {
logger.warn("Unable to copy relationship properties of relationship {}: {}", new Object[] { rel.getUuid(), t.getMessage() });
}
@Override
public void handleTransactionFailure(SecurityContext securityContext, Throwable t) {
logger.warn("Unable to copy relationship properties: {}", t.getMessage());
}
});
logger.info("Finished setting properties on {} nodes", count);
}
}
Aggregations