Add REST service for reseting user password.

This commit is contained in:
Paulo Gustavo Veiga 2012-02-21 17:41:51 -03:00
parent 6ff556b317
commit a4e097ad15
9 changed files with 83 additions and 30 deletions

View File

@ -256,9 +256,9 @@
<version>1.9.4</version>
</dependency>
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.3.1</version>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.4</version>
</dependency>
</dependencies>

View File

@ -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)

View File

@ -47,4 +47,7 @@ public interface UserManager {
public Collaborator getCollaboratorBy(String email);
public User createUser(User user, Collaborator col);
public void deleteUser(User user);
}

View File

@ -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,14 +96,15 @@ 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";
@ -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);

View File

@ -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) {
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");
}
// 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("responseView", "message", "User created successfully");
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");
}
}

View File

@ -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");
}
}

View File

@ -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);
}

View File

@ -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) {

View File

@ -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 <20>