use of org.structr.web.entity.User in project structr by structr.
the class UiScriptingTest method testGroupFunctions.
@Test
public void testGroupFunctions() {
Group group = null;
User tester = null;
try (final Tx tx = app.tx()) {
// create test user
tester = createTestNode(User.class, new NodeAttribute<>(StructrApp.key(User.class, "name"), "tester"), new NodeAttribute<>(StructrApp.key(User.class, "password"), "test"));
// create test group
group = createTestNode(Group.class, new NodeAttribute<>(StructrApp.key(Group.class, "name"), "test"));
tx.success();
} catch (FrameworkException fex) {
fex.printStackTrace();
fail("Unexpected exception.");
}
final RenderContext renderContext = new RenderContext(securityContext, new RequestMockUp(), new ResponseMockUp(), RenderContext.EditMode.NONE);
try (final Tx tx = app.tx()) {
// check that the user is not in the group at first
assertFalse("User should not be in the test group before testing", group.getMembers().contains(tester));
// check that is_in_group returns the correct result
assertEquals("Function is_in_group should return false.", false, Scripting.evaluate(renderContext, null, "${is_in_group(first(find('Group')), first(find('User')))}", "test"));
// add user to group
Scripting.evaluate(renderContext, null, "${add_to_group(first(find('Group')), first(find('User')))}", "test");
// check that the user is in the group after the call to add_to_group
final List<Principal> members = group.getMembers();
assertTrue("User should be in the test group now", members.contains(tester));
// check that is_in_group returns the correct result
assertEquals("Function is_in_group should return true.", true, Scripting.evaluate(renderContext, null, "${is_in_group(first(find('Group')), first(find('User')))}", "test"));
// remove user from group
Scripting.evaluate(renderContext, null, "${remove_from_group(first(find('Group')), first(find('User')))}", "test");
// check that the user is not in the group any more after the call to remove_from_group
assertFalse("User should not be in the test group before testing", group.getMembers().contains(tester));
// check that is_in_group returns the correct result
assertEquals("Function is_in_group should return false.", false, Scripting.evaluate(renderContext, null, "${is_in_group(first(find('Group')), first(find('User')))}", "test"));
tx.success();
} catch (FrameworkException fex) {
fex.printStackTrace();
fail("Unexpected exception.");
}
}
use of org.structr.web.entity.User in project structr by structr.
the class RenderDataTest method testRenderListFromRestQuery.
@Test
public void testRenderListFromRestQuery() {
String name = null;
try (final Tx tx = app.tx()) {
final Page doc = (Page) getDocument();
name = doc.getName();
final List<User> users = createTestNodes(User.class, 3);
assertEquals(3, users.size());
User user1 = users.get(0);
user1.setProperty(AbstractNode.name, "user1");
User user2 = users.get(1);
user2.setProperty(AbstractNode.name, "user2");
User user3 = users.get(2);
user3.setProperty(AbstractNode.name, "user3");
final List<File> files = createTestNodes(File.class, 6);
assertEquals(6, files.size());
File nodeA = files.get(0);
nodeA.setProperty(AbstractNode.name, "fileA");
File nodeB = files.get(1);
nodeB.setProperty(AbstractNode.name, "fileB");
File nodeC = files.get(2);
nodeC.setProperty(AbstractNode.name, "fileC");
File nodeD = files.get(3);
nodeD.setProperty(AbstractNode.name, "fileD");
File nodeE = files.get(4);
nodeE.setProperty(AbstractNode.name, "fileE");
File nodeF = files.get(5);
nodeF.setProperty(AbstractNode.name, "fileF");
// create dom tree
Element html = doc.createElement("html");
Element body = doc.createElement("body");
Element b = doc.createElement("b");
final Element p1 = doc.createElement("p");
final PropertyMap p1Properties = new PropertyMap();
p1Properties.put(StructrApp.key(DOMElement.class, "restQuery"), "users?sort=name");
p1Properties.put(StructrApp.key(DOMElement.class, "dataKey"), "user");
((DOMElement) p1).setProperties(((DOMElement) p1).getSecurityContext(), p1Properties);
Content userNameContentNode = (Content) doc.createTextNode("${user.name}");
p1.appendChild(userNameContentNode);
Element div = doc.createElement("div");
final Element p2 = doc.createElement("p");
final PropertyMap p2Properties = new PropertyMap();
p2Properties.put(StructrApp.key(DOMElement.class, "restQuery"), "files?sort=name");
p2Properties.put(StructrApp.key(DOMElement.class, "dataKey"), "file");
((DOMElement) p2).setProperties(((DOMElement) p2).getSecurityContext(), p2Properties);
Content fileNameContentNode = (Content) doc.createTextNode("${file.name}");
p2.appendChild(fileNameContentNode);
doc.appendChild(html);
html.appendChild(body);
body.appendChild(b);
body.appendChild(div);
b.appendChild(p1);
div.appendChild(p2);
makePublic(doc, html, body, div, b, p1, p2, fileNameContentNode, userNameContentNode, nodeA, nodeB, nodeC, nodeD, nodeE, nodeF, user1, user2, user3);
tx.success();
} catch (Exception ex) {
ex.printStackTrace();
fail("Unexpected exception");
}
try (final Tx tx = app.tx()) {
Document parsedDocument = Jsoup.connect(baseUri + name).timeout(100000).get();
System.out.println(parsedDocument.outerHtml());
assertEquals("user1", parsedDocument.select("html > body > b > p").get(0).ownText());
assertEquals("user2", parsedDocument.select("html > body > b > p").get(1).ownText());
assertEquals("user3", parsedDocument.select("html > body > b > p").get(2).ownText());
assertEquals("fileA", parsedDocument.select("html > body > div > p").get(0).ownText());
assertEquals("fileB", parsedDocument.select("html > body > div > p").get(1).ownText());
assertEquals("fileC", parsedDocument.select("html > body > div > p").get(2).ownText());
assertEquals("fileD", parsedDocument.select("html > body > div > p").get(3).ownText());
assertEquals("fileE", parsedDocument.select("html > body > div > p").get(4).ownText());
assertEquals("fileF", parsedDocument.select("html > body > div > p").get(5).ownText());
tx.success();
} catch (Exception ex) {
ex.printStackTrace();
fail("unexpected exception");
}
}
use of org.structr.web.entity.User in project structr by structr.
the class SSHTest method createFTPUser.
protected User createFTPUser(final String username, final String password) throws FrameworkException {
PropertyMap props = new PropertyMap();
props.put(StructrApp.key(Principal.class, "name"), username);
props.put(StructrApp.key(Principal.class, "password"), password);
return (User) createTestNodes(User.class, 1, props).get(0);
}
use of org.structr.web.entity.User in project structr by structr.
the class StructrShellCommand method start.
@Override
public void start(final Environment env) throws IOException {
env.addSignalListener(this);
final String userName = env.getEnv().get("USER");
if (userName != null) {
final App app = StructrApp.getInstance();
try (final Tx tx = app.tx()) {
user = app.nodeQuery(User.class).andName(userName).getFirst();
if (user != null) {
// set home directory first
if (Settings.FilesystemEnabled.getValue()) {
currentFolder = user.getHomeDirectory();
}
}
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
}
} else {
logger.warn("Cannot start Structr shell, no username set!");
return;
}
if (isInteractive()) {
// abort if no user was found
if (user == null) {
logger.warn("Cannot start Structr shell, user not found for name {}!", userName);
return;
}
final String terminalType = env.getEnv().get("TERM");
if (terminalType != null) {
switch(terminalType) {
case "xterm":
case "vt100":
case "vt220":
term = new XTermTerminalEmulator(in, out, this);
break;
default:
logger.warn("Unsupported terminal type {}, aborting.", terminalType);
break;
}
logger.warn("No terminal type provided, aborting.", terminalType);
}
if (term != null) {
term.start();
term.print("Welcome to ");
term.setBold(true);
term.print("Structr");
term.print(" 2.0");
term.setBold(false);
term.println();
// display first prompt
displayPrompt();
} else {
callback.onExit(1);
}
} else {
// create terminal emulation
term = new XTermTerminalEmulator(in, out, this);
}
}
use of org.structr.web.entity.User in project structr by structr.
the class WebsocketController method getMessageForEvent.
// ----- private methods -----
private WebSocketMessage getMessageForEvent(final SecurityContext securityContext, final ModificationEvent modificationEvent) throws FrameworkException {
final String callbackId = modificationEvent.getCallbackId();
if (modificationEvent.isNode()) {
final NodeInterface node = (NodeInterface) modificationEvent.getGraphObject();
if (modificationEvent.isDeleted()) {
final WebSocketMessage message = createMessage("DELETE", callbackId);
message.setId(modificationEvent.getRemovedProperties().get(GraphObject.id));
message.setCode(200);
return message;
}
if (modificationEvent.isCreated()) {
final WebSocketMessage message = createMessage("CREATE", callbackId);
message.setGraphObject(node);
message.setResult(Arrays.asList(new GraphObject[] { node }));
message.setCode(201);
return message;
}
if (modificationEvent.isModified()) {
final WebSocketMessage message = createMessage("UPDATE", callbackId);
// at login the securityContext is still null
if (securityContext != null) {
// only include changed properties (+ id and type)
LinkedHashSet<String> propertySet = new LinkedHashSet();
propertySet.add("id");
propertySet.add("type");
for (Iterator<PropertyKey> it = modificationEvent.getModifiedProperties().keySet().iterator(); it.hasNext(); ) {
final String jsonName = ((PropertyKey) it.next()).jsonName();
if (!propertySet.contains(jsonName)) {
propertySet.add(jsonName);
}
}
for (Iterator<PropertyKey> it = modificationEvent.getRemovedProperties().keySet().iterator(); it.hasNext(); ) {
final String jsonName = ((PropertyKey) it.next()).jsonName();
if (!propertySet.contains(jsonName)) {
propertySet.add(jsonName);
}
}
if (propertySet.size() > 2) {
securityContext.setCustomView(propertySet);
}
}
message.setGraphObject(node);
message.setResult(Arrays.asList(new GraphObject[] { node }));
message.setId(node.getUuid());
message.getModifiedProperties().addAll(modificationEvent.getModifiedProperties().keySet());
message.getRemovedProperties().addAll(modificationEvent.getRemovedProperties().keySet());
message.setNodeData(modificationEvent.getData(securityContext));
message.setCode(200);
if (securityContext != null) {
// Clear custom view here. This is necessary because the security context is reused for all websocket frames.
securityContext.clearCustomView();
}
return message;
}
} else {
// handle relationship
final RelationshipInterface relationship = (RelationshipInterface) modificationEvent.getGraphObject();
final RelationshipType relType = modificationEvent.getRelationshipType();
// special treatment of CONTAINS relationships
if ("CONTAINS".equals(relType.name())) {
if (modificationEvent.isDeleted()) {
final WebSocketMessage message = createMessage("REMOVE_CHILD", callbackId);
message.setNodeData("parentId", relationship.getSourceNodeId());
message.setId(relationship.getTargetNodeId());
message.setCode(200);
return message;
}
if (modificationEvent.isCreated()) {
final WebSocketMessage message = new WebSocketMessage();
final NodeInterface startNode = relationship.getSourceNode();
final NodeInterface endNode = relationship.getTargetNode();
// don't send a notification
if (startNode == null || endNode == null) {
return null;
}
message.setResult(Arrays.asList(new GraphObject[] { endNode }));
message.setId(endNode.getUuid());
message.setNodeData("parentId", startNode.getUuid());
message.setCode(200);
message.setCommand("APPEND_CHILD");
if (endNode instanceof DOMNode) {
org.w3c.dom.Node refNode = ((DOMNode) endNode).getNextSibling();
if (refNode != null) {
message.setCommand("INSERT_BEFORE");
message.setNodeData("refId", ((AbstractNode) refNode).getUuid());
}
} else if (endNode instanceof User) {
message.setCommand("APPEND_USER");
message.setNodeData("refId", startNode.getUuid());
} else if (endNode instanceof AbstractFile) {
message.setCommand("APPEND_FILE");
message.setNodeData("refId", startNode.getUuid());
}
return message;
}
}
if (modificationEvent.isDeleted()) {
final WebSocketMessage message = createMessage("DELETE", callbackId);
message.setId(modificationEvent.getRemovedProperties().get(GraphObject.id));
message.setCode(200);
return message;
}
if (modificationEvent.isModified()) {
final WebSocketMessage message = createMessage("UPDATE", callbackId);
message.getModifiedProperties().addAll(modificationEvent.getModifiedProperties().keySet());
message.getRemovedProperties().addAll(modificationEvent.getRemovedProperties().keySet());
message.setNodeData(modificationEvent.getData(securityContext));
message.setGraphObject(relationship);
message.setId(relationship.getUuid());
message.setCode(200);
final PropertyMap relProperties = relationship.getProperties();
// final NodeInterface startNode = relationship.getSourceNode();
// final NodeInterface endNode = relationship.getTargetNode();
// relProperties.put(new StringProperty("startNodeId"), startNode.getUuid());
// relProperties.put(new StringProperty("endNodeId"), endNode.getUuid());
final Map<String, Object> properties = PropertyMap.javaTypeToInputType(securityContext, relationship.getClass(), relProperties);
message.setRelData(properties);
return message;
}
}
return null;
}
Aggregations