From d308c41b2dded75212ead3ac8c61fc8b3a057c7b Mon Sep 17 00:00:00 2001 From: michaeljguarino Date: Thu, 22 Jun 2023 15:37:46 -0400 Subject: [PATCH] feat: Sync admin state in console logins (#375) --- lib/console/services/users.ex | 5 +++++ test/console/services/users_test.exs | 2 ++ 2 files changed, 7 insertions(+) diff --git a/lib/console/services/users.ex b/lib/console/services/users.ex index f562e88969..696eaa5f34 100644 --- a/lib/console/services/users.ex +++ b/lib/console/services/users.ex @@ -79,6 +79,7 @@ defmodule Console.Services.Users do @spec bootstrap_user(map) :: user_resp def bootstrap_user(%{"email" => email} = attrs) do + attrs = token_attrs(attrs) start_transaction() |> add_operation(:user, fn _ -> case get_user_by_email(email) do @@ -90,6 +91,10 @@ defmodule Console.Services.Users do |> execute(extract: :user) end + defp token_attrs(%{"admin" => true} = attrs), do: Map.put(attrs, "roles", %{"admin" => true}) + defp token_attrs(%{"admin" => false} = attrs), do: Map.put(attrs, "roles", %{"admin" => false}) + defp token_attrs(attrs), do: attrs + def temporary_token(%User{} = user) do with {:ok, token, _} <- Console.Guardian.encode_and_sign(user, %{}, ttl: {1, :hour}) do handle_notify(PubSub.TemporaryTokenCreated, user) diff --git a/test/console/services/users_test.exs b/test/console/services/users_test.exs index 273a6d7265..285eab8e11 100644 --- a/test/console/services/users_test.exs +++ b/test/console/services/users_test.exs @@ -55,6 +55,7 @@ defmodule Console.Services.UsersTest do "name" => "Some User", "profile" => "https://some.image.com", "groups" => ["general"], + "admin" => true, "plural_id" => "abcdef-123456789-ghijkl" }) @@ -62,6 +63,7 @@ defmodule Console.Services.UsersTest do assert user.email == "someone@example.com" assert user.profile == "https://some.image.com" assert user.plural_id == "abcdef-123456789-ghijkl" + assert user.roles.admin group = Users.get_group_by_name("general") assert group.description == "synced from Plural"