Skip to content

Commit

Permalink
chore: add user ownership check
Browse files Browse the repository at this point in the history
  • Loading branch information
guqing committed Nov 27, 2023
1 parent 5309e94 commit ffef498
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import org.springframework.web.server.ServerWebInputException;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import run.halo.app.core.extension.notification.Notification;
Expand Down Expand Up @@ -50,8 +51,15 @@ public Flux<String> markSpecifiedAsRead(String username, List<String> names) {
}

@Override
public Mono<Notification> deleteByName(String name) {
public Mono<Notification> deleteByName(String username, String name) {
return client.get(Notification.class, name)
.doOnNext(notification -> {
var recipient = notification.getSpec().getRecipient();
if (!username.equals(recipient)) {
throw new ServerWebInputException(
"You have no permission to delete this notification.");
}
})
.flatMap(client::delete);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@ public interface UserNotificationService {
*/
Flux<String> markSpecifiedAsRead(String username, List<String> names);

Mono<Notification> deleteByName(String name);
Mono<Notification> deleteByName(String username, String name);
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ Supplier<RouterFunction<ServerResponse>> userspaceScopedApis() {

private Mono<ServerResponse> deleteNotification(ServerRequest request) {
var name = request.pathVariable("name");
return notificationService.deleteByName(name)
var username = request.pathVariable("username");
return notificationService.deleteByName(username, name)
.flatMap(notification -> ServerResponse.ok().bodyValue(notification));
}

Expand Down

0 comments on commit ffef498

Please sign in to comment.