From 8f4672b5168f746702544fbd9d4387885020b526 Mon Sep 17 00:00:00 2001 From: SphericalKat Date: Thu, 7 Oct 2021 05:58:00 +0530 Subject: [PATCH] refactor(auth): use heex templates Signed-off-by: SphericalKat --- config/releases.exs | 51 ++++++++++--------- lib/ketbin/pastes/utils.ex | 11 ++-- lib/ketbin_web/controllers/page_controller.ex | 13 ++++- lib/ketbin_web/controllers/user_auth.ex | 5 +- .../controllers/user_settings_controller.ex | 1 + .../{new.html.eex => new.html.heex} | 0 .../{new.html.eex => new.html.heex} | 0 .../{edit.html.eex => edit.html.heex} | 0 .../{new.html.eex => new.html.heex} | 0 .../{new.html.eex => new.html.heex} | 0 .../{edit.html.eex => edit.html.heex} | 4 +- mix.exs | 2 +- .../controllers/paste_controller_test.exs | 1 + 13 files changed, 54 insertions(+), 34 deletions(-) rename lib/ketbin_web/templates/user_confirmation/{new.html.eex => new.html.heex} (100%) rename lib/ketbin_web/templates/user_registration/{new.html.eex => new.html.heex} (100%) rename lib/ketbin_web/templates/user_reset_password/{edit.html.eex => edit.html.heex} (100%) rename lib/ketbin_web/templates/user_reset_password/{new.html.eex => new.html.heex} (100%) rename lib/ketbin_web/templates/user_session/{new.html.eex => new.html.heex} (100%) rename lib/ketbin_web/templates/user_settings/{edit.html.eex => edit.html.heex} (96%) diff --git a/config/releases.exs b/config/releases.exs index d82088a..89e5b6e 100644 --- a/config/releases.exs +++ b/config/releases.exs @@ -33,31 +33,34 @@ config :ketbin, KetbinWeb.Endpoint, secret_key_base: secret_key_base, server: true - smtp_relay = - System.get_env("SWOOSH_SMTP_RELAY") || - raise """ - environment variable SWOOSH_SMTP_RELAY is missing. - """ - username = - System.get_env("SWOOSH_USERNAME") || - raise """ - environment variable SWOOSH_USERNAME is missing. - """ - password = - System.get_env("SWOOSH_PASSWORD") || - raise """ - environment variable SWOOSH_PASSWORD is missing. - """ +smtp_relay = + System.get_env("SWOOSH_SMTP_RELAY") || + raise """ + environment variable SWOOSH_SMTP_RELAY is missing. + """ + +username = + System.get_env("SWOOSH_USERNAME") || + raise """ + environment variable SWOOSH_USERNAME is missing. + """ + +password = + System.get_env("SWOOSH_PASSWORD") || + raise """ + environment variable SWOOSH_PASSWORD is missing. + """ + +# configure mailer +config :ketbin, Ketbin.Mailer, + adapter: Swoosh.Adapters.SMTP, + relay: smtp_relay, + username: username, + password: password, + tls: :always, + auth: :always, + port: 587 - # configure mailer - config :ketbin, Ketbin.Mailer, - adapter: Swoosh.Adapters.SMTP, - relay: smtp_relay, - username: username, - password: password, - tls: :always, - auth: :always, - port: 587 # ## Using releases (Elixir v1.9+) # # If you are doing OTP releases, you need to instruct Phoenix diff --git a/lib/ketbin/pastes/utils.ex b/lib/ketbin/pastes/utils.ex index 15c3c62..d59fe30 100644 --- a/lib/ketbin/pastes/utils.ex +++ b/lib/ketbin/pastes/utils.ex @@ -13,14 +13,19 @@ defmodule Ketbin.Pastes.Utils do def generate_key(length \\ 10) do random = Enum.random([0, 1]) - Enum.map(0..length, fn i -> if Integer.mod(i, 2) == random, do: rand_consonant(), else: rand_vowel() end) - |> List.to_string + + Enum.map(0..length, fn i -> + if Integer.mod(i, 2) == random, do: rand_consonant(), else: rand_vowel() + end) + |> List.to_string() end def is_url?(url) do try do uri = URI.parse(url) - uri.scheme != nil && uri.host =~ "." && Enum.member?(["https", "http", "mailto"], uri.scheme) && !(uri.host =~ "katb.in") + + uri.scheme != nil && uri.host =~ "." && + Enum.member?(["https", "http", "mailto"], uri.scheme) && !(uri.host =~ "katb.in") rescue FunctionClauseError -> false end diff --git a/lib/ketbin_web/controllers/page_controller.ex b/lib/ketbin_web/controllers/page_controller.ex index 26ae932..df921c7 100644 --- a/lib/ketbin_web/controllers/page_controller.ex +++ b/lib/ketbin_web/controllers/page_controller.ex @@ -23,14 +23,23 @@ defmodule KetbinWeb.PageController do if paste.is_url do redirect(conn, external: paste.content) else - render(conn, "show.html", paste: paste, show_edit: show_edit, extension: List.first(tail) || "") + render(conn, "show.html", + paste: paste, + show_edit: show_edit, + extension: List.first(tail) || "" + ) end end def showlink(%{assigns: %{show_edit: show_edit}} = conn, %{"id" => id}) do [head | tail] = String.split(id, ".") paste = Pastes.get_paste!(head) - render(conn, "show.html", paste: paste, show_edit: show_edit, extension: (if tail == [], do: "", else: tail)) + + render(conn, "show.html", + paste: paste, + show_edit: show_edit, + extension: if(tail == [], do: "", else: tail) + ) end def raw(conn, %{"id" => id}) do diff --git a/lib/ketbin_web/controllers/user_auth.ex b/lib/ketbin_web/controllers/user_auth.ex index 8a37115..748265e 100644 --- a/lib/ketbin_web/controllers/user_auth.ex +++ b/lib/ketbin_web/controllers/user_auth.ex @@ -97,15 +97,16 @@ defmodule KetbinWeb.UserAuth do end def owns_paste(%{params: %{"id" => id}, assigns: %{current_user: user}} = conn, _params) do - [head|_tail] = String.split(id, ".") + [head | _tail] = String.split(id, ".") paste = Pastes.get_paste!(head) assign(conn, :show_edit, (user && user.id == paste.belongs_to) || false) end def ensure_owns_paste(%{params: %{"id" => id}, assigns: %{current_user: user}} = conn, _params) do - [head|_tail] = String.split(id, ".") + [head | _tail] = String.split(id, ".") paste = Pastes.get_paste!(head) allow_edit = (user && user.id == paste.belongs_to) || false + unless allow_edit do conn |> put_flash(:error, "You don't own this paste!") diff --git a/lib/ketbin_web/controllers/user_settings_controller.ex b/lib/ketbin_web/controllers/user_settings_controller.ex index 76d3a2f..a3f3f78 100644 --- a/lib/ketbin_web/controllers/user_settings_controller.ex +++ b/lib/ketbin_web/controllers/user_settings_controller.ex @@ -46,6 +46,7 @@ defmodule KetbinWeb.UserSettingsController do |> UserAuth.log_in_user(user) conn + {:error, changeset} -> render(conn, "edit.html", password_changeset: changeset) end diff --git a/lib/ketbin_web/templates/user_confirmation/new.html.eex b/lib/ketbin_web/templates/user_confirmation/new.html.heex similarity index 100% rename from lib/ketbin_web/templates/user_confirmation/new.html.eex rename to lib/ketbin_web/templates/user_confirmation/new.html.heex diff --git a/lib/ketbin_web/templates/user_registration/new.html.eex b/lib/ketbin_web/templates/user_registration/new.html.heex similarity index 100% rename from lib/ketbin_web/templates/user_registration/new.html.eex rename to lib/ketbin_web/templates/user_registration/new.html.heex diff --git a/lib/ketbin_web/templates/user_reset_password/edit.html.eex b/lib/ketbin_web/templates/user_reset_password/edit.html.heex similarity index 100% rename from lib/ketbin_web/templates/user_reset_password/edit.html.eex rename to lib/ketbin_web/templates/user_reset_password/edit.html.heex diff --git a/lib/ketbin_web/templates/user_reset_password/new.html.eex b/lib/ketbin_web/templates/user_reset_password/new.html.heex similarity index 100% rename from lib/ketbin_web/templates/user_reset_password/new.html.eex rename to lib/ketbin_web/templates/user_reset_password/new.html.heex diff --git a/lib/ketbin_web/templates/user_session/new.html.eex b/lib/ketbin_web/templates/user_session/new.html.heex similarity index 100% rename from lib/ketbin_web/templates/user_session/new.html.eex rename to lib/ketbin_web/templates/user_session/new.html.heex diff --git a/lib/ketbin_web/templates/user_settings/edit.html.eex b/lib/ketbin_web/templates/user_settings/edit.html.heex similarity index 96% rename from lib/ketbin_web/templates/user_settings/edit.html.eex rename to lib/ketbin_web/templates/user_settings/edit.html.heex index 19cd6c8..3bde23a 100644 --- a/lib/ketbin_web/templates/user_settings/edit.html.eex +++ b/lib/ketbin_web/templates/user_settings/edit.html.heex @@ -1,4 +1,4 @@ -

Settings

+

Settings

<%= form_for @email_changeset, Routes.user_settings_path(@conn, :update), [class: "flex flex-col h-full justify-center items-start m-auto"], fn f -> %> @@ -63,4 +63,4 @@ <%= submit "Change password" %>
<% end %> -
+
diff --git a/mix.exs b/mix.exs index c097277..5d0a672 100644 --- a/mix.exs +++ b/mix.exs @@ -20,7 +20,7 @@ defmodule Ketbin.MixProject do def application do [ mod: {Ketbin.Application, []}, - extra_applications: [:logger, :runtime_tools, :ssl], + extra_applications: [:logger, :runtime_tools, :ssl] ] end diff --git a/test/ketbin_web/controllers/paste_controller_test.exs b/test/ketbin_web/controllers/paste_controller_test.exs index 06aaf5c..0ef8bcb 100644 --- a/test/ketbin_web/controllers/paste_controller_test.exs +++ b/test/ketbin_web/controllers/paste_controller_test.exs @@ -75,6 +75,7 @@ defmodule KetbinWeb.PasteControllerTest do test "deletes chosen paste", %{conn: conn, paste: paste} do conn = delete(conn, Routes.paste_path(conn, :delete, paste)) assert redirected_to(conn) == Routes.paste_path(conn, :index) + assert_error_sent 404, fn -> get(conn, Routes.paste_path(conn, :show, paste)) end