- Add more tests to save map

- Add new rest operations for saving map content
- Fix password encoding bug
- Fix bug that allow different users being created with the same password.
This commit is contained in:
Paulo Gustavo Veiga
2012-03-14 01:49:05 -03:00
parent 8716ff4feb
commit eb6aac4a5e
18 changed files with 297 additions and 144 deletions

View File

@@ -85,7 +85,7 @@ public class MindmapSharingController extends BaseMultiActionController {
final ColaborationEmail email = new ColaborationEmail();
email.setSubject(request.getParameter("subject"));
email.setMessage(request.getParameter("message"));
getMindmapService().addColaborators(mindMap, emails, role, email);
getMindmapService().addCollaborators(mindMap, emails, role, email);
return new MindMapBean(mindMap);
}

View File

@@ -51,25 +51,11 @@ public class NewMindmapController
mindmap.setTitle(title);
mindmap.setOwner(user);
final String xml = getDefaultMindmapXml(title);
final String xml = MindMap.getDefaultMindmapXml(title);
mindmap.setXmlStr(xml);
final User dbUSer = getUserService().getUserBy(user.getId());
service.addMindmap(mindmap, dbUSer);
service.addMindmap(mindmap, user);
return new ModelAndView("redirect:editor.htm?mapId=" + mindmap.getId() + "&action=open");
}
private String getDefaultMindmapXml(final String title) {
final StringBuffer map = new StringBuffer();
map.append("<map>");
map.append("<topic central=\"true\" text=\"");
map.append(title);
map.append("\"/></map>");
return map.toString();
}
}

View File

