Add permission checking to file snapshot rpc methods

This commit is contained in:
Andrey Antukh 2024-10-29 15:39:55 +01:00 committed by Alonso Torres
parent b4f868be91
commit 5f4af76d28
9 changed files with 308 additions and 202 deletions

View file

@ -33,7 +33,7 @@ test("Save and restore version", async ({ page }) => {
await page.getByLabel("History (Alt+H)").click();
await workspacePage.mockRPC(
"take-file-snapshot",
"create-file-snapshot",
"workspace/versions-take-snapshot-1.json",
);
@ -57,7 +57,11 @@ test("Save and restore version", async ({ page }) => {
await page.getByRole("textbox").fill("INIT");
await page.getByRole("textbox").press("Enter");
await page.getByTestId("version(INIT)").getByRole("button").click();
await page
.locator("li")
.filter({ hasText: "INIT" })
.getByRole("button")
.click();
await page.getByRole("button", { name: "Restore" }).click();
await workspacePage.mockRPC(

View file

@ -64,7 +64,7 @@
(->> (rx/from-atom refs/persistence-state {:emit-current-value? true})
(rx/filter #(or (nil? %) (= :saved %)))
(rx/take 1)
(rx/mapcat #(rp/cmd! :take-file-snapshot {:file-id file-id :label label}))
(rx/mapcat #(rp/cmd! :create-file-snapshot {:file-id file-id :label label}))
(rx/mapcat
(fn [{:keys [id]}]
(rx/of
@ -101,7 +101,6 @@
(->> (rx/from-atom refs/persistence-state {:emit-current-value? true})
(rx/filter #(or (nil? %) (= :saved %)))
(rx/take 1)
(rx/mapcat #(rp/cmd! :take-file-snapshot {:file-id file-id :created-by "system" :label (dt/format (dt/now) :date-full)}))
(rx/mapcat #(rp/cmd! :restore-file-snapshot {:file-id file-id :id id}))
(rx/map #(dw/initialize-file project-id file-id)))
(rx/of (ptk/event ::ev/event {::ev/name "restore-version"}))))))
@ -114,7 +113,7 @@
(ptk/reify ::delete-version
ptk/WatchEvent
(watch [_ _ _]
(->> (rp/cmd! :remove-file-snapshot {:id id})
(->> (rp/cmd! :delete-file-snapshot {:id id})
(rx/map #(fetch-versions file-id))))))
(defn pin-version

View file

@ -109,8 +109,7 @@
(kbd/esc? event)
(st/emit! (dwv/update-version-state {:editing nil})))))]
[:li {:data-testid (dm/str "version(" (:label entry) ")")
:class (stl/css :version-entry-wrap)}
[:li {:class (stl/css :version-entry-wrap)}
[:div {:class (stl/css :version-entry :is-snapshot)}
[:img {:class (stl/css :version-entry-avatar)
:alt (:fullname profile)

View file

@ -12,12 +12,9 @@
[app.common.files.validate :as cfv]
[app.common.json :as json]
[app.common.logging :as l]
[app.common.schema :as sm]
[app.common.transit :as t]
[app.common.types.file :as ctf]
[app.common.uri :as u]
[app.common.uuid :as uuid]
[app.config :as cf]
[app.main.data.changes :as dwc]
[app.main.data.dashboard.shortcuts]
[app.main.data.preview :as dp]
@ -33,7 +30,6 @@
[app.main.store :as st]
[app.util.debug :as dbg]
[app.util.dom :as dom]
[app.util.http :as http]
[app.util.object :as obj]
[app.util.timers :as timers]
[beicon.v2.core :as rx]
@ -454,66 +450,6 @@
[id shape-ref]
(st/emit! (dw/set-shape-ref id shape-ref)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; SNAPSHOTS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn ^:export list-available-snapshots
[file-id]
(let [file-id (or (d/parse-uuid file-id)
(:current-file-id @st/state))]
(->> (http/send! {:method :get
:uri (u/join cf/public-uri "api/rpc/command/get-file-snapshots")
:query {:file-id file-id}})
(rx/map http/conditional-decode-transit)
(rx/mapcat rp/handle-response)
(rx/subs! (fn [result]
(let [result (map (fn [row]
(update row :id str))
result)]
(js/console.table (json/->js result))))
(fn [cause]
(js/console.log "EE:" cause))))
nil))
(defn ^:export take-snapshot
[label file-id]
(when-let [file-id (or (d/parse-uuid file-id)
(:current-file-id @st/state))]
(->> (http/send! {:method :post
:uri (u/join cf/public-uri "api/rpc/command/take-file-snapshot")
:body (http/transit-data {:file-id file-id :label label})})
(rx/map http/conditional-decode-transit)
(rx/mapcat rp/handle-response)
(rx/subs! (fn [{:keys [id]}]
(println "Snapshot saved:" (str id) label))
(fn [cause]
(js/console.log "EE:" cause))))))
(defn ^:export restore-snapshot
[label file-id]
(when-let [file-id (or (d/parse-uuid file-id)
(:current-file-id @st/state))]
(let [snapshot-id (sm/parse-uuid label)
label (if snapshot-id nil label)
params (cond-> {:file-id file-id}
(uuid? snapshot-id)
(assoc :id snapshot-id)
(string? label)
(assoc :label label))]
(->> (http/send! {:method :post
:uri (u/join cf/public-uri "api/rpc/command/restore-file-snapshot")
:body (http/transit-data params)})
(rx/map http/conditional-decode-transit)
(rx/mapcat rp/handle-response)
(rx/subs! (fn [_]
(println "Snapshot restored " (or snapshot-id label)))
#_(.reload js/location)
(fn [cause]
(js/console.log "EE:" cause)))))))
(defn ^:export enable-text-v2
[]
(st/emit! (features/enable-feature "text-editor/v2")))