Skip to content

Commit

Permalink
Use verified paths
Browse files Browse the repository at this point in the history
  • Loading branch information
niknetniko committed Jan 19, 2025
1 parent 37618af commit 34fe08b
Show file tree
Hide file tree
Showing 22 changed files with 86 additions and 74 deletions.
2 changes: 1 addition & 1 deletion .formatter.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
import_deps: [:ecto, :phoenix, :ecto_sql],
inputs: ["*.{ex,exs,heex}", "priv/*/seeds.exs", "{config,lib,test}/**/*.{ex,exs,heex}"],
subdirectories: ["priv/*/migrations"],
plugins: [Phoenix.LiveView.HTMLFormatter],
plugins: [Phoenix.LiveView.HTMLFormatter]
]
16 changes: 14 additions & 2 deletions lib/zout_web.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@ defmodule ZoutWeb do
below. Instead, define any helper function in modules
and import those modules here.
"""
def static_paths, do: ~w(assets fonts images favicon.ico robots.txt)

def controller do
quote do
use Phoenix.Controller, namespace: ZoutWeb

import Plug.Conn
use Gettext, backend: ZoutWeb.Gettext
alias ZoutWeb.Router.Helpers, as: Routes

unquote(verified_routes())
end
end

Expand Down Expand Up @@ -99,7 +101,8 @@ defmodule ZoutWeb do

import ZoutWeb.ErrorHelpers
use Gettext, backend: ZoutWeb.Gettext
alias ZoutWeb.Router.Helpers, as: Routes

unquote(verified_routes())
end
end

Expand All @@ -109,4 +112,13 @@ defmodule ZoutWeb do
defmacro __using__(which) when is_atom(which) do
apply(__MODULE__, which, [])
end

def verified_routes do
quote do
use Phoenix.VerifiedRoutes,
endpoint: ZoutWeb.Endpoint,
router: ZoutWeb.Router,
statics: ZoutWeb.static_paths()
end
end
end
8 changes: 4 additions & 4 deletions lib/zout_web/controllers/auth_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ defmodule ZoutWeb.AuthController do

redirect_url =
case get_session(conn, :after_login_redirect) do
nil -> Routes.page_path(conn, :index)
nil -> ~p"/"
url -> url
end

Expand All @@ -54,14 +54,14 @@ defmodule ZoutWeb.AuthController do
def logout(conn, _) do
conn
|> Guardian.Plug.sign_out()
|> redirect(to: Routes.project_path(conn, :index))
|> redirect(to: ~p"/projects")
end

@impl true
def auth_error(conn, {:unauthenticated, reason}, _opts) do
IO.inspect("Unauthenticated due to")
IO.inspect(reason)
redirect(conn, to: Routes.auth_path(conn, :request, :zeus, %{from: current_path(conn)}))
redirect(conn, to: ~p"/auth/zeus?from=#{current_path(conn)}")
end

# Handle invalid tokens. This error needs special attention: if we do nothing,
Expand All @@ -74,7 +74,7 @@ defmodule ZoutWeb.AuthController do
conn
|> Guardian.Plug.sign_out()
|> put_flash(:error, "Ongeldige token, meld opnieuw aan.")
|> redirect(to: Routes.project_path(conn, :index))
|> redirect(to: ~p"/projects")
end

@impl true
Expand Down
2 changes: 1 addition & 1 deletion lib/zout_web/controllers/import_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ defmodule ZoutWeb.ImportController do
{:ok, _} ->
conn
|> put_flash(:info, "Import geslaagd.")
|> redirect(to: Routes.project_path(conn, :index))
|> redirect(to: ~p"/projects")

{:error, e} ->
conn
Expand Down
2 changes: 1 addition & 1 deletion lib/zout_web/controllers/page_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule ZoutWeb.PageController do

def index(conn, _params) do
# TODO, do something useful here.
redirect(conn, to: Routes.project_path(conn, :index))
redirect(conn, to: ~p"/projects")
end

def crash(conn, _params) do
Expand Down
4 changes: 2 additions & 2 deletions lib/zout_web/controllers/project_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ defmodule ZoutWeb.ProjectController do
case Data.create_project(params) do
{:ok, _project} ->
conn
|> redirect(to: Routes.project_path(conn, :index))
|> redirect(to: ~p"/projects")

{:error, changeset} ->
projects = Data.list_projects()
Expand Down Expand Up @@ -96,7 +96,7 @@ defmodule ZoutWeb.ProjectController do

case Data.update_project(project, params) do
{:ok, project} ->
redirect(conn, to: Routes.project_path(conn, :show, project))
redirect(conn, to: ~p"/projects/#{project}")

{:error, changeset} ->
projects = Data.list_projects()
Expand Down
2 changes: 1 addition & 1 deletion lib/zout_web/endpoint.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ defmodule ZoutWeb.Endpoint do
at: "/",
from: :zout,
gzip: false,
only: ~w(assets fonts images favicon.ico robots.txt)
only: ZoutWeb.static_paths()

# Code reloading can be explicitly enabled under the
# :code_reloader configuration of your endpoint.
Expand Down
2 changes: 1 addition & 1 deletion lib/zout_web/templates/import/index.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
</div>
<div class="card">
<div class="card-content">
<%= form_for @conn, Routes.import_path(@conn, :import), [as: :import], fn f -> %>
<%= form_for @conn, ~p"/import", [as: :import], fn f -> %>
<div>
<div class="field">
<%= label(f, :contents, "Inhoud DOT-bestand*", class: "label") %>
Expand Down
21 changes: 10 additions & 11 deletions lib/zout_web/templates/layout/_navbar.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,33 @@
<span aria-hidden="true"></span>
</a>
</div>

<!-- Menu -->
<!-- Menu -->
<div class="navbar-menu">

<div class="navbar-start">
<span class="navbar-item">
<small>Het Zeus Overzicht met Uitgebreide Toestanden</small>
</span>
</span>
</div>

<div class="navbar-end">
<% user = Guardian.Plug.current_resource(@conn) %>
<%= link "Overzicht", to: Routes.project_path(@conn, :index), class: "navbar-item" %>
{link("Overzicht", to: ~p"/projects", class: "navbar-item")}

<%= if Bodyguard.permit?(Data.Policy, :project_import, user) do %>
<%= link "Import", to: Routes.import_path(@conn, :index), class: "navbar-item" %>
{link("Import", to: ~p"/import", class: "navbar-item")}
<% end %>

<!-- User -->
<!-- User -->
<%= if is_nil(user) do %>
<%= link "Aanmelden met Zeus", to: Routes.auth_path(@conn, :request, :zeus), class: "navbar-item" %>
{link("Aanmelden met Zeus", to: ~p"/auth/zeus", class: "navbar-item")}
<% else %>
<div class="navbar-item has-dropdown is-hoverable">
<a class="navbar-link">
<%= user.nickname %>
{user.nickname}
</a>
<div class="navbar-dropdown is-boxed">
<%= link "Afmelden", to: Routes.auth_path(@conn, :logout), method: :post, class: "navbar-item" %>
{link("Afmelden", to: ~p"/auth/logout", method: :post, class: "navbar-item")}
</div>
</div>
<% end %>
Expand Down
6 changes: 3 additions & 3 deletions lib/zout_web/templates/layout/app.html.heex
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<main class="container container--main">
<p class="alert alert-info" role="alert"><%= get_flash(@conn, :info) %></p>
<p class="alert alert-danger" role="alert"><%= get_flash(@conn, :error) %></p>
<%= @inner_content %>
<p class="alert alert-info" role="alert">{get_flash(@conn, :info)}</p>
<p class="alert alert-danger" role="alert">{get_flash(@conn, :error)}</p>
{@inner_content}
</main>
10 changes: 3 additions & 7 deletions lib/zout_web/templates/layout/live.html.heex
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
<main class="container">
<p class="alert alert-info" role="alert"
phx-click="lv:clear-flash"
phx-value-key="info"><%# live_flash(@flash, :info) %></p>
<p class="alert alert-info" role="alert" phx-click="lv:clear-flash" phx-value-key="info"></p>

<p class="alert alert-danger" role="alert"
phx-click="lv:clear-flash"
phx-value-key="error"><%# live_flash(@flash, :error) %></p>
<p class="alert alert-danger" role="alert" phx-click="lv:clear-flash" phx-value-key="error"></p>

<%= @inner_content %>
{@inner_content}
</main>
21 changes: 13 additions & 8 deletions lib/zout_web/templates/layout/root.html.heex
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<meta name="csrf-token" content={csrf_token_value()}>
<%= live_title_tag assigns[:page_title] || view_module(@conn).title(view_template(@conn), assigns) || "Zeus Overzicht met Uitgebreide Toestanden", suffix: " · ZOUT" %>
<link phx-track-static rel="stylesheet" href={Routes.static_path(@conn, "/assets/app.css")}/>
<script phx-track-static type="text/javascript" src={Routes.static_path(@conn, "/assets/app.js")}></script>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="csrf-token" content={csrf_token_value()} />
{live_title_tag(
assigns[:page_title] || view_module(@conn).title(view_template(@conn), assigns) ||
"Zeus Overzicht met Uitgebreide Toestanden",
suffix: " · ZOUT"
)}
<link phx-track-static rel="stylesheet" href={~p"/assets/app.css"} />
<script phx-track-static type="text/javascript" src={~p"/assets/app.js"}>
</script>
</head>
<body>
<header>
<%= render "_navbar.html", conn: @conn %>
{render("_navbar.html", conn: @conn)}
</header>
<%= @inner_content %>
{@inner_content}
</body>
</html>
2 changes: 1 addition & 1 deletion lib/zout_web/templates/ping/show.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
Geldig van: <%= FormatHelpers.human_datetime(@ping.start) %><br>
Geldig tot: <%= FormatHelpers.human_datetime(@ping.stop) %><br>
Status: <%= ProjectView.status_icon(@ping) %> <%= status_text(@ping) %> <br>
Project: <%= link @ping.project.name, to: Routes.project_path(@conn, :show, @ping.project) %><br>
Project: <%= link @ping.project.name, to: ~p"/projects/#{@ping.project}" %><br>
</div>
</div>
2 changes: 1 addition & 1 deletion lib/zout_web/templates/project/_form.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
<div class="control">
<div class="is-flex is-justify-content-flex-end">
<%= if can?(@conn, :project_delete, @changeset.data) and !is_nil(@changeset.data.id) do %>
<%= button "Verwijderen", to: Routes.project_path(@conn, :delete, @changeset.data), class: "button is-danger" %>
<%= button "Verwijderen", to: ~p"/projects/#{@changeset.data}", method: :delete, class: "button is-danger" %>
<% end %>
<%= submit @submit, class: "button is-primary" %>
</div>
Expand Down
2 changes: 1 addition & 1 deletion lib/zout_web/templates/project/_graph.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
{
data: {
id: "<%= project.id %>",
name: '<%= javascript_escape(link "#{project.name}", to: Routes.project_path(@conn, :show, project)) %>',
name: '<%= javascript_escape(link "#{project.name}", to: ~p"/projects/#{project}") %>',
color: '<%= status_colour(ping) %>',
text: '<%= render_status(ping, false) %>',
textColor: '<%= text_colour(ping) %>',
Expand Down
8 changes: 4 additions & 4 deletions lib/zout_web/templates/project/_table.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@
<tbody>
<%= for %{ project: project, ping: ping } <- @projects_and_pings do %>
<tr>
<td class="order-item-info"><%= link project.name, to: Routes.project_path(@conn, :show, project) %></td>
<td class="order-item-info"><%= link project.name, to: ~p"/projects/#{project}" %></td>
<td>
<%= if is_nil(ping) do %>
<%= render_status(ping) %>
<% else %>
<%= link to: Routes.ping_path(@conn, :show, Data.get_ping_id(ping)) do %>
<%= link to: ~p"/pings/#{Data.get_ping_id(ping)}" do %>
<%= render_status(ping) %>
<% end %>
<% end %>
</td>
<td>
<%= if can?(@conn, :project_edit, project) do %>
<%= link "Bewerken", to: Routes.project_path(@conn, :edit, project) %>
<%= link "Bewerken", to: ~p"/projects/#{project}/edit" %>
<% end %>
</td>
</tr>
Expand All @@ -32,7 +32,7 @@
<div class="is-flex is-justify-content-space-between is-align-content-center">
<span class="mt-4">Laatst gecontroleerd: <%= last_checked(@projects_and_pings) |> FormatHelpers.human_datetime() %></span>
<%= if can?(@conn, :project_new) do %>
<%= link "Nieuw project", to: Routes.project_path(@conn, :new), class: "button is-primary" %>
<%= link "Nieuw project", to: ~p"/projects/new", class: "button is-primary" %>
<% end %>
</div>
</div>
2 changes: 1 addition & 1 deletion lib/zout_web/templates/project/edit.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
</div>
<div class="card">
<div class="card-content">
<%= render "_form.html", conn: @conn, path: Routes.project_path(@conn, :update, @changeset.data), changeset: @changeset, projects: @projects, submit: "Opslaan" %>
<%= render "_form.html", conn: @conn, path: ~p"/projects/#{@changeset.data}", changeset: @changeset, projects: @projects, submit: "Opslaan" %>
</div>
</div>
6 changes: 3 additions & 3 deletions lib/zout_web/templates/project/index.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
<div class="tabs is-toggle">
<ul>
<li class="<%= if @format == :table, do: "is-active", else: "" %>">
<%= link "Tabel", to: Routes.project_path(@conn, :index, format: "table") %>
<%= link "Tabel", to: ~p"/projects?format=table" %>
</li>
<li class="<%= if @format == :avail, do: "is-active", else: "" %>">
<%= link "Overzichtsgrafiek", to: Routes.project_path(@conn, :index, format: "avail") %>
<%= link "Overzichtsgrafiek", to: ~p"/projects?format=avail" %>
</li>
<li class="<%= if @format == :graph, do: "is-active", else: "" %>">
<%= link "Graaf", to: Routes.project_path(@conn, :index, format: "graph") %>
<%= link "Graaf", to: ~p"/projects?format=graph" %>
</li>
</ul>
</div>
Expand Down
2 changes: 1 addition & 1 deletion lib/zout_web/templates/project/new.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
</div>
<div class="card">
<div class="card-content">
<%= render "_form.html", conn: @conn, path: Routes.project_path(@conn, :create), changeset: @changeset, projects: @projects, submit: "Toevoegen" %>
<%= render "_form.html", conn: @conn, path: ~p"/projects", changeset: @changeset, projects: @projects, submit: "Toevoegen" %>
</div>
</div>
2 changes: 1 addition & 1 deletion lib/zout_web/templates/project/show.html.eex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<h1 class="title">
<%= @project.name %>
<%= if can?(@conn, :project_edit, @project) do %>
<%= link "Bewerken", to: Routes.project_path(@conn, :edit, @project), class: "button is-primary is-small" %>
<%= link "Bewerken", to: ~p"/projects/#{@project}/edit", class: "button is-primary is-small" %>
<% end %>
</h1>
<div class="subtitle">
Expand Down
4 changes: 2 additions & 2 deletions test/support/conn_case.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ defmodule ZoutWeb.ConnCase do
import ZoutWeb.ConnCase
import Zout.Factory

alias ZoutWeb.Router.Helpers, as: Routes

# The default endpoint for testing
@endpoint ZoutWeb.Endpoint

use ZoutWeb, :verified_routes

@doc """
Log the user in by putting the data in the conn.
"""
Expand Down
Loading

0 comments on commit 34fe08b

Please sign in to comment.