diff --git a/wise-webapp/pom.xml b/wise-webapp/pom.xml index ce5cd55e..48d2e549 100644 --- a/wise-webapp/pom.xml +++ b/wise-webapp/pom.xml @@ -256,9 +256,9 @@ 1.9.4 - com.thoughtworks.xstream - xstream - 1.3.1 + org.slf4j + slf4j-api + 1.6.4 diff --git a/wise-webapp/src/main/java/com/wisemapping/controller/ActivationController.java b/wise-webapp/src/main/java/com/wisemapping/controller/ActivationController.java index 3a663c5e..50f9e866 100755 --- a/wise-webapp/src/main/java/com/wisemapping/controller/ActivationController.java +++ b/wise-webapp/src/main/java/com/wisemapping/controller/ActivationController.java @@ -43,7 +43,7 @@ public class ActivationController try { final String code = request.getParameter("code"); - userService.activateAcount(Long.parseLong(code)); + userService.activateAccount(Long.parseLong(code)); modelAndView = new ModelAndView("activationAccountConfirmation"); } catch (Throwable exc) diff --git a/wise-webapp/src/main/java/com/wisemapping/dao/UserManager.java b/wise-webapp/src/main/java/com/wisemapping/dao/UserManager.java index d06581e5..7439e3e9 100644 --- a/wise-webapp/src/main/java/com/wisemapping/dao/UserManager.java +++ b/wise-webapp/src/main/java/com/wisemapping/dao/UserManager.java @@ -47,4 +47,7 @@ public interface UserManager { public Collaborator getCollaboratorBy(String email); public User createUser(User user, Collaborator col); + + public void deleteUser(User user); + } diff --git a/wise-webapp/src/main/java/com/wisemapping/dao/UserManagerImpl.java b/wise-webapp/src/main/java/com/wisemapping/dao/UserManagerImpl.java index d30b04b1..5f4df024 100644 --- a/wise-webapp/src/main/java/com/wisemapping/dao/UserManagerImpl.java +++ b/wise-webapp/src/main/java/com/wisemapping/dao/UserManagerImpl.java @@ -44,6 +44,8 @@ public class UserManagerImpl return getHibernateTemplate().find("from com.wisemapping.model.User user"); } + + @Override public User getUserBy(final String email) { final User user; final List users = getHibernateTemplate().find("from com.wisemapping.model.User colaborator where email=?", email); @@ -56,6 +58,7 @@ public class UserManagerImpl return user; } + @Override public Collaborator getCollaboratorBy(final String email) { final Collaborator cola; final List cols = getHibernateTemplate().find("from com.wisemapping.model.Collaborator colaborator where email=?", email); @@ -68,11 +71,11 @@ public class UserManagerImpl return cola; } - public User getUserBy(long id) - { - return (User)getHibernateTemplate().get(User.class,id); + public User getUserBy(long id) { + return getHibernateTemplate().get(User.class, id); } + @Override public User getUserByUsername(String username) { final User user; final List users = getHibernateTemplate().find("from com.wisemapping.model.User colaborator where username=?", username); @@ -85,6 +88,7 @@ public class UserManagerImpl return user; } + @Override public boolean authenticate(final String email, final String password) { final boolean result; final User user = getUserBy(email); @@ -92,20 +96,21 @@ public class UserManagerImpl return result; } + @Override public void createUser(User user) { assert user != null : "Trying to store a null user"; // user.setPassword(passwordEncoder.encodePassword(user.getPassword(),null)); getHibernateTemplate().saveOrUpdate(user); } - public User createUser(@NotNull User user, @NotNull Collaborator col) - { + @Override + public User createUser(@NotNull User user, @NotNull Collaborator col) { // user.setPassword(passwordEncoder.encodePassword(user.getPassword(),null)); assert user != null : "Trying to store a null user"; final Set set = col.getMindmapUsers(); for (MindmapUser mindmapUser : set) { - MindmapUser newMapUser = new MindmapUser(); + MindmapUser newMapUser = new MindmapUser(); newMapUser.setRoleId(mindmapUser.getRole().ordinal()); newMapUser.setMindMap(mindmapUser.getMindMap()); newMapUser.setCollaborator(user); @@ -118,6 +123,14 @@ public class UserManagerImpl return user; } + @Override + public void deleteUser(@NotNull User user) { + final Collaborator collaborator = this.getCollaboratorBy(user.getEmail()); + getHibernateTemplate().delete(collaborator); + getHibernateTemplate().delete(user); + getHibernateTemplate().flush(); + } + public void auditLogin(UserLogin userLogin) { assert userLogin != null : "userLogin is null"; getHibernateTemplate().save(userLogin); diff --git a/wise-webapp/src/main/java/com/wisemapping/rest/AdminController.java b/wise-webapp/src/main/java/com/wisemapping/rest/AdminController.java index 69a13f4b..b306515a 100644 --- a/wise-webapp/src/main/java/com/wisemapping/rest/AdminController.java +++ b/wise-webapp/src/main/java/com/wisemapping/rest/AdminController.java @@ -5,7 +5,6 @@ import com.wisemapping.exceptions.WiseMappingException; import com.wisemapping.model.User; import com.wisemapping.rest.model.RestUser; import com.wisemapping.service.UserService; -import org.jetbrains.annotations.NotNull; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; @@ -15,12 +14,13 @@ import java.io.IOException; @Controller public class AdminController { + private static final String RESPONSE_VIEW = "responseView"; @Autowired private UserService userService; - @RequestMapping(method = RequestMethod.GET, value = "admin/users/{id}", produces = {"application/xml", "application/json","text/html"}) + @RequestMapping(method = RequestMethod.GET, value = "admin/users/{id}", produces = {"application/json", "text/html", "application/xml"}) @ResponseBody - public ModelAndView getUserById(@PathVariable int id) throws IOException { + public ModelAndView getUserById(@PathVariable long id) throws IOException { final User userBy = userService.getUserBy(id); if (userBy == null) { throw new IllegalArgumentException("User could not be found"); @@ -28,24 +28,53 @@ public class AdminController { return new ModelAndView("userView", "user", new RestUser(userBy)); } - @RequestMapping(method = RequestMethod.GET, value = "admin/users/email/{email}", produces = {"application/xml", "application/json","text/html"}) + @RequestMapping(method = RequestMethod.GET, value = "admin/users/email/{email}", produces = {"application/json", "text/html", "application/xml"}) @ResponseBody public ModelAndView getUserByEmail(@PathVariable String email) throws IOException { - final User userBy = userService.getUserBy(email); - if (userBy == null) { - throw new IllegalArgumentException("User '" + email + "' could not be found" ); + final User user = userService.getUserBy(email); + if (user == null) { + throw new IllegalArgumentException("User '" + email + "' could not be found"); } - return new ModelAndView("userView", "user", new RestUser(userBy)); + return new ModelAndView("userView", "user", new RestUser(user)); } - @RequestMapping(method = RequestMethod.POST, value = "admin/users", consumes = {"application/xml", "application/json"}) + @RequestMapping(method = RequestMethod.POST, value = "admin/users", consumes = {"application/xml", "application/json"}, produces = {"application/json", "text/html", "application/xml"}) public ModelAndView getUserByEmail(@RequestBody RestUser user) throws IOException, WiseMappingException { if (user == null) { throw new IllegalArgumentException("User could not be found"); } - userService.createUser(user.getDelegated(), false); - return new ModelAndView("responseView", "message", "User created successfully"); + // User already exists ? + final String email = user.getEmail(); + if(userService.getUserBy(email)!=null){ + throw new IllegalArgumentException("User already exists with this email."); + } + + userService.createUser(user.getDelegated(), false); + return new ModelAndView(RESPONSE_VIEW, "message", "User '" + user.getId() + "' created successfully"); + } + + @RequestMapping(method = RequestMethod.PUT, value = "admin/users/{id}/password", consumes = {"text/plain"}, produces = {"application/json", "text/html", "application/xml"}) + public ModelAndView changePassword(@RequestBody String password, @PathVariable long id) throws IOException, WiseMappingException { + if (password == null) { + throw new IllegalArgumentException("Password can not be null"); + } + + final User user = userService.getUserBy(id); + user.setPassword(password); + + userService.changePassword(user); + return new ModelAndView(RESPONSE_VIEW, "message", "User '" + user.getId() + "' password has been updated."); + } + + @RequestMapping(method = RequestMethod.DELETE, value = "admin/users/{id}", produces = {"application/json", "text/html", "application/xml"}) + public ModelAndView getUserByEmail(@PathVariable long id) throws IOException, WiseMappingException { + final User user = userService.getUserBy(id); + if (user == null) { + throw new IllegalArgumentException("User '" + id + "' could not be found"); + } + userService.deleteUser(user); + return new ModelAndView(RESPONSE_VIEW, "message", "User deleted successfully"); } } diff --git a/wise-webapp/src/main/java/com/wisemapping/rest/MindmapController.java b/wise-webapp/src/main/java/com/wisemapping/rest/MindmapController.java index fdc16102..9b57cd1b 100644 --- a/wise-webapp/src/main/java/com/wisemapping/rest/MindmapController.java +++ b/wise-webapp/src/main/java/com/wisemapping/rest/MindmapController.java @@ -26,7 +26,7 @@ public class MindmapController { @Autowired private MindmapService mindmapService; - @RequestMapping(method = RequestMethod.GET, value = "/maps/{id}", produces = {"application/xml", "application/json", "text/html"}) + @RequestMapping(method = RequestMethod.GET, value = "/maps/{id}", produces = {"application/json", "text/html", "application/xml"}) @ResponseBody public ModelAndView getMindmap(@PathVariable int id) throws IOException { final MindMap mindMap = mindmapService.getMindmapById(id); @@ -34,7 +34,7 @@ public class MindmapController { return new ModelAndView("mapView", "map", map); } - @RequestMapping(method = RequestMethod.GET, value = "/maps", produces = {"application/xml", "application/json", "text/html"}) + @RequestMapping(method = RequestMethod.GET, value = "/maps", produces = {"application/json", "text/html", "application/xml"}) public ModelAndView getMindmaps() throws IOException { final User user = com.wisemapping.security.Utils.getUser(); @@ -48,7 +48,7 @@ public class MindmapController { return new ModelAndView("mapsView", "list", restMindmapList); } - @RequestMapping(method = RequestMethod.PUT, value = "/maps/{id}", consumes = {"application/xml", "application/json"}) + @RequestMapping(method = RequestMethod.PUT, value = "/maps/{id}", consumes = {"application/xml", "application/json"},produces = {"application/json", "text/html", "application/xml"}) public ModelAndView updateMap(@RequestBody RestMindmap restMindmap, @PathVariable int id) throws IOException, WiseMappingException { final MindMap mindMap = mindmapService.getMindmapById(id); @@ -70,6 +70,5 @@ public class MindmapController { mindmapService.updateMindmap(mindMap, true); return new ModelAndView("responseView", "message", "Map has been updated successfully"); - } } diff --git a/wise-webapp/src/main/java/com/wisemapping/service/UserService.java b/wise-webapp/src/main/java/com/wisemapping/service/UserService.java index 40887ce4..ff06cd5d 100755 --- a/wise-webapp/src/main/java/com/wisemapping/service/UserService.java +++ b/wise-webapp/src/main/java/com/wisemapping/service/UserService.java @@ -20,12 +20,13 @@ package com.wisemapping.service; import com.wisemapping.model.User; import com.wisemapping.exceptions.WiseMappingException; +import org.jetbrains.annotations.NotNull; public interface UserService { - public void activateAcount(long code) throws InvalidActivationCodeException; + public void activateAccount(long code) throws InvalidActivationCodeException; - public void createUser(User user, boolean emailConfirmEnabled) throws WiseMappingException; + public User createUser(@NotNull User user, boolean emailConfirmEnabled) throws WiseMappingException; public void changePassword(User user); @@ -40,4 +41,6 @@ public interface UserService { public void sendEmailPassword(String email) throws InvalidUserEmailException; public User reloadUser(final User user); + + public void deleteUser(@NotNull User user); } diff --git a/wise-webapp/src/main/java/com/wisemapping/service/UserServiceImpl.java b/wise-webapp/src/main/java/com/wisemapping/service/UserServiceImpl.java index c158b318..75158993 100755 --- a/wise-webapp/src/main/java/com/wisemapping/service/UserServiceImpl.java +++ b/wise-webapp/src/main/java/com/wisemapping/service/UserServiceImpl.java @@ -24,6 +24,7 @@ import com.wisemapping.mail.Mailer; import com.wisemapping.model.Collaborator; import com.wisemapping.model.User; import org.apache.log4j.Logger; +import org.jetbrains.annotations.NotNull; import java.util.Calendar; import java.util.HashMap; @@ -37,7 +38,7 @@ public class UserServiceImpl private Mailer mailer; final static Logger logger = Logger.getLogger("org.wisemapping.service"); - public void activateAcount(long code) + public void activateAccount(long code) throws InvalidActivationCodeException { final User user = userManager.getUserByActivationCode(code); if (user == null || user.isActive()) { @@ -90,7 +91,11 @@ public class UserServiceImpl return lo + i; } - public void createUser(User user, boolean emailConfirmEnabled) throws WiseMappingException { + public void deleteUser(@NotNull User user){ + userManager.deleteUser(user); + } + + public User createUser(@NotNull User user, boolean emailConfirmEnabled) throws WiseMappingException { final UUID uuid = UUID.randomUUID(); user.setCreationDate(Calendar.getInstance()); user.setActivationCode(uuid.getLeastSignificantBits()); @@ -116,6 +121,7 @@ public class UserServiceImpl if (emailConfirmEnabled) { sendRegistrationEmail(user); } + return user; } private void sendRegistrationEmail(User user) { diff --git a/wise-webapp/src/main/webapp/WEB-INF/classes/log4j.properties b/wise-webapp/src/main/webapp/WEB-INF/classes/log4j.properties index 8abef6ba..69310dd9 100644 --- a/wise-webapp/src/main/webapp/WEB-INF/classes/log4j.properties +++ b/wise-webapp/src/main/webapp/WEB-INF/classes/log4j.properties @@ -1,6 +1,6 @@ log4j.rootLogger=WARN, stdout, R log4j.logger.com.wisemapping=WARN,stdout,R -log4j.logger.org.springframework=WARN,stdout,R +log4j.logger.org.springframework=DEBUG,stdout,R log4j.logger.org.codehaus.jackson=WARN,stdout,R # Stdout logger �