Skip to content

Commit

Permalink
feat: #1225 (#1235)
Browse files Browse the repository at this point in the history
* feat: #1225

* fix: compatible with jdk1.8

* fix: format code

* fix: fix the problem that the status of the recycle bin file is incorrect when revocering

* fix: format code

* fix: format code

* fix: fix the post cannot be converted to recyling mode

* fix: post cannot be published on deleting the category password

* fix: fix jpa error

* fix: format code

* fix: encryption type extracted into enum

* fix: format code

* fix: format code

* fix: changes requested

* fix: format code

* fix: revert checkstyle.xml

* fix: change request

* fix: not encrypt 方法改为重载方法

* fix: 修复因调整 git 版本被回退的代码

Co-authored-by: xiangbei.yzx <[email protected]>
  • Loading branch information
zhixiangyuan and xiangbei.yzx authored Jan 31, 2021
1 parent f28d833 commit 7b88fca
Show file tree
Hide file tree
Showing 28 changed files with 1,197 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public AdminController(AdminService adminService, OptionService optionService) {
@GetMapping(value = "/is_installed")
@ApiOperation("Checks Installation status")
public boolean isInstall() {
return optionService
.getByPropertyOrDefault(PrimaryProperties.IS_INSTALLED, Boolean.class, false);
return optionService.getByPropertyOrDefault(PrimaryProperties.IS_INSTALLED, Boolean.class,
false);
}

@PostMapping("login/precheck")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ public List<? extends CategoryDTO> listAll(
@SortDefault(sort = "createTime", direction = DESC) Sort sort,
@RequestParam(name = "more", required = false, defaultValue = "false") boolean more) {
if (more) {
return postCategoryService.listCategoryWithPostCountDto(sort);
return postCategoryService.listCategoryWithPostCountDto(sort, true);
}

return categoryService.convertTo(categoryService.listAll(sort));
return categoryService.convertTo(categoryService.listAll(sort, true));
}

@GetMapping("tree_view")
Expand All @@ -81,7 +81,8 @@ public CategoryDTO createBy(@RequestBody @Valid CategoryParam categoryParam) {
@PutMapping("{categoryId:\\d+}")
@ApiOperation("Updates category")
public CategoryDTO updateBy(@PathVariable("categoryId") Integer categoryId,
@RequestBody @Valid CategoryParam categoryParam) {
@RequestBody @Valid CategoryParam categoryParam
) {
Category categoryToUpdate = categoryService.getById(categoryId);
categoryParam.update(categoryToUpdate);
return categoryService.convertTo(categoryService.update(categoryToUpdate));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public MigrateController(MigrateService migrateService,
@PostMapping("halo")
@ApiOperation("Migrate from Halo")
public void migrateHalo(@RequestPart("file") MultipartFile file) {
if (optionService
.getByPropertyOrDefault(PrimaryProperties.IS_INSTALLED, Boolean.class, false)) {
if (optionService.getByPropertyOrDefault(
PrimaryProperties.IS_INSTALLED, Boolean.class, false)) {
throw new BadRequestException("无法在博客初始化完成之后迁移数据");
}
migrateService.migrate(file, MigrateType.HALO);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

import static org.springframework.data.domain.Sort.Direction.DESC;

import cn.hutool.core.util.IdUtil;
import io.swagger.annotations.ApiOperation;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.validation.Valid;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
Expand All @@ -22,12 +20,10 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import run.halo.app.cache.AbstractStringCacheStore;
import run.halo.app.model.dto.post.BasePostDetailDTO;
import run.halo.app.model.dto.post.BasePostMinimalDTO;
import run.halo.app.model.dto.post.BasePostSimpleDTO;
import run.halo.app.model.entity.Post;
import run.halo.app.model.enums.PostPermalinkType;
import run.halo.app.model.enums.PostStatus;
import run.halo.app.model.params.PostContentParam;
import run.halo.app.model.params.PostParam;
Expand All @@ -50,15 +46,12 @@ public class PostController {

private final PostService postService;

private final AbstractStringCacheStore cacheStore;

private final OptionService optionService;


public PostController(PostService postService,
AbstractStringCacheStore cacheStore,
OptionService optionService) {
this.postService = postService;
this.cacheStore = cacheStore;
this.optionService = optionService;
}

Expand All @@ -70,7 +63,7 @@ public Page<? extends BasePostSimpleDTO> pageBy(
@RequestParam(value = "more", defaultValue = "true") Boolean more) {
Page<Post> postPage = postService.pageBy(postQuery, pageable);
if (more) {
return postService.convertToListVo(postPage);
return postService.convertToListVo(postPage, true);
}

return postService.convertToSimple(postPage);
Expand All @@ -92,7 +85,7 @@ public Page<? extends BasePostSimpleDTO> pageByStatus(
Page<Post> posts = postService.pageBy(status, pageable);

if (more) {
return postService.convertToListVo(posts);
return postService.convertToListVo(posts, true);
}

return postService.convertToSimple(posts);
Expand All @@ -102,7 +95,7 @@ public Page<? extends BasePostSimpleDTO> pageByStatus(
@ApiOperation("Gets a post")
public PostDetailVO getBy(@PathVariable("postId") Integer postId) {
Post post = postService.getById(postId);
return postService.convertToDetailVo(post);
return postService.convertToDetailVo(post, true);
}

@PutMapping("{postId:\\d+}/likes")
Expand All @@ -114,8 +107,8 @@ public void likes(@PathVariable("postId") Integer postId) {
@PostMapping
@ApiOperation("Creates a post")
public PostDetailVO createBy(@Valid @RequestBody PostParam postParam,
@RequestParam(value = "autoSave", required = false, defaultValue = "false")
Boolean autoSave) {
@RequestParam(value = "autoSave", required = false, defaultValue = "false") Boolean autoSave
) {
// Convert to
Post post = postParam.convertTo();
return postService.createBy(post, postParam.getTagIds(), postParam.getCategoryIds(),
Expand All @@ -126,8 +119,8 @@ public PostDetailVO createBy(@Valid @RequestBody PostParam postParam,
@ApiOperation("Updates a post")
public PostDetailVO updateBy(@Valid @RequestBody PostParam postParam,
@PathVariable("postId") Integer postId,
@RequestParam(value = "autoSave", required = false, defaultValue = "false")
Boolean autoSave) {
@RequestParam(value = "autoSave", required = false, defaultValue = "false") Boolean autoSave
) {
// Get the post info
Post postToUpdate = postService.getById(postId);

Expand Down Expand Up @@ -186,11 +179,6 @@ public String preview(@PathVariable("postId") Integer postId)

BasePostMinimalDTO postMinimalDTO = postService.convertToMinimal(post);

String token = IdUtil.simpleUUID();

// cache preview token
cacheStore.putAny(token, token, 10, TimeUnit.MINUTES);

StringBuilder previewUrl = new StringBuilder();

if (!optionService.isEnabledAbsolutePath()) {
Expand All @@ -199,14 +187,6 @@ public String preview(@PathVariable("postId") Integer postId)

previewUrl.append(postMinimalDTO.getFullPath());

if (optionService.getPostPermalinkType().equals(PostPermalinkType.ID)) {
previewUrl.append("&token=")
.append(token);
} else {
previewUrl.append("?token=")
.append(token);
}

// build preview post url and return
return previewUrl.toString();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package run.halo.app.controller.content;

import cn.hutool.core.util.IdUtil;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
Expand All @@ -24,12 +22,17 @@
import run.halo.app.controller.content.model.SheetModel;
import run.halo.app.controller.content.model.TagModel;
import run.halo.app.exception.NotFoundException;
import run.halo.app.exception.UnsupportedException;
import run.halo.app.model.dto.post.BasePostMinimalDTO;
import run.halo.app.model.entity.Category;
import run.halo.app.model.entity.Post;
import run.halo.app.model.entity.Sheet;
import run.halo.app.model.enums.EncryptTypeEnum;
import run.halo.app.model.enums.PostPermalinkType;
import run.halo.app.model.enums.PostStatus;
import run.halo.app.model.enums.SheetPermalinkType;
import run.halo.app.service.AuthenticationService;
import run.halo.app.service.CategoryService;
import run.halo.app.service.OptionService;
import run.halo.app.service.PostService;
import run.halo.app.service.SheetService;
Expand Down Expand Up @@ -65,6 +68,10 @@ public class ContentContentController {

private final AbstractStringCacheStore cacheStore;

private final AuthenticationService authenticationService;

private final CategoryService categoryService;

public ContentContentController(PostModel postModel,
SheetModel sheetModel,
CategoryModel categoryModel,
Expand All @@ -75,7 +82,9 @@ public ContentContentController(PostModel postModel,
OptionService optionService,
PostService postService,
SheetService sheetService,
AbstractStringCacheStore cacheStore) {
AbstractStringCacheStore cacheStore,
AuthenticationService authenticationService,
CategoryService categoryService) {
this.postModel = postModel;
this.sheetModel = sheetModel;
this.categoryModel = categoryModel;
Expand All @@ -87,6 +96,8 @@ public ContentContentController(PostModel postModel,
this.postService = postService;
this.sheetService = sheetService;
this.cacheStore = cacheStore;
this.authenticationService = authenticationService;
this.categoryService = categoryService;
}

@GetMapping("{prefix}")
Expand Down Expand Up @@ -225,14 +236,32 @@ public String content(@PathVariable("year") Integer year,
throw new NotFoundException("Not Found");
}

@PostMapping(value = "archives/{slug:.*}/password")
@PostMapping(value = "archives/{type}/{slug:.*}/password")
@CacheLock(traceRequest = true, expired = 2)
public String password(@PathVariable("slug") String slug,
public String password(@PathVariable("type") String type,
@PathVariable("slug") String slug,
@RequestParam(value = "password") String password) throws UnsupportedEncodingException {

String redirectUrl;

if (EncryptTypeEnum.POST.getName().equals(type)) {
redirectUrl = doAuthenticationPost(slug, password);
} else if (EncryptTypeEnum.CATEGORY.getName().equals(type)) {
redirectUrl = doAuthenticationCategory(slug, password);
} else {
throw new UnsupportedException("未知的加密类型");
}
return "redirect:" + redirectUrl;
}

private String doAuthenticationPost(
String slug, String password) throws UnsupportedEncodingException {
Post post = postService.getBy(PostStatus.INTIMATE, slug);

post.setSlug(URLEncoder.encode(post.getSlug(), StandardCharsets.UTF_8.name()));

authenticationService.postAuthentication(post, password);

BasePostMinimalDTO postMinimalDTO = postService.convertToMinimal(post);

StringBuilder redirectUrl = new StringBuilder();
Expand All @@ -243,19 +272,22 @@ public String password(@PathVariable("slug") String slug,

redirectUrl.append(postMinimalDTO.getFullPath());

if (password.equals(post.getPassword())) {
String token = IdUtil.simpleUUID();
cacheStore.putAny(token, token, 10, TimeUnit.SECONDS);
return redirectUrl.toString();
}

if (optionService.getPostPermalinkType().equals(PostPermalinkType.ID)) {
redirectUrl.append("&token=")
.append(token);
} else {
redirectUrl.append("?token=")
.append(token);
}
private String doAuthenticationCategory(String slug, String password) {
Category category = categoryService.getBySlugOfNonNull(slug, true);

authenticationService.categoryAuthentication(category.getId(), password);

StringBuilder redirectUrl = new StringBuilder();

if (!optionService.isEnabledAbsolutePath()) {
redirectUrl.append(optionService.getBlogBaseUrl());
}

return "redirect:" + redirectUrl;
redirectUrl.append(optionService.getCategoriesPrefix()).append(slug);

return redirectUrl.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.springframework.data.domain.Sort.Direction.DESC;

import com.google.common.collect.Sets;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import org.springframework.data.domain.Page;
Expand All @@ -14,11 +15,13 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import run.halo.app.exception.ForbiddenException;
import run.halo.app.model.dto.CategoryDTO;
import run.halo.app.model.entity.Category;
import run.halo.app.model.entity.Post;
import run.halo.app.model.enums.PostStatus;
import run.halo.app.model.vo.PostListVO;
import run.halo.app.service.AuthenticationService;
import run.halo.app.service.CategoryService;
import run.halo.app.service.PostCategoryService;
import run.halo.app.service.PostService;
Expand All @@ -39,12 +42,16 @@ public class CategoryController {

private final PostService postService;

private final AuthenticationService authenticationService;

public CategoryController(CategoryService categoryService,
PostCategoryService postCategoryService,
PostService postService) {
PostService postService,
AuthenticationService authenticationService) {
this.categoryService = categoryService;
this.postCategoryService = postCategoryService;
this.postService = postService;
this.authenticationService = authenticationService;
}

@GetMapping
Expand All @@ -53,21 +60,27 @@ public List<? extends CategoryDTO> listCategories(
@SortDefault(sort = "updateTime", direction = DESC) Sort sort,
@RequestParam(name = "more", required = false, defaultValue = "false") Boolean more) {
if (more) {
return postCategoryService.listCategoryWithPostCountDto(sort);
return postCategoryService.listCategoryWithPostCountDto(sort, false);
}
return categoryService.convertTo(categoryService.listAll(sort));
}

@GetMapping("{slug}/posts")
@ApiOperation("Lists posts by category slug")
public Page<PostListVO> listPostsBy(@PathVariable("slug") String slug,
@RequestParam(value = "password", required = false) String password,
@PageableDefault(sort = {"topPriority", "updateTime"}, direction = DESC)
Pageable pageable) {
// Get category by slug
Category category = categoryService.getBySlugOfNonNull(slug);
Category category = categoryService.getBySlugOfNonNull(slug, true);

if (!authenticationService.categoryAuthentication(category.getId(), password)) {
throw new ForbiddenException("您没有该分类的访问权限");
}

Page<Post> postPage =
postCategoryService.pagePostBy(category.getId(), PostStatus.PUBLISHED, pageable);
postCategoryService.pagePostBy(category.getId(),
Sets.immutableEnumSet(PostStatus.PUBLISHED, PostStatus.INTIMATE), pageable);
return postService.convertToListVo(postPage);
}
}
Loading

0 comments on commit 7b88fca

Please sign in to comment.