mirror of
https://github.com/penpot/penpot.git
synced 2025-06-01 06:41:39 +02:00
✨ Add more flexible prepl api for external tools
This commit is contained in:
parent
8d60b3fc3e
commit
bf8a514871
2 changed files with 49 additions and 16 deletions
|
@ -30,7 +30,7 @@
|
||||||
:init repl-init
|
:init repl-init
|
||||||
:read ccs/repl-read))
|
:read ccs/repl-read))
|
||||||
|
|
||||||
(defn json-prepl
|
(defn json-repl
|
||||||
[]
|
[]
|
||||||
(let [out *out*
|
(let [out *out*
|
||||||
lock (locks/create)]
|
lock (locks/create)]
|
||||||
|
@ -61,7 +61,7 @@
|
||||||
[[type _] {:keys [::flag port host] :as cfg}]
|
[[type _] {:keys [::flag port host] :as cfg}]
|
||||||
(when (contains? cf/flags flag)
|
(when (contains? cf/flags flag)
|
||||||
(let [accept (case type
|
(let [accept (case type
|
||||||
::prepl 'app.srepl/json-prepl
|
::prepl 'app.srepl/json-repl
|
||||||
::urepl 'app.srepl/user-repl)
|
::urepl 'app.srepl/user-repl)
|
||||||
params {:address host
|
params {:address host
|
||||||
:port port
|
:port port
|
||||||
|
|
|
@ -8,37 +8,70 @@
|
||||||
"PREPL API for external usage (CLI or ADMIN)"
|
"PREPL API for external usage (CLI or ADMIN)"
|
||||||
(:require
|
(:require
|
||||||
[app.auth :as auth]
|
[app.auth :as auth]
|
||||||
|
[app.common.exceptions :as ex]
|
||||||
[app.common.uuid :as uuid]
|
[app.common.uuid :as uuid]
|
||||||
[app.db :as db]
|
[app.db :as db]
|
||||||
[app.rpc.commands.auth :as cmd.auth]))
|
[app.rpc.commands.auth :as cmd.auth]
|
||||||
|
[app.util.json :as json]
|
||||||
|
[cuerdas.core :as str]))
|
||||||
|
|
||||||
(defn- get-current-system
|
(defn- get-current-system
|
||||||
[]
|
[]
|
||||||
(or (deref (requiring-resolve 'app.main/system))
|
(or (deref (requiring-resolve 'app.main/system))
|
||||||
(deref (requiring-resolve 'user/system))))
|
(deref (requiring-resolve 'user/system))))
|
||||||
|
|
||||||
(defn derive-password
|
(defmulti ^:private run-json-cmd* ::cmd)
|
||||||
[password]
|
|
||||||
(auth/derive-password password))
|
|
||||||
|
|
||||||
(defn create-profile
|
(defn run-json-cmd
|
||||||
[fullname, email, password]
|
"Entry point with external tools integrations that uses PREPL
|
||||||
|
interface for interacting with running penpot backend."
|
||||||
|
[data]
|
||||||
|
(let [data (json/decode data)
|
||||||
|
params (merge {::cmd (keyword (:cmd data "default"))}
|
||||||
|
(:params data))]
|
||||||
|
(run-json-cmd* params)))
|
||||||
|
|
||||||
|
(defmethod run-json-cmd* :create-profile
|
||||||
|
[{:keys [fullname email password is-active]
|
||||||
|
:or {is-active true}}]
|
||||||
(when-let [system (get-current-system)]
|
(when-let [system (get-current-system)]
|
||||||
(db/with-atomic [conn (:app.db/pool system)]
|
(db/with-atomic [conn (:app.db/pool system)]
|
||||||
(let [params {:id (uuid/next)
|
(let [params {:id (uuid/next)
|
||||||
:email email
|
:email email
|
||||||
:fullname fullname
|
:fullname fullname
|
||||||
:is-active true
|
:is-active is-active
|
||||||
:password password
|
:password password
|
||||||
:props {}}
|
:props {}}]
|
||||||
|
(->> (cmd.auth/create-profile conn params)
|
||||||
profile (->> (cmd.auth/create-profile conn params)
|
(cmd.auth/create-profile-relations conn))))))
|
||||||
(cmd.auth/create-profile-relations conn))]
|
|
||||||
|
|
||||||
(str (:id profile))))))
|
|
||||||
|
|
||||||
|
|
||||||
|
(defmethod run-json-cmd* :update-profile
|
||||||
|
[{:keys [fullname email password is-active]}]
|
||||||
|
(when-let [system (get-current-system)]
|
||||||
|
(db/with-atomic [conn (:app.db/pool system)]
|
||||||
|
(let [params (cond-> {}
|
||||||
|
(some? fullname)
|
||||||
|
(assoc :fullname fullname)
|
||||||
|
|
||||||
|
(some? password)
|
||||||
|
(assoc :password (auth/derive-password password))
|
||||||
|
|
||||||
|
(some? is-active)
|
||||||
|
(assoc :is-active is-active))]
|
||||||
|
(when (seq params)
|
||||||
|
(let [res (db/update! conn :profile
|
||||||
|
params
|
||||||
|
{:email email
|
||||||
|
:deleted-at nil}
|
||||||
|
{:return-keys false})]
|
||||||
|
(pos? (:next.jdbc/update-count res))))))))
|
||||||
|
|
||||||
|
(defmethod run-json-cmd* :derive-password
|
||||||
|
[{:keys [password]}]
|
||||||
|
(auth/derive-password password))
|
||||||
|
|
||||||
|
(defmethod run-json-cmd* :default
|
||||||
|
[{:keys [::cmd]}]
|
||||||
|
(ex/raise :type :internal
|
||||||
|
:code :not-implemented
|
||||||
|
:hint (str/ffmt "command '%' not implemented" (name cmd))))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue