From 9ecbddc18cd1dd9103914e7c75621adaa6288eb6 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Tue, 5 Oct 2021 15:58:45 +0200 Subject: [PATCH] :recycle: Refactor internal handling of profile props. --- backend/src/app/http/oauth.clj | 22 +++++++++----- backend/src/app/rpc/mutations/profile.clj | 35 ++++++++++------------ backend/src/app/rpc/queries/profile.clj | 6 +++- backend/test/app/services_profile_test.clj | 15 +--------- 4 files changed, 36 insertions(+), 42 deletions(-) diff --git a/backend/src/app/http/oauth.clj b/backend/src/app/http/oauth.clj index a60be8490..64b1c7036 100644 --- a/backend/src/app/http/oauth.clj +++ b/backend/src/app/http/oauth.clj @@ -62,6 +62,13 @@ :cause e) nil))) +(defn- qualify-props + [provider props] + (reduce-kv (fn [result k v] + (assoc result (keyword (:name provider) (name k)) v)) + {} + props)) + (defn- retrieve-user-info [{:keys [provider] :as cfg} tdata] (try @@ -76,8 +83,8 @@ {:backend (:name provider) :email (:email info) :fullname (:name info) - :props (dissoc info :name :email)}))) - + :props (->> (dissoc info :name :email) + (qualify-props provider))}))) (catch Exception e (l/error :hint "unexpected exception on retrieve-user-info" :cause e) @@ -138,15 +145,14 @@ ;; --- HTTP HANDLERS -(defn extract-props +(defn extract-utm-props + "Extracts additional data from user params." [params] (reduce-kv (fn [params k v] (let [sk (name k)] (cond-> params - (or (str/starts-with? sk "pm_") - (str/starts-with? sk "pm-") - (str/starts-with? sk "utm_")) - (assoc (-> sk str/kebab keyword) v)))) + (str/starts-with? sk "utm_") + (assoc (->> sk str/kebab (keyword "penpot")) v)))) {} params)) @@ -210,7 +216,7 @@ (defn- auth-handler [{:keys [tokens] :as cfg} {:keys [params] :as request}] (let [invitation (:invitation-token params) - props (extract-props params) + props (extract-utm-props params) state (tokens :generate {:iss :oauth :invitation-token invitation diff --git a/backend/src/app/rpc/mutations/profile.clj b/backend/src/app/rpc/mutations/profile.clj index bbef6f54e..8c5e3a7ba 100644 --- a/backend/src/app/rpc/mutations/profile.clj +++ b/backend/src/app/rpc/mutations/profile.clj @@ -12,7 +12,7 @@ [app.config :as cf] [app.db :as db] [app.emails :as eml] - [app.http.oauth :refer [extract-props]] + [app.http.oauth :refer [extract-utm-props]] [app.loggers.audit :as audit] [app.media :as media] [app.metrics :as mtx] @@ -127,23 +127,16 @@ ;; --- MUTATION: Register Profile (s/def ::accept-terms-and-privacy ::us/boolean) -(s/def ::accept-newsletter-subscription ::us/boolean) (s/def ::token ::us/not-empty-string) (s/def ::register-profile - (s/keys :req-un [::token ::fullname - ::accept-terms-and-privacy] - :opt-un [::accept-newsletter-subscription])) + (s/keys :req-un [::token ::fullname])) (sv/defmethod ::register-profile {:auth false :rlimit :password} [{:keys [pool] :as cfg} params] - (when-not (:accept-terms-and-privacy params) - (ex/raise :type :validation - :code :invalid-terms-and-privacy)) - (db/with-atomic [conn pool] - (let [cfg (assoc cfg :conn conn)] - (register-profile cfg params)))) + (-> (assoc cfg :conn conn) + (register-profile params)))) (defn- annotate-profile-register "A helper for properly increase the profile-register metric once the @@ -162,6 +155,7 @@ (create-profile conn) (create-profile-relations conn) (decode-profile-row))] + (sid/load-initial-project! conn profile) (cond @@ -223,18 +217,17 @@ [conn params] (let [id (or (:id params) (uuid/next)) - props (-> (extract-props params) + props (-> (extract-utm-props params) (merge (:props params)) - (assoc :accept-terms-and-privacy (:accept-terms-and-privacy params true)) - (assoc :accept-newsletter-subscription (:accept-newsletter-subscription params false)) (db/tjson)) password (if-let [password (:password params)] (derive-password password) "!") - locale (as-> (:locale params) locale - (and (string? locale) (not (str/blank? locale)) locale)) + locale (:locale params) + locale (when (and (string? locale) (not (str/blank? locale))) + locale) backend (:backend params "penpot") is-demo (:is-demo params false) @@ -591,11 +584,15 @@ (db/with-atomic [conn pool] (let [profile (profile/retrieve-profile-data conn profile-id) props (reduce-kv (fn [props k v] - (if (nil? v) - (dissoc props k) - (assoc props k v))) + ;; We don't accept namespaced keys + (if (simple-ident? k) + (if (nil? v) + (dissoc props k) + (assoc props k v)) + props)) (:props profile) props)] + (db/update! conn :profile {:props (db/tjson props)} {:id profile-id}) diff --git a/backend/src/app/rpc/queries/profile.clj b/backend/src/app/rpc/queries/profile.clj index 41f322881..6843a0719 100644 --- a/backend/src/app/rpc/queries/profile.clj +++ b/backend/src/app/rpc/queries/profile.clj @@ -70,6 +70,10 @@ [conn profile] (merge profile (retrieve-additional-data conn (:id profile)))) +(defn- filter-profile-props + [props] + (into {} (filter (fn [[k _]] (simple-ident? k))) props)) + (defn decode-profile-row [{:keys [props] :as row}] (cond-> row @@ -90,7 +94,7 @@ (ex/raise :type :not-found :hint "Object doest not exists.")) - profile)) + (update profile :props filter-profile-props))) (def ^:private sql:profile-by-email "select p.* from profile as p diff --git a/backend/test/app/services_profile_test.clj b/backend/test/app/services_profile_test.clj index a2bc0acdc..35d3990cf 100644 --- a/backend/test/app/services_profile_test.clj +++ b/backend/test/app/services_profile_test.clj @@ -177,17 +177,6 @@ (t/is (string? token)) - ;; try register without accepting terms - (let [data {::th/type :register-profile - :token token - :fullname "foobar" - :accept-terms-and-privacy false} - out (th/mutation! data)] - (let [error (:error out)] - (t/is (th/ex-info? error)) - (t/is (th/ex-of-type? error :validation)) - (t/is (th/ex-of-code? error :invalid-terms-and-privacy)))) - ;; try register without token (let [data {::th/type :register-profile :fullname "foobar" @@ -205,9 +194,7 @@ :accept-terms-and-privacy true :accept-newsletter-subscription true}] (let [{:keys [result error]} (th/mutation! data)] - (t/is (nil? error)) - (t/is (true? (get-in result [:props :accept-newsletter-subscription]))) - (t/is (true? (get-in result [:props :accept-terms-and-privacy]))))) + (t/is (nil? error)))) )) (t/deftest prepare-register-with-registration-disabled