Skip to content

Commit

Permalink
PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
ErinLMoore committed Jun 17, 2024
1 parent d2cb217 commit aa23bf1
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 39 deletions.
5 changes: 4 additions & 1 deletion apps/alert_processor/lib/model/user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,11 @@ defmodule AlertProcessor.Model.User do
@spec get(id()) :: t() | nil
def get(id), do: Repo.get(__MODULE__, id)

@spec get_by_alternate_id(%{id: id() | nil, mbta_uuid: id() | nil}) :: [t()]
@spec get_by_alternate_id(%{id: id(), mbta_uuid: id() | nil}) :: [t()]
def get_by_alternate_id(%{id: id, mbta_uuid: mbta_uuid}) do
# can get accounts using id or mbta_uuid
# which is the old-style ID for accounts from the time before SSO.

from(u in __MODULE__,
where: u.id in [^id, ^mbta_uuid]
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,12 @@ defmodule AlertProcessor.Model.UserTest do
assert User.get_by_alternate_id(%{id: nil, mbta_uuid: user.id}) == [user]
end

test "returns two users if both present (unlikely)" do
test "returns a user if both id's are the same " do
user = insert(:user)
assert User.get_by_alternate_id(%{id: user.id, mbta_uuid: user.id}) == [user]
end

test "returns two users if both ids present and different (unlikely)" do
user = insert(:user)
user2 = insert(:user)
assert User.get_by_alternate_id(%{id: user.id, mbta_uuid: user2.id}) == [user, user2]
Expand Down
65 changes: 28 additions & 37 deletions apps/concierge_site/lib/controllers/auth_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,30 @@ defmodule ConciergeSite.AuthController do
phone_number = PhoneNumber.strip_us_country_code(phone_number)

role = parse_role({:ok, userinfo})
user_list = get_or_create_user(%{id: id, mbta_uuid: mbta_uuid})

user =
%{id: id, mbta_uuid: mbta_uuid}
|> get_or_create_user(email, phone_number, role)
|> use_props_from_token(email, phone_number, role)
case length(user_list) do
0 ->
# If neither ID is found, the user just created their account in Keycloak so we need to add them to our database
Repo.insert!(%User{
id: id,
email: email,
phone_number: phone_number,
role: role
})

id |> User.get() |> use_props_from_token(email, phone_number, role)

1 ->
# If 1 user is found, we want to return that user
user_list |> hd |> use_props_from_token(email, phone_number, role)

2 ->
# If 2 users are found, something weird happened. Log and return nil. User will be redirected to landing page.
Logger.warn("User with 2 ids found. sub id: #{id}, mbta_uuid: #{mbta_uuid}")
nil
end

logout_params = %{
post_logout_redirect_uri: page_url(conn, :landing)
Expand Down Expand Up @@ -73,38 +92,11 @@ defmodule ConciergeSite.AuthController do
SessionHelper.sign_out(conn)
end

@spec get_or_create_user(
%{id: User.id() | nil, mbta_uuid: User.id() | nil},
String.t(),
String.t() | nil,
String.t()
) ::
User.t() | :find_user_error
defp get_or_create_user(%{id: id, mbta_uuid: mbta_uuid} = id_map, email, phone_number, role) do
@spec get_or_create_user(%{id: User.id(), mbta_uuid: User.id() | nil}) ::
[User.t()]
defp get_or_create_user(id_map) do
# This checks both the normal id from Keycloak, and the legacy mbta_uuid. We should get either 0 or 1 users back.
user_list = User.get_by_alternate_id(id_map)

case length(user_list) do
0 ->
# If neither ID is found, the user just created their account in Keycloak so we need to add them to our database
Repo.insert!(%User{
id: id,
email: email,
phone_number: phone_number,
role: role
})

User.get(id)

1 ->
# If 1 user is found, we want to return that user
hd(user_list)

2 ->
# If 2 users are found, something weird happened. Log and return nil. User will be redirected to landing page.
Logger.warn("User with 2 ids found. sub id: #{id}, mbta_uuid: #{mbta_uuid}")
:find_user_error
end
User.get_by_alternate_id(id_map)
end

# Display the email and phone number we received in the token rather than the
Expand All @@ -114,13 +106,12 @@ defmodule ConciergeSite.AuthController do
# changed one of these fields in Keycloak, we might not have had time receive
# that message yet, so the values in the token are more authoritative.
@spec use_props_from_token(
User.t() | :find_user_error,
User.t(),
String.t(),
String.t() | nil,
String.t()
) ::
User.t() | nil
defp use_props_from_token(:find_user_error, _, _, _), do: nil
User.t()

defp use_props_from_token(user, email, phone_number, role) do
%User{
Expand Down

0 comments on commit aa23bf1

Please sign in to comment.