mirror of
https://github.com/Athou/commafeed.git
synced 2026-03-21 21:37:29 +00:00
hide session management inside UserService
This commit is contained in:
@@ -100,8 +100,6 @@ public class CommaFeedApplication extends Application<CommaFeedConfiguration> {
|
|||||||
public static final String USERNAME_ADMIN = "admin";
|
public static final String USERNAME_ADMIN = "admin";
|
||||||
public static final String USERNAME_DEMO = "demo";
|
public static final String USERNAME_DEMO = "demo";
|
||||||
|
|
||||||
public static final String SESSION_USER = "user";
|
|
||||||
|
|
||||||
public static final Date STARTUP_TIME = new Date();
|
public static final Date STARTUP_TIME = new Date();
|
||||||
|
|
||||||
private HibernateBundle<CommaFeedConfiguration> hibernateBundle;
|
private HibernateBundle<CommaFeedConfiguration> hibernateBundle;
|
||||||
@@ -221,9 +219,9 @@ public class CommaFeedApplication extends Application<CommaFeedConfiguration> {
|
|||||||
|
|
||||||
// Servlets
|
// Servlets
|
||||||
NextUnreadServlet nextUnreadServlet = new NextUnreadServlet(sessionFactory, feedSubscriptionDAO, feedEntryStatusDAO,
|
NextUnreadServlet nextUnreadServlet = new NextUnreadServlet(sessionFactory, feedSubscriptionDAO, feedEntryStatusDAO,
|
||||||
feedCategoryDAO, config);
|
feedCategoryDAO, userService, config);
|
||||||
LogoutServlet logoutServlet = new LogoutServlet(config);
|
LogoutServlet logoutServlet = new LogoutServlet(config);
|
||||||
CustomCssServlet customCssServlet = new CustomCssServlet(sessionFactory, userSettingsDAO);
|
CustomCssServlet customCssServlet = new CustomCssServlet(sessionFactory, userSettingsDAO, userService);
|
||||||
AnalyticsServlet analyticsServlet = new AnalyticsServlet(config);
|
AnalyticsServlet analyticsServlet = new AnalyticsServlet(config);
|
||||||
environment.servlets().addServlet("next", nextUnreadServlet).addMapping("/next");
|
environment.servlets().addServlet("next", nextUnreadServlet).addMapping("/next");
|
||||||
environment.servlets().addServlet("logout", logoutServlet).addMapping("/logout");
|
environment.servlets().addServlet("logout", logoutServlet).addMapping("/logout");
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ import java.util.Collection;
|
|||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpSession;
|
||||||
|
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
|
||||||
import org.apache.commons.codec.digest.DigestUtils;
|
import org.apache.commons.codec.digest.DigestUtils;
|
||||||
@@ -23,6 +25,8 @@ import com.google.common.base.Preconditions;
|
|||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class UserService {
|
public class UserService {
|
||||||
|
|
||||||
|
private static final String SESSION_KEY_USER = "user";
|
||||||
|
|
||||||
private final FeedCategoryDAO feedCategoryDAO;
|
private final FeedCategoryDAO feedCategoryDAO;
|
||||||
private final UserDAO userDAO;
|
private final UserDAO userDAO;
|
||||||
private final UserSettingsDAO userSettingsDAO;
|
private final UserSettingsDAO userSettingsDAO;
|
||||||
@@ -31,6 +35,9 @@ public class UserService {
|
|||||||
private final PasswordEncryptionService encryptionService;
|
private final PasswordEncryptionService encryptionService;
|
||||||
private final CommaFeedConfiguration config;
|
private final CommaFeedConfiguration config;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* try to log in with given credentials
|
||||||
|
*/
|
||||||
public Optional<User> login(String nameOrEmail, String password) {
|
public Optional<User> login(String nameOrEmail, String password) {
|
||||||
if (nameOrEmail == null || password == null) {
|
if (nameOrEmail == null || password == null) {
|
||||||
return Optional.absent();
|
return Optional.absent();
|
||||||
@@ -50,6 +57,32 @@ public class UserService {
|
|||||||
return Optional.absent();
|
return Optional.absent();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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) {
|
||||||
|
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
|
||||||
|
*/
|
||||||
public Optional<User> login(String apiKey) {
|
public Optional<User> login(String apiKey) {
|
||||||
if (apiKey == null) {
|
if (apiKey == null) {
|
||||||
return Optional.absent();
|
return Optional.absent();
|
||||||
@@ -63,7 +96,10 @@ public class UserService {
|
|||||||
return Optional.absent();
|
return Optional.absent();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void afterLogin(User user) {
|
/**
|
||||||
|
* should triggers after successful login
|
||||||
|
*/
|
||||||
|
private void afterLogin(User user) {
|
||||||
Date lastLogin = user.getLastLogin();
|
Date lastLogin = user.getLastLogin();
|
||||||
Date now = new Date();
|
Date now = new Date();
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package com.commafeed.frontend.auth;
|
package com.commafeed.frontend.auth;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpSession;
|
|
||||||
import javax.ws.rs.WebApplicationException;
|
import javax.ws.rs.WebApplicationException;
|
||||||
import javax.ws.rs.core.Context;
|
import javax.ws.rs.core.Context;
|
||||||
import javax.ws.rs.core.HttpHeaders;
|
import javax.ws.rs.core.HttpHeaders;
|
||||||
@@ -13,7 +12,6 @@ import lombok.RequiredArgsConstructor;
|
|||||||
import org.eclipse.jetty.util.B64Code;
|
import org.eclipse.jetty.util.B64Code;
|
||||||
import org.eclipse.jetty.util.StringUtil;
|
import org.eclipse.jetty.util.StringUtil;
|
||||||
|
|
||||||
import com.commafeed.CommaFeedApplication;
|
|
||||||
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.service.UserService;
|
import com.commafeed.backend.service.UserService;
|
||||||
@@ -56,7 +54,12 @@ public class SecurityCheckProvider implements InjectableProvider<SecurityCheck,
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (user.isPresent()) {
|
if (user.isPresent()) {
|
||||||
return user.get();
|
if (user.get().hasRole(role)) {
|
||||||
|
return user.get();
|
||||||
|
} else {
|
||||||
|
throw new WebApplicationException(Response.status(Response.Status.FORBIDDEN)
|
||||||
|
.entity("You don't have the required role to access this resource.").type(MediaType.TEXT_PLAIN_TYPE).build());
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new WebApplicationException(Response.status(Response.Status.UNAUTHORIZED)
|
throw new WebApplicationException(Response.status(Response.Status.UNAUTHORIZED)
|
||||||
.entity("Credentials are required to access this resource.").type(MediaType.TEXT_PLAIN_TYPE).build());
|
.entity("Credentials are required to access this resource.").type(MediaType.TEXT_PLAIN_TYPE).build());
|
||||||
@@ -64,15 +67,7 @@ public class SecurityCheckProvider implements InjectableProvider<SecurityCheck,
|
|||||||
}
|
}
|
||||||
|
|
||||||
private Optional<User> cookieSessionLogin() {
|
private Optional<User> cookieSessionLogin() {
|
||||||
HttpSession session = request.getSession(false);
|
return userService.login(request.getSession());
|
||||||
if (session != null) {
|
|
||||||
User user = (User) session.getAttribute(CommaFeedApplication.SESSION_USER);
|
|
||||||
if (user != null) {
|
|
||||||
userService.afterLogin(user);
|
|
||||||
return Optional.of(user);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return Optional.absent();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Optional<User> basicAuthenticationLogin(HttpContext c) {
|
private Optional<User> basicAuthenticationLogin(HttpContext c) {
|
||||||
@@ -87,10 +82,7 @@ public class SecurityCheckProvider implements InjectableProvider<SecurityCheck,
|
|||||||
if (i > 0) {
|
if (i > 0) {
|
||||||
String username = decoded.substring(0, i);
|
String username = decoded.substring(0, i);
|
||||||
String password = decoded.substring(i + 1);
|
String password = decoded.substring(i + 1);
|
||||||
Optional<User> user = userService.login(username, password);
|
return userService.login(username, password);
|
||||||
if (user.isPresent() && user.get().hasRole(role)) {
|
|
||||||
return user;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -101,10 +93,7 @@ public class SecurityCheckProvider implements InjectableProvider<SecurityCheck,
|
|||||||
private Optional<User> apiKeyLogin(HttpContext c) {
|
private Optional<User> apiKeyLogin(HttpContext c) {
|
||||||
String apiKey = c.getUriInfo().getPathParameters().getFirst("apiKey");
|
String apiKey = c.getUriInfo().getPathParameters().getFirst("apiKey");
|
||||||
if (apiKey != null && apiKeyAllowed) {
|
if (apiKey != null && apiKeyAllowed) {
|
||||||
Optional<User> user = userService.login(apiKey);
|
return userService.login(apiKey);
|
||||||
if (user.isPresent() && user.get().hasRole(role)) {
|
|
||||||
return user;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return Optional.absent();
|
return Optional.absent();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -239,9 +239,8 @@ public class UserREST {
|
|||||||
@UnitOfWork
|
@UnitOfWork
|
||||||
@ApiOperation(value = "Login and create a session")
|
@ApiOperation(value = "Login and create a session")
|
||||||
public Response login(@ApiParam(required = true) LoginRequest req, @Session HttpSession session) {
|
public Response login(@ApiParam(required = true) LoginRequest req, @Session HttpSession session) {
|
||||||
Optional<User> user = userService.login(req.getName(), req.getPassword());
|
Optional<User> user = userService.login(req.getName(), req.getPassword(), session);
|
||||||
if (user.isPresent()) {
|
if (user.isPresent()) {
|
||||||
session.setAttribute(CommaFeedApplication.SESSION_USER, user.get());
|
|
||||||
return Response.ok().build();
|
return Response.ok().build();
|
||||||
} else {
|
} else {
|
||||||
return Response.status(Response.Status.UNAUTHORIZED).entity("wrong username or password").build();
|
return Response.status(Response.Status.UNAUTHORIZED).entity("wrong username or password").build();
|
||||||
|
|||||||
@@ -11,11 +11,12 @@ import lombok.RequiredArgsConstructor;
|
|||||||
|
|
||||||
import org.hibernate.SessionFactory;
|
import org.hibernate.SessionFactory;
|
||||||
|
|
||||||
import com.commafeed.CommaFeedApplication;
|
|
||||||
import com.commafeed.backend.dao.UnitOfWork;
|
import com.commafeed.backend.dao.UnitOfWork;
|
||||||
import com.commafeed.backend.dao.UserSettingsDAO;
|
import com.commafeed.backend.dao.UserSettingsDAO;
|
||||||
import com.commafeed.backend.model.User;
|
import com.commafeed.backend.model.User;
|
||||||
import com.commafeed.backend.model.UserSettings;
|
import com.commafeed.backend.model.UserSettings;
|
||||||
|
import com.commafeed.backend.service.UserService;
|
||||||
|
import com.google.common.base.Optional;
|
||||||
|
|
||||||
@SuppressWarnings("serial")
|
@SuppressWarnings("serial")
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
@@ -23,20 +24,21 @@ public class CustomCssServlet extends HttpServlet {
|
|||||||
|
|
||||||
private final SessionFactory sessionFactory;
|
private final SessionFactory sessionFactory;
|
||||||
private final UserSettingsDAO userSettingsDAO;
|
private final UserSettingsDAO userSettingsDAO;
|
||||||
|
private final UserService userService;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||||
resp.setContentType("text/css");
|
resp.setContentType("text/css");
|
||||||
|
|
||||||
final User user = (User) req.getSession().getAttribute(CommaFeedApplication.SESSION_USER);
|
final Optional<User> user = userService.login(req.getSession());
|
||||||
if (user == null) {
|
if (!user.isPresent()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
UserSettings settings = new UnitOfWork<UserSettings>(sessionFactory) {
|
UserSettings settings = new UnitOfWork<UserSettings>(sessionFactory) {
|
||||||
@Override
|
@Override
|
||||||
protected UserSettings runInSession() {
|
protected UserSettings runInSession() {
|
||||||
return userSettingsDAO.findByUser(user);
|
return userSettingsDAO.findByUser(user.get());
|
||||||
}
|
}
|
||||||
}.run();
|
}.run();
|
||||||
|
|
||||||
|
|||||||
@@ -13,7 +13,6 @@ import lombok.RequiredArgsConstructor;
|
|||||||
import org.apache.commons.lang.StringUtils;
|
import org.apache.commons.lang.StringUtils;
|
||||||
import org.hibernate.SessionFactory;
|
import org.hibernate.SessionFactory;
|
||||||
|
|
||||||
import com.commafeed.CommaFeedApplication;
|
|
||||||
import com.commafeed.CommaFeedConfiguration;
|
import com.commafeed.CommaFeedConfiguration;
|
||||||
import com.commafeed.backend.dao.FeedCategoryDAO;
|
import com.commafeed.backend.dao.FeedCategoryDAO;
|
||||||
import com.commafeed.backend.dao.FeedEntryStatusDAO;
|
import com.commafeed.backend.dao.FeedEntryStatusDAO;
|
||||||
@@ -24,7 +23,9 @@ import com.commafeed.backend.model.FeedEntryStatus;
|
|||||||
import com.commafeed.backend.model.FeedSubscription;
|
import com.commafeed.backend.model.FeedSubscription;
|
||||||
import com.commafeed.backend.model.User;
|
import com.commafeed.backend.model.User;
|
||||||
import com.commafeed.backend.model.UserSettings.ReadingOrder;
|
import com.commafeed.backend.model.UserSettings.ReadingOrder;
|
||||||
|
import com.commafeed.backend.service.UserService;
|
||||||
import com.commafeed.frontend.resource.CategoryREST;
|
import com.commafeed.frontend.resource.CategoryREST;
|
||||||
|
import com.google.common.base.Optional;
|
||||||
import com.google.common.collect.Iterables;
|
import com.google.common.collect.Iterables;
|
||||||
|
|
||||||
@SuppressWarnings("serial")
|
@SuppressWarnings("serial")
|
||||||
@@ -38,6 +39,7 @@ public class NextUnreadServlet extends HttpServlet {
|
|||||||
private final FeedSubscriptionDAO feedSubscriptionDAO;
|
private final FeedSubscriptionDAO feedSubscriptionDAO;
|
||||||
private final FeedEntryStatusDAO feedEntryStatusDAO;
|
private final FeedEntryStatusDAO feedEntryStatusDAO;
|
||||||
private final FeedCategoryDAO feedCategoryDAO;
|
private final FeedCategoryDAO feedCategoryDAO;
|
||||||
|
private final UserService userService;
|
||||||
private final CommaFeedConfiguration config;
|
private final CommaFeedConfiguration config;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -45,8 +47,8 @@ public class NextUnreadServlet extends HttpServlet {
|
|||||||
final String categoryId = req.getParameter(PARAM_CATEGORYID);
|
final String categoryId = req.getParameter(PARAM_CATEGORYID);
|
||||||
String orderParam = req.getParameter(PARAM_READINGORDER);
|
String orderParam = req.getParameter(PARAM_READINGORDER);
|
||||||
|
|
||||||
final User user = (User) req.getSession().getAttribute(CommaFeedApplication.SESSION_USER);
|
final Optional<User> user = userService.login(req.getSession());
|
||||||
if (user == null) {
|
if (!user.isPresent()) {
|
||||||
resp.sendRedirect(resp.encodeRedirectURL(config.getApplicationSettings().getPublicUrl()));
|
resp.sendRedirect(resp.encodeRedirectURL(config.getApplicationSettings().getPublicUrl()));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -58,17 +60,17 @@ public class NextUnreadServlet extends HttpServlet {
|
|||||||
protected FeedEntryStatus runInSession() throws Exception {
|
protected FeedEntryStatus runInSession() throws Exception {
|
||||||
FeedEntryStatus status = null;
|
FeedEntryStatus status = null;
|
||||||
if (StringUtils.isBlank(categoryId) || CategoryREST.ALL.equals(categoryId)) {
|
if (StringUtils.isBlank(categoryId) || CategoryREST.ALL.equals(categoryId)) {
|
||||||
List<FeedSubscription> subs = feedSubscriptionDAO.findAll(user);
|
List<FeedSubscription> subs = feedSubscriptionDAO.findAll(user.get());
|
||||||
List<FeedEntryStatus> statuses = feedEntryStatusDAO.findBySubscriptions(user, subs, true, null, null, 0, 1, order,
|
List<FeedEntryStatus> statuses = feedEntryStatusDAO.findBySubscriptions(user.get(), subs, true, null, null, 0, 1,
|
||||||
true, false, null);
|
order, true, false, null);
|
||||||
status = Iterables.getFirst(statuses, null);
|
status = Iterables.getFirst(statuses, null);
|
||||||
} else {
|
} else {
|
||||||
FeedCategory category = feedCategoryDAO.findById(user, Long.valueOf(categoryId));
|
FeedCategory category = feedCategoryDAO.findById(user.get(), Long.valueOf(categoryId));
|
||||||
if (category != null) {
|
if (category != null) {
|
||||||
List<FeedCategory> children = feedCategoryDAO.findAllChildrenCategories(user, category);
|
List<FeedCategory> children = feedCategoryDAO.findAllChildrenCategories(user.get(), category);
|
||||||
List<FeedSubscription> subscriptions = feedSubscriptionDAO.findByCategories(user, children);
|
List<FeedSubscription> subscriptions = feedSubscriptionDAO.findByCategories(user.get(), children);
|
||||||
List<FeedEntryStatus> statuses = feedEntryStatusDAO.findBySubscriptions(user, subscriptions, true, null, null, 0,
|
List<FeedEntryStatus> statuses = feedEntryStatusDAO.findBySubscriptions(user.get(), subscriptions, true, null,
|
||||||
1, order, true, false, null);
|
null, 0, 1, order, true, false, null);
|
||||||
status = Iterables.getFirst(statuses, null);
|
status = Iterables.getFirst(statuses, null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user