Skip to content

Commit

Permalink
#871 When participant logson with google account, actual profile is t…
Browse files Browse the repository at this point in the history
…hat of creator
  • Loading branch information
mellelieuwes committed Aug 7, 2024
1 parent 1a9806b commit 5f74658
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 28 deletions.
2 changes: 1 addition & 1 deletion core/bundles/next/lib/account/participant_signin_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ defmodule Next.Account.ParticipantSigninView do
<div>
<%= for block <- @blocks do %>
<%= if block == :google do %>
<.google_signin />
<.google_signin creator?={false} />
<% end %>
<%= if block == :password do %>
<.password_signin for={@password_form} user_type={:participant}/>
Expand Down
4 changes: 2 additions & 2 deletions core/lib/google_sign_in.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ defmodule GoogleSignIn do
|> Repo.one()
end

def register_user(attrs) do
def register_user(attrs, creator?) do
fullname =
~w(given_name family_name)
|> Enum.map(&Map.get(attrs, &1, ""))
Expand All @@ -26,7 +26,7 @@ defmodule GoogleSignIn do
# Revert: https://github.com/eyra/mono/issues/563

sso_info = %{
creator: true,
creator: creator?,
email: Map.get(attrs, "email"),
displayname: display_name,
profile: %{
Expand Down
18 changes: 7 additions & 11 deletions core/lib/google_sign_in/plug.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,19 @@ defmodule GoogleSignIn.PlugUtils do
end

defmodule GoogleSignIn.AuthorizePlug do
@moduledoc """
This controller manages the OpenID Connect flow with SurfConext.
See this site for more info: https://sp.google_sign_in.nl/
"""
import Plug.Conn
import GoogleSignIn.PlugUtils

def init(otp_app) when is_atom(otp_app), do: otp_app

def call(conn, otp_app) do
def call(%{params: conn_params} = conn, otp_app) do
config = config(otp_app)

{:ok, %{url: url, session_params: session_params}} =
google_module(config).authorize_url(config)

conn
|> put_session(:google_sign_in, session_params)
|> put_session(:google_sign_in, Map.merge(conn_params, session_params))
|> set_return_to()
|> Phoenix.Controller.redirect(external: url)
end
Expand All @@ -42,7 +37,7 @@ defmodule GoogleSignIn.AuthorizePlug do
end
end

defmodule(GoogleSignIn.CallbackPlug) do
defmodule GoogleSignIn.CallbackPlug do
import Plug.Conn
import GoogleSignIn.PlugUtils
use Core.FeatureFlags
Expand All @@ -51,6 +46,7 @@ defmodule(GoogleSignIn.CallbackPlug) do

def call(conn, otp_app) do
session_params = get_session(conn, :google_sign_in)
creator? = Map.get(session_params || %{}, "creator", nil) == "true"

config = config(otp_app) |> Keyword.put(:session_params, session_params)

Expand All @@ -64,14 +60,14 @@ defmodule(GoogleSignIn.CallbackPlug) do
if user = GoogleSignIn.get_user_by_sub(google_user["sub"]) do
{user, false}
else
{register_user(google_user), true}
{register_user(google_user, creator?), true}
end

log_in_user(config, conn, user, first_time?)
end

defp register_user(info) do
{:ok, google_sign_in_user} = GoogleSignIn.register_user(info)
defp register_user(info, creator?) do
{:ok, google_sign_in_user} = GoogleSignIn.register_user(info, creator?)
google_sign_in_user.user
end

Expand Down
4 changes: 3 additions & 1 deletion core/systems/account/user_form.ex
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,11 @@ defmodule Systems.Account.UserForm do
"""
end

attr(:creator?, :boolean, required: true)

def google_signin(assigns) do
~H"""
<a href="/google-sign-in">
<a href={"/google-sign-in?creator=#{@creator?}"}>
<div class="pt-2px pb-2px active:pt-3px active:pb-1px active:shadow-top4px bg-google rounded pl-4 pr-4">
<div class="flex w-full justify-center items-center">
<div>
Expand Down
2 changes: 1 addition & 1 deletion core/systems/admin/login_page.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ defmodule Systems.Admin.LoginPage do
Log in
</div>
<div class="mb-6" />
<a href="/google-sign-in">
<a href="/google-sign-in?creator=true">
<div class="pt-2px pb-2px active:pt-3px active:pb-1px active:shadow-top4px bg-grey1 rounded pl-4 pr-4">
<div class="flex w-full justify-center items-center">
<div>
Expand Down
26 changes: 15 additions & 11 deletions core/test/google_sign_in/plug_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ defmodule GoogleSignIn.AuthorizePlug.Test do
{:ok, domain: domain}
end

test "redirects to SurfConext login page" do
test "redirects to Google login page" do
conn =
conn(:get, "/google")
|> init_test_session(%{})
Expand Down Expand Up @@ -118,22 +118,26 @@ defmodule GoogleSignIn.CallbackPlug.Test do
given_name = Faker.Person.first_name()
family_name = Faker.Person.last_name()

GoogleSignIn.register_user(%{
"sub" => GoogleSignIn.FakeGoogle.sub(),
"name" => "#{given_name} #{family_name}",
"email" => email,
"email_verified" => true,
"given_name" => given_name,
"family_name" => family_name,
"picture" => Faker.Internet.url(),
"locale" => "en"
})
GoogleSignIn.register_user(
%{
"sub" => GoogleSignIn.FakeGoogle.sub(),
"name" => "#{given_name} #{family_name}",
"email" => email,
"email_verified" => true,
"given_name" => given_name,
"family_name" => family_name,
"picture" => Faker.Internet.url(),
"locale" => "en"
},
true
)

user =
conn(:get, "/google")
|> init_test_session(%{})
|> CallbackPlug.call(:test)

assert user.creator == true
assert user.email == email
end

Expand Down
3 changes: 2 additions & 1 deletion core/test/google_sign_in_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ defmodule GoogleSignIn.Test do
"locale" => "en"
}

{:ok, google_user} = GoogleSignIn.register_user(sso_info)
{:ok, google_user} = GoogleSignIn.register_user(sso_info, false)

for key <- Map.keys(sso_info) do
assert Map.get(google_user, String.to_atom(key)) == Map.get(sso_info, key)
end

assert google_user.user.creator == false
assert google_user.user.email == Map.get(sso_info, "email")
assert google_user.user.displayname == "#{given_name}"
assert google_user.user.profile.fullname == "#{given_name} #{family_name}"
Expand Down

0 comments on commit 5f74658

Please sign in to comment.