Created
October 19, 2011 09:17
-
-
Save lukaspili/1297815 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package service; | |
import models.user.User; | |
import play.db.jpa.Model; | |
import java.util.ArrayList; | |
import java.util.List; | |
/** | |
* @author Lukasz Piliszczuk <lukasz.piliszczuk AT zenika.com> | |
*/ | |
public abstract class AbstractService<T> { | |
protected T detach(T t) { | |
Model.em().detach(t); | |
return t; | |
} | |
protected List<T> detach(List<T> list) { | |
List<T> detachedList = new ArrayList<T>(); | |
for (T t : list) { | |
Model.em().detach(t); | |
detachedList.add(t); | |
} | |
return detachedList; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package service; | |
import exceptions.CoreException; | |
import models.user.User; | |
import org.apache.commons.lang3.RandomStringUtils; | |
import org.jasypt.util.password.StrongPasswordEncryptor; | |
import play.db.jpa.Model; | |
import java.util.ArrayList; | |
import java.util.List; | |
import static com.google.common.base.Preconditions.checkNotNull; | |
/** | |
* @author Lukasz Piliszczuk <lukasz.piliszczuk AT zenika.com> | |
*/ | |
public class UserService extends AbstractService<User> { | |
public User save(User user) throws CoreException { | |
if (null != getByIdBooster(user.idBooster)) { | |
throw new CoreException().type(CoreException.Type.UNIQUE_CONSTRAINT_VIOLATION); | |
} | |
user.password = RandomStringUtils.randomAlphanumeric(9); | |
user.save(); | |
return detach(user); | |
} | |
public User getById(long id) { | |
User user = User.findById(id); | |
return detach(user); | |
} | |
public User getByIdBooster(String idBooser) { | |
checkNotNull(idBooser, "ID booster is required"); | |
User user = User.find("byIdBooster", idBooser).first(); | |
return detach(user); | |
} | |
public List<User> getUsers() { | |
List<User> users = User.findAll(); | |
return detach(users); | |
} | |
public User getFromLogin(String idBooster, String password) { | |
checkNotNull(idBooster, "ID booster is required"); | |
checkNotNull(password, "Password is required"); | |
User user = User.find("byIdBooster", idBooster).first(); | |
if (null == user) { | |
return null; | |
} | |
boolean isPasswordValid; | |
if (user.active) { | |
isPasswordValid = new StrongPasswordEncryptor().checkPassword(password, user.password); | |
} else { | |
isPasswordValid = password.equals(user.password); | |
} | |
if (!isPasswordValid) { | |
return null; | |
} | |
return detach(user); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment