Skip to content

Commit 5c7f6e4

Browse files
committed
Merge remote-tracking branch 'upstream/main'
2 parents 5776bb6 + 6d1e35d commit 5c7f6e4

File tree

7 files changed

+64
-14
lines changed

7 files changed

+64
-14
lines changed

application/src/main/java/run/halo/app/content/comment/CommentServiceImpl.java

+22-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import static run.halo.app.extension.index.query.QueryFactory.isNull;
66

77
import java.time.Instant;
8+
import java.util.Set;
89
import java.util.function.Function;
910
import org.apache.commons.lang3.BooleanUtils;
1011
import org.springframework.data.domain.Sort;
@@ -16,6 +17,7 @@
1617
import reactor.core.publisher.Mono;
1718
import run.halo.app.core.extension.User;
1819
import run.halo.app.core.extension.content.Comment;
20+
import run.halo.app.core.extension.service.RoleService;
1921
import run.halo.app.core.extension.service.UserService;
2022
import run.halo.app.extension.Extension;
2123
import run.halo.app.extension.ListOptions;
@@ -30,6 +32,7 @@
3032
import run.halo.app.metrics.CounterService;
3133
import run.halo.app.metrics.MeterUtils;
3234
import run.halo.app.plugin.ExtensionComponentsFinder;
35+
import run.halo.app.security.authorization.AuthorityUtils;
3336

