Guest User

Untitled

a guest
Aug 16th, 2018
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. package service;
  2.  
  3. import exceptions.CoreException;
  4. import models.user.User;
  5. import org.apache.commons.lang3.RandomStringUtils;
  6. import org.jasypt.util.password.StrongPasswordEncryptor;
  7. import play.db.jpa.Model;
  8.  
  9. import java.util.ArrayList;
  10. import java.util.List;
  11.  
  12. import static com.google.common.base.Preconditions.checkNotNull;
  13.  
  14. /**
  15. * @author Lukasz Piliszczuk <lukasz.piliszczuk AT zenika.com>
  16. */
  17. public class UserService extends AbstractService<User> {
  18.  
  19. public User save(User user) throws CoreException {
  20.  
  21. if (null != getByIdBooster(user.idBooster)) {
  22. throw new CoreException().type(CoreException.Type.UNIQUE_CONSTRAINT_VIOLATION);
  23. }
  24.  
  25. user.password = RandomStringUtils.randomAlphanumeric(9);
  26. user.save();
  27.  
  28. return detach(user);
  29. }
  30.  
  31. public User getById(long id) {
  32. User user = User.findById(id);
  33. return detach(user);
  34. }
  35.  
  36. public User getByIdBooster(String idBooser) {
  37.  
  38. checkNotNull(idBooser, "ID booster is required");
  39.  
  40. User user = User.find("byIdBooster", idBooser).first();
  41. return detach(user);
  42. }
  43.  
  44. public List<User> getUsers() {
  45. List<User> users = User.findAll();
  46. return detach(users);
  47. }
  48.  
  49. public User getFromLogin(String idBooster, String password) {
  50.  
  51. checkNotNull(idBooster, "ID booster is required");
  52. checkNotNull(password, "Password is required");
  53.  
  54. User user = User.find("byIdBooster", idBooster).first();
  55.  
  56. if (null == user) {
  57. return null;
  58. }
  59.  
  60. boolean isPasswordValid;
  61. if (user.active) {
  62. isPasswordValid = new StrongPasswordEncryptor().checkPassword(password, user.password);
  63. } else {
  64. isPasswordValid = password.equals(user.password);
  65. }
  66.  
  67. if (!isPasswordValid) {
  68. return null;
  69. }
  70.  
  71. return detach(user);
  72. }
  73. }
Add Comment
Please, Sign In to add comment