Skip to content

Commit

Permalink
better permission denied pages for broadcast forms
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Jun 17, 2024
1 parent 62bbbee commit bd12c53
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 9 deletions.
13 changes: 11 additions & 2 deletions app/controllers/RelayRound.scala
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,17 @@ final class RelayRound(
}

def edit(id: RelayRoundId) = Auth { ctx ?=> me ?=>
FoundPage(env.relay.api.formNavigation(id)): (round, nav) =>
views.relay.form.round.edit(round, env.relay.roundForm.edit(round), nav)
env.relay.api
.byIdAndContributor(id)
.flatMap:
case None =>
Found(env.relay.api.formNavigation(id)): (_, nav) =>
Forbidden.page(views.relay.form.noAccess(nav))
case Some(rt) =>
env.relay.api
.formNavigation(rt)
.flatMap: (round, nav) =>
Ok.page(views.relay.form.round.edit(round, env.relay.roundForm.edit(round), nav))
}

def update(id: RelayRoundId) = AuthOrScopedBody(_.Study.Write) { ctx ?=> me ?=>
Expand Down
9 changes: 5 additions & 4 deletions app/controllers/RelayTour.scala
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,11 @@ final class RelayTour(env: Env, apiC: => Api) extends LilaController(env):
id: RelayTourId
)(f: (FormNavigation) => Fu[Result])(using Context, Me): Fu[Result] =
WithTour(id): tour =>
env.relay.api
.canUpdate(tour)
.elseNotFound:
env.relay.api.formNavigation(tour).flatMap(f)
for
canUpdate <- env.relay.api.canUpdate(tour)
nav <- env.relay.api.formNavigation(tour)
res <- if canUpdate then f(nav) else Forbidden.page(views.relay.form.noAccess(nav))
yield res

private[controllers] def rateLimitCreation(
fail: => Fu[Result]
Expand Down
8 changes: 5 additions & 3 deletions modules/relay/src/main/RelayApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,11 @@ final class RelayApi(
relay.withTour(tour)

def formNavigation(id: RelayRoundId)(using me: Me): Fu[Option[(RelayRound, ui.FormNavigation)]] =
byIdAndContributor(id).flatMapz: rt =>
formNavigation(rt.tour).map: nav =>
(rt.round, nav.copy(round = rt.round.id.some)).some
byIdWithTour(id).flatMapz(rt => formNavigation(rt).dmap(some))

def formNavigation(rt: RelayRound.WithTour)(using me: Me): Fu[(RelayRound, ui.FormNavigation)] =
formNavigation(rt.tour).map: nav =>
(rt.round, nav.copy(round = rt.round.id.some))

def formNavigation(tour: RelayTour)(using me: Me): Fu[ui.FormNavigation] = for
group <- withTours.get(tour.id)
Expand Down
12 changes: 12 additions & 0 deletions modules/relay/src/main/ui/FormUi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,18 @@ final class FormUi(helpers: Helpers, ui: RelayUi, tourUi: RelayTourUi):
)
)

def noAccess(nav: FormNavigation)(using Context) =
Page("Insufficient permissions")
.css("bits.relay.form")
.wrap: body =>
main(cls := "page page-menu")(
navigationMenu(nav),
div(cls := "page-menu__content box box-pad")(
boxTop(h1("Insufficient permissions")),
p("You are not allowed to edit this broadcast or round.")
)
)

object round:

private def page(title: String, nav: FormNavigation)(using Context) =
Expand Down

0 comments on commit bd12c53

Please sign in to comment.