use of org.structr.core.property.PropertyMap in project structr by structr.
the class StructrUiTest method createTestNodes.
protected <T extends NodeInterface> List<T> createTestNodes(final Class<T> type, final int number) throws FrameworkException {
final PropertyMap props = new PropertyMap();
props.put(AbstractNode.type, type.getSimpleName());
List<T> nodes = new LinkedList<>();
for (int i = 0; i < number; i++) {
props.put(AbstractNode.name, type.getSimpleName() + i);
nodes.add(app.create(type, props));
}
return nodes;
}
use of org.structr.core.property.PropertyMap in project structr by structr.
the class LoginResource method doPost.
@Override
public RestMethodResult doPost(Map<String, Object> propertySet) throws FrameworkException {
final ConfigurationProvider config = StructrApp.getConfiguration();
final PropertyMap properties = PropertyMap.inputTypeToJavaType(securityContext, User.class, propertySet);
final PropertyKey<String> nameKey = StructrApp.key(User.class, "name");
final PropertyKey<String> eMailKey = StructrApp.key(User.class, "eMail");
final PropertyKey<String> pwdKey = StructrApp.key(User.class, "password");
final String name = properties.get(nameKey);
final String email = properties.get(eMailKey);
final String password = properties.get(pwdKey);
String emailOrUsername = StringUtils.isNotEmpty(email) ? email : name;
if (StringUtils.isNotEmpty(emailOrUsername) && StringUtils.isNotEmpty(password)) {
Principal user = securityContext.getAuthenticator().doLogin(securityContext.getRequest(), emailOrUsername, password);
if (user != null) {
logger.info("Login successful: {}", new Object[] { user });
// make logged in user available to caller
securityContext.setCachedUser(user);
RestMethodResult methodResult = new RestMethodResult(200);
methodResult.addContent(user);
return methodResult;
}
}
logger.info("Invalid credentials (name, email, password): {}, {}, {}", new Object[] { name, email, password });
return new RestMethodResult(401);
}
use of org.structr.core.property.PropertyMap in project structr by structr.
the class ResetPasswordResource method doPost.
@Override
public RestMethodResult doPost(Map<String, Object> propertySet) throws FrameworkException {
if (propertySet.containsKey("eMail")) {
final String emailString = (String) propertySet.get("eMail");
if (StringUtils.isEmpty(emailString)) {
return new RestMethodResult(HttpServletResponse.SC_BAD_REQUEST);
}
final ConfigurationProvider config = StructrApp.getConfiguration();
final PropertyKey<String> confirmationKey = StructrApp.key(User.class, "confirmationKey");
final PropertyKey<String> eMail = StructrApp.key(User.class, "eMail");
final String localeString = (String) propertySet.get("locale");
final String confKey = UUID.randomUUID().toString();
final Principal user = StructrApp.getInstance().nodeQuery(User.class).and(eMail, emailString).getFirst();
if (user != null) {
// update confirmation key
user.setProperties(SecurityContext.getSuperUserInstance(), new PropertyMap(confirmationKey, confKey));
if (!sendResetPasswordLink(user, propertySet, localeString, confKey)) {
// return 400 Bad request
return new RestMethodResult(HttpServletResponse.SC_BAD_REQUEST);
}
// return 200 OK
return new RestMethodResult(HttpServletResponse.SC_OK);
} else {
// so we're failing silently here
return new RestMethodResult(HttpServletResponse.SC_OK);
}
} else {
// return 400 Bad request
return new RestMethodResult(HttpServletResponse.SC_BAD_REQUEST);
}
}
use of org.structr.core.property.PropertyMap 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;
}
use of org.structr.core.property.PropertyMap in project structr by structr.
the class CreateAndAppendDOMNodeCommand method processMessage.
@Override
public void processMessage(final WebSocketMessage webSocketData) {
final Map<String, Object> nodeData = webSocketData.getNodeData();
final String parentId = (String) nodeData.get("parentId");
final String childContent = (String) nodeData.get("childContent");
final String pageId = webSocketData.getPageId();
Boolean inheritVisibilityFlags = (Boolean) nodeData.get("inheritVisibilityFlags");
if (inheritVisibilityFlags == null) {
inheritVisibilityFlags = false;
}
// remove configuration elements from the nodeData so we don't set it on the node
nodeData.remove("parentId");
nodeData.remove("inheritVisibilityFlags");
if (pageId != null) {
// check for parent ID before creating any nodes
if (parentId == null) {
getWebSocket().send(MessageBuilder.status().code(422).message("Cannot add node without parentId").build(), true);
return;
}
// check if parent node with given ID exists
final DOMNode parentNode = getDOMNode(parentId);
if (parentNode == null) {
getWebSocket().send(MessageBuilder.status().code(404).message("Parent node not found").build(), true);
return;
}
final Document document = getPage(pageId);
if (document != null) {
final String tagName = (String) nodeData.get("tagName");
nodeData.remove("tagName");
try {
DOMNode newNode;
if (tagName != null && "comment".equals(tagName)) {
newNode = (DOMNode) document.createComment("#comment");
} else if (tagName != null && "template".equals(tagName)) {
newNode = (DOMNode) document.createTextNode("#template");
try {
newNode.unlockSystemPropertiesOnce();
newNode.setProperties(newNode.getSecurityContext(), new PropertyMap(NodeInterface.type, Template.class.getSimpleName()));
} catch (FrameworkException fex) {
logger.warn("Unable to set type of node {} to Template: {}", new Object[] { newNode.getUuid(), fex.getMessage() });
}
} else if (tagName != null && !tagName.isEmpty()) {
if ("custom".equals(tagName)) {
try {
// experimental: create DOM element with literal tag
newNode = (DOMElement) StructrApp.getInstance(webSocket.getSecurityContext()).create(DOMElement.class, new NodeAttribute(StructrApp.key(DOMElement.class, "tag"), "custom"), new NodeAttribute(StructrApp.key(DOMElement.class, "hideOnDetail"), false), new NodeAttribute(StructrApp.key(DOMElement.class, "hideOnIndex"), false));
if (newNode != null && document != null) {
newNode.doAdopt((Page) document);
}
} catch (FrameworkException fex) {
// abort
getWebSocket().send(MessageBuilder.status().code(422).message(fex.getMessage()).build(), true);
return;
}
} else {
newNode = (DOMNode) document.createElement(tagName);
}
} else {
newNode = (DOMNode) document.createTextNode("#text");
}
// Instantiate node again to get correct class
newNode = getDOMNode(newNode.getUuid());
// append new node to parent
if (newNode != null) {
parentNode.appendChild(newNode);
for (Entry entry : nodeData.entrySet()) {
final String key = (String) entry.getKey();
final Object val = entry.getValue();
PropertyKey propertyKey = StructrApp.getConfiguration().getPropertyKeyForDatabaseName(newNode.getClass(), key);
if (propertyKey != null) {
try {
Object convertedValue = val;
PropertyConverter inputConverter = propertyKey.inputConverter(SecurityContext.getSuperUserInstance());
if (inputConverter != null) {
convertedValue = inputConverter.convert(val);
}
newNode.setProperties(newNode.getSecurityContext(), new PropertyMap(propertyKey, convertedValue));
} catch (FrameworkException fex) {
logger.warn("Unable to set node property {} of node {} to {}: {}", new Object[] { propertyKey, newNode.getUuid(), val, fex.getMessage() });
}
}
}
PropertyMap visibilityFlags = null;
if (inheritVisibilityFlags) {
visibilityFlags = new PropertyMap();
visibilityFlags.put(DOMNode.visibleToAuthenticatedUsers, parentNode.getProperty(DOMNode.visibleToAuthenticatedUsers));
visibilityFlags.put(DOMNode.visibleToPublicUsers, parentNode.getProperty(DOMNode.visibleToPublicUsers));
try {
newNode.setProperties(newNode.getSecurityContext(), visibilityFlags);
} catch (FrameworkException fex) {
logger.warn("Unable to inherit visibility flags for node {} from parent node {}", newNode, parentNode);
}
}
// create a child text node if content is given
if (StringUtils.isNotBlank(childContent)) {
final DOMNode childNode = (DOMNode) document.createTextNode(childContent);
newNode.appendChild(childNode);
if (inheritVisibilityFlags) {
try {
childNode.setProperties(childNode.getSecurityContext(), visibilityFlags);
} catch (FrameworkException fex) {
logger.warn("Unable to inherit visibility flags for node {} from parent node {}", childNode, newNode);
}
}
}
}
} catch (DOMException dex) {
// send DOM exception
getWebSocket().send(MessageBuilder.status().code(422).message(dex.getMessage()).build(), true);
}
} else {
getWebSocket().send(MessageBuilder.status().code(404).message("Page not found").build(), true);
}
} else {
getWebSocket().send(MessageBuilder.status().code(422).message("Cannot create node without pageId").build(), true);
}
}
Aggregations