Skip to content

Commit

Permalink
add createUser mutation
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeljguarino committed Dec 6, 2024
1 parent a32ff04 commit 44d8b77
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 0 deletions.
6 changes: 6 additions & 0 deletions assets/src/generated/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5370,6 +5370,7 @@ export type RootMutationType = {
createStackDefinition?: Maybe<StackDefinition>;
createThread?: Maybe<ChatThread>;
createUpgradePolicy?: Maybe<UpgradePolicy>;
createUser?: Maybe<User>;
createWebhook?: Maybe<Webhook>;
deleteAccessToken?: Maybe<AccessToken>;
deleteCatalog?: Maybe<Catalog>;
Expand Down Expand Up @@ -5792,6 +5793,11 @@ export type RootMutationTypeCreateUpgradePolicyArgs = {
};


export type RootMutationTypeCreateUserArgs = {
attributes: UserAttributes;
};


export type RootMutationTypeCreateWebhookArgs = {
attributes: WebhookAttributes;
};
Expand Down
42 changes: 42 additions & 0 deletions go/client/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions go/client/graph/users.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ query GetUser($email: String!) {
}
}

mutation CreateUser($attributes: UserAttributes!) {
createUser(attributes: $attributes) {
...UserFragment
}
}

mutation UpdateUser($id: ID, $attributes: UserAttributes!) {
updateUser(id: $id, attributes: $attributes) {
...UserFragment
Expand Down
3 changes: 3 additions & 0 deletions lib/console/graphql/resolvers/user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ defmodule Console.GraphQl.Resolvers.User do
def update_user(%{attributes: attrs}, %{context: %{current_user: user}}),
do: Users.update_user(attrs, user)

def create_user(%{attributes: attrs}, _),
do: Users.create_user(attrs)

def delete_user(%{id: id}, %{context: %{current_user: user}}),
do: Users.delete_user(id, user)

Expand Down
8 changes: 8 additions & 0 deletions lib/console/graphql/users.ex
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,14 @@ defmodule Console.GraphQl.Users do
safe_resolve &User.create_invite/2
end

field :create_user, :user do
middleware Authenticated
middleware AdminRequired
arg :attributes, non_null(:user_attributes)

resolve &User.create_user/2
end

field :create_service_account, :user do
middleware Authenticated
middleware AdminRequired
Expand Down
2 changes: 2 additions & 0 deletions schema/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,8 @@ type RootMutationType {

createInvite(attributes: InviteAttributes!): Invite

createUser(attributes: UserAttributes!): User

createServiceAccount(attributes: ServiceAccountAttributes!): User

updateServiceAccount(id: ID!, attributes: ServiceAccountAttributes!): User
Expand Down
29 changes: 29 additions & 0 deletions test/console/graphql/mutations/user_mutations_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,35 @@ defmodule Console.GraphQl.UserMutationsTest do
end
end

describe "createUser" do
test "admins can create users" do
{:ok, %{data: %{"createUser" => user}}} = run_query("""
mutation Create($attrs: UserAttributes!) {
createUser(attributes: $attrs) {
id
email
}
}
""", %{"attrs" => %{"email" => "someone@example.com", "name" => "Some User"}}, %{current_user: admin_user()})

assert user["email"] == "someone@example.com"
end

test "nonadmins cannot create users" do
{:ok, %{errors: [_ | _]}} = run_query("""
mutation Create($attrs: UserAttributes!) {
createUser(attributes: $attrs) {
id
email
}
}
""", %{"attrs" => %{
"email" => "someone@example.com",
"name" => "Some User"
}}, %{current_user: insert(:user)})
end
end

describe "logout" do
test "it will wipe refresh tokens" do
user = insert(:user)
Expand Down

0 comments on commit 44d8b77

Please sign in to comment.