Skip to content

Commit

Permalink
feat: Sync admin state in console logins (#375)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeljguarino authored Jun 22, 2023
1 parent 815ee72 commit d308c41
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/console/services/users.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions test/console/services/users_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,15 @@ defmodule Console.Services.UsersTest do
"name" => "Some User",
"profile" => "https://some.image.com",
"groups" => ["general"],
"admin" => true,
"plural_id" => "abcdef-123456789-ghijkl"
})

assert user.name == "Some User"
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"
Expand Down

0 comments on commit d308c41

Please sign in to comment.