From 40d7bb04b470ef036429e3592407bfa14130fd1f Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Wed, 16 Oct 2024 17:35:47 +0200 Subject: [PATCH] :sparkles: Reuse permission from rpc/cond middleware for get-file rpc method --- backend/src/app/rpc/commands/files.clj | 10 +++++++++- backend/src/app/rpc/cond.clj | 11 ++++++++--- .../test/backend_tests/rpc_cond_middleware_test.clj | 1 - 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/backend/src/app/rpc/commands/files.clj b/backend/src/app/rpc/commands/files.clj index c8aa562045..1ca28fd64d 100644 --- a/backend/src/app/rpc/commands/files.clj +++ b/backend/src/app/rpc/commands/files.clj @@ -295,8 +295,16 @@ ::sm/result schema:file-with-permissions} [cfg {:keys [::rpc/profile-id id project-id] :as params}] (db/tx-run! cfg (fn [{:keys [::db/conn] :as cfg}] - (let [perms (get-permissions conn profile-id id)] + ;; The COND middleware makes initial request for a file and + ;; permissions when the incoming request comes with an + ;; ETAG. When ETAG does not matches, the request is resolved + ;; and this code is executed, in this case the permissions + ;; will be already prefetched and we just reuse them instead + ;; of making an additional database queries. + (let [perms (or (:permissions (::cond/object params)) + (get-permissions conn profile-id id))] (check-read-permissions! perms) + (let [team (teams/get-team conn :profile-id profile-id :project-id project-id diff --git a/backend/src/app/rpc/cond.clj b/backend/src/app/rpc/cond.clj index bf724d13f5..2c79d8f664 100644 --- a/backend/src/app/rpc/cond.clj +++ b/backend/src/app/rpc/cond.clj @@ -54,11 +54,16 @@ (l/trc :hint "instrumenting method" :service (::sv/name mdata)) (fn [cfg {:keys [::key] :as params}] (if *enabled* - (let [key' (when (or reuse-key? key) - (some->> (get-object cfg params) (key-fn params) (fmt-key)))] + (let [object (when (some? key) + (get-object cfg params)) + key' (when (some? object) + (->> object (key-fn params) (fmt-key)))] (if (and (some? key) (= key key')) (fn [_] {::rres/status 304}) - (let [result (f cfg params) + (let [params (if (some? object) + (assoc params ::object object) + params) + result (f cfg params) etag (or (and reuse-key? key') (some->> result meta ::key fmt-key) (some->> result (key-fn params) fmt-key))] diff --git a/backend/test/backend_tests/rpc_cond_middleware_test.clj b/backend/test/backend_tests/rpc_cond_middleware_test.clj index e74a9c5497..e737fc5f5e 100644 --- a/backend/test/backend_tests/rpc_cond_middleware_test.clj +++ b/backend/test/backend_tests/rpc_cond_middleware_test.clj @@ -39,7 +39,6 @@ (t/is (nil? error)) (t/is (map? result)) (t/is (contains? (meta result) :app.http/headers)) - (t/is (contains? (meta result) :app.rpc.cond/key)) (let [etag (-> result meta :app.http/headers (get "etag")) {:keys [error result]} (th/command! (assoc params ::cond/key etag))]