Skip to content

Commit

Permalink
Merge pull request #26 from RatioPBC/verified_routes
Browse files Browse the repository at this point in the history
convert to verified routes
  • Loading branch information
lei0zhou authored Aug 20, 2024
2 parents 6c9036a + d1b0db3 commit dc72350
Show file tree
Hide file tree
Showing 81 changed files with 190 additions and 301 deletions.
5 changes: 2 additions & 3 deletions lib/epicenter/release.ex
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
defmodule Epicenter.Release do
alias Epicenter.Accounts
alias Epicenter.AuditLog
alias EpicenterWeb.Endpoint
alias EpicenterWeb.Router.Helpers, as: Routes
use Phoenix.VerifiedRoutes, endpoint: EpicenterWeb.Endpoint, router: EpicenterWeb.Router

@app :epicenter

Expand Down Expand Up @@ -143,7 +142,7 @@ defmodule Epicenter.Release do
defp generated_password_reset_url(user) do
{:ok, %{body: body}} =
Accounts.deliver_user_reset_password_instructions(user, fn encoded_token ->
Routes.user_reset_password_url(Endpoint, :edit, encoded_token)
url(~p"/users/reset-password/#{encoded_token}")
end)

[_body, url] = Regex.run(~r|\n(https?://[^\n]+)\n|, body)
Expand Down
13 changes: 11 additions & 2 deletions lib/epicenter_web.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ defmodule EpicenterWeb do

import Plug.Conn
import Epicenter.Gettext
alias EpicenterWeb.Router.Helpers, as: Routes
use Phoenix.VerifiedRoutes, endpoint: EpicenterWeb.Endpoint, router: EpicenterWeb.Router
end
end

Expand All @@ -49,6 +49,7 @@ defmodule EpicenterWeb do
use PhoenixHTMLHelpers
use Phoenix.LiveView, layout: {EpicenterWeb.LayoutView, :live}
use EpicenterWeb.SearchHandling
use Phoenix.VerifiedRoutes, endpoint: EpicenterWeb.Endpoint, router: EpicenterWeb.Router

unquote(view_helpers())
end
Expand Down Expand Up @@ -81,6 +82,14 @@ defmodule EpicenterWeb do
end
end

def presenter do
quote do
use Phoenix.Component
use Phoenix.VerifiedRoutes, endpoint: EpicenterWeb.Endpoint, router: EpicenterWeb.Router
alias EpicenterWeb.Format
end
end

defp view_helpers do
quote do
# Use all HTML functionality (forms, tags, etc)
Expand All @@ -97,7 +106,7 @@ defmodule EpicenterWeb do

import Epicenter.Gettext
import EpicenterWeb.ErrorHelpers
alias EpicenterWeb.Router.Helpers, as: Routes
use Phoenix.VerifiedRoutes, endpoint: EpicenterWeb.Endpoint, router: EpicenterWeb.Router
end
end

Expand Down
7 changes: 3 additions & 4 deletions lib/epicenter_web/controllers/import_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ defmodule EpicenterWeb.ImportController do
LiveView doesn't currently support file uploads, so this is a regular controller. It could probably be enhanced
with some ajax or something.
"""

use EpicenterWeb, :controller

import EpicenterWeb.ControllerHelpers, only: [assign_defaults: 2]
Expand All @@ -28,17 +27,17 @@ defmodule EpicenterWeb.ImportController do

conn
|> Session.set_last_csv_import_info(popped_import_info)
|> redirect(to: Routes.import_path(conn, :show))
|> redirect(to: ~p"/import/complete")

{:error, [user_readable: user_readable_message]} ->
conn
|> Session.set_import_error_message(user_readable_message)
|> redirect(to: Routes.import_start_path(conn, EpicenterWeb.ImportLive))
|> redirect(to: ~p"/import/start")

{:error, %DateParsingError{user_readable: user_readable_message}} ->
conn
|> Session.set_import_error_message(user_readable_message)
|> redirect(to: Routes.import_start_path(conn, EpicenterWeb.ImportLive))
|> redirect(to: ~p"/import/start")
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/epicenter_web/controllers/root_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ defmodule EpicenterWeb.RootController do
use EpicenterWeb, :controller

def show(conn, _params) do
conn |> redirect(to: Routes.people_path(conn, EpicenterWeb.PeopleLive))
conn |> redirect(to: ~p"/people")
end
end
11 changes: 5 additions & 6 deletions lib/epicenter_web/controllers/user_auth.ex
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
defmodule EpicenterWeb.UserAuth do
import Plug.Conn
import Phoenix.Controller
use EpicenterWeb, :controller

alias Epicenter.Accounts
alias EpicenterWeb.Session
alias EpicenterWeb.Router.Helpers, as: Routes

@doc """
Logs the user in.
Expand All @@ -22,7 +21,7 @@ defmodule EpicenterWeb.UserAuth do
user_token = Accounts.generate_user_session_token(user)

user_return_to = get_session(conn, :user_return_to)
mfa_path = user.mfa_secret == nil && Routes.user_multifactor_auth_setup_path(conn, :new)
mfa_path = user.mfa_secret == nil && ~p"/users/mfa-setup"
user_agent = get_req_header(conn, "user-agent") |> Euclid.Extra.List.first("user_agent_not_found")

{:ok, _} = Accounts.create_login(%{session_id: user_token.id, user_agent: user_agent, user_id: user.id})
Expand Down Expand Up @@ -125,9 +124,9 @@ defmodule EpicenterWeb.UserAuth do
they use the application at all, here would be a good place.
"""
def require_authenticated_user(conn, opts) do
login_path = Routes.user_session_path(conn, :new)
mfa_setup_path = Routes.user_multifactor_auth_setup_path(conn, :new)
mfa_path = Routes.user_multifactor_auth_path(conn, :new)
login_path = ~p"/users/login"
mfa_setup_path = ~p"/users/mfa-setup"
mfa_path = ~p"/users/mfa"

error =
case user_authentication_status(conn, opts) do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
defmodule EpicenterWeb.UserMultifactorAuthSetupController do
use EpicenterWeb, :controller

import EpicenterWeb.ControllerHelpers, only: [assign_defaults: 1]

alias Epicenter.Accounts
Expand Down Expand Up @@ -30,7 +29,7 @@ defmodule EpicenterWeb.UserMultifactorAuthSetupController do
}}
)

conn |> Session.put_multifactor_auth_success(true) |> redirect(to: Routes.root_path(conn, :show))
conn |> Session.put_multifactor_auth_success(true) |> redirect(to: ~p"/")

{:error, message} ->
conn
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
defmodule EpicenterWeb.UserResetPasswordController do
use EpicenterWeb, :controller

import EpicenterWeb.ControllerHelpers, only: [assign_defaults: 2]

alias Epicenter.Accounts
Expand All @@ -21,7 +20,7 @@ defmodule EpicenterWeb.UserResetPasswordController do
{:ok, _} =
Accounts.deliver_user_reset_password_instructions(
user,
&Routes.user_reset_password_url(conn, :edit, &1)
&url(~p"/users/reset-password/#{&1}")
)
else
conn
Expand Down Expand Up @@ -50,7 +49,7 @@ defmodule EpicenterWeb.UserResetPasswordController do
{:ok, _} ->
conn
|> put_flash(:info, "Password reset successfully — please log in")
|> redirect(to: Routes.user_session_path(conn, :new))
|> redirect(to: ~p"/users/login")

{:error, changeset} ->
conn |> assign_defaults(@common_assigns) |> render("edit.html", changeset: changeset)
Expand Down
3 changes: 1 addition & 2 deletions lib/epicenter_web/controllers/user_session_controller.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
defmodule EpicenterWeb.UserSessionController do
use EpicenterWeb, :controller

import EpicenterWeb.ControllerHelpers, only: [assign_defaults: 2]

alias Epicenter.Accounts
Expand Down Expand Up @@ -44,7 +43,7 @@ defmodule EpicenterWeb.UserSessionController do

with {:user, {:ok, user}} <- {:user, Accounts.register_user({new_user_attrs, audit_meta})},
{:token, {:ok, token}} <- {:token, Accounts.generate_user_reset_password_token(user)} do
conn |> redirect(to: Routes.user_reset_password_path(conn, :edit, token))
conn |> redirect(to: ~p"/users/reset-password/#{token}")
else
{:user, {:error, %Ecto.Changeset{valid?: false} = changeset}} ->
error_message =
Expand Down
3 changes: 1 addition & 2 deletions lib/epicenter_web/controllers/user_settings_controller.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
defmodule EpicenterWeb.UserSettingsController do
use EpicenterWeb, :controller

import EpicenterWeb.ControllerHelpers, only: [assign_defaults: 2]

alias Epicenter.Accounts
Expand Down Expand Up @@ -30,7 +29,7 @@ defmodule EpicenterWeb.UserSettingsController do
{:ok, user} ->
conn
|> put_flash(:info, "Password updated successfully")
|> put_session(:user_return_to, Routes.user_settings_path(conn, :edit))
|> put_session(:user_return_to, ~p"/users/settings")
|> UserAuth.log_in_user(user)

{:error, changeset} ->
Expand Down
2 changes: 1 addition & 1 deletion lib/epicenter_web/live/add_visit_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ defmodule EpicenterWeb.AddVisitLive do
data = data |> Map.put(:place_id, socket.assigns.place.id),
{:save, {:ok, _visit}} <- {:save, Cases.create_visit({data, audit_meta_create_visit(socket)})} do
socket
|> push_navigate(to: "#{Routes.profile_path(socket, EpicenterWeb.ProfileLive, socket.assigns.case_investigation.person)}#case-investigations")
|> push_navigate(to: ~p"/people/#{socket.assigns.case_investigation.person}/#case-investigations")
|> noreply()
else
{:data, {:error, %Ecto.Changeset{} = form_changeset}} ->
Expand Down
2 changes: 1 addition & 1 deletion lib/epicenter_web/live/add_visit_live.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
>
<section>
<header id="subnav">
<.link navigate={Routes.place_search_path(EpicenterWeb.Endpoint, EpicenterWeb.PlaceSearchLive, @case_investigation)}
<.link navigate={~p"/case-investigations/#{@case_investigation}/place-search"}
data-role="back-link"><%= back_icon() %><span>Back</span></.link>
</header>
</section>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ defmodule EpicenterWeb.CaseInvestigationClinicalDetailsLive do
{:form, {:ok, case_investigation_attrs}} <- {:form, ClinicalDetailsForm.case_investigation_attrs(form_changeset)},
{:case_investigation, {:ok, _case_investigation}} <- {:case_investigation, update_case_investigation(socket, case_investigation_attrs)} do
socket
|> push_navigate(to: "#{Routes.profile_path(socket, EpicenterWeb.ProfileLive, socket.assigns.case_investigation.person)}#case-investigations")
|> push_navigate(to: ~p"/people/#{socket.assigns.case_investigation.person}/#case-investigations")
|> noreply()
else
{:form, {:error, form_changeset}} ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
>
<section>
<header id="subnav">
<.link navigate={Routes.profile_path(EpicenterWeb.Endpoint, EpicenterWeb.ProfileLive, @case_investigation.person)}
<.link navigate={~p"/people/#{@case_investigation.person}"}
data-role="back-link"><%= back_icon() %><span>Back</span></.link>
</header>
</section>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ defmodule EpicenterWeb.CaseInvestigationConcludeIsolationMonitoringLive do
}}
)} do
socket
|> push_navigate(to: "#{Routes.profile_path(socket, EpicenterWeb.ProfileLive, socket.assigns.case_investigation.person)}#case-investigations")
|> push_navigate(to: ~p"/people/#{socket.assigns.case_investigation.person}/#case-investigations")
|> noreply()
else
{:form, {:error, %Ecto.Changeset{valid?: false} = form_changeset}} ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
>
<section>
<header id="subnav">
<.link navigate={Routes.profile_path(EpicenterWeb.Endpoint, EpicenterWeb.ProfileLive, @case_investigation.person)}
<.link navigate={~p"/people/#{@case_investigation.person}"}
data-role="back-link"><%= back_icon() %><span>Back</span></.link>
</header>
</section>
Expand Down
2 changes: 1 addition & 1 deletion lib/epicenter_web/live/case_investigation_contact_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ defmodule EpicenterWeb.CaseInvestigationContactLive do
data = data |> Map.put(:exposing_case_id, socket.assigns.case_investigation.id),
{:created, {:ok, _}} <- {:created, create_or_update_contact_investigation(contact_investigation, data, socket.assigns.current_user)} do
socket
|> push_navigate(to: "#{Routes.profile_path(socket, EpicenterWeb.ProfileLive, socket.assigns.case_investigation.person)}#case-investigations")
|> push_navigate(to: ~p"/people/#{socket.assigns.case_investigation.person}/#case-investigations")
|> noreply()
else
{:form, {:error, changeset}} ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
>
<section>
<header id="subnav">
<.link navigate={Routes.profile_path(EpicenterWeb.Endpoint, EpicenterWeb.ProfileLive, @case_investigation.person)}
<.link navigate={~p"/people/#{@case_investigation.person}"}
data-role="back-link"><%= back_icon() %><span>Back</span></.link>
</header>
</section>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ defmodule EpicenterWeb.CaseInvestigationDiscontinueLive do
}}
) do
socket
|> push_navigate(to: "#{Routes.profile_path(socket, EpicenterWeb.ProfileLive, socket.assigns.case_investigation.person)}#case-investigations")
|> push_navigate(to: ~p"/people/#{socket.assigns.case_investigation.person}/#case-investigations")
|> noreply()
else
{:error, changeset} ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
>
<section>
<header id="subnav">
<.link navigate={Routes.profile_path(EpicenterWeb.Endpoint, EpicenterWeb.ProfileLive, @case_investigation.person)}
<.link navigate={~p"/people/#{@case_investigation.person}"}
data-role="back-link"><%= back_icon() %><span>Back</span></.link>
</header>
</section>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ defmodule EpicenterWeb.CaseInvestigationIsolationMonitoringLive do
{:form, {:ok, model_attrs}} <- {:form, IsolationMonitoringForm.form_changeset_to_model_attrs(form_changeset)},
{:case_investigation, {:ok, _case_investigation}} <- {:case_investigation, update_case_investigation(socket, model_attrs)} do
socket
|> push_navigate(to: "#{Routes.profile_path(socket, EpicenterWeb.ProfileLive, socket.assigns.case_investigation.person)}#case-investigations")
|> push_navigate(to: ~p"/people/#{socket.assigns.case_investigation.person}/#case-investigations")
|> noreply()
else
{:form, {:error, %Ecto.Changeset{valid?: false} = form_changeset}} ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
>
<section>
<header id="subnav">
<.link navigate={Routes.profile_path(EpicenterWeb.Endpoint, EpicenterWeb.ProfileLive, @case_investigation.person)}
<.link navigate={~p"/people/#{@case_investigation.person}"}
data-role="back-link"><%= back_icon() %><span>Back</span></.link>
</header>
</section>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ defmodule EpicenterWeb.CaseInvestigationIsolationOrderLive do
{:form, {:ok, model_attrs}} <- {:form, IsolationOrderForm.form_changeset_to_model_attrs(form_changeset)},
{:case_investigation, {:ok, _case_investigation}} <- {:case_investigation, update_case_investigation(socket, model_attrs)} do
socket
|> push_navigate(to: "#{Routes.profile_path(socket, EpicenterWeb.ProfileLive, socket.assigns.case_investigation.person)}#case-investigations")
|> push_navigate(to: ~p"/people/#{socket.assigns.case_investigation.person}/#case-investigations")
|> noreply()
else
{:form, {:error, %Ecto.Changeset{valid?: false} = form_changeset}} ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
>
<section>
<header id="subnav">
<.link navigate={Routes.profile_path(EpicenterWeb.Endpoint, EpicenterWeb.ProfileLive, @case_investigation.person)}
<.link navigate={~p"/people/#{@case_investigation.person}"}
data-role="back-link"><%= back_icon() %><span>Back</span></.link>
</header>
</section>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ defmodule EpicenterWeb.CaseInvestigationStartInterviewLive do
{:form, {:ok, cast_investigation_attrs}} <- {:form, StartInterviewForm.investigation_attrs(form_changeset)},
{:case_investigation, {:ok, _case_investigation}} <- {:case_investigation, update_case_investigation(socket, cast_investigation_attrs)} do
socket
|> push_navigate(to: "#{Routes.profile_path(socket, EpicenterWeb.ProfileLive, socket.assigns.case_investigation.person)}#case-investigations")
|> push_navigate(to: ~p"/people/#{socket.assigns.case_investigation.person}/#case-investigations")
|> noreply()
else
{:form, {:error, %Ecto.Changeset{valid?: false} = form_changeset}} ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
>
<section>
<header id="subnav">
<.link navigate={Routes.profile_path(EpicenterWeb.Endpoint, EpicenterWeb.ProfileLive, @case_investigation.person)}
<.link navigate={~p"/people/#{@case_investigation.person}"}
data-role="back-link"><%= back_icon() %><span>Back</span></.link>
</header>
</section>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ defmodule EpicenterWeb.ContactInvestigationClinicalDetailsLive do
person = socket.assigns.contact_investigation.exposed_person

socket
|> push_navigate(to: "#{Routes.profile_path(socket, EpicenterWeb.ProfileLive, person)}#contact-investigations")
|> push_navigate(to: ~p"/people/#{person}/#contact-investigations")
|> noreply()
else
{:form, {:error, form_changeset}} ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
>
<section>
<header id="subnav">
<.link navigate={Routes.profile_path(EpicenterWeb.Endpoint, EpicenterWeb.ProfileLive, @contact_investigation.exposed_person)}
<.link navigate={~p"/people/#{@contact_investigation.exposed_person}"}
data-role="back-link"><%= back_icon() %><span>Back</span></.link>
</header>
</section>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,7 @@ defmodule EpicenterWeb.ContactInvestigationConcludeQuarantineMonitoringLive do
}}
)} do
socket
|> push_navigate(
to: "#{Routes.profile_path(socket, EpicenterWeb.ProfileLive, socket.assigns.contact_investigation.exposed_person)}#contact-investigations"
)
|> push_navigate(to: ~p"/people/#{socket.assigns.contact_investigation.exposed_person}/#contact-investigations")
|> noreply()
else
{:form, {:error, %Ecto.Changeset{valid?: false} = form_changeset}} ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
>
<section>
<header id="subnav">
<.link navigate={Routes.profile_path(EpicenterWeb.Endpoint, EpicenterWeb.ProfileLive, @contact_investigation.exposed_person)}
<.link navigate={~p"/people/#{@contact_investigation.exposed_person}"}
data-role="back-link"><%= back_icon() %><span>Back</span></.link>
</header>
</section>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ defmodule EpicenterWeb.ContactInvestigationDiscontinueLive do
)

socket
|> push_navigate(
to: "#{Routes.profile_path(socket, EpicenterWeb.ProfileLive, socket.assigns.contact_investigation.exposed_person)}#contact-investigations"
)
|> push_navigate(to: ~p"/people/#{socket.assigns.contact_investigation.exposed_person}/#contact-investigations")
|> noreply()
else
{:error, changeset} ->
Expand Down
Loading

0 comments on commit dc72350

Please sign in to comment.