2013-04-11 20:49:08 +02:00
|
|
|
package com.commafeed.backend.services;
|
2013-03-20 20:33:42 +01:00
|
|
|
|
2013-04-18 16:58:56 +02:00
|
|
|
import java.util.Calendar;
|
2013-03-30 09:22:49 +01:00
|
|
|
import java.util.Collection;
|
2013-05-20 21:53:13 +02:00
|
|
|
import java.util.UUID;
|
2013-03-20 20:33:42 +01:00
|
|
|
|
2013-03-22 19:43:19 +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-05-08 11:15:50 +02:00
|
|
|
import com.commafeed.backend.dao.FeedCategoryDAO;
|
|
|
|
|
import com.commafeed.backend.dao.FeedEntryStatusDAO;
|
|
|
|
|
import com.commafeed.backend.dao.FeedSubscriptionDAO;
|
2013-04-11 20:49:08 +02:00
|
|
|
import com.commafeed.backend.dao.UserDAO;
|
2013-05-08 11:15:50 +02:00
|
|
|
import com.commafeed.backend.dao.UserRoleDAO;
|
|
|
|
|
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;
|
2013-05-08 11:15:50 +02:00
|
|
|
import com.commafeed.backend.model.UserSettings.ReadingOrder;
|
2013-04-23 09:06:35 +02:00
|
|
|
import com.google.common.base.Preconditions;
|
2013-03-20 20:33:42 +01:00
|
|
|
|
2013-03-22 19:43:19 +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
|
|
|
|
2013-05-08 11:15:50 +02:00
|
|
|
@Inject
|
|
|
|
|
FeedEntryStatusDAO feedEntryStatusDAO;
|
|
|
|
|
|
|
|
|
|
@Inject
|
|
|
|
|
FeedSubscriptionDAO feedSubscriptionDAO;
|
|
|
|
|
|
|
|
|
|
@Inject
|
|
|
|
|
FeedCategoryDAO feedCategoryDAO;
|
|
|
|
|
|
|
|
|
|
@Inject
|
|
|
|
|
UserSettingsDAO userSettingsDAO;
|
|
|
|
|
|
|
|
|
|
@Inject
|
|
|
|
|
UserRoleDAO userRoleDAO;
|
|
|
|
|
|
2013-04-11 20:49:08 +02:00
|
|
|
@Inject
|
|
|
|
|
PasswordEncryptionService encryptionService;
|
2013-04-06 17:10:38 +02:00
|
|
|
|
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-23 09:06:35 +02:00
|
|
|
|
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-04-18 16:58:56 +02:00
|
|
|
user.setLastLogin(Calendar.getInstance().getTime());
|
|
|
|
|
userDAO.update(user);
|
2013-03-20 20:33:42 +01:00
|
|
|
return user;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2013-03-30 09:22:49 +01:00
|
|
|
|
2013-03-30 19:06:32 +01:00
|
|
|
public User register(String name, String password, Collection<Role> roles) {
|
2013-04-06 21:38:18 +02:00
|
|
|
return register(name, password, null, roles);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public User register(String name, String password, String email,
|
|
|
|
|
Collection<Role> roles) {
|
2013-04-23 09:06:35 +02:00
|
|
|
Preconditions.checkNotNull(name);
|
|
|
|
|
Preconditions.checkNotNull(password);
|
|
|
|
|
|
2013-04-11 20:49:08 +02:00
|
|
|
if (userDAO.findByName(name) != null) {
|
2013-03-30 09:22:49 +01:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
User user = new User();
|
|
|
|
|
byte[] salt = encryptionService.generateSalt();
|
|
|
|
|
user.setName(name);
|
2013-04-06 21:38:18 +02:00
|
|
|
user.setEmail(email);
|
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-04-11 20:49:08 +02:00
|
|
|
userDAO.save(user);
|
2013-03-30 09:22:49 +01:00
|
|
|
return user;
|
|
|
|
|
}
|
2013-05-08 11:15:50 +02:00
|
|
|
|
|
|
|
|
public void unregister(User user) {
|
|
|
|
|
feedEntryStatusDAO.delete(feedEntryStatusDAO.findAll(user, false,
|
|
|
|
|
ReadingOrder.desc, false));
|
|
|
|
|
feedSubscriptionDAO.delete(feedSubscriptionDAO.findAll(user));
|
|
|
|
|
feedCategoryDAO.delete(feedCategoryDAO.findAll(user));
|
|
|
|
|
userSettingsDAO.delete(userSettingsDAO.findByUser(user));
|
|
|
|
|
userRoleDAO.delete(userRoleDAO.findAll(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
|
|
|
}
|