Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change Registrations to use Pow #44

Merged
merged 38 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
e2fd8de
Add Pow
backspace Jul 14, 2024
4c1c813
Add Pow session to endpoint
backspace Jul 14, 2024
dfd4357
Change migration
backspace Jul 14, 2024
0d0e373
Add more blueprint changes
backspace Jul 14, 2024
fda8c70
Fix Pow config for module name
backspace Jul 14, 2024
19f4112
Rename existing User changeset function
backspace Jul 14, 2024
8d2c2e7
Add overridden templates
backspace Jul 14, 2024
98c9a3b
Change to using Pow’s session user
backspace Jul 14, 2024
8230b21
Change some links
backspace Jul 14, 2024
d692755
Remove unused files
backspace Jul 14, 2024
87ebe68
Change crypted_password references
backspace Jul 14, 2024
3d07580
Add some form ids
backspace Jul 14, 2024
5f649f5
Remove unused routes
backspace Jul 14, 2024
a73ebbb
Fix post-login routing
backspace Jul 14, 2024
476bf84
Remove currently-broken links
backspace Jul 14, 2024
928381b
Fix setup of accounts in tests
backspace Jul 14, 2024
4040003
Fix login redirection
backspace Jul 14, 2024
07cdb53
Fix some flash message assertions
backspace Jul 15, 2024
724237f
Fix various test failures
backspace Jul 15, 2024
e36ed59
Move Pow overrides
backspace Jul 15, 2024
35fac49
Add preliminary mailer
backspace Jul 15, 2024
3baf5a0
Add email confirmation extension
backspace Jul 15, 2024
c0cc28c
Add password reset
backspace Jul 16, 2024
f81346a
Add confirmation templates
backspace Jul 16, 2024
17f5c77
Fix deprecation warning
backspace Jul 16, 2024
6237f22
Restore account deletion
backspace Jul 16, 2024
4ac74dc
Remove outdated comment
backspace Jul 16, 2024
3652a11
Fix password reset test
backspace Jul 16, 2024
6bb50a1
Restore email downcasing
backspace Jul 16, 2024
2eb169f
Remove reset code
backspace Jul 17, 2024
9bfeba7
Fix loading of user when editing
backspace Jul 17, 2024
d8487df
Remove confirmation
backspace Jul 17, 2024
8009a54
Fix sending of backlog
backspace Jul 17, 2024
6030d67
Fix more tests
backspace Jul 17, 2024
6ded620
Merge remote-tracking branch 'origin/main' into pow
backspace Jul 17, 2024
440486d
Restore warning when registration closed
backspace Jul 17, 2024
f1f8d73
Add user reload after voicepass save
backspace Jul 17, 2024
e0bd990
Remove superseded templates
backspace Jul 17, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions registrations/config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ import_config "#{Mix.env()}.exs"
# Use Jason for JSON parsing in Phoenix
config :phoenix, :json_library, Jason

config :registrations, :pow,
web_mailer_module: RegistrationsWeb,
extensions: [PowResetPassword],
controller_callbacks: Pow.Extension.Phoenix.ControllerCallbacks,
web_module: RegistrationsWeb,
user: RegistrationsWeb.User,
repo: Registrations.Repo,
routes_backend: RegistrationsWeb.Pow.Routes,
mailer_backend: Registrations.Mailer,
messages_backend: RegistrationsWeb.Pow.Messages,
users_context: RegistrationsWeb.Pow.Users

# Configure phoenix generators
config :phoenix, :generators,
migration: true,
Expand Down
51 changes: 36 additions & 15 deletions registrations/lib/registrations/mailer.ex
Original file line number Diff line number Diff line change
@@ -1,13 +1,39 @@
defmodule Registrations.Mailer do
use Pow.Phoenix.Mailer
use Swoosh.Mailer, otp_app: :registrations

import Swoosh.Email
import RegistrationsWeb.SharedHelpers

alias RegistrationsWeb.Router
alias RegistrationsWeb.Endpoint
require Logger

@from "[email protected]"

@impl true
def cast(%{user: user, subject: subject, text: text, html: html}) do
%Swoosh.Email{}
|> to({"", user.email})
|> from(adventure_from())
|> subject("[#{phrase("email_title")}] #{subject}")
|> html_body(html)
|> text_body(text)
end

