|
|
@@ -0,0 +1,330 @@
|
|
|
+package space.apps.moudules.website.util;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.HashSet;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Set;
|
|
|
+
|
|
|
+import apps.pojo.Article;
|
|
|
+import apps.pojo.WebsiteNavigation;
|
|
|
+import space.core.common.utils.StringUtil;
|
|
|
+import space.core.db.jdbc.GeneralDBAccess;
|
|
|
+import space.core.db.jdbc.SqlCommandImpl;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 前台游客权限解析工具。
|
|
|
+ */
|
|
|
+public final class WebsiteGuestAccessUtil {
|
|
|
+
|
|
|
+ public static final int GUEST_ACCESS_PUBLIC = 0;
|
|
|
+ public static final int GUEST_ACCESS_PROTECTED = 1;
|
|
|
+
|
|
|
+ public static final int ARTICLE_GUEST_FOLLOW_NAV = 0;
|
|
|
+ public static final int ARTICLE_GUEST_STANDALONE = 1;
|
|
|
+
|
|
|
+ public static final long GUEST_ACCOUNT_ENABLE = 61L;
|
|
|
+ public static final long GUEST_ACCOUNT_DISABLE = 71L;
|
|
|
+
|
|
|
+ private WebsiteGuestAccessUtil() {
|
|
|
+ }
|
|
|
+
|
|
|
+ public static int getNavigationGuestAccessFlag(GeneralDBAccess gdba, Long navigationId) throws Exception {
|
|
|
+ if (navigationId == null) {
|
|
|
+ return GUEST_ACCESS_PUBLIC;
|
|
|
+ }
|
|
|
+ String sql = " select guest_access_flag from website_navigation where navigationid = :navigationid ";
|
|
|
+ SqlCommandImpl sqlCmd = new SqlCommandImpl(sql);
|
|
|
+ sqlCmd.setString("navigationid", navigationId);
|
|
|
+ Map<String, Object> row = gdba.selectAsMap(sqlCmd);
|
|
|
+ if (row == null || row.get("guest_access_flag") == null) {
|
|
|
+ return GUEST_ACCESS_PUBLIC;
|
|
|
+ }
|
|
|
+ return Integer.parseInt(String.valueOf(row.get("guest_access_flag")));
|
|
|
+ }
|
|
|
+
|
|
|
+ public static int getArticleGuestAccessFlag(GeneralDBAccess gdba, Long articleId) throws Exception {
|
|
|
+ if (articleId == null) {
|
|
|
+ return ARTICLE_GUEST_FOLLOW_NAV;
|
|
|
+ }
|
|
|
+ String sql = " select guest_access_flag from article where articleid = :articleid ";
|
|
|
+ SqlCommandImpl sqlCmd = new SqlCommandImpl(sql);
|
|
|
+ sqlCmd.setString("articleid", articleId);
|
|
|
+ Map<String, Object> row = gdba.selectAsMap(sqlCmd);
|
|
|
+ if (row == null || row.get("guest_access_flag") == null) {
|
|
|
+ return ARTICLE_GUEST_FOLLOW_NAV;
|
|
|
+ }
|
|
|
+ return Integer.parseInt(String.valueOf(row.get("guest_access_flag")));
|
|
|
+ }
|
|
|
+
|
|
|
+ public static boolean isNavigationProtected(GeneralDBAccess gdba, WebsiteNavigation nav) throws Exception {
|
|
|
+ if (nav == null) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ return getNavigationGuestAccessFlag(gdba, nav.getNavigationid()) == GUEST_ACCESS_PROTECTED;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static boolean isArticleProtected(GeneralDBAccess gdba, Article article, WebsiteNavigation nav)
|
|
|
+ throws Exception {
|
|
|
+ if (article == null) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ int articleFlag = getArticleGuestAccessFlag(gdba, article.getArticleid());
|
|
|
+ if (articleFlag == ARTICLE_GUEST_STANDALONE) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ return isNavigationProtected(gdba, nav);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static List<Long> getNavigationAllowedAccountIds(GeneralDBAccess gdba, Long navigationId) throws Exception {
|
|
|
+ if (navigationId == null) {
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+ List<Long> directIds = queryNavigationAllowedAccountIds(gdba, navigationId);
|
|
|
+ if (!directIds.isEmpty()) {
|
|
|
+ return directIds;
|
|
|
+ }
|
|
|
+ return resolveDiyLinkMirroredAccountIds(gdba, navigationId);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static List<Long> queryNavigationAllowedAccountIds(GeneralDBAccess gdba, Long navigationId)
|
|
|
+ throws Exception {
|
|
|
+ String sql = " select guest_account_id from website_navigation_guest_access where navigationid = :navigationid ";
|
|
|
+ SqlCommandImpl sqlCmd = new SqlCommandImpl(sql);
|
|
|
+ sqlCmd.setString("navigationid", navigationId);
|
|
|
+ List<Map<String, Object>> rows = gdba.selectAsMapList(sqlCmd);
|
|
|
+ return extractLongIds(rows, "guest_account_id");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 自定义链接栏目保存游客账号时挂在源节点;真实列表栏目 url 与 jump_url 一致时应继承允许名单。
|
|
|
+ */
|
|
|
+ private static List<Long> resolveDiyLinkMirroredAccountIds(GeneralDBAccess gdba, Long navigationId)
|
|
|
+ throws Exception {
|
|
|
+ String navSql = " select websiteid, url from website_navigation where navigationid = :navigationid ";
|
|
|
+ SqlCommandImpl navCmd = new SqlCommandImpl(navSql);
|
|
|
+ navCmd.setString("navigationid", navigationId);
|
|
|
+ Map<String, Object> navRow = gdba.selectAsMap(navCmd);
|
|
|
+ if (navRow == null || navRow.isEmpty()) {
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+ Object websiteIdObj = navRow.get("websiteid");
|
|
|
+ Object urlObj = navRow.get("url");
|
|
|
+ if (websiteIdObj == null || urlObj == null || StringUtil.isNull(String.valueOf(urlObj))) {
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+ String targetUrl = String.valueOf(urlObj).trim();
|
|
|
+ String sourceSql = " select navigationid from website_navigation "
|
|
|
+ + " where websiteid = :websiteid and wntcode = 10030 and dcode = 61 "
|
|
|
+ + " and jump_url = :jumpUrl and guest_access_flag = 1 ";
|
|
|
+ SqlCommandImpl sourceCmd = new SqlCommandImpl(sourceSql);
|
|
|
+ sourceCmd.setString("websiteid", websiteIdObj).setString("jumpUrl", targetUrl);
|
|
|
+ List<Map<String, Object>> sourceRows = gdba.selectAsMapList(sourceCmd);
|
|
|
+ if (sourceRows == null || sourceRows.isEmpty()) {
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+ Set<Long> mergedIds = new HashSet<Long>();
|
|
|
+ for (Map<String, Object> sourceRow : sourceRows) {
|
|
|
+ if (sourceRow.get("navigationid") == null) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ Long sourceNavigationId = Long.valueOf(String.valueOf(sourceRow.get("navigationid")));
|
|
|
+ mergedIds.addAll(queryNavigationAllowedAccountIds(gdba, sourceNavigationId));
|
|
|
+ }
|
|
|
+ if (mergedIds.isEmpty()) {
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+ return new ArrayList<Long>(mergedIds);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static List<Long> getArticleAllowedAccountIds(GeneralDBAccess gdba, Long articleId) throws Exception {
|
|
|
+ if (articleId == null) {
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+ String sql = " select guest_account_id from website_article_guest_access where articleid = :articleid ";
|
|
|
+ SqlCommandImpl sqlCmd = new SqlCommandImpl(sql);
|
|
|
+ sqlCmd.setString("articleid", articleId);
|
|
|
+ List<Map<String, Object>> rows = gdba.selectAsMapList(sqlCmd);
|
|
|
+ return extractLongIds(rows, "guest_account_id");
|
|
|
+ }
|
|
|
+
|
|
|
+ public static List<Long> resolveAllowedAccountIds(GeneralDBAccess gdba, Article article, WebsiteNavigation nav)
|
|
|
+ throws Exception {
|
|
|
+ if (article == null) {
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+ int articleFlag = getArticleGuestAccessFlag(gdba, article.getArticleid());
|
|
|
+ if (articleFlag == ARTICLE_GUEST_STANDALONE) {
|
|
|
+ return getArticleAllowedAccountIds(gdba, article.getArticleid());
|
|
|
+ }
|
|
|
+ if (nav == null) {
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+ return getNavigationAllowedAccountIds(gdba, nav.getNavigationid());
|
|
|
+ }
|
|
|
+
|
|
|
+ public static GuestProtectionContext resolveForNavigation(GeneralDBAccess gdba, WebsiteNavigation nav)
|
|
|
+ throws Exception {
|
|
|
+ GuestProtectionContext ctx = new GuestProtectionContext();
|
|
|
+ if (nav == null) {
|
|
|
+ return ctx;
|
|
|
+ }
|
|
|
+ ctx.setResourceType("navigation");
|
|
|
+ ctx.setResourceId(nav.getNavigationid());
|
|
|
+ if (!isNavigationProtected(gdba, nav)) {
|
|
|
+ return ctx;
|
|
|
+ }
|
|
|
+ ctx.setProtectedResource(true);
|
|
|
+ ctx.setAllowedAccountIds(getNavigationAllowedAccountIds(gdba, nav.getNavigationid()));
|
|
|
+ return ctx;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static GuestProtectionContext resolveForArticle(GeneralDBAccess gdba, Article article,
|
|
|
+ WebsiteNavigation nav) throws Exception {
|
|
|
+ GuestProtectionContext ctx = new GuestProtectionContext();
|
|
|
+ if (article == null) {
|
|
|
+ return ctx;
|
|
|
+ }
|
|
|
+ ctx.setResourceType("article");
|
|
|
+ ctx.setResourceId(article.getArticleid());
|
|
|
+ if (!isArticleProtected(gdba, article, nav)) {
|
|
|
+ return ctx;
|
|
|
+ }
|
|
|
+ ctx.setProtectedResource(true);
|
|
|
+ ctx.setAllowedAccountIds(resolveAllowedAccountIds(gdba, article, nav));
|
|
|
+ return ctx;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void replaceNavigationGuestAccess(GeneralDBAccess gdba, Long navigationId, String guestAccountIds,
|
|
|
+ Long personId) throws Exception {
|
|
|
+ String deleteSql = " delete from website_navigation_guest_access where navigationid = :navigationid ";
|
|
|
+ SqlCommandImpl deleteCmd = new SqlCommandImpl(deleteSql);
|
|
|
+ deleteCmd.setString("navigationid", navigationId);
|
|
|
+ gdba.modify(deleteCmd);
|
|
|
+
|
|
|
+ Set<Long> ids = parseIdSet(guestAccountIds);
|
|
|
+ for (Long guestAccountId : ids) {
|
|
|
+ String insertSql = " insert into website_navigation_guest_access (navigationid, guest_account_id, create_time, create_pid) "
|
|
|
+ + " values (:navigationid, :guestAccountId, now(), :createPid) ";
|
|
|
+ SqlCommandImpl insertCmd = new SqlCommandImpl(insertSql);
|
|
|
+ insertCmd.setString("navigationid", navigationId).setString("guestAccountId", guestAccountId)
|
|
|
+ .setString("createPid", personId);
|
|
|
+ gdba.modify(insertCmd);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void replaceArticleGuestAccess(GeneralDBAccess gdba, Long articleId, String guestAccountIds,
|
|
|
+ Long personId) throws Exception {
|
|
|
+ String deleteSql = " delete from website_article_guest_access where articleid = :articleid ";
|
|
|
+ SqlCommandImpl deleteCmd = new SqlCommandImpl(deleteSql);
|
|
|
+ deleteCmd.setString("articleid", articleId);
|
|
|
+ gdba.modify(deleteCmd);
|
|
|
+
|
|
|
+ Set<Long> ids = parseIdSet(guestAccountIds);
|
|
|
+ for (Long guestAccountId : ids) {
|
|
|
+ String insertSql = " insert into website_article_guest_access (articleid, guest_account_id, create_time, create_pid) "
|
|
|
+ + " values (:articleid, :guestAccountId, now(), :createPid) ";
|
|
|
+ SqlCommandImpl insertCmd = new SqlCommandImpl(insertSql);
|
|
|
+ insertCmd.setString("articleid", articleId).setString("guestAccountId", guestAccountId)
|
|
|
+ .setString("createPid", personId);
|
|
|
+ gdba.modify(insertCmd);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void updateNavigationGuestAccessFlag(GeneralDBAccess gdba, Long navigationId, int guestAccessFlag)
|
|
|
+ throws Exception {
|
|
|
+ String sql = " update website_navigation set guest_access_flag = :guestAccessFlag where navigationid = :navigationid ";
|
|
|
+ SqlCommandImpl sqlCmd = new SqlCommandImpl(sql);
|
|
|
+ sqlCmd.setString("guestAccessFlag", guestAccessFlag).setString("navigationid", navigationId);
|
|
|
+ gdba.modify(sqlCmd);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void updateArticleGuestAccessFlag(GeneralDBAccess gdba, Long articleId, int guestAccessFlag)
|
|
|
+ throws Exception {
|
|
|
+ String sql = " update article set guest_access_flag = :guestAccessFlag where articleid = :articleid ";
|
|
|
+ SqlCommandImpl sqlCmd = new SqlCommandImpl(sql);
|
|
|
+ sqlCmd.setString("guestAccessFlag", guestAccessFlag).setString("articleid", articleId);
|
|
|
+ gdba.modify(sqlCmd);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String joinIds(List<Long> ids) {
|
|
|
+ if (ids == null || ids.isEmpty()) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ StringBuilder builder = new StringBuilder();
|
|
|
+ for (int i = 0; i < ids.size(); i++) {
|
|
|
+ if (i > 0) {
|
|
|
+ builder.append(",");
|
|
|
+ }
|
|
|
+ builder.append(ids.get(i));
|
|
|
+ }
|
|
|
+ return builder.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ private static List<Long> extractLongIds(List<Map<String, Object>> rows, String key) {
|
|
|
+ if (rows == null || rows.isEmpty()) {
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+ List<Long> ids = new ArrayList<Long>();
|
|
|
+ for (Map<String, Object> row : rows) {
|
|
|
+ if (row.get(key) != null) {
|
|
|
+ ids.add(Long.valueOf(String.valueOf(row.get(key))));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return ids;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static Set<Long> parseIdSet(String guestAccountIds) {
|
|
|
+ Set<Long> ids = new HashSet<Long>();
|
|
|
+ if (StringUtil.isNull(guestAccountIds)) {
|
|
|
+ return ids;
|
|
|
+ }
|
|
|
+ String[] parts = guestAccountIds.split(",");
|
|
|
+ for (String part : parts) {
|
|
|
+ if (StringUtil.notNull(part)) {
|
|
|
+ ids.add(Long.valueOf(part.trim()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return ids;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static class GuestProtectionContext {
|
|
|
+ private boolean protectedResource;
|
|
|
+ private String resourceType;
|
|
|
+ private Long resourceId;
|
|
|
+ private List<Long> allowedAccountIds = Collections.emptyList();
|
|
|
+
|
|
|
+ public boolean isProtectedResource() {
|
|
|
+ return protectedResource;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setProtectedResource(boolean protectedResource) {
|
|
|
+ this.protectedResource = protectedResource;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getResourceType() {
|
|
|
+ return resourceType;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setResourceType(String resourceType) {
|
|
|
+ this.resourceType = resourceType;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Long getResourceId() {
|
|
|
+ return resourceId;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setResourceId(Long resourceId) {
|
|
|
+ this.resourceId = resourceId;
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<Long> getAllowedAccountIds() {
|
|
|
+ return allowedAccountIds;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setAllowedAccountIds(List<Long> allowedAccountIds) {
|
|
|
+ this.allowedAccountIds = allowedAccountIds == null ? Collections.<Long>emptyList() : allowedAccountIds;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|