0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-24 23:49:45 -05:00

Improve input validation in plugins

This commit is contained in:
alonso.torres 2024-06-14 15:19:57 +02:00
parent e13d543dcd
commit c5c8be4b4a
10 changed files with 120 additions and 40 deletions

View file

@ -26,7 +26,7 @@
[app.plugins.page :as page]
[app.plugins.shape :as shape]
[app.plugins.user :as user]
[app.plugins.utils :as utils]
[app.plugins.utils :as u]
[app.plugins.viewport :as viewport]
[app.util.object :as obj]
[beicon.v2.core :as rx]
@ -61,7 +61,7 @@
(getViewport
[_]
(viewport/create-proxy $plugin))
(viewport/viewport-proxy $plugin))
(getFile
[_]
@ -108,28 +108,50 @@
(uploadMediaUrl
[_ name url]
(let [file-id (:current-file-id @st/state)]
(p/create
(fn [resolve reject]
(->> (dwm/upload-media-url name file-id url)
(rx/map utils/to-js)
(rx/take 1)
(rx/subs! resolve reject))))))
(cond
(not (string? name))
(u/display-not-valid :uploadMedia-name name)
(not (string? url))
(u/display-not-valid :uploadMedia-url url)
:else
(let [file-id (:current-file-id @st/state)]
(p/create
(fn [resolve reject]
(->> (dwm/upload-media-url name file-id url)
(rx/map u/to-js)
(rx/take 1)
(rx/subs! resolve reject)))))))
(group
[_ shapes]
(let [file-id (:current-file-id @st/state)
page-id (:current-page-id @st/state)
id (uuid/next)
ids (into #{} (map #(obj/get % "$id")) shapes)]
(st/emit! (dwg/group-shapes id ids))
(shape/shape-proxy $plugin file-id page-id id)))
(cond
(or (not (array? shapes)) (not (every? shape/shape-proxy? shapes)))
(u/display-not-valid :group-shapes shapes)
:else
(let [file-id (:current-file-id @st/state)
page-id (:current-page-id @st/state)
id (uuid/next)
ids (into #{} (map #(obj/get % "$id")) shapes)]
(st/emit! (dwg/group-shapes id ids))
(shape/shape-proxy $plugin file-id page-id id))))
(ungroup
[_ group & rest]
(let [shapes (concat [group] rest)
ids (into #{} (map #(obj/get % "$id")) shapes)]
(st/emit! (dwg/ungroup-shapes ids))))
(cond
(not (shape/shape-proxy? group))
(u/display-not-valid :ungroup group)
(and (some? rest) (not (every? shape/shape-proxy? rest)))
(u/display-not-valid :ungroup rest)
:else
(let [shapes (concat [group] rest)
ids (into #{} (map #(obj/get % "$id")) shapes)]
(st/emit! (dwg/ungroup-shapes ids)))))
(createFrame
[_]
@ -161,23 +183,32 @@
(createText
[_ text]
(let [file-id (:current-file-id @st/state)
page-id (:current-page-id @st/state)
page (dm/get-in @st/state [:workspace-data :pages-index page-id])
shape (-> (cts/setup-shape {:type :text :x 0 :y 0 :grow-type :auto-width})
(txt/change-text text)
(assoc :position-data nil))
changes
(-> (cb/empty-changes)
(cb/with-page page)
(cb/with-objects (:objects page))
(cb/add-object shape))]
(st/emit! (ch/commit-changes changes))
(shape/shape-proxy $plugin file-id page-id (:id shape))))
(cond
(or (not (string? text)) (empty? text))
(u/display-not-valid :createText text)
:else
(let [file-id (:current-file-id @st/state)
page-id (:current-page-id @st/state)
page (dm/get-in @st/state [:workspace-data :pages-index page-id])
shape (-> (cts/setup-shape {:type :text :x 0 :y 0 :grow-type :auto-width})
(txt/change-text text)
(assoc :position-data nil))
changes
(-> (cb/empty-changes)
(cb/with-page page)
(cb/with-objects (:objects page))
(cb/add-object shape))]
(st/emit! (ch/commit-changes changes))
(shape/shape-proxy $plugin file-id page-id (:id shape)))))
(createShapeFromSvg
[_ svg-string]
(when (some? svg-string)
(cond
(not (string? svg-string))
(u/display-not-valid :createShapeFromSvg svg-string)
:else
(let [id (uuid/next)
file-id (:current-file-id @st/state)
page-id (:current-page-id @st/state)]
@ -185,15 +216,19 @@
(shape/shape-proxy $plugin file-id page-id id))))
(createBoolean [_ bool-type shapes]
(let [ids (into #{} (map #(obj/get % "$id")) shapes)
bool-type (keyword bool-type)]
(let [bool-type (keyword bool-type)]
(cond
(not (contains? cts/bool-types bool-type))
(u/display-not-valid :createBoolean-boolType bool-type)
(if (contains? cts/bool-types bool-type)
(let [id-ret (atom nil)]
(or (not (array? shapes)) (empty? shapes) (not (every? shape/shape-proxy? shapes)))
(u/display-not-valid :createBoolean-shapes shapes)
:else
(let [ids (into #{} (map #(obj/get % "$id")) shapes)
id-ret (atom nil)]
(st/emit! (dwb/create-bool bool-type ids {:id-ret id-ret}))
(shape/shape-proxy $plugin @id-ret))
(utils/display-not-valid :bool-shape bool-type)))))
(shape/shape-proxy $plugin @id-ret))))))
(defn create-context
[plugin-id]

View file

@ -93,6 +93,9 @@
{:name js/Symbol.toStringTag
:get (fn [] (str "FileProxy"))})
(defn file-proxy? [p]
(instance? FileProxy p))
(defn file-proxy
[plugin-id id]
(crc/add-properties!

View file

@ -29,6 +29,9 @@
(st/emit! (dwt/move-shapes-to-frame #{child-id} $id nil nil)
(ptk/data-event :layout/update {:ids [$id]})))))
(defn flex-layout-proxy? [p]
(instance? FlexLayout p))
(defn flex-layout-proxy
[plugin-id file-id page-id id]
(-> (FlexLayout. plugin-id file-id page-id id)
@ -151,6 +154,9 @@
(deftype LayoutChildProxy [$plugin $file $page $id])
(defn layout-child-proxy? [p]
(instance? LayoutChildProxy p))
(defn layout-child-proxy
[plugin-id file-id page-id id]
(-> (LayoutChildProxy. plugin-id file-id page-id id)

View file

@ -39,6 +39,9 @@
:font-weight (d/nilv (obj/get variant "fontWeight") fontWeight)}]
(st/emit! (dwt/update-text-range id start end values)))))
(defn font-proxy? [p]
(instance? PenpotFont p))
(defn font-proxy
[{:keys [id name variants] :as font}]
(when (some? font)

View file

@ -74,6 +74,9 @@
(st/emit! (dwt/move-shapes-to-frame #{child-id} $id nil [row column])
(ptk/data-event :layout/update {:ids [$id]})))))
(defn grid-layout-proxy? [p]
(instance? GridLayout p))
(defn grid-layout-proxy
[plugin-id file-id page-id id]
(-> (GridLayout. plugin-id file-id page-id id)
@ -199,6 +202,9 @@
(deftype GridCellProxy [$plugin $file $page $id])
(defn layout-cell-proxy? [p]
(instance? GridCellProxy p))
(defn layout-cell-proxy
[plugin-id file-id page-id id]
(letfn [(locate-cell [_]

View file

@ -137,6 +137,9 @@
(let [color (u/proxy->library-color self)]
(apply array (keys (dm/get-in color [:plugin-data (keyword "shared" namespace)])))))))
(defn lib-color-proxy? [p]
(instance? LibraryColorProxy p))
(defn lib-color-proxy
[plugin-id file-id id]
(assert (uuid? file-id))
@ -316,6 +319,9 @@
(let [typography (u/proxy->library-typography self)]
(apply array (keys (dm/get-in typography [:plugin-data (keyword "shared" namespace)])))))))
(defn lib-typography-proxy? [p]
(instance? LibraryTypographyProxy p))
(defn lib-typography-proxy
[plugin-id file-id id]
(assert (uuid? file-id))
@ -522,6 +528,9 @@
(let [component (u/proxy->library-component self)]
(apply array (keys (dm/get-in component [:plugin-data (keyword "shared" namespace)])))))))
(defn lib-component-proxy? [p]
(instance? LibraryComponentProxy p))
(defn lib-component-proxy
[plugin-id file-id id]
(assert (uuid? file-id))
@ -643,6 +652,9 @@
(let [file (u/proxy->file self)]
(apply array (keys (dm/get-in file [:data :plugin-data (keyword "shared" namespace)])))))))
(defn library-proxy? [p]
(instance? Library p))
(defn library-proxy
[plugin-id file-id]
(assert (uuid? file-id) "File id not valid")

View file

@ -107,6 +107,9 @@
{:name js/Symbol.toStringTag
:get (fn [] (str "PageProxy"))})
(defn page-proxy? [p]
(instance? PageProxy p))
(defn page-proxy
[plugin-id file-id id]
(crc/add-properties!

View file

@ -380,6 +380,9 @@
{:name js/Symbol.toStringTag
:get (fn [] (str "ShapeProxy"))})
(defn shape-proxy? [p]
(instance? ShapeProxy p))
(defn shape-proxy
([plugin-id id]
(shape-proxy plugin-id (:current-file-id @st/state) (:current-page-id @st/state) id))

View file

@ -38,11 +38,17 @@
{:name "sessionId"
:get (fn [_] (str session-id))})))
(defn current-user-proxy? [p]
(instance? CurrentUserProxy p))
(defn current-user-proxy
[plugin-id session-id]
(-> (CurrentUserProxy. plugin-id session-id)
(add-user-properties)))
(defn active-user-proxy? [p]
(instance? ActiveUserProxy p))
(defn active-user-proxy
[plugin-id session-id]
(-> (ActiveUserProxy. plugin-id session-id)

View file

@ -32,7 +32,10 @@
{:name js/Symbol.toStringTag
:get (fn [] (str "ViewportProxy"))})
(defn create-proxy
(defn viewport-proxy? [p]
(instance? ViewportProxy p))
(defn viewport-proxy
[plugin-id]
(crc/add-properties!
(ViewportProxy. plugin-id)