Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add last_name to ApiKey users #37234

Merged
merged 1 commit into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/metabase/api/api_key.clj
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,13 @@
(let [unhashed-key (key-with-unique-prefix)
email (format "api-key-user-%s@api-key.invalid" (u/slugify name))]
(t2/with-transaction [_conn]
(let [user (user/insert-new-user! {:email email
:first_name name
:type :api-key})]
(let [user (first
(t2/insert-returning-instances! :model/User
{:email email
:first_name name
:last_name ""
:type :api-key
:password (str (random-uuid))}))]
(user/set-permissions-groups! user [(perms-group/all-users) group_id])
(let [api-key (-> (t2/insert-returning-instance! :model/ApiKey
(-> {:user_id (u/the-id user)
Expand Down Expand Up @@ -93,7 +97,8 @@
(user/set-permissions-groups! user [(perms-group/all-users) {:id group_id}])))
(when name
;; A bit of a pain to keep these in sync, but oh well.
(t2/update! :model/User (:user_id api-key-before) {:first_name name})
(t2/update! :model/User (:user_id api-key-before) {:first_name name
:last_name ""})
(t2/update! :model/ApiKey id (with-updated-by {:name name}))))
(let [updated-api-key (-> (t2/select-one :model/ApiKey :id id)
(t2/hydrate :group :updated_by))]
Expand Down
2 changes: 1 addition & 1 deletion src/metabase/models/user.clj
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@
[:email ms/Email]
[:first_name [:maybe ms/NonBlankString]]])

(mu/defn insert-new-user!
(mu/defn ^:private insert-new-user!
"Creates a new user, defaulting the password when not provided"
[new-user :- NewUser]
(first (t2/insert-returning-instances! User (update new-user :password #(or % (str (random-uuid)))))))
Expand Down