@@ -147,7 +147,7 @@ public class MindmapManagerImpl
}
public MindMap getMindmapById(int mindmapId) {
return (MindMap) getHibernateTemplate().get(MindMap.class, mindmapId);
return getHibernateTemplate().get(MindMap.class, mindmapId);
}
public MindMap getMindmapByTitle(final String title, final User user) {

View File

@@ -22,8 +22,10 @@ import com.wisemapping.model.Collaborator;
import com.wisemapping.model.MindmapUser;
import com.wisemapping.model.User;
import com.wisemapping.model.UserLogin;
import com.wisemapping.security.CustomPasswordEncoder;
import org.jetbrains.annotations.NotNull;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.security.authentication.encoding.PasswordEncoder;
//import org.acegisecurity.providers.encoding.PasswordEncoder;
import java.util.List;
@@ -33,12 +35,12 @@ public class UserManagerImpl
extends HibernateDaoSupport
implements UserManager {
// private PasswordEncoder passwordEncoder;
//
// public void setEncoder(PasswordEncoder passwordEncoder)
// {
// this.passwordEncoder = passwordEncoder;
// }
private PasswordEncoder passwordEncoder;
public void setEncoder(PasswordEncoder passwordEncoder)
{
this.passwordEncoder = passwordEncoder;
}
public List<User> getAllUsers() {
return getHibernateTemplate().find("from com.wisemapping.model.User user");
@@ -99,13 +101,13 @@ public class UserManagerImpl
@Override
public void createUser(User user) {
assert user != null : "Trying to store a null user";
// user.setPassword(passwordEncoder.encodePassword(user.getPassword(),null));
user.setPassword(passwordEncoder.encodePassword(user.getPassword(),null));
getHibernateTemplate().saveOrUpdate(user);
}
@Override
public User createUser(@NotNull User user, @NotNull Collaborator col) {
// user.setPassword(passwordEncoder.encodePassword(user.getPassword(),null));
user.setPassword(passwordEncoder.encodePassword(user.getPassword(),null));
assert user != null : "Trying to store a null user";
final Set<MindmapUser> set = col.getMindmapUsers();
@@ -138,7 +140,7 @@ public class UserManagerImpl
public void updateUser(User user) {
assert user != null : "user is null";
// user.setPassword(passwordEncoder.encodePassword(user.getPassword(),null));
user.setPassword(passwordEncoder.encodePassword(user.getPassword(),null));
getHibernateTemplate().update(user);
}

View File

@@ -84,7 +84,11 @@ public class MindMap {
}
public String getXmlStr() throws UnsupportedEncodingException {
return new String(this.xml, UTF_8);
String result = null;
if (this.xml != null) {
result = new String(this.xml, UTF_8);
}
return result;
}
public byte[] getZippedXml()
@@ -219,4 +223,15 @@ public class MindMap {
public User getOwner() {
return owner;
}
public static String getDefaultMindmapXml(@NotNull final String title) {
final StringBuilder result = new StringBuilder();
result.append("<result version='tango'>");
result.append("<topic central=\"true\" text=\"");
result.append(title);
result.append("\"/></result>");
return result.toString();
}
}

View File

@@ -26,7 +26,7 @@ public class AdminController extends BaseController {
if (userBy == null) {
throw new IllegalArgumentException("User could not be found");
}
return new ModelAndView("userView", "user", new RestUser(userBy));
return new ModelAndView("userView", "user", new RestUser(userBy));
}
@RequestMapping(method = RequestMethod.GET, value = "admin/users/email/{email}", produces = {"application/json", "text/html", "application/xml"})
@@ -39,9 +39,19 @@ public class AdminController extends BaseController {
return new ModelAndView("userView", "user", new RestUser(user));
}
@RequestMapping(method = RequestMethod.GET, value = "admin/users/username/{username}", produces = {"application/json", "text/html", "application/xml"})
@ResponseBody
public ModelAndView getUserByUsername(@PathVariable String username) throws IOException {
final User user = userService.getUserByUsername(username);
if (user == null) {
throw new IllegalArgumentException("User '" + username + "' could not be found");
}
return new ModelAndView("userView", "user", new RestUser(user));
}
@RequestMapping(method = RequestMethod.POST, value = "admin/users", consumes = {"application/xml", "application/json"}, produces = {"application/json", "text/html", "application/xml"})
@ResponseStatus(value = HttpStatus.CREATED)
public void getUserByEmail(@RequestBody RestUser user, HttpServletResponse response) throws IOException, WiseMappingException {
public void createUser(@RequestBody RestUser user, HttpServletResponse response) throws IOException, WiseMappingException {
if (user == null) {
throw new IllegalArgumentException("User could not be found");
}
@@ -52,6 +62,16 @@ public class AdminController extends BaseController {
throw new IllegalArgumentException("User already exists with this email.");
}
final String username = user.getUsername();
if (username == null || username.isEmpty()) {
throw new IllegalArgumentException("username can not be null");
}
if (userService.getUserByUsername(username) != null) {
throw new IllegalArgumentException("User already exists with this username.");
}
// Run some other validations ...
final User delegated = user.getDelegated();
final String lastname = delegated.getLastname();
if (lastname == null || lastname.isEmpty()) {
@@ -59,17 +79,13 @@ public class AdminController extends BaseController {
}
final String firstName = delegated.getFirstname();
if (firstName == null || firstName.isEmpty()) {
throw new IllegalArgumentException("firstname can not be null");
}
final String username = delegated.getUsername();
if (username == null || username.isEmpty()) {
throw new IllegalArgumentException("username can not be null");
}
if (firstName == null || firstName.isEmpty()) {
throw new IllegalArgumentException("firstname can not be null");
}
// Finally create the user ...
userService.createUser(delegated, false);
response.setHeader("Location","/service/admin/users/" + user.getId());
response.setHeader("Location", "/service/admin/users/" + user.getId());
}
@RequestMapping(method = RequestMethod.PUT, value = "admin/users/{id}/password", consumes = {"text/plain"})

View File

@@ -7,14 +7,17 @@ import com.wisemapping.model.MindmapUser;
import com.wisemapping.model.User;
import com.wisemapping.rest.model.RestMindmap;
import com.wisemapping.rest.model.RestMindmapList;
import com.wisemapping.rest.model.RestUser;
import com.wisemapping.security.Utils;
import com.wisemapping.service.MindmapService;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
@@ -55,43 +58,83 @@ public class MindmapController extends BaseController {
final MindMap mindMap = mindmapService.getMindmapById(id);
final User user = Utils.getUser();
// Validate arguments ...
final String properties = restMindmap.getProperties();
if (properties == null) {
throw new IllegalArgumentException("Map properties can not be null");
}
mindMap.setProperties(properties);
final Calendar now = Calendar.getInstance();
mindMap.setLastModificationTime(now);
mindMap.setLastModifierUser(user.getUsername());
final Calendar lastModification = Calendar.getInstance();
lastModification.setTime(new Date());
mindMap.setLastModificationTime(lastModification);
// Validate content ...
final String xml = restMindmap.getXml();
if (xml == null) {
throw new IllegalArgumentException("Map xml can not be null");
}
mindMap.setXmlStr(xml);
mindmapService.updateMindmap(mindMap, minor);
// Update map ...
updateMindmap(minor, mindMap, user);
}
@RequestMapping(method = RequestMethod.POST, value = "/maps", consumes = {"application/xml", "application/json"}, produces = {"application/json", "text/html", "application/xml"})
@RequestMapping(method = RequestMethod.PUT, value = "/maps/{id}/xml", consumes = {"application/xml"}, produces = {"application/json", "text/html", "application/xml"})
@ResponseStatus(value = HttpStatus.NO_CONTENT)
public void createMap(@RequestBody RestMindmap restMindmap, @PathVariable int id, @RequestParam(required = false) boolean minor) throws IOException, WiseMappingException {
public void updateMapXml(@RequestBody String xml, @PathVariable int id, @RequestParam(required = false) boolean minor) throws IOException, WiseMappingException {
final MindMap mindMap = mindmapService.getMindmapById(id);
final User user = Utils.getUser();
final String properties = restMindmap.getProperties();
mindMap.setProperties(properties);
if (xml == null || xml.isEmpty()) {
throw new IllegalArgumentException("Map xml can not be null");
}
mindMap.setXmlStr(xml);
// Update map ...
updateMindmap(minor, mindMap, user);
}
private void updateMindmap(boolean minor, MindMap mindMap, User user) throws WiseMappingException {
final Calendar now = Calendar.getInstance();
mindMap.setLastModificationTime(now);
mindMap.setLastModifierUser(user.getUsername());
final Calendar lastModification = Calendar.getInstance();
lastModification.setTime(new Date());
mindMap.setLastModificationTime(lastModification);
final String xml = restMindmap.getXml();
mindMap.setXmlStr(xml);
mindmapService.updateMindmap(mindMap, minor);
}
@RequestMapping(method = RequestMethod.POST, value = "/maps", consumes = {"application/xml", "application/json"})
@ResponseStatus(value = HttpStatus.CREATED)
public void createMap(@RequestBody RestMindmap restMindmap, @NotNull HttpServletResponse response) throws IOException, WiseMappingException {
final String title = restMindmap.getTitle();
if (title == null || title.isEmpty()) {
throw new IllegalArgumentException("Map title can not be null");
}
final String description = restMindmap.getDescription();
if (description == null || description.isEmpty()) {
throw new IllegalArgumentException("Map details can not be null");
}
// Some basic validations ...
final User user = Utils.getUser();
final MindMap mindMap = mindmapService.getMindmapByTitle(title, user);
if (mindMap != null) {
throw new IllegalArgumentException("Map already exists with title '" + title + "'");
}
// If the user has not specified the xml content, add one ...
final MindMap delegated = restMindmap.getDelegated();
String xml = restMindmap.getXml();
if (xml == null || xml.isEmpty()) {
xml = MindMap.getDefaultMindmapXml(restMindmap.getTitle());
}
delegated.setXmlStr(xml);
delegated.setOwner(user);
// Add new mindmap ...
mindmapService.addMindmap(delegated, user);
// Return the new created map ...
response.setHeader("Location", "/service/maps/" + delegated.getId());
}
}

View File

@@ -4,7 +4,9 @@ package com.wisemapping.rest.model;
import com.wisemapping.model.MindMap;
import com.wisemapping.model.User;
import org.codehaus.jackson.annotate.*;
import org.codehaus.jackson.map.annotate.JsonSerialize;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
@@ -21,6 +23,7 @@ import java.util.Date;
isGetterVisibility = JsonAutoDetect.Visibility.NONE,
getterVisibility = JsonAutoDetect.Visibility.PUBLIC_ONLY
)
@JsonIgnoreProperties(ignoreUnknown = true)
public class RestMindmap {
@JsonIgnore
@@ -35,10 +38,6 @@ public class RestMindmap {
this.mindmap = mindmap;
}
public String getOwner() {
return mindmap.getOwner().getUsername();
}
public Calendar getCreationTime() {
return mindmap.getCreationTime();
}
@@ -79,9 +78,10 @@ public class RestMindmap {
return mindmap.getXmlStr();
}
public void setXml(@NotNull String xml) throws IOException {
public void setXml(@Nullable String xml) throws IOException {
mindmap.setXmlStr(xml);
if (xml != null)
mindmap.setXmlStr(xml);
}
public void setId(int id) {
@@ -101,11 +101,10 @@ public class RestMindmap {
}
public void setOwner(User owner) {
mindmap.setOwner(owner);
}
public void setCreator(String creatorUser) {
mindmap.setCreator(creatorUser);
}
@@ -114,11 +113,9 @@ public class RestMindmap {
}
public void setLastModificationTime(Calendar lastModificationTime) {
mindmap.setLastModificationTime(lastModificationTime);
}
public void setLastModifierUser(String lastModifierUser) {
mindmap.setLastModifierUser(lastModifierUser);
}
public String getProperties() {

View File

@@ -16,8 +16,10 @@ import java.util.List;
@XmlRootElement(name = "maps")
@XmlAccessorType(XmlAccessType.PROPERTY)
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.NONE,
getterVisibility = JsonAutoDetect.Visibility.PUBLIC_ONLY, isGetterVisibility = JsonAutoDetect.Visibility.PUBLIC_ONLY)
@JsonAutoDetect(
fieldVisibility = JsonAutoDetect.Visibility.NONE,
getterVisibility = JsonAutoDetect.Visibility.PUBLIC_ONLY,
isGetterVisibility = JsonAutoDetect.Visibility.PUBLIC_ONLY)
public class RestMindmapList {
private List<RestMindmap> mindmaps;

View File

@@ -4,6 +4,8 @@ package com.wisemapping.rest.model;
import com.wisemapping.model.User;
import org.codehaus.jackson.annotate.JsonAutoDetect;
import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.map.annotate.JsonSerialize;
import org.jetbrains.annotations.NotNull;
import javax.xml.bind.annotation.XmlAccessType;
@@ -14,8 +16,11 @@ import java.util.Set;
@XmlRootElement(name = "user")
@XmlAccessorType(XmlAccessType.PROPERTY)
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.NONE,
getterVisibility = JsonAutoDetect.Visibility.PUBLIC_ONLY, isGetterVisibility = JsonAutoDetect.Visibility.PUBLIC_ONLY)
@JsonAutoDetect(
fieldVisibility = JsonAutoDetect.Visibility.NONE,
getterVisibility = JsonAutoDetect.Visibility.PUBLIC_ONLY,
isGetterVisibility = JsonAutoDetect.Visibility.PUBLIC_ONLY)
@JsonIgnoreProperties(ignoreUnknown = true)
public class RestUser {
private User user;

View File

@@ -40,7 +40,7 @@ public interface MindmapService {
public void addMindmap(MindMap map, User user) throws WiseMappingException;
public void addColaborators(MindMap mindmap, String[] colaboratorEmails, UserRole role, ColaborationEmail email)
public void addCollaborators(MindMap mindmap, String[] colaboratorEmails, UserRole role, ColaborationEmail email)
throws InvalidColaboratorException;
public void addTags(MindMap mindmap, String tags);
@@ -61,7 +61,7 @@ public interface MindmapService {
public boolean isAllowedToColaborate(User user, int mapId, UserRole grantedRole);
public boolean isAllowedToColaborate(User user, MindMap map, UserRole grantedRole);
public boolean isAllowedToCollaborate(User user, MindMap map, UserRole grantedRole);
public void addWelcomeMindmap(User user) throws WiseMappingException;

View File

@@ -22,6 +22,8 @@ import com.wisemapping.dao.MindmapManager;
import com.wisemapping.exceptions.WiseMappingException;
import com.wisemapping.mail.Mailer;
import com.wisemapping.model.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.IOException;
import java.util.*;
@@ -36,7 +38,7 @@ public class MindmapServiceImpl
public boolean isAllowedToColaborate(User user, int mapId, UserRole grantedRole) {
final MindMap map = mindmapManager.getMindmapById(mapId);
return isAllowedToColaborate(user, map, grantedRole);
return isAllowedToCollaborate(user, map, grantedRole);
}
public boolean isAllowedToView(User user, int mapId, UserRole grantedRole) {
@@ -51,13 +53,13 @@ public class MindmapServiceImpl
if (map.isPublic()) {
isAllowed = true;
} else if (user != null) {
isAllowed = isAllowedToColaborate(user, map, grantedRole);
isAllowed = isAllowedToCollaborate(user, map, grantedRole);
}
}
return isAllowed;
}
public boolean isAllowedToColaborate(User user, MindMap map, UserRole grantedRole) {
public boolean isAllowedToCollaborate(@NotNull User user, @Nullable MindMap map, UserRole grantedRole) {
boolean isAllowed = false;
if (map != null) {
if (map.getOwner().getId() == user.getId()) {
@@ -99,6 +101,7 @@ public class MindmapServiceImpl
if (mindMap.getTitle() == null || mindMap.getTitle().length() == 0) {
throw new WiseMappingException("The tile can not be empty");
}
mindmapManager.updateMindmap(mindMap, saveHistory);
}
@@ -149,7 +152,7 @@ public class MindmapServiceImpl
}
}
public void addMindmap(MindMap map, User user) throws WiseMappingException {
public void addMindmap(@NotNull MindMap map, @NotNull User user) throws WiseMappingException {
final String title = map.getTitle();
@@ -169,13 +172,15 @@ public class MindmapServiceImpl
map.setLastModificationTime(creationTime);
map.setOwner(user);
final MindmapUser mindmapUser = new MindmapUser(UserRole.OWNER.ordinal(), user, map);
// Hack to reload dbuser ...
final User dbUser = userService.getUserBy(user.getId());
final MindmapUser mindmapUser = new MindmapUser(UserRole.OWNER.ordinal(), dbUser, map);
map.getMindmapUsers().add(mindmapUser);
mindmapManager.addMindmap(user, map);
}
public void addColaborators(MindMap mindmap, String[] colaboratorEmails, UserRole role, ColaborationEmail email)
public void addCollaborators(MindMap mindmap, String[] colaboratorEmails, UserRole role, ColaborationEmail email)
throws InvalidColaboratorException {
if (colaboratorEmails != null && colaboratorEmails.length > 0) {
final Collaborator owner = mindmap.getOwner();
@@ -187,7 +192,7 @@ public class MindmapServiceImpl
}
MindmapUser mindmapUser = getMindmapUserBy(colaboratorEmail, mindmapUsers);
if (mindmapUser == null) {
addColaborator(colaboratorEmail, role, mindmap, email);
addCollaborator(colaboratorEmail, role, mindmap, email);
} else if (mindmapUser.getRole() != role) {
// If the relationship already exists and the role changed then only update the role
mindmapUser.setRoleId(role.ordinal());
@@ -260,7 +265,7 @@ public class MindmapServiceImpl
return mindmapUser;
}
private void addColaborator(String colaboratorEmail, UserRole role, MindMap mindmap, ColaborationEmail email) {
private void addCollaborator(String colaboratorEmail, UserRole role, MindMap mindmap, ColaborationEmail email) {
Collaborator collaborator = mindmapManager.getCollaboratorBy(colaboratorEmail);
if (collaborator == null) {

View File

@@ -64,11 +64,11 @@ public class MapInfoValidator implements Validator {
}
}
ValidatorUtils.rejectIfExceeded(errors,
"description",
"The description must have less than "+Constants.MAX_MAP_DESCRIPTION_LENGTH + " characters.",
desc,
Constants.MAX_MAP_DESCRIPTION_LENGTH);
}
"description",
"The description must have less than " + Constants.MAX_MAP_DESCRIPTION_LENGTH + " characters.",
desc,
Constants.MAX_MAP_DESCRIPTION_LENGTH);
}
}