Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: category authentication and post authentication #1826

Merged
merged 1 commit into from
Apr 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package run.halo.app.controller.content.auth;

import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;
Expand Down Expand Up @@ -37,11 +39,8 @@ public Object getPrincipal() {
@Override
public boolean isAuthenticated(Integer categoryId) {
Category category = categoryService.getById(categoryId);
if (StringUtils.isBlank(category.getPassword())) {
// All parent category is not encrypted
if (categoryService.lookupFirstEncryptedBy(category.getId()).isEmpty()) {
return true;
}
if (!isPrivate(category)) {
return true;
}

String sessionId = getSessionId();
Expand All @@ -55,6 +54,13 @@ public boolean isAuthenticated(Integer categoryId) {
return cacheStore.get(cacheKey).isPresent();
}

private boolean isPrivate(Category category) {
if (StringUtils.isNotBlank(category.getPassword())) {
return true;
}
return categoryService.lookupFirstEncryptedBy(category.getId()).isPresent();
}

@Override
public void setAuthenticated(Integer resourceId, boolean isAuthenticated) {
String sessionId = getSessionId();
Expand All @@ -74,12 +80,21 @@ public void setAuthenticated(Integer resourceId, boolean isAuthenticated) {

@Override
public void clearByResourceId(Integer resourceId) {
String resourceCachePrefix =
StringUtils.joinWith(":", CACHE_PREFIX, getPrincipal(), resourceId);
cacheStore.toMap().forEach((key, value) -> {
if (StringUtils.startsWith(key, resourceCachePrefix)) {
cacheStore.delete(key);
}
});
String sessionId = getSessionId();
if (StringUtils.isBlank(sessionId)) {
return;
}
String categoryCacheKey =
buildCacheKey(sessionId, getPrincipal().toString(), String.valueOf(resourceId));
// clean category cache
cacheStore.delete(categoryCacheKey);

Set<Integer> postIds = categoryService.listPostIdsByCategoryIdRecursively(resourceId);
Set<String> postCacheKeys = postIds.stream()
.map(postId ->
buildCacheKey(sessionId, EncryptTypeEnum.POST.getName(), String.valueOf(postId)))
.collect(Collectors.toSet());
// clean category post cache
postCacheKeys.forEach(cacheStore::delete);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,15 @@ public Object getPrincipal() {
@Override
public boolean isAuthenticated(Integer postId) {
Post post = postService.getById(postId);
if (StringUtils.isBlank(post.getPassword())) {
List<PostCategory> postCategories = postCategoryService.listByPostId(postId);
boolean categoryEncrypted = postCategories.stream()
.anyMatch(postCategory -> categoryService.isPrivate(postCategory.getCategoryId()));
if (!categoryEncrypted) {
return true;
if (!isPrivate(post)) {
return true;
}
List<PostCategory> postCategories = postCategoryService.listByPostId(postId);
for (PostCategory postCategory : postCategories) {
if (!categoryService.isPrivate(postCategory.getCategoryId())) {
continue;
}

boolean anyCategoryAuthenticated = postCategories.stream()
.anyMatch(postCategory ->
categoryAuthentication.isAuthenticated(postCategory.getCategoryId()));
if (anyCategoryAuthenticated) {
if (categoryAuthentication.isAuthenticated(postCategory.getCategoryId())) {
return true;
}
}
Expand All @@ -74,6 +71,15 @@ public boolean isAuthenticated(Integer postId) {
return cacheStore.get(cacheKey).isPresent();
}

private boolean isPrivate(Post post) {
if (StringUtils.isNotBlank(post.getPassword())) {
return true;
}
List<PostCategory> postCategories = postCategoryService.listByPostId(post.getId());
return postCategories.stream()
.anyMatch(postCategory -> categoryService.isPrivate(postCategory.getCategoryId()));
}

@Override
public void setAuthenticated(Integer resourceId, boolean isAuthenticated) {
String sessionId = getSessionId();
Expand All @@ -93,12 +99,13 @@ public void setAuthenticated(Integer resourceId, boolean isAuthenticated) {

@Override
public void clearByResourceId(Integer resourceId) {
String resourceCachePrefix =
StringUtils.joinWith(":", CACHE_PREFIX, getPrincipal(), resourceId);
cacheStore.toMap().forEach((key, value) -> {
if (StringUtils.startsWith(key, resourceCachePrefix)) {
cacheStore.delete(key);
}
});
String sessionId = getSessionId();
if (StringUtils.isBlank(sessionId)) {
return;
}
String cacheKey =
buildCacheKey(sessionId, getPrincipal().toString(), String.valueOf(resourceId));
// clean category cache
cacheStore.delete(cacheKey);
}
}