@impl true
def process(email) do
# An asynchronous process should be used here to prevent enumeration
# attacks. Synchronous e-mail delivery can reveal whether a user already
# exists in the system or not.

Task.start(fn ->
email
|> deliver()
|> log_warnings()
end)

:ok
end

def send_welcome_email(email) do
new()
|> to(email)
Expand Down Expand Up @@ -47,6 +73,8 @@ defmodule Registrations.Mailer do
|> deliver
end

@spec send_registration(atom() | %{:email => any(), optional(any()) => any()}) ::
{:error, any()} | {:ok, any()}
def send_registration(user) do
new()
|> to(adventure_from())
Expand Down Expand Up @@ -82,19 +110,6 @@ defmodule Registrations.Mailer do
|> deliver
end

@spec send_password_reset(atom | %{:email => any, :recovery_hash => any, optional(any) => any}) ::
{:error, any} | {:ok, any}
def send_password_reset(user) do
new()
|> to(user.email)
|> from(adventure_from())
|> subject("[#{phrase("email_title")}] Password reset")
|> html_body(
"Here is a <a href='#{Router.Helpers.reset_url(Endpoint, :edit, user.recovery_hash)}'>password reset link</a>"
)
|> deliver
end

defp welcome_html do
Phoenix.View.render_to_string(
RegistrationsWeb.EmailView,
Expand Down Expand Up @@ -164,4 +179,10 @@ defmodule Registrations.Mailer do
_ -> "From: #{message.from_name} <#{message.from_address}>"
end
end

defp log_warnings({:error, reason}) do
Logger.warning("Mailer backend failed with: #{inspect(reason)}")
end

defp log_warnings({:ok, response}), do: {:ok, response}
end
11 changes: 11 additions & 0 deletions registrations/lib/registrations_web.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ defmodule RegistrationsWeb do
import Ecto.Query, only: [from: 1, from: 2]

alias RegistrationsWeb.Router.Helpers, as: Routes

import RegistrationsWeb.Pow.ControllerHelper
end
end

Expand Down Expand Up @@ -82,6 +84,15 @@ defmodule RegistrationsWeb do
end
end

def mailer_view do
quote do
use Phoenix.View, root: "lib/registrations_web/templates",
namespace: RegistrationsWeb

use Phoenix.HTML
end
end

def router do
quote do
use Phoenix.Router
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ defmodule RegistrationsWeb.MessageController do

users =
if(me == "true",
do: [conn.assigns[:current_user_object]],
do: [conn.assigns[:current_user]],
else: Repo.all(RegistrationsWeb.User)
)
|> Repo.preload(team: [:users])
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ defmodule RegistrationsWeb.UserController do

def edit(conn, _) do
users = Repo.all(User)
current_user_only = conn.assigns[:current_user_object]
current_user_only = Repo.get_by(User, email: conn.assigns[:current_user].email)
changeset = User.details_changeset(current_user_only)

current_user = Repo.preload(current_user_only, team: [:users])
Expand Down Expand Up @@ -55,7 +55,7 @@ defmodule RegistrationsWeb.UserController do

def update(conn, %{"user" => user_params}) do
users = Repo.all(User)
current_user = conn.assigns[:current_user_object]
current_user = conn.assigns[:current_user]
changeset = User.details_changeset(current_user, user_params)

current_user = Repo.preload(current_user, team: [:users])
Expand All @@ -79,15 +79,16 @@ defmodule RegistrationsWeb.UserController do
end

def voicepass(conn, _params) do
current_user = conn.assigns[:current_user_object]
current_user = conn.assigns[:current_user]

lines = User.voicepass_candidates()
random_index = :rand.uniform(length(lines))
new_voicepass = Enum.at(lines, random_index)

changeset = User.voicepass_changeset(current_user, %{voicepass: new_voicepass})

Repo.update(changeset)
{:ok, new_user} = Repo.update(changeset)
conn = sync_user(conn, new_user)

json(conn, %{data: %{voicepass: new_voicepass}})
end
Expand Down
2 changes: 2 additions & 0 deletions registrations/lib/registrations_web/endpoint.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,7 @@ defmodule RegistrationsWeb.Endpoint do
max_age: 60 * 60 * 24 * 365
)

plug(Pow.Plug.Session, otp_app: :registrations)

plug(RegistrationsWeb.Router)
end
Loading
Loading