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

130 lines
3.8 KiB
Java
Raw Normal View History

2013-04-11 20:49:08 +02:00
package com.commafeed.backend.services;
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
import javax.ejb.Stateless;
2013-03-20 20:33:42 +01:00
import javax.inject.Inject;
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-06-27 11:10:08 +02:00
import org.apache.commons.lang.time.DateUtils;
2013-05-20 21:53:13 +02:00
import com.commafeed.backend.dao.FeedCategoryDAO;
import com.commafeed.backend.dao.FeedEntryStatusDAO;
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.google.common.base.Preconditions;
2013-03-20 20:33:42 +01:00
@Stateless
2013-04-11 20:49:08 +02:00
public class UserService {
2013-03-20 20:33:42 +01:00
@Inject
2013-04-11 20:49:08 +02:00
UserDAO userDAO;
2013-04-06 17:10:38 +02:00
@Inject
FeedEntryStatusDAO feedEntryStatusDAO;
@Inject
FeedCategoryDAO feedCategoryDAO;
@Inject
UserSettingsDAO userSettingsDAO;
2013-04-11 20:49:08 +02:00
@Inject
PasswordEncryptionService encryptionService;
2013-04-06 17:10:38 +02:00
2013-06-18 12:31:09 +02:00
@Inject
ApplicationSettingsService applicationSettingsService;
2013-03-20 20:33:42 +01:00
public User login(String name, String password) {
2013-05-24 09:21:20 +02:00
if (name == null || password == null) {
return null;
}
2013-04-11 20:49:08 +02:00
User user = userDAO.findByName(name);
2013-03-29 12:59:21 +01:00
if (user != null && !user.isDisabled()) {
2013-03-20 20:33:42 +01:00
boolean authenticated = encryptionService.authenticate(password,
user.getPassword(), user.getSalt());
if (authenticated) {
2013-06-27 11:10:08 +02:00
Date lastLogin = user.getLastLogin();
Date now = new Date();
// only update lastLogin field every hour in order to not
// invalidate the cache everytime someone logs in
if (lastLogin == null
|| lastLogin.before(DateUtils.addHours(now, -1))) {
user.setLastLogin(now);
userDAO.saveOrUpdate(user);
}
2013-03-20 20:33:42 +01:00
return user;
}
}
return null;
}
2013-03-30 09:22:49 +01:00
2013-06-18 12:31:09 +02:00
public User register(String name, String password, String email,
Collection<Role> roles) {
return register(name, password, email, roles, false);
2013-04-06 21:38:18 +02:00
}
public User register(String name, String password, String email,
2013-06-18 12:31:09 +02:00
Collection<Role> roles, boolean forceRegistration) {
Preconditions.checkNotNull(name);
2013-06-18 12:31:09 +02:00
Preconditions.checkArgument(StringUtils.length(name) <= 32,
"Name too long (32 characters maximum)");
Preconditions.checkNotNull(password);
if (!forceRegistration) {
Preconditions.checkState(applicationSettingsService.get()
.isAllowRegistrations(),
"Registrations are closed on this CommaFeed instance");
Preconditions.checkNotNull(email);
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-06-18 12:31:09 +02:00
Preconditions.checkArgument(userDAO.findByName(name) == null,
"Name already taken");
if (StringUtils.isNotBlank(email)) {
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) {
byte[] key = encryptionService.getEncryptedPassword(UUID.randomUUID()
.toString(), user.getSalt());
return DigestUtils.sha1Hex(key);
}
2013-03-20 20:33:42 +01:00
}