Skip to content

Commit

Permalink
[BUG FIX] [MER-3619] Fix an issue where account creation silently fai…
Browse files Browse the repository at this point in the history
…ls in certain cases (#5108)

* fix an issue where account creation was not properly filtering out LTI accounts

* remove redundant clause

* Auto format

---------

Co-authored-by: eliknebel <eliknebel@users.noreply.github.com>
  • Loading branch information
eliknebel and eliknebel authored Sep 18, 2024
1 parent 259b7bb commit 61180dc
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 30 deletions.
11 changes: 11 additions & 0 deletions lib/oli/accounts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,17 @@ defmodule Oli.Accounts do
"""
def get_user_by(clauses), do: Repo.get_by(User, clauses)

@doc """
Gets a single independent user by query parameter
## Examples
iex> get_independent_user_by(email: "student1@example.com")
%User{independent_learner: true, ...}
iex> get_independent_user_by(email: "student2@example.com")
nil
"""
def get_independent_user_by(clauses),
do: Repo.get_by(User, Enum.into([independent_learner: true], clauses))

@doc """
Gets a single user with platform roles and author preloaded
Returns `nil` if the User does not exist.
Expand Down
14 changes: 1 addition & 13 deletions lib/oli_web/pow/messages.ex
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,5 @@ defmodule OliWeb.Pow.Messages do
end

def pow_assent_login_with_provider(conn),
do:
interpolate("Continue with %{provider}", provider: Naming.humanize(conn.params["provider"]))

defp interpolate(msg, opts) do
Enum.reduce(opts, msg, fn {key, value}, msg ->
token = "%{#{key}}"

case String.contains?(msg, token) do
true -> String.replace(msg, token, to_string(value), global: false)
false -> msg
end
end)
end
do: "Continue with #{Naming.humanize(conn.params["provider"])}"
end
7 changes: 4 additions & 3 deletions lib/oli_web/pow/user_context.ex
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ defmodule OliWeb.Pow.UserContext do
"""
@impl true
def create(params) do
case Accounts.get_user_by(%{email: params["email"]}) do
case Accounts.get_independent_user_by(%{email: params["email"]}) do
%User{email: email} = user ->
if user.email_confirmed_at,
do:
Expand All @@ -70,8 +70,9 @@ defmodule OliWeb.Pow.UserContext do
"Account already exists",
"account_already_exists.html",
%{
url: Utils.ensure_absolute_url(Routes.pow_session_path(OliWeb.Endpoint, :new)),
forgot_password:
login_url:
Utils.ensure_absolute_url(Routes.pow_session_path(OliWeb.Endpoint, :new)),
forgot_password_url:
Utils.ensure_absolute_url(
Routes.pow_reset_password_reset_password_path(OliWeb.Endpoint, :new)
)
Expand Down
3 changes: 1 addition & 2 deletions lib/oli_web/pow/user_identities_context.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ defmodule OliWeb.Pow.UserIdentities do
%{"email" => email, "email_verified" => true} = user_params,
user_id_params
) do
case Accounts.get_user_by(email: email) do
case Accounts.get_independent_user_by(email: email) do
nil ->
# user account with the given email doesnt exist, so create it
# user_params = Map.merge(user_params, %{"sub" => UUID.uuid4()})
pow_assent_create_user(user_identity_params, user_params, user_id_params)

user ->
Expand Down
35 changes: 23 additions & 12 deletions lib/oli_web/templates/email/account_already_exists.html.eex
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
<div class="flex flex-column gap-y-4">
<h3>Are you trying to create a new account?</h3>
<p>
Someone tried to create an account with this email but an account already exists.
If this was you, you can login <%= link "here", to: @url, target: "_blank" %> with your existing email and password.
If you forgot your password, you can reset it by clicking <%= link "Forgot Password?", to: @forgot_password, target: "_blank" %>.
</p>
<p>If this was not you, you can disregard this email.</p>
</div>



<table align="center" role="presentation" cellspacing="0" cellpadding="0" border="0" width="100%" style="margin: auto;">
<tr>
<td style="background-color: #ffffff; padding: 20px 10px;">
<table role="presentation" cellspacing="0" cellpadding="0" border="0" width="100%">
<tr>
<td style="padding: 20px; font-family: sans-serif; font-size: 15px; line-height: 20px; color: #555555;">
<h1 style="margin: 0 0 10px 0; font-family: sans-serif; font-size: 25px; line-height: 30px; color: #333333; font-weight: normal;">Trying to create a new account?</h1>
<p style="margin: 20px 0;">
We noticed there was an attempt to create a new account with this email but an account already exists.
</p>
<p style="margin: 20px 0;">
If this was you, <%= link "sign in to your existing account", to: @login_url, target: "_blank", style: "color: #2C67C4;" %>.
If you forgot your password, you can reset it <%= link "here", to: @forgot_password_url, target: "_blank", style: "color: #2C67C4;" %>.
</p>
<p style="margin: 20px 0;">
If this was not you, you can ignore this email.
</p>
</td>
</tr>
</table>
</td>
</tr>
</table>

0 comments on commit 61180dc

Please sign in to comment.