forked from Archives/Athou_commafeed
move logic to user service
This commit is contained in:
@@ -3,10 +3,13 @@ package com.commafeed.backend.services;
|
||||
import java.util.Collection;
|
||||
import java.util.Date;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import javax.ejb.Stateless;
|
||||
import javax.inject.Inject;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import org.apache.commons.codec.digest.DigestUtils;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.apache.commons.lang.time.DateUtils;
|
||||
@@ -21,6 +24,7 @@ import com.commafeed.backend.model.UserRole.Role;
|
||||
import com.google.common.base.Preconditions;
|
||||
|
||||
@Stateless
|
||||
@Slf4j
|
||||
public class UserService {
|
||||
|
||||
@Inject
|
||||
@@ -41,6 +45,9 @@ public class UserService {
|
||||
@Inject
|
||||
ApplicationSettingsService applicationSettingsService;
|
||||
|
||||
@Inject
|
||||
FeedSubscriptionService feedSubscriptionService;
|
||||
|
||||
public User login(String name, String password) {
|
||||
if (name == null || password == null) {
|
||||
return null;
|
||||
@@ -52,10 +59,20 @@ public class UserService {
|
||||
if (authenticated) {
|
||||
Date lastLogin = user.getLastLogin();
|
||||
Date now = new Date();
|
||||
|
||||
boolean saveUser = false;
|
||||
// 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);
|
||||
saveUser = true;
|
||||
}
|
||||
if (user.getLastFullRefresh() == null || user.getLastFullRefresh().before(DateUtils.addMinutes(now, -30))) {
|
||||
user.setLastFullRefresh(now);
|
||||
saveUser = true;
|
||||
feedSubscriptionService.refreshAll(user);
|
||||
}
|
||||
if (saveUser) {
|
||||
userDAO.saveOrUpdate(user);
|
||||
}
|
||||
return user;
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
package com.commafeed.frontend.rest.resources;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Date;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.inject.Inject;
|
||||
@@ -18,8 +16,6 @@ import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.Response;
|
||||
import javax.ws.rs.core.Response.Status;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import org.apache.wicket.ThreadContext;
|
||||
import org.apache.wicket.authentication.IAuthenticationStrategy;
|
||||
import org.apache.wicket.authroles.authorization.strategies.role.Roles;
|
||||
@@ -32,7 +28,6 @@ import com.codahale.metrics.MetricRegistry;
|
||||
import com.commafeed.backend.dao.UserDAO;
|
||||
import com.commafeed.backend.model.User;
|
||||
import com.commafeed.backend.model.UserRole.Role;
|
||||
import com.commafeed.backend.services.FeedSubscriptionService;
|
||||
import com.commafeed.frontend.CommaFeedApplication;
|
||||
import com.commafeed.frontend.CommaFeedSession;
|
||||
import com.commafeed.frontend.SecurityCheck;
|
||||
@@ -40,7 +35,6 @@ import com.commafeed.frontend.SecurityCheck;
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
@SecurityCheck(Role.USER)
|
||||
@Slf4j
|
||||
public abstract class AbstractREST {
|
||||
|
||||
@Context
|
||||
@@ -55,9 +49,6 @@ public abstract class AbstractREST {
|
||||
@Inject
|
||||
private UserDAO userDAO;
|
||||
|
||||
@Inject
|
||||
FeedSubscriptionService feedSubscriptionService;
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
CommaFeedApplication app = CommaFeedApplication.get();
|
||||
@@ -107,8 +98,6 @@ public abstract class AbstractREST {
|
||||
|
||||
@AroundInvoke
|
||||
public Object intercept(InvocationContext context) throws Exception {
|
||||
startFullRefresh(getUser());
|
||||
|
||||
Method method = context.getMethod();
|
||||
|
||||
// check security
|
||||
@@ -148,19 +137,6 @@ public abstract class AbstractREST {
|
||||
return result;
|
||||
}
|
||||
|
||||
private void startFullRefresh(User user) {
|
||||
if (user == null)
|
||||
return;
|
||||
|
||||
Date now = new Date();
|
||||
if (user.getLastFullRefresh() == null || (now.getTime() - user.getLastFullRefresh().getTime()) > TimeUnit.MINUTES.toMillis(30)) {
|
||||
log.info("Starting full refresh for {}", user.getName());
|
||||
user.setLastFullRefresh(now);
|
||||
userDAO.saveOrUpdate(user);
|
||||
feedSubscriptionService.refreshAll(user);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean checkRole(Role requiredRole) {
|
||||
if (requiredRole == Role.NONE) {
|
||||
return true;
|
||||
|
||||
Reference in New Issue
Block a user