use of eu.esdihumboldt.hale.server.model.User in project hale by halestudio.
the class TemplateUploadForm method onUploadSuccess.
/**
* Called after a successful upload.
*
* @param form the form
* @param templateId the template identifier
* @param projectInfo the project info
* @param updateInfo if for an updated template, the template information
* should be updated from the project
*/
protected void onUploadSuccess(Form<?> form, String templateId, ProjectInfo projectInfo, boolean updateInfo) {
boolean newTemplate = TemplateUploadForm.this.templateId == null;
OrientGraph graph = DatabaseHelper.getGraph();
try {
Template template = Template.getByTemplateId(graph, templateId);
if (template == null) {
form.error("Template could not be created");
return;
}
if (newTemplate) {
// created template was a new template
// associate user as owner to template
String login = UserUtil.getLogin();
if (login != null) {
User user = User.getByLogin(graph, login);
graph.addEdge(null, template.getV(), user.getV(), "owner");
}
// forward to page to fill in template information
setResponsePage(new NewTemplatePage(templateId));
} else {
// created template already existed
// set last updated
template.setLastUpdate(new Date());
// update template info from project info
if (updateInfo) {
template.setName(projectInfo.getName());
template.setAuthor(projectInfo.getAuthor());
template.setDescription(projectInfo.getDescription());
}
// forward to template page
setResponsePage(TemplatePage.class, new PageParameters().set(0, templateId));
}
} catch (NonUniqueResultException e) {
form.error("Internal error");
log.error("Duplicate template or user");
} finally {
graph.shutdown();
}
}
use of eu.esdihumboldt.hale.server.model.User in project hale by halestudio.
the class EditTemplatePage method addControls.
@Override
protected void addControls() {
StringValue idParam = getPageParameters().get(0);
if (!idParam.isNull() && !idParam.isEmpty()) {
String templateId = idParam.toString();
OrientGraph graph = DatabaseHelper.getGraph();
try {
Template template = null;
try {
template = Template.getByTemplateId(graph, templateId);
} catch (NonUniqueResultException e) {
log.error("Duplicate template representation: " + templateId, e);
}
if (template != null) {
// get associated user
Vertex v = template.getV();
Iterator<Vertex> owners = v.getVertices(Direction.OUT, "owner").iterator();
if (// user is admin
UserUtil.isAdmin() || // or user is owner
(owners.hasNext() && UserUtil.getLogin().equals(new User(owners.next(), graph).getLogin()))) {
add(new TemplateForm("edit-form", false, templateId));
} else {
throw new AbortWithHttpErrorCodeException(HttpServletResponse.SC_FORBIDDEN);
}
} else {
throw new AbortWithHttpErrorCodeException(HttpServletResponse.SC_NOT_FOUND, "Template not found.");
}
} finally {
graph.shutdown();
}
} else
throw new AbortWithHttpErrorCodeException(HttpServletResponse.SC_BAD_REQUEST, "Template identifier must be specified.");
}
use of eu.esdihumboldt.hale.server.model.User in project hale by halestudio.
the class UserDetailsServiceImpl method loadUserDetails.
@Override
public UserDetails loadUserDetails(OpenIDAuthenticationToken token) throws UsernameNotFoundException {
OrientGraph graph = DatabaseHelper.getGraph();
try {
User user;
try {
user = User.getByLogin(graph, token.getIdentityUrl());
} catch (NonUniqueResultException e) {
// multiple users w/ same login?!
log.error("Found multiple user with same identity URL: " + token.getIdentityUrl(), e);
throw new IllegalStateException("Multiple users found for login");
}
boolean newUser = false;
if (user == null) {
// if using OpenID every user is automatically registered
// create a default user
user = User.create(graph);
user.setLogin(token.getIdentityUrl());
user.setPassword("");
newUser = true;
// try to retrieve info from attribute exchange
for (OpenIDAttribute attribute : token.getAttributes()) {
if (attribute.getCount() >= 0) {
switch(attribute.getName()) {
// fall through
case "email":
case "emailAlt":
user.setEmail(attribute.getValues().get(0));
break;
case "firstName":
user.setName(attribute.getValues().get(0));
break;
case "lastName":
user.setSurname(attribute.getValues().get(0));
break;
}
}
}
log.info("Create default user for Open ID " + token.getIdentityUrl());
}
return createUserDetails(user, newUser);
} finally {
graph.shutdown();
}
}
use of eu.esdihumboldt.hale.server.model.User in project hale by halestudio.
the class UserUtil method getUserName.
/**
* Get the current user's display name.
*
* @param graph a graph to retrieve the user from, or <code>null</code>
* @return the current user's display name
*/
public static String getUserName(Graph graph) {
String login = getLogin();
if (login == null)
return null;
boolean cleanup = false;
if (graph == null) {
graph = DatabaseHelper.getGraph();
cleanup = true;
}
try {
User user = User.getByLogin(graph, login);
return getDisplayName(user);
} catch (NonUniqueResultException e) {
log.error("Duplicate login in user database: " + login);
} finally {
if (cleanup) {
graph.shutdown();
}
}
return getDisplayName(null);
}
use of eu.esdihumboldt.hale.server.model.User in project hale by halestudio.
the class TemplatePage method addControls.
@SuppressWarnings("serial")
@Override
protected void addControls(boolean loggedIn) {
super.addControls(loggedIn);
StringValue idParam = getPageParameters().get(0);
if (!idParam.isNull() && !idParam.isEmpty()) {
String templateId = idParam.toString();
OrientGraph graph = DatabaseHelper.getGraph();
try {
Template template = null;
try {
template = Template.getByTemplateId(graph, templateId);
} catch (NonUniqueResultException e) {
log.error("Duplicate template representation: " + templateId, e);
}
if (template != null) {
// name
Label name = new Label("name", template.getName());
add(name);
// download
String href = TemplateLocations.getTemplateDownloadUrl(templateId);
ExternalLink download = new ExternalLink("download", href);
add(download);
// project location
WebMarkupContainer project = new WebMarkupContainer("project");
project.add(AttributeModifier.replace("value", TemplateLocations.getTemplateProjectUrl(scavenger, templateId)));
add(project);
// author
Label author = new Label("author", template.getAuthor());
add(author);
// edit-buttons container
WebMarkupContainer editButtons = new WebMarkupContainer("edit-buttons");
editButtons.setVisible(false);
add(editButtons);
// deleteDialog container
WebMarkupContainer deleteDialog = new WebMarkupContainer("deleteDialog");
deleteDialog.setVisible(false);
add(deleteDialog);
// user
String userName;
Vertex v = template.getV();
boolean showEditPart = UserUtil.isAdmin();
Iterator<Vertex> owners = v.getVertices(Direction.OUT, "owner").iterator();
if (owners.hasNext()) {
User user = new User(owners.next(), graph);
userName = UserUtil.getDisplayName(user);
showEditPart = loggedIn && UserUtil.getLogin().equals(user.getLogin());
} else {
userName = "Unregistered user";
}
// edit buttons
if (showEditPart) {
editButtons.setVisible(true);
deleteDialog.setVisible(true);
// edit
editButtons.add(new BookmarkablePageLink<>("edit", EditTemplatePage.class, new PageParameters().set(0, templateId)));
// update
editButtons.add(new BookmarkablePageLink<>("update", UpdateTemplatePage.class, new PageParameters().set(0, templateId)));
// delete
deleteDialog.add(new DeleteTemplateLink("delete", templateId));
}
Label user = new Label("user", userName);
add(user);
// description
String descr = template.getDescription();
String htmlDescr = null;
if (descr == null || descr.isEmpty()) {
descr = "No description for the project template available.";
} else {
// convert markdown to
htmlDescr = new PegDownProcessor(Extensions.AUTOLINKS | Extensions.SUPPRESS_ALL_HTML | Extensions.HARDWRAPS | Extensions.SMARTYPANTS).markdownToHtml(descr);
}
// description in pre
Label description = new Label("description", descr);
description.setVisible(htmlDescr == null);
add(description);
// description in div
Label htmlDescription = new Label("html-description", htmlDescr);
htmlDescription.setVisible(htmlDescr != null);
htmlDescription.setEscapeModelStrings(false);
add(htmlDescription);
// invalid
WebMarkupContainer statusInvalid = new WebMarkupContainer("invalid");
statusInvalid.setVisible(!template.isValid());
add(statusInvalid);
// resources
ResourcesPanel resources = new ResourcesPanel("resources", templateId);
add(resources);
// project popover
WebMarkupContainer projectPopover = new WebMarkupContainer("project-popover");
projectPopover.add(new HTMLPopoverBehavior(Model.of("Load template in HALE"), new PopoverConfig().withPlacement(Placement.bottom)) {
@Override
public Component newBodyComponent(String markupId) {
return new ProjectURLPopover(markupId);
}
});
add(projectPopover);
} else {
throw new AbortWithHttpErrorCodeException(HttpServletResponse.SC_NOT_FOUND, "Template not found.");
}
} finally {
graph.shutdown();
}
} else
throw new AbortWithHttpErrorCodeException(HttpServletResponse.SC_BAD_REQUEST, "Template identifier must be specified.");
}
Aggregations