mirror of
https://github.com/Athou/commafeed.git
synced 2026-03-21 21:37:29 +00:00
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.Collection;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import javax.ejb.Stateless;
|
import javax.ejb.Stateless;
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
import org.apache.commons.codec.digest.DigestUtils;
|
import org.apache.commons.codec.digest.DigestUtils;
|
||||||
import org.apache.commons.lang.StringUtils;
|
import org.apache.commons.lang.StringUtils;
|
||||||
import org.apache.commons.lang.time.DateUtils;
|
import org.apache.commons.lang.time.DateUtils;
|
||||||
@@ -21,6 +24,7 @@ import com.commafeed.backend.model.UserRole.Role;
|
|||||||
import com.google.common.base.Preconditions;
|
import com.google.common.base.Preconditions;
|
||||||
|
|
||||||
@Stateless
|
@Stateless
|
||||||
|
@Slf4j
|
||||||
public class UserService {
|
public class UserService {
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
@@ -41,6 +45,9 @@ public class UserService {
|
|||||||
@Inject
|
@Inject
|
||||||
ApplicationSettingsService applicationSettingsService;
|
ApplicationSettingsService applicationSettingsService;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
FeedSubscriptionService feedSubscriptionService;
|
||||||
|
|
||||||
public User login(String name, String password) {
|
public User login(String name, String password) {
|
||||||
if (name == null || password == null) {
|
if (name == null || password == null) {
|
||||||
return null;
|
return null;
|
||||||
@@ -52,10 +59,20 @@ public class UserService {
|
|||||||
if (authenticated) {
|
if (authenticated) {
|
||||||
Date lastLogin = user.getLastLogin();
|
Date lastLogin = user.getLastLogin();
|
||||||
Date now = new Date();
|
Date now = new Date();
|
||||||
|
|
||||||
|
boolean saveUser = false;
|
||||||
// only update lastLogin field every hour in order to not
|
// only update lastLogin field every hour in order to not
|
||||||
// invalidate the cache everytime someone logs in
|
// invalidate the cache everytime someone logs in
|
||||||
if (lastLogin == null || lastLogin.before(DateUtils.addHours(now, -1))) {
|
if (lastLogin == null || lastLogin.before(DateUtils.addHours(now, -1))) {
|
||||||
user.setLastLogin(now);
|
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);
|
userDAO.saveOrUpdate(user);
|
||||||
}
|
}
|
||||||
return user;
|
return user;
|
||||||
|
|||||||
@@ -1,8 +1,6 @@
|
|||||||
package com.commafeed.frontend.rest.resources;
|
package com.commafeed.frontend.rest.resources;
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.util.Date;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
|
||||||
import javax.annotation.PostConstruct;
|
import javax.annotation.PostConstruct;
|
||||||
import javax.inject.Inject;
|
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;
|
||||||
import javax.ws.rs.core.Response.Status;
|
import javax.ws.rs.core.Response.Status;
|
||||||
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
|
|
||||||
import org.apache.wicket.ThreadContext;
|
import org.apache.wicket.ThreadContext;
|
||||||
import org.apache.wicket.authentication.IAuthenticationStrategy;
|
import org.apache.wicket.authentication.IAuthenticationStrategy;
|
||||||
import org.apache.wicket.authroles.authorization.strategies.role.Roles;
|
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.dao.UserDAO;
|
||||||
import com.commafeed.backend.model.User;
|
import com.commafeed.backend.model.User;
|
||||||
import com.commafeed.backend.model.UserRole.Role;
|
import com.commafeed.backend.model.UserRole.Role;
|
||||||
import com.commafeed.backend.services.FeedSubscriptionService;
|
|
||||||
import com.commafeed.frontend.CommaFeedApplication;
|
import com.commafeed.frontend.CommaFeedApplication;
|
||||||
import com.commafeed.frontend.CommaFeedSession;
|
import com.commafeed.frontend.CommaFeedSession;
|
||||||
import com.commafeed.frontend.SecurityCheck;
|
import com.commafeed.frontend.SecurityCheck;
|
||||||
@@ -40,7 +35,6 @@ import com.commafeed.frontend.SecurityCheck;
|
|||||||
@Produces(MediaType.APPLICATION_JSON)
|
@Produces(MediaType.APPLICATION_JSON)
|
||||||
@Consumes(MediaType.APPLICATION_JSON)
|
@Consumes(MediaType.APPLICATION_JSON)
|
||||||
@SecurityCheck(Role.USER)
|
@SecurityCheck(Role.USER)
|
||||||
@Slf4j
|
|
||||||
public abstract class AbstractREST {
|
public abstract class AbstractREST {
|
||||||
|
|
||||||
@Context
|
@Context
|
||||||
@@ -55,9 +49,6 @@ public abstract class AbstractREST {
|
|||||||
@Inject
|
@Inject
|
||||||
private UserDAO userDAO;
|
private UserDAO userDAO;
|
||||||
|
|
||||||
@Inject
|
|
||||||
FeedSubscriptionService feedSubscriptionService;
|
|
||||||
|
|
||||||
@PostConstruct
|
@PostConstruct
|
||||||
public void init() {
|
public void init() {
|
||||||
CommaFeedApplication app = CommaFeedApplication.get();
|
CommaFeedApplication app = CommaFeedApplication.get();
|
||||||
@@ -107,8 +98,6 @@ public abstract class AbstractREST {
|
|||||||
|
|
||||||
@AroundInvoke
|
@AroundInvoke
|
||||||
public Object intercept(InvocationContext context) throws Exception {
|
public Object intercept(InvocationContext context) throws Exception {
|
||||||
startFullRefresh(getUser());
|
|
||||||
|
|
||||||
Method method = context.getMethod();
|
Method method = context.getMethod();
|
||||||
|
|
||||||
// check security
|
// check security
|
||||||
@@ -148,19 +137,6 @@ public abstract class AbstractREST {
|
|||||||
return result;
|
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) {
|
private boolean checkRole(Role requiredRole) {
|
||||||
if (requiredRole == Role.NONE) {
|
if (requiredRole == Role.NONE) {
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
Reference in New Issue
Block a user