Skip to content

Commit c9dbdc4

Browse files
committed
Fixing some access control.
1 parent 6f311b1 commit c9dbdc4

File tree

3 files changed

+56
-13
lines changed

3 files changed

+56
-13
lines changed

src/main/java/no/java/submit/controller/TalkController.java

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import no.java.submit.service.TimelineService;
2121
import no.java.submit.util.UserHelper;
2222
import org.eclipse.microprofile.config.inject.ConfigProperty;
23+
import org.jboss.resteasy.reactive.ClientWebApplicationException;
2324

2425
import java.util.ArrayList;
2526
import java.util.Collections;
@@ -67,12 +68,17 @@ public TemplateInstance all() {
6768
@Path("{sessionId}")
6869
public TemplateInstance view(@PathParam("sessionId") String sessionId, @Context SecurityIdentity securityIdentity) {
6970
var email = UserHelper.getEmail(securityIdentity);
70-
var session = talksService.getSession(email, sessionId);
7171

72-
if (!session.containsEmail(email))
73-
throw new NotAllowedException("Not allowed to view this session");
72+
try {
73+
var session = talksService.getSession(email, sessionId);
7474

75-
return talk.data("session", session);
75+
if (!session.containsEmail(email))
76+
throw new NotAuthorizedException("Not allowed to view this session");
77+
78+
return talk.data("session", session);
79+
} catch (ClientWebApplicationException e) {
80+
throw new NotAuthorizedException("Not allowed to view this session", e);
81+
}
7682
}
7783

7884
@GET
@@ -135,18 +141,22 @@ public Object newSessionPost(SessionForm form, @Context SecurityIdentity securit
135141
public TemplateInstance editSession(@PathParam("sessionId") String sessionId, @Context SecurityIdentity securityIdentity) {
136142
var email = UserHelper.getEmail(securityIdentity);
137143

138-
var session = talksService.getSession(email, sessionId);
144+
try {
145+
var session = talksService.getSession(email, sessionId);
139146

140-
if (!session.containsEmail(email))
141-
throw new NotAllowedException("Not allowed to view this session");
147+
if (!session.containsEmail(email))
148+
throw new NotAuthorizedException("Not allowed to view this session");
142149

143-
if (!conferenceService.current().id.equals(session.conferenceId))
144-
throw new NotFoundException();
150+
if (!conferenceService.current().id.equals(session.conferenceId))
151+
throw new NotFoundException();
145152

146-
return sessionForm
147-
.data("form", SessionForm.parse(session))
148-
.data("val", Collections.emptyMap())
149-
.data("sessionId", sessionId);
153+
return sessionForm
154+
.data("form", SessionForm.parse(session))
155+
.data("val", Collections.emptyMap())
156+
.data("sessionId", sessionId);
157+
} catch (ClientWebApplicationException e) {
158+
throw new NotAuthorizedException("Not allowed to view this session", e);
159+
}
150160
}
151161

152162
@POST
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package no.java.submit.template;
2+
3+
import io.quarkus.qute.Template;
4+
import jakarta.inject.Inject;
5+
import jakarta.ws.rs.NotAuthorizedException;
6+
import jakarta.ws.rs.core.Response;
7+
import jakarta.ws.rs.ext.ExceptionMapper;
8+
import jakarta.ws.rs.ext.Provider;
9+
10+
@Provider
11+
public class NotAuthorizedExceptionMapper implements ExceptionMapper<NotAuthorizedException> {
12+
13+
@Inject
14+
Template noAccess;
15+
16+
@Override
17+
public Response toResponse(NotAuthorizedException e) {
18+
return Response
19+
.status(401)
20+
.header("Content-Type", "text/html")
21+
.entity(noAccess
22+
.data("title", "Sorry, no access")
23+
.data("message", e.getMessage()))
24+
.build();
25+
}
26+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{#include partial/centered}
2+
<div class="front">
3+
<h1>{title}</h1>
4+
5+
<p>{message}</p>
6+
</div>
7+
{/include}

0 commit comments

Comments
 (0)