3437
/**
3538
* Comment service implementation.
@@ -42,6 +45,7 @@ public class CommentServiceImpl implements CommentService {
4245

4346
private final ReactiveExtensionClient client;
4447
private final UserService userService;
48+
private final RoleService roleService;
4549
private final ExtensionComponentsFinder extensionComponentsFinder;
4650

4751
private final SystemConfigurableEnvironmentFetcher environmentFetcher;
@@ -50,12 +54,14 @@ public class CommentServiceImpl implements CommentService {
5054
public CommentServiceImpl(ReactiveExtensionClient client,
5155
UserService userService, ExtensionComponentsFinder extensionComponentsFinder,
5256
SystemConfigurableEnvironmentFetcher environmentFetcher,
53-
CounterService counterService) {
57+
CounterService counterService, RoleService roleService
58+
) {
5459
this.client = client;
5560
this.userService = userService;
5661
this.extensionComponentsFinder = extensionComponentsFinder;
5762
this.environmentFetcher = environmentFetcher;
5863
this.counterService = counterService;
64+
this.roleService = roleService;
5965
}
6066

6167
@Override
@@ -113,7 +119,21 @@ public Mono<Comment> create(Comment comment) {
113119
}
114120
// populate owner from current user
115121
return fetchCurrentUser()
116-
.map(this::toCommentOwner)
122+
.flatMap(currentUser -> ReactiveSecurityContextHolder.getContext()
123+
.flatMap(securityContext -> {
124+
var authentication = securityContext.getAuthentication();
125+
var roles = AuthorityUtils.authoritiesToRoles(
126+
authentication.getAuthorities());
127+
return roleService.contains(roles,
128+
Set.of(AuthorityUtils.COMMENT_MANAGEMENT_ROLE_NAME))
129+
.doOnNext(result -> {
130+
if (result) {
131+
comment.getSpec().setApproved(true);
132+
comment.getSpec().setApprovedTime(Instant.now());
133+
}
134+
})
135+
.thenReturn(toCommentOwner(currentUser));
136+
}))
117137
.map(owner -> {
118138
comment.getSpec().setOwner(owner);
119139
return comment;

application/src/main/java/run/halo/app/content/comment/ReplyServiceImpl.java

+22-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import static run.halo.app.extension.router.selector.SelectorUtil.labelAndFieldSelectorToPredicate;
77

88
import java.time.Instant;
9+
import java.util.Set;
910
import java.util.function.Function;
1011
import java.util.function.Predicate;
1112
import lombok.RequiredArgsConstructor;
@@ -19,6 +20,7 @@
1920
import run.halo.app.core.extension.User;
2021
import run.halo.app.core.extension.content.Comment;
2122
import run.halo.app.core.extension.content.Reply;
23+
import run.halo.app.core.extension.service.RoleService;
2224
import run.halo.app.core.extension.service.UserService;
2325
import run.halo.app.extension.Extension;
2426
import run.halo.app.extension.ListOptions;
@@ -29,6 +31,7 @@
2931
import run.halo.app.extension.router.selector.FieldSelector;
3032
import run.halo.app.metrics.CounterService;
3133
import run.halo.app.metrics.MeterUtils;
34+
import run.halo.app.security.authorization.AuthorityUtils;
3235

3336
/**
3437
* A default implementation of {@link ReplyService}.
@@ -42,6 +45,7 @@ public class ReplyServiceImpl implements ReplyService {
4245

4346
private final ReactiveExtensionClient client;
4447
private final UserService userService;
48+
private final RoleService roleService;
4549
private final CounterService counterService;
4650

4751
@Override
@@ -75,10 +79,24 @@ public Mono<Reply> create(String commentName, Reply reply) {
7579
}
7680
// populate owner from current user
7781
return fetchCurrentUser()
78-
.map(user -> {
79-
replyToUse.getSpec().setOwner(toCommentOwner(user));
80-
return replyToUse;
81-
})
82+
.flatMap(user ->
83+
ReactiveSecurityContextHolder.getContext()
84+
.flatMap(securityContext -> {
85+
var authentication = securityContext.getAuthentication();
86+
var roles = AuthorityUtils.authoritiesToRoles(
87+
authentication.getAuthorities());
88+
return roleService.contains(roles,
89+
Set.of(AuthorityUtils.COMMENT_MANAGEMENT_ROLE_NAME))
90+
.doOnNext(result -> {
91+
if (result) {
92+
reply.getSpec().setApproved(true);
93+
reply.getSpec().setApprovedTime(Instant.now());
94+
}
95+
replyToUse.getSpec().setOwner(toCommentOwner(user));
96+
})
97+
.thenReturn(replyToUse);
98+
})
99+
)
82100
.switchIfEmpty(
83101
Mono.error(new IllegalArgumentException("Reply owner must not be null.")));
84102
})

application/src/main/java/run/halo/app/security/authorization/AuthorityUtils.java

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ public enum AuthorityUtils {
2424

2525
public static final String ANONYMOUS_ROLE_NAME = "anonymous";
2626

27+
public static final String COMMENT_MANAGEMENT_ROLE_NAME = "role-template-manage-comments";
28+
2729
/**
2830
* Converts an array of GrantedAuthority objects to a role set.
2931
*

application/src/test/java/run/halo/app/content/comment/CommentServiceImplTest.java

+10
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import java.util.List;
1212
import java.util.Map;
13+
import java.util.Set;
1314
import org.json.JSONException;
1415
import org.junit.jupiter.api.BeforeEach;
1516
import org.junit.jupiter.api.Test;
@@ -33,6 +34,7 @@
3334
import run.halo.app.core.extension.User;
3435
import run.halo.app.core.extension.content.Comment;
3536
import run.halo.app.core.extension.content.Post;
37+
import run.halo.app.core.extension.service.RoleService;
3638
import run.halo.app.core.extension.service.UserService;
3739
import run.halo.app.extension.ListOptions;
3840
import run.halo.app.extension.ListResult;
@@ -46,6 +48,7 @@
4648
import run.halo.app.metrics.CounterService;
4749
import run.halo.app.metrics.MeterUtils;
4850
import run.halo.app.plugin.ExtensionComponentsFinder;
51+
import run.halo.app.security.authorization.AuthorityUtils;
4952

5053
/**
5154
* Tests for {@link CommentServiceImpl}.
@@ -65,6 +68,9 @@ class CommentServiceImplTest {
6568
@Mock
6669
private UserService userService;
6770

71+
@Mock
72+
private RoleService roleService;
73+
6874
@Mock
6975
private ExtensionComponentsFinder extensionComponentsFinder;
7076

@@ -90,6 +96,10 @@ void setUp() {
9096
when(client.fetch(eq(User.class), eq("C-owner")))
9197
.thenReturn(Mono.empty());
9298

99+
when(roleService.contains(Set.of("USER"),
100+
Set.of(AuthorityUtils.COMMENT_MANAGEMENT_ROLE_NAME)))
101+
.thenReturn(Mono.just(false));
102+
93103
PostCommentSubject postCommentSubject = Mockito.mock(PostCommentSubject.class);
94104
when(extensionComponentsFinder.getExtensions(eq(CommentSubject.class)))
95105
.thenReturn(List.of(postCommentSubject));

build.gradle

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
plugins {
2-
id 'org.springframework.boot' version '3.2.3' apply false
3-
id 'io.spring.dependency-management' version '1.1.0' apply false
4-
id "com.gorylenko.gradle-git-properties" version "2.3.2" apply false
5-
id "de.undercouch.download" version "5.3.1" apply false
6-
id "io.freefair.lombok" version "8.4" apply false
2+
id 'org.springframework.boot' version '3.2.4' apply false
3+
id 'io.spring.dependency-management' version '1.1.4' apply false
4+
id "com.gorylenko.gradle-git-properties" version "2.4.1" apply false
5+
id "de.undercouch.download" version "5.6.0" apply false
6+
id "io.freefair.lombok" version "8.6" apply false
77
id 'org.gradle.crypto.checksum' version '1.4.0' apply false
8-
id "com.github.node-gradle.node" version "7.0.1" apply false
8+
id "com.github.node-gradle.node" version "7.0.2" apply false
99
}

platform/application/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ ext {
1818
guava = "32.0.1-jre"
1919
jsoup = '1.15.3'
2020
jsonPatch = "1.13"
21-
springDocOpenAPI = "2.3.0"
21+
springDocOpenAPI = "2.4.0"
2222
lucene = "9.9.1"
2323
resilience4jVersion = "2.2.0"
2424
twoFactorAuth = "1.3"

ui/packages/editor/src/components/Editor.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ watch(
3737
<editor-bubble-menu :editor="editor" />
3838
<editor-header :editor="editor" />
3939
<div class="h-full flex flex-row w-full overflow-hidden">
40-
<div class="overflow-y-auto flex-1 bg-white">
40+
<div class="overflow-y-auto flex-1 bg-white relative">
4141
<div v-if="$slots.content" class="editor-header-extra">
4242
<slot name="content" />
4343
</div>

0 commit comments

Comments
 (0)