Files
Athou_commafeed/src/main/java/com/commafeed/backend/service/UserService.java

164 lines
5.2 KiB
Java
Raw Normal View History

package com.commafeed.backend.service;
2013-03-20 20:33:42 +01:00
2013-03-30 09:22:49 +01:00
import java.util.Collection;
import java.util.Date;
2013-05-20 21:53:13 +02:00
import java.util.UUID;
2013-03-20 20:33:42 +01:00
2014-08-17 14:16:30 +02:00
import javax.inject.Inject;
import javax.inject.Singleton;
import javax.servlet.http.HttpSession;
import lombok.RequiredArgsConstructor;
2013-03-20 20:33:42 +01:00
2013-05-20 21:53:13 +02:00
import org.apache.commons.codec.digest.DigestUtils;
2013-06-18 12:31:09 +02:00
import org.apache.commons.lang.StringUtils;
2013-05-20 21:53:13 +02:00
import com.commafeed.CommaFeedConfiguration;
import com.commafeed.backend.dao.FeedCategoryDAO;
2013-04-11 20:49:08 +02:00
import com.commafeed.backend.dao.UserDAO;
import com.commafeed.backend.dao.UserSettingsDAO;
2013-03-23 16:17:19 +01:00
import com.commafeed.backend.model.User;
2013-03-30 09:22:49 +01:00
import com.commafeed.backend.model.UserRole;
2013-03-30 19:06:32 +01:00
import com.commafeed.backend.model.UserRole.Role;
import com.commafeed.backend.service.internal.PostLoginActivities;
2014-08-09 15:25:41 +02:00
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
2013-03-20 20:33:42 +01:00
2014-08-17 14:16:30 +02:00
@RequiredArgsConstructor(onConstructor = @__({ @Inject }))
@Singleton
2013-04-11 20:49:08 +02:00
public class UserService {
2013-03-20 20:33:42 +01:00
private static final String SESSION_KEY_USER = "user";
private final FeedCategoryDAO feedCategoryDAO;
private final UserDAO userDAO;
private final UserSettingsDAO userSettingsDAO;
2013-04-06 17:10:38 +02:00
private final FeedSubscriptionService feedSubscriptionService;
private final PasswordEncryptionService encryptionService;
private final CommaFeedConfiguration config;
2013-11-26 15:09:32 +01:00
/**
* try to log in with given credentials
*/
2014-08-12 19:55:57 +02:00
public Optional<User> login(String nameOrEmail, String password) {
if (nameOrEmail == null || password == null) {
2014-08-09 16:07:24 +02:00
return Optional.absent();
2013-05-24 09:21:20 +02:00
}
2014-08-12 19:55:57 +02:00
User user = userDAO.findByName(nameOrEmail);
if (user == null) {
user = userDAO.findByEmail(nameOrEmail);
}
2013-03-29 12:59:21 +01:00
if (user != null && !user.isDisabled()) {
2013-07-25 09:17:33 +02:00
boolean authenticated = encryptionService.authenticate(password, user.getPassword(), user.getSalt());
2013-03-20 20:33:42 +01:00
if (authenticated) {
afterLogin(user);
2014-08-09 15:25:41 +02:00
return Optional.fromNullable(user);
2013-03-20 20:33:42 +01:00
}
}
2014-08-09 15:25:41 +02:00
return Optional.absent();
2014-08-08 21:57:16 +02:00
}
/**
* try to log in with given credentials and create a session for the user
*/
public Optional<User> login(String nameOrEmail, String password, HttpSession sessionToFill) {
Optional<User> user = login(nameOrEmail, password);
if (user.isPresent()) {
sessionToFill.setAttribute(SESSION_KEY_USER, user.get());
}
return user;
}
/**
* try to log in by checking if the user has an active session
*/
public Optional<User> login(HttpSession session) {
if (session != null) {
User user = (User) session.getAttribute(SESSION_KEY_USER);
if (user != null) {
afterLogin(user);
return Optional.of(user);
}
}
return Optional.absent();
}
/**
* try to log in with given api key
*/
2014-08-09 15:25:41 +02:00
public Optional<User> login(String apiKey) {
2014-08-08 21:57:16 +02:00
if (apiKey == null) {
2014-08-09 15:25:41 +02:00
return Optional.absent();
2014-08-08 21:57:16 +02:00
}
User user = userDAO.findByApiKey(apiKey);
if (user != null && !user.isDisabled()) {
afterLogin(user);
2014-08-09 15:25:41 +02:00
return Optional.fromNullable(user);
2014-08-08 21:57:16 +02:00
}
2014-08-09 15:25:41 +02:00
return Optional.absent();
2013-03-20 20:33:42 +01:00
}
2013-03-30 09:22:49 +01:00
/**
* should triggers after successful login
*
2014-10-08 21:03:53 -04:00
* Note: Visibility changed to package private to enabled spying on this method
*/
2014-10-08 21:03:53 -04:00
void afterLogin(User user) {
new PostLoginActivities(userDAO, feedSubscriptionService, config).afterLogin(user);
}
2013-07-25 09:17:33 +02:00
public User register(String name, String password, String email, Collection<Role> roles) {
2013-06-18 12:31:09 +02:00
return register(name, password, email, roles, false);
2013-04-06 21:38:18 +02:00
}
2013-07-25 09:17:33 +02:00
public User register(String name, String password, String email, Collection<Role> roles, boolean forceRegistration) {
2013-06-18 12:31:09 +02:00
Preconditions.checkNotNull(name);
2013-07-25 09:17:33 +02:00
Preconditions.checkArgument(StringUtils.length(name) <= 32, "Name too long (32 characters maximum)");
Preconditions.checkNotNull(password);
if (!forceRegistration) {
Preconditions.checkState(config.getApplicationSettings().isAllowRegistrations(),
"Registrations are closed on this CommaFeed instance");
Preconditions.checkNotNull(email);
2013-07-25 09:17:33 +02:00
Preconditions.checkArgument(StringUtils.length(name) >= 3, "Name too short (3 characters minimum)");
Preconditions
.checkArgument(forceRegistration || StringUtils.length(password) >= 6, "Password too short (6 characters maximum)");
Preconditions.checkArgument(StringUtils.contains(email, "@"), "Invalid email address");
}
2013-07-25 09:17:33 +02:00
Preconditions.checkArgument(userDAO.findByName(name) == null, "Name already taken");
if (StringUtils.isNotBlank(email)) {
2013-07-25 09:17:33 +02:00
Preconditions.checkArgument(userDAO.findByEmail(email) == null, "Email already taken");
}
2013-06-18 12:31:09 +02:00
2013-03-30 09:22:49 +01:00
User user = new User();
byte[] salt = encryptionService.generateSalt();
user.setName(name);
2013-04-06 21:38:18 +02:00
user.setEmail(email);
user.setCreated(new Date());
2013-03-30 09:22:49 +01:00
user.setSalt(salt);
user.setPassword(encryptionService.getEncryptedPassword(password, salt));
2013-03-30 19:06:32 +01:00
for (Role role : roles) {
2013-03-30 09:22:49 +01:00
user.getRoles().add(new UserRole(user, role));
}
2013-06-06 09:54:17 +02:00
userDAO.saveOrUpdate(user);
2013-03-30 09:22:49 +01:00
return user;
}
public void unregister(User user) {
feedCategoryDAO.delete(feedCategoryDAO.findAll(user));
userSettingsDAO.delete(userSettingsDAO.findByUser(user));
userDAO.delete(user);
}
2013-05-20 21:53:13 +02:00
public String generateApiKey(User user) {
2013-07-25 09:17:33 +02:00
byte[] key = encryptionService.getEncryptedPassword(UUID.randomUUID().toString(), user.getSalt());
2013-05-20 21:53:13 +02:00
return DigestUtils.sha1Hex(key);
}
2013-03-20 20:33:42 +01:00
}