diff --git a/core/bundles/next/lib/account/participant_signin_view.ex b/core/bundles/next/lib/account/participant_signin_view.ex index e831d09e1..573a60dae 100644 --- a/core/bundles/next/lib/account/participant_signin_view.ex +++ b/core/bundles/next/lib/account/participant_signin_view.ex @@ -33,7 +33,7 @@ defmodule Next.Account.ParticipantSigninView do
<%= 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}/> diff --git a/core/lib/google_sign_in.ex b/core/lib/google_sign_in.ex index 4d005e5a0..50a553a10 100644 --- a/core/lib/google_sign_in.ex +++ b/core/lib/google_sign_in.ex @@ -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, "")) @@ -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: %{ diff --git a/core/lib/google_sign_in/plug.ex b/core/lib/google_sign_in/plug.ex index a4c81650a..a88ab070c 100644 --- a/core/lib/google_sign_in/plug.ex +++ b/core/lib/google_sign_in/plug.ex @@ -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 @@ -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 @@ -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) @@ -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 diff --git a/core/systems/account/user_form.ex b/core/systems/account/user_form.ex index 70b548179..4eea607e6 100644 --- a/core/systems/account/user_form.ex +++ b/core/systems/account/user_form.ex @@ -63,9 +63,11 @@ defmodule Systems.Account.UserForm do """ end + attr(:creator?, :boolean, required: true) + def google_signin(assigns) do ~H""" - +
diff --git a/core/systems/admin/login_page.ex b/core/systems/admin/login_page.ex index db2a066cb..85f613964 100644 --- a/core/systems/admin/login_page.ex +++ b/core/systems/admin/login_page.ex @@ -34,7 +34,7 @@ defmodule Systems.Admin.LoginPage do Log in
- +
diff --git a/core/test/google_sign_in/plug_test.exs b/core/test/google_sign_in/plug_test.exs index b214ace60..8a9c6c057 100644 --- a/core/test/google_sign_in/plug_test.exs +++ b/core/test/google_sign_in/plug_test.exs @@ -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(%{}) @@ -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 diff --git a/core/test/google_sign_in_test.exs b/core/test/google_sign_in_test.exs index a6f68d80b..707fb5082 100644 --- a/core/test/google_sign_in_test.exs +++ b/core/test/google_sign_in_test.exs @@ -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}"