5
5
import static run .halo .app .extension .index .query .QueryFactory .isNull ;
6
6
7
7
import java .time .Instant ;
8
+ import java .util .Set ;
8
9
import java .util .function .Function ;
9
10
import org .apache .commons .lang3 .BooleanUtils ;
10
11
import org .springframework .data .domain .Sort ;
16
17
import reactor .core .publisher .Mono ;
17
18
import run .halo .app .core .extension .User ;
18
19
import run .halo .app .core .extension .content .Comment ;
20
+ import run .halo .app .core .extension .service .RoleService ;
19
21
import run .halo .app .core .extension .service .UserService ;
20
22
import run .halo .app .extension .Extension ;
21
23
import run .halo .app .extension .ListOptions ;
30
32
import run .halo .app .metrics .CounterService ;
31
33
import run .halo .app .metrics .MeterUtils ;
32
34
import run .halo .app .plugin .ExtensionComponentsFinder ;
35
+ import run .halo .app .security .authorization .AuthorityUtils ;
33
36
34
37
/**
35
38
* Comment service implementation.
@@ -42,6 +45,7 @@ public class CommentServiceImpl implements CommentService {
42
45
43
46
private final ReactiveExtensionClient client ;
44
47
private final UserService userService ;
48
+ private final RoleService roleService ;
45
49
private final ExtensionComponentsFinder extensionComponentsFinder ;
46
50
47
51
private final SystemConfigurableEnvironmentFetcher environmentFetcher ;
@@ -50,12 +54,14 @@ public class CommentServiceImpl implements CommentService {
50
54
public CommentServiceImpl (ReactiveExtensionClient client ,
51
55
UserService userService , ExtensionComponentsFinder extensionComponentsFinder ,
52
56
SystemConfigurableEnvironmentFetcher environmentFetcher ,
53
- CounterService counterService ) {
57
+ CounterService counterService , RoleService roleService
58
+ ) {
54
59
this .client = client ;
55
60
this .userService = userService ;
56
61
this .extensionComponentsFinder = extensionComponentsFinder ;
57
62
this .environmentFetcher = environmentFetcher ;
58
63
this .counterService = counterService ;
64
+ this .roleService = roleService ;
59
65
}
60
66
61
67
@ Override
@@ -113,7 +119,21 @@ public Mono<Comment> create(Comment comment) {
113
119
}
114
120
// populate owner from current user
115
121
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
+ }))
117
137
.map (owner -> {
118
138
comment .getSpec ().setOwner (owner );
119
139
return comment ;
0 commit comments