Commit e00248b9 authored by Liang Ding's avatar Liang Ding

重构请求路由 #61

parent 3dc658dc
...@@ -454,12 +454,12 @@ public final class Server extends BaseServer { ...@@ -454,12 +454,12 @@ public final class Server extends BaseServer {
private static void routeConsoleProcessors() { private static void routeConsoleProcessors() {
final BeanManager beanManager = BeanManager.getInstance(); final BeanManager beanManager = BeanManager.getInstance();
final ConsoleAuthAdvice consoleAuthAdvice = beanManager.getReference(ConsoleAuthAdvice.class); final ConsoleAuthMidware consoleAuthMidware = beanManager.getReference(ConsoleAuthMidware.class);
final ConsoleAdminAuthAdvice consoleAdminAuthAdvice = beanManager.getReference(ConsoleAdminAuthAdvice.class); final ConsoleAdminAuthMidware consoleAdminAuthMidware = beanManager.getReference(ConsoleAdminAuthMidware.class);
final AdminConsole adminConsole = beanManager.getReference(AdminConsole.class); final AdminConsole adminConsole = beanManager.getReference(AdminConsole.class);
final Dispatcher.RouterGroup adminConsoleGroup = Dispatcher.group(); final Dispatcher.RouterGroup adminConsoleGroup = Dispatcher.group();
adminConsoleGroup.middlewares(consoleAuthAdvice::handle); adminConsoleGroup.middlewares(consoleAuthMidware::handle);
adminConsoleGroup.get("/admin-index.do", adminConsole::showAdminIndex). adminConsoleGroup.get("/admin-index.do", adminConsole::showAdminIndex).
get("/admin-preference.do", adminConsole::showAdminPreferenceFunction). get("/admin-preference.do", adminConsole::showAdminPreferenceFunction).
get("/console/export/sql", adminConsole::exportSQL). get("/console/export/sql", adminConsole::exportSQL).
...@@ -482,7 +482,7 @@ public final class Server extends BaseServer { ...@@ -482,7 +482,7 @@ public final class Server extends BaseServer {
final ArticleConsole articleConsole = beanManager.getReference(ArticleConsole.class); final ArticleConsole articleConsole = beanManager.getReference(ArticleConsole.class);
final Dispatcher.RouterGroup articleConsoleGroup = Dispatcher.group(); final Dispatcher.RouterGroup articleConsoleGroup = Dispatcher.group();
articleConsoleGroup.middlewares(consoleAuthAdvice::handle); articleConsoleGroup.middlewares(consoleAuthMidware::handle);
articleConsoleGroup.get("/console/article/push2rhy", articleConsole::pushArticleToCommunity). articleConsoleGroup.get("/console/article/push2rhy", articleConsole::pushArticleToCommunity).
get("/console/thumbs", articleConsole::getArticleThumbs). get("/console/thumbs", articleConsole::getArticleThumbs).
get("/console/article/{id}", articleConsole::getArticle). get("/console/article/{id}", articleConsole::getArticle).
...@@ -496,20 +496,20 @@ public final class Server extends BaseServer { ...@@ -496,20 +496,20 @@ public final class Server extends BaseServer {
final CommentConsole commentConsole = beanManager.getReference(CommentConsole.class); final CommentConsole commentConsole = beanManager.getReference(CommentConsole.class);
final Dispatcher.RouterGroup commentConsoleGroup = Dispatcher.group(); final Dispatcher.RouterGroup commentConsoleGroup = Dispatcher.group();
commentConsoleGroup.middlewares(consoleAuthAdvice::handle); commentConsoleGroup.middlewares(consoleAuthMidware::handle);
commentConsoleGroup.delete("/console/article/comment/{id}", commentConsole::removeArticleComment). commentConsoleGroup.delete("/console/article/comment/{id}", commentConsole::removeArticleComment).
get("/console/comments/{page}/{pageSize}/{windowSize}", commentConsole::getComments). get("/console/comments/{page}/{pageSize}/{windowSize}", commentConsole::getComments).
get("/console/comments/article/{id}", commentConsole::getArticleComments); get("/console/comments/article/{id}", commentConsole::getArticleComments);
final TagConsole tagConsole = beanManager.getReference(TagConsole.class); final TagConsole tagConsole = beanManager.getReference(TagConsole.class);
final Dispatcher.RouterGroup tagConsoleGroup = Dispatcher.group(); final Dispatcher.RouterGroup tagConsoleGroup = Dispatcher.group();
tagConsoleGroup.middlewares(consoleAuthAdvice::handle); tagConsoleGroup.middlewares(consoleAuthMidware::handle);
tagConsoleGroup.get("/console/tags", tagConsole::getTags). tagConsoleGroup.get("/console/tags", tagConsole::getTags).
get("/console/tag/unused", tagConsole::getUnusedTags); get("/console/tag/unused", tagConsole::getUnusedTags);
final CategoryConsole categoryConsole = beanManager.getReference(CategoryConsole.class); final CategoryConsole categoryConsole = beanManager.getReference(CategoryConsole.class);
final Dispatcher.RouterGroup categoryGroup = Dispatcher.group(); final Dispatcher.RouterGroup categoryGroup = Dispatcher.group();
categoryGroup.middlewares(consoleAdminAuthAdvice::handle); categoryGroup.middlewares(consoleAdminAuthMidware::handle);
categoryGroup.put("/console/category/order/", categoryConsole::changeOrder). categoryGroup.put("/console/category/order/", categoryConsole::changeOrder).
get("/console/category/{id}", categoryConsole::getCategory). get("/console/category/{id}", categoryConsole::getCategory).
delete("/console/category/{id}", categoryConsole::removeCategory). delete("/console/category/{id}", categoryConsole::removeCategory).
...@@ -519,7 +519,7 @@ public final class Server extends BaseServer { ...@@ -519,7 +519,7 @@ public final class Server extends BaseServer {
final LinkConsole linkConsole = beanManager.getReference(LinkConsole.class); final LinkConsole linkConsole = beanManager.getReference(LinkConsole.class);
final Dispatcher.RouterGroup linkConsoleGroup = Dispatcher.group(); final Dispatcher.RouterGroup linkConsoleGroup = Dispatcher.group();
linkConsoleGroup.middlewares(consoleAdminAuthAdvice::handle); linkConsoleGroup.middlewares(consoleAdminAuthMidware::handle);
linkConsoleGroup.delete("/console/link/{id}", linkConsole::removeLink). linkConsoleGroup.delete("/console/link/{id}", linkConsole::removeLink).
put("/console/link/", linkConsole::updateLink). put("/console/link/", linkConsole::updateLink).
put("/console/link/order/", linkConsole::changeOrder). put("/console/link/order/", linkConsole::changeOrder).
...@@ -529,7 +529,7 @@ public final class Server extends BaseServer { ...@@ -529,7 +529,7 @@ public final class Server extends BaseServer {
final PageConsole pageConsole = beanManager.getReference(PageConsole.class); final PageConsole pageConsole = beanManager.getReference(PageConsole.class);
final Dispatcher.RouterGroup pageConsoleGroup = Dispatcher.group(); final Dispatcher.RouterGroup pageConsoleGroup = Dispatcher.group();
pageConsoleGroup.middlewares(consoleAdminAuthAdvice::handle); pageConsoleGroup.middlewares(consoleAdminAuthMidware::handle);
pageConsoleGroup.put("/console/page/", pageConsole::updatePage). pageConsoleGroup.put("/console/page/", pageConsole::updatePage).
delete("/console/page/{id}", pageConsole::removePage). delete("/console/page/{id}", pageConsole::removePage).
post("/console/page/", pageConsole::addPage). post("/console/page/", pageConsole::addPage).
...@@ -539,7 +539,7 @@ public final class Server extends BaseServer { ...@@ -539,7 +539,7 @@ public final class Server extends BaseServer {
final PluginConsole pluginConsole = beanManager.getReference(PluginConsole.class); final PluginConsole pluginConsole = beanManager.getReference(PluginConsole.class);
final Dispatcher.RouterGroup pluginConsoleGroup = Dispatcher.group(); final Dispatcher.RouterGroup pluginConsoleGroup = Dispatcher.group();
pluginConsoleGroup.middlewares(consoleAdminAuthAdvice::handle); pluginConsoleGroup.middlewares(consoleAdminAuthMidware::handle);
pluginConsoleGroup.put("/console/plugin/status/", pluginConsole::setPluginStatus). pluginConsoleGroup.put("/console/plugin/status/", pluginConsole::setPluginStatus).
get("/console/plugins/{page}/{pageSize}/{windowSize}", pluginConsole::getPlugins). get("/console/plugins/{page}/{pageSize}/{windowSize}", pluginConsole::getPlugins).
post("/console/plugin/toSetting", pluginConsole::toSetting). post("/console/plugin/toSetting", pluginConsole::toSetting).
...@@ -547,32 +547,32 @@ public final class Server extends BaseServer { ...@@ -547,32 +547,32 @@ public final class Server extends BaseServer {
final PreferenceConsole preferenceConsole = beanManager.getReference(PreferenceConsole.class); final PreferenceConsole preferenceConsole = beanManager.getReference(PreferenceConsole.class);
final Dispatcher.RouterGroup preferenceConsoleGroup = Dispatcher.group(); final Dispatcher.RouterGroup preferenceConsoleGroup = Dispatcher.group();
preferenceConsoleGroup.middlewares(consoleAdminAuthAdvice::handle); preferenceConsoleGroup.middlewares(consoleAdminAuthMidware::handle);
preferenceConsoleGroup.get("/console/signs/", preferenceConsole::getSigns). preferenceConsoleGroup.get("/console/signs/", preferenceConsole::getSigns).
get("/console/preference/", preferenceConsole::getPreference). get("/console/preference/", preferenceConsole::getPreference).
put("/console/preference/", preferenceConsole::updatePreference); put("/console/preference/", preferenceConsole::updatePreference);
final SkinConsole skinConsole = beanManager.getReference(SkinConsole.class); final SkinConsole skinConsole = beanManager.getReference(SkinConsole.class);
final Dispatcher.RouterGroup skinConsoleGroup = Dispatcher.group(); final Dispatcher.RouterGroup skinConsoleGroup = Dispatcher.group();
skinConsoleGroup.middlewares(consoleAdminAuthAdvice::handle); skinConsoleGroup.middlewares(consoleAdminAuthMidware::handle);
skinConsoleGroup.get("/console/skin", skinConsole::getSkin). skinConsoleGroup.get("/console/skin", skinConsole::getSkin).
put("/console/skin", skinConsole::updateSkin); put("/console/skin", skinConsole::updateSkin);
final RepairConsole repairConsole = beanManager.getReference(RepairConsole.class); final RepairConsole repairConsole = beanManager.getReference(RepairConsole.class);
final Dispatcher.RouterGroup repairConsoleGroup = Dispatcher.group(); final Dispatcher.RouterGroup repairConsoleGroup = Dispatcher.group();
repairConsoleGroup.middlewares(consoleAdminAuthAdvice::handle); repairConsoleGroup.middlewares(consoleAdminAuthMidware::handle);
repairConsoleGroup.get("/fix/restore-signs", repairConsole::restoreSigns). repairConsoleGroup.get("/fix/restore-signs", repairConsole::restoreSigns).
get("/fix/archivedate-articles", repairConsole::cleanArchiveDateArticles); get("/fix/archivedate-articles", repairConsole::cleanArchiveDateArticles);
final OtherConsole otherConsole = beanManager.getReference(OtherConsole.class); final OtherConsole otherConsole = beanManager.getReference(OtherConsole.class);
final Dispatcher.RouterGroup otherConsoleGroup = Dispatcher.group(); final Dispatcher.RouterGroup otherConsoleGroup = Dispatcher.group();
otherConsoleGroup.middlewares(consoleAdminAuthAdvice::handle); otherConsoleGroup.middlewares(consoleAdminAuthMidware::handle);
otherConsoleGroup.delete("/console/archive/unused", otherConsole::removeUnusedArchives). otherConsoleGroup.delete("/console/archive/unused", otherConsole::removeUnusedArchives).
delete("/console/tag/unused", otherConsole::removeUnusedTags); delete("/console/tag/unused", otherConsole::removeUnusedTags);
final UserConsole userConsole = beanManager.getReference(UserConsole.class); final UserConsole userConsole = beanManager.getReference(UserConsole.class);
final Dispatcher.RouterGroup userConsoleGroup = Dispatcher.group(); final Dispatcher.RouterGroup userConsoleGroup = Dispatcher.group();
userConsoleGroup.middlewares(consoleAdminAuthAdvice::handle); userConsoleGroup.middlewares(consoleAdminAuthMidware::handle);
userConsoleGroup.put("/console/user/", userConsole::updateUser). userConsoleGroup.put("/console/user/", userConsole::updateUser).
delete("/console/user/{id}", userConsole::removeUser). delete("/console/user/{id}", userConsole::removeUser).
get("/console/users/{page}/{pageSize}/{windowSize}", userConsole::getUsers). get("/console/users/{page}/{pageSize}/{windowSize}", userConsole::getUsers).
...@@ -581,7 +581,7 @@ public final class Server extends BaseServer { ...@@ -581,7 +581,7 @@ public final class Server extends BaseServer {
final StaticSiteConsole staticSiteConsole = beanManager.getReference(StaticSiteConsole.class); final StaticSiteConsole staticSiteConsole = beanManager.getReference(StaticSiteConsole.class);
final Dispatcher.RouterGroup staticSiteConsoleGroup = Dispatcher.group(); final Dispatcher.RouterGroup staticSiteConsoleGroup = Dispatcher.group();
staticSiteConsoleGroup.middlewares(consoleAdminAuthAdvice::handle); staticSiteConsoleGroup.middlewares(consoleAdminAuthMidware::handle);
staticSiteConsoleGroup.put("/console/staticsite", staticSiteConsole::genSite); staticSiteConsoleGroup.put("/console/staticsite", staticSiteConsole::genSite);
} }
......
...@@ -191,7 +191,7 @@ public class CommentProcessor { ...@@ -191,7 +191,7 @@ public class CommentProcessor {
* @param context the specified HTTP request context * @param context the specified HTTP request context
*/ */
private void fillCommenter(final JSONObject requestJSONObject, final RequestContext context) { private void fillCommenter(final JSONObject requestJSONObject, final RequestContext context) {
final JSONObject currentUser = Solos.getCurrentUser(context.getRequest(), context.getResponse()); final JSONObject currentUser = Solos.getCurrentUser(context);
if (null == currentUser) { if (null == currentUser) {
return; return;
} }
......
...@@ -163,6 +163,12 @@ public class IndexProcessor { ...@@ -163,6 +163,12 @@ public class IndexProcessor {
* @param context the specified context * @param context the specified context
*/ */
public void showStart(final RequestContext context) { public void showStart(final RequestContext context) {
if (initService.isInited() && null != Solos.getCurrentUser(context)) {
context.sendRedirect(Latkes.getServePath());
return;
}
String referer = context.param("referer"); String referer = context.param("referer");
if (StringUtils.isBlank(referer)) { if (StringUtils.isBlank(referer)) {
referer = context.header("referer"); referer = context.header("referer");
......
...@@ -118,7 +118,7 @@ public class AdminConsole { ...@@ -118,7 +118,7 @@ public class AdminConsole {
final Map<String, String> langs = langPropsService.getAll(Latkes.getLocale()); final Map<String, String> langs = langPropsService.getAll(Latkes.getLocale());
final Map<String, Object> dataModel = renderer.getDataModel(); final Map<String, Object> dataModel = renderer.getDataModel();
dataModel.putAll(langs); dataModel.putAll(langs);
final JSONObject currentUser = Solos.getCurrentUser(context.getRequest(), context.getResponse()); final JSONObject currentUser = Solos.getCurrentUser(context);
final String userName = currentUser.optString(User.USER_NAME); final String userName = currentUser.optString(User.USER_NAME);
dataModel.put(User.USER_NAME, userName); dataModel.put(User.USER_NAME, userName);
final String roleName = currentUser.optString(User.USER_ROLE); final String roleName = currentUser.optString(User.USER_ROLE);
......
...@@ -180,7 +180,7 @@ public class ArticleConsole { ...@@ -180,7 +180,7 @@ public class ArticleConsole {
context.setRenderer(renderer); context.setRenderer(renderer);
try { try {
final String articleId = context.pathVar("id"); final String articleId = context.pathVar("id");
final JSONObject currentUser = Solos.getCurrentUser(context.getRequest(), context.getResponse()); final JSONObject currentUser = Solos.getCurrentUser(context);
if (!articleQueryService.canAccessArticle(articleId, currentUser)) { if (!articleQueryService.canAccessArticle(articleId, currentUser)) {
final JSONObject ret = new JSONObject(); final JSONObject ret = new JSONObject();
renderer.setJSONObject(ret); renderer.setJSONObject(ret);
...@@ -298,7 +298,7 @@ public class ArticleConsole { ...@@ -298,7 +298,7 @@ public class ArticleConsole {
final JSONObject ret = new JSONObject(); final JSONObject ret = new JSONObject();
renderer.setJSONObject(ret); renderer.setJSONObject(ret);
final String articleId = context.pathVar("id"); final String articleId = context.pathVar("id");
final JSONObject currentUser = Solos.getCurrentUser(context.getRequest(), context.getResponse()); final JSONObject currentUser = Solos.getCurrentUser(context);
try { try {
if (!articleQueryService.canAccessArticle(articleId, currentUser)) { if (!articleQueryService.canAccessArticle(articleId, currentUser)) {
...@@ -344,7 +344,7 @@ public class ArticleConsole { ...@@ -344,7 +344,7 @@ public class ArticleConsole {
try { try {
final String articleId = context.pathVar("id"); final String articleId = context.pathVar("id");
final JSONObject currentUser = Solos.getCurrentUser(context.getRequest(), context.getResponse()); final JSONObject currentUser = Solos.getCurrentUser(context);
if (!articleQueryService.canAccessArticle(articleId, currentUser)) { if (!articleQueryService.canAccessArticle(articleId, currentUser)) {
ret.put(Keys.STATUS_CODE, false); ret.put(Keys.STATUS_CODE, false);
ret.put(Keys.MSG, langPropsService.get("forbiddenLabel")); ret.put(Keys.MSG, langPropsService.get("forbiddenLabel"));
...@@ -492,7 +492,7 @@ public class ArticleConsole { ...@@ -492,7 +492,7 @@ public class ArticleConsole {
final String articleId = article.getString(Keys.OBJECT_ID); final String articleId = article.getString(Keys.OBJECT_ID);
renderer.setJSONObject(ret); renderer.setJSONObject(ret);
final JSONObject currentUser = Solos.getCurrentUser(context.getRequest(), context.getResponse()); final JSONObject currentUser = Solos.getCurrentUser(context);
if (!articleQueryService.canAccessArticle(articleId, currentUser)) { if (!articleQueryService.canAccessArticle(articleId, currentUser)) {
ret.put(Keys.MSG, langPropsService.get("forbiddenLabel")); ret.put(Keys.MSG, langPropsService.get("forbiddenLabel"));
ret.put(Keys.STATUS_CODE, false); ret.put(Keys.STATUS_CODE, false);
...@@ -554,7 +554,7 @@ public class ArticleConsole { ...@@ -554,7 +554,7 @@ public class ArticleConsole {
final JSONObject ret = new JSONObject(); final JSONObject ret = new JSONObject();
try { try {
final JSONObject requestJSONObject = context.requestJSON(); final JSONObject requestJSONObject = context.requestJSON();
final JSONObject currentUser = Solos.getCurrentUser(context.getRequest(), context.getResponse()); final JSONObject currentUser = Solos.getCurrentUser(context);
requestJSONObject.getJSONObject(Article.ARTICLE).put(Article.ARTICLE_AUTHOR_ID, currentUser.getString(Keys.OBJECT_ID)); requestJSONObject.getJSONObject(Article.ARTICLE).put(Article.ARTICLE_AUTHOR_ID, currentUser.getString(Keys.OBJECT_ID));
// 打印请求日志,如果发生特殊情况丢失数据,至少还可以根据日志寻回内容 // 打印请求日志,如果发生特殊情况丢失数据,至少还可以根据日志寻回内容
......
...@@ -90,7 +90,7 @@ public class CommentConsole { ...@@ -90,7 +90,7 @@ public class CommentConsole {
try { try {
final String commentId = context.pathVar("id"); final String commentId = context.pathVar("id");
final JSONObject currentUser = Solos.getCurrentUser(context.getRequest(), context.getResponse()); final JSONObject currentUser = Solos.getCurrentUser(context);
if (!commentQueryService.canAccessComment(commentId, currentUser)) { if (!commentQueryService.canAccessComment(commentId, currentUser)) {
ret.put(Keys.STATUS_CODE, false); ret.put(Keys.STATUS_CODE, false);
ret.put(Keys.MSG, langPropsService.get("forbiddenLabel")); ret.put(Keys.MSG, langPropsService.get("forbiddenLabel"));
......
...@@ -22,19 +22,21 @@ import org.b3log.latke.ioc.Singleton; ...@@ -22,19 +22,21 @@ import org.b3log.latke.ioc.Singleton;
import org.b3log.solo.util.Solos; import org.b3log.solo.util.Solos;
/** /**
* The common auth check before advice for admin console. * The common auth check middleware for admin console.
* *
* @author <a href="http://88250.b3log.org">Liang Ding</a> * @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 2.0.0.0, Feb 9, 2020 * @version 2.0.0.0, Feb 9, 2020
* @since 2.9.5 * @since 2.9.5
*/ */
@Singleton @Singleton
public class ConsoleAdminAuthAdvice { public class ConsoleAdminAuthMidware {
public void handle(final RequestContext context) { public void handle(final RequestContext context) {
if (!Solos.isAdminLoggedIn(context)) { if (!Solos.isAdminLoggedIn(context)) {
context.sendError(401); context.sendError(401);
context.abort(); context.abort();
return;
} }
context.handle(); context.handle();
......
...@@ -25,20 +25,22 @@ import org.b3log.solo.util.Solos; ...@@ -25,20 +25,22 @@ import org.b3log.solo.util.Solos;
import org.json.JSONObject; import org.json.JSONObject;
/** /**
* The common auth check before advice for admin console. * The common auth check middleware for admin console.
* *
* @author <a href="http://88250.b3log.org">Liang Ding</a> * @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 2.0.0.0, Feb 9, 2020 * @version 2.0.0.0, Feb 9, 2020
* @since 2.9.5 * @since 2.9.5
*/ */
@Singleton @Singleton
public class ConsoleAuthAdvice { public class ConsoleAuthMidware {
public void handle(final RequestContext context) { public void handle(final RequestContext context) {
final JSONObject currentUser = Solos.getCurrentUser(context.getRequest(), context.getResponse()); final JSONObject currentUser = Solos.getCurrentUser(context);
if (null == currentUser) { if (null == currentUser) {
context.sendError(401); context.sendError(401);
context.abort(); context.abort();
return;
} }
final String userRole = currentUser.optString(User.USER_ROLE); final String userRole = currentUser.optString(User.USER_ROLE);
......
...@@ -565,7 +565,7 @@ public class DataModelService { ...@@ -565,7 +565,7 @@ public class DataModelService {
dataModel.put(Keys.Server.SERVER, Latkes.getServer()); dataModel.put(Keys.Server.SERVER, Latkes.getServer());
dataModel.put(Common.IS_INDEX, "/".equals(context.requestURI())); dataModel.put(Common.IS_INDEX, "/".equals(context.requestURI()));
dataModel.put(User.USER_NAME, ""); dataModel.put(User.USER_NAME, "");
final JSONObject currentUser = Solos.getCurrentUser(context.getRequest(), context.getResponse()); final JSONObject currentUser = Solos.getCurrentUser(context);
if (null != currentUser) { if (null != currentUser) {
final String userAvatar = currentUser.optString(UserExt.USER_AVATAR); final String userAvatar = currentUser.optString(UserExt.USER_AVATAR);
dataModel.put(Common.GRAVATAR, userAvatar); dataModel.put(Common.GRAVATAR, userAvatar);
...@@ -625,7 +625,7 @@ public class DataModelService { ...@@ -625,7 +625,7 @@ public class DataModelService {
} }
dataModel.put(Option.ID_C_META_DESCRIPTION, metaDescription); dataModel.put(Option.ID_C_META_DESCRIPTION, metaDescription);
dataModel.put(Common.YEAR, String.valueOf(Calendar.getInstance().get(Calendar.YEAR))); dataModel.put(Common.YEAR, String.valueOf(Calendar.getInstance().get(Calendar.YEAR)));
dataModel.put(Common.IS_LOGGED_IN, null != Solos.getCurrentUser(context.getRequest(), context.getResponse())); dataModel.put(Common.IS_LOGGED_IN, null != Solos.getCurrentUser(context));
dataModel.put(Common.FAVICON_API, Solos.FAVICON_API); dataModel.put(Common.FAVICON_API, Solos.FAVICON_API);
final String noticeBoard = preference.getString(Option.ID_C_NOTICE_BOARD); final String noticeBoard = preference.getString(Option.ID_C_NOTICE_BOARD);
dataModel.put(Option.ID_C_NOTICE_BOARD, noticeBoard); dataModel.put(Option.ID_C_NOTICE_BOARD, noticeBoard);
...@@ -956,7 +956,7 @@ public class DataModelService { ...@@ -956,7 +956,7 @@ public class DataModelService {
final Template topBarTemplate = Skins.getTemplate("common-template/top-bar.ftl"); final Template topBarTemplate = Skins.getTemplate("common-template/top-bar.ftl");
final StringWriter stringWriter = new StringWriter(); final StringWriter stringWriter = new StringWriter();
final Map<String, Object> topBarModel = new HashMap<>(); final Map<String, Object> topBarModel = new HashMap<>();
final JSONObject currentUser = Solos.getCurrentUser(context.getRequest(), context.getResponse()); final JSONObject currentUser = Solos.getCurrentUser(context);
Keys.fillServer(topBarModel); Keys.fillServer(topBarModel);
topBarModel.put(Common.IS_LOGGED_IN, false); topBarModel.put(Common.IS_LOGGED_IN, false);
......
...@@ -188,7 +188,7 @@ public final class Solos { ...@@ -188,7 +188,7 @@ public final class Solos {
*/ */
public static JSONObject getUploadToken(final RequestContext context) { public static JSONObject getUploadToken(final RequestContext context) {
try { try {
final JSONObject currentUser = getCurrentUser(context.getRequest(), context.getResponse()); final JSONObject currentUser = getCurrentUser(context);
if (null == currentUser) { if (null == currentUser) {
return null; return null;
} }
...@@ -275,16 +275,17 @@ public final class Solos { ...@@ -275,16 +275,17 @@ public final class Solos {
/** /**
* Gets the current logged-in user. * Gets the current logged-in user.
* *
* @param request the specified request * @param context the specified context
* @param response the specified response
* @return the current logged-in user, returns {@code null} if not found * @return the current logged-in user, returns {@code null} if not found
*/ */
public static JSONObject getCurrentUser(final Request request, final Response response) { public static JSONObject getCurrentUser(final RequestContext context) {
final Request request = context.getRequest();
final Set<Cookie> cookies = request.getCookies(); final Set<Cookie> cookies = request.getCookies();
if (cookies.isEmpty()) { if (cookies.isEmpty()) {
return null; return null;
} }
final Response response = context.getResponse();
final BeanManager beanManager = BeanManager.getInstance(); final BeanManager beanManager = BeanManager.getInstance();
final UserRepository userRepository = beanManager.getReference(UserRepository.class); final UserRepository userRepository = beanManager.getReference(UserRepository.class);
try { try {
...@@ -376,7 +377,7 @@ public final class Solos { ...@@ -376,7 +377,7 @@ public final class Solos {
* @return {@code true} if the current request is made by logged in user, returns {@code false} otherwise * @return {@code true} if the current request is made by logged in user, returns {@code false} otherwise
*/ */
public static boolean isLoggedIn(final RequestContext context) { public static boolean isLoggedIn(final RequestContext context) {
return null != Solos.getCurrentUser(context.getRequest(), context.getResponse()); return null != Solos.getCurrentUser(context);
} }
/** /**
...@@ -387,7 +388,7 @@ public final class Solos { ...@@ -387,7 +388,7 @@ public final class Solos {
* administrator, returns {@code false} otherwise * administrator, returns {@code false} otherwise
*/ */
public static boolean isAdminLoggedIn(final RequestContext context) { public static boolean isAdminLoggedIn(final RequestContext context) {
final JSONObject user = getCurrentUser(context.getRequest(), context.getResponse()); final JSONObject user = getCurrentUser(context);
if (null == user) { if (null == user) {
return false; return false;
} }
...@@ -435,8 +436,7 @@ public final class Solos { ...@@ -435,8 +436,7 @@ public final class Solos {
} }
} }
final Response response = context.getResponse(); final JSONObject currentUser = getCurrentUser(context);
final JSONObject currentUser = getCurrentUser(request, response);
return !(null != currentUser && !Role.VISITOR_ROLE.equals(currentUser.optString(User.USER_ROLE))); return !(null != currentUser && !Role.VISITOR_ROLE.equals(currentUser.optString(User.USER_ROLE)));
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment