Skip to content
Snippets Groups Projects
Commit bc57e929 authored by Tom Robinson's avatar Tom Robinson
Browse files

Add group_ids to User model and GET /api/user* endpoints

parent 91fe28b9
Branches
Tags
No related merge requests found
......@@ -41,7 +41,7 @@
(hh/merge-where (when-not include_deactivated
[:= :is_active true]))))
;; For admins, also include the IDs of the Users' Personal Collections
api/*is-superuser?* (hydrate :personal_collection_id)))
api/*is-superuser?* (hydrate :personal_collection_id :group_ids)))
(defn- fetch-user [& query-criteria]
(apply db/select-one (vec (cons User user/admin-or-self-visible-columns)) query-criteria))
......@@ -79,14 +79,15 @@
"Fetch the current `User`."
[]
(-> (api/check-404 @api/*current-user*)
(hydrate :personal_collection_id)))
(hydrate :personal_collection_id :group_ids)))
(api/defendpoint GET "/:id"
"Fetch a `User`. You must be fetching yourself *or* be a superuser."
[id]
(check-self-or-superuser id)
(api/check-404 (fetch-user :id id, :is_active true)))
(-> (api/check-404 (fetch-user :id id, :is_active true))
(hydrate :group_ids)))
(defn- valid-email-update?
"This predicate tests whether or not the user is allowed to update the email address associated with this account."
......
......@@ -135,6 +135,19 @@
:pre-delete pre-delete
:types (constantly {:login_attributes :json-no-keywordization})}))
(defn ^{:hydrate :group_ids} group-ids
"Fetch set of IDs of PermissionsGroup a User belongs to."
[user-or-id]
(db/select-field :group_id PermissionsGroupMembership :user_id (u/get-id user-or-id)))
(defn ^{:batched-hydrate :group_ids} add-group-ids
"Efficiently add PermissionsGroup `group_ids` to a collection of `users`."
[users]
(let [user-id->memberships (when (seq users)
(group-by :user_id (db/select [PermissionsGroupMembership :user_id :group_id]
:user_id [:in (set (map u/get-id users))])))]
(for [user users]
(assoc user :group_ids (map :group_id (user-id->memberships (u/get-id user)))))))
;;; --------------------------------------------------- Helper Fns ---------------------------------------------------
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment