0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-03-12 15:51:37 -05:00

Make profile query work for unauthenticated users.

This commit is contained in:
Andrey Antukh 2020-04-07 16:10:15 +02:00
parent 4465db130d
commit 115ba72572
3 changed files with 43 additions and 41 deletions

View file

@ -17,26 +17,25 @@
[vertx.web :as vw] [vertx.web :as vw]
[vertx.eventbus :as ve])) [vertx.eventbus :as ve]))
(def mutation-types-hierarchy (def unauthorized-services
(-> (make-hierarchy) #{:create-demo-profile
(derive :login ::unauthenticated) :logout
(derive :logout ::unauthenticated) :profile
(derive :register-profile ::unauthenticated) :recover-profile
(derive :request-profile-recovery ::unauthenticated) :register-profile
(derive :recover-profile ::unauthenticated) :request-profile-recovery
(derive :create-demo-profile ::unauthenticated))) :viewer-bundle
:login})
(def query-types-hierarchy
(make-hierarchy))
(defn query-handler (defn query-handler
[req] [req]
(let [type (keyword (get-in req [:path-params :type])) (let [type (keyword (get-in req [:path-params :type]))
data (merge (:params req) data (merge (:params req)
{::sq/type type {::sq/type type})
:profile-id (:profile-id req)})] data (cond-> data
(:profile-id req) (assoc :profile-id (:profile-id req)))]
(if (or (:profile-id req) (if (or (:profile-id req)
(isa? query-types-hierarchy type ::unauthenticated)) (contains? unauthorized-services type))
(-> (sq/handle (with-meta data {:req req})) (-> (sq/handle (with-meta data {:req req}))
(p/then' (fn [result] (p/then' (fn [result]
{:status 200 {:status 200
@ -51,10 +50,11 @@
data (merge (:params req) data (merge (:params req)
(:body-params req) (:body-params req)
(:uploads req) (:uploads req)
{::sm/type type {::sm/type type})
:profile-id (:profile-id req)})] data (cond-> data
(:profile-id req) (assoc :profile-id (:profile-id req)))]
(if (or (:profile-id req) (if (or (:profile-id req)
(isa? mutation-types-hierarchy type ::unauthenticated)) (contains? unauthorized-services type))
(-> (sm/handle (with-meta data {:req req})) (-> (sm/handle (with-meta data {:req req}))
(p/then' (fn [result] (p/then' (fn [result]
{:status 200 :body result}))) {:status 200 :body result})))

View file

@ -91,15 +91,6 @@
(db/query-one conn [sql:profile-by-email email])) (db/query-one conn [sql:profile-by-email email]))
;; --- Mutation: Add additional email
;; TODO
;; --- Mutation: Mark email as main email
;; TODO
;; --- Mutation: Verify email (or maybe query?)
;; TODO
;; --- Mutation: Update Profile (own) ;; --- Mutation: Update Profile (own)
(def ^:private sql:update-profile (def ^:private sql:update-profile
@ -158,6 +149,7 @@
(update-password conn params))) (update-password conn params)))
;; --- Mutation: Update Photo ;; --- Mutation: Update Photo
(declare upload-photo) (declare upload-photo)

View file

@ -15,6 +15,7 @@
[uxbox.images :as images] [uxbox.images :as images]
[uxbox.services.queries :as sq] [uxbox.services.queries :as sq]
[uxbox.services.util :as su] [uxbox.services.util :as su]
[uxbox.util.uuid :as uuid]
[uxbox.util.blob :as blob])) [uxbox.util.blob :as blob]))
;; --- Helpers & Specs ;; --- Helpers & Specs
@ -32,11 +33,19 @@
;; --- Query: Profile (own) ;; --- Query: Profile (own)
(defn retrieve-profile (declare retrieve-profile)
[conn id] (declare retrieve-additional-data)
(let [sql "select * from profile where id=$1 and deleted_at is null"]
(db/query-one db/pool [sql id])))
(s/def ::profile
(s/keys :opt-un [::profile-id]))
(sq/defquery ::profile
[{:keys [profile-id] :as params}]
(if profile-id
(db/with-atomic [conn db/pool]
(retrieve-profile conn profile-id))
{:id uuid/zero
:fullname "Anonymous User"}))
;; NOTE: this query make the assumption that union all preserves the ;; NOTE: this query make the assumption that union all preserves the
;; order so the first id will always be the team id and the second the ;; order so the first id will always be the team id and the second the
@ -65,18 +74,19 @@
{:default-team-id (:id team) {:default-team-id (:id team)
:default-project-id (:id project)})))) :default-project-id (:id project)}))))
(s/def ::profile (defn retrieve-profile-data
(s/keys :req-un [::profile-id])) [conn id]
(let [sql "select * from profile where id=$1 and deleted_at is null"]
(db/query-one conn [sql id])))
(sq/defquery ::profile (defn retrieve-profile
[{:keys [profile-id] :as params}] [conn id]
(db/with-atomic [conn db/pool] (p/let [prof (-> (retrieve-profile-data conn id)
(p/let [prof (-> (retrieve-profile conn profile-id)
(p/then' su/raise-not-found-if-nil) (p/then' su/raise-not-found-if-nil)
(p/then' strip-private-attrs) (p/then' strip-private-attrs)
(p/then' #(images/resolve-media-uris % [:photo :photo-uri]))) (p/then' #(images/resolve-media-uris % [:photo :photo-uri])))
addt (retrieve-additional-data conn profile-id)] addt (retrieve-additional-data conn id)]
(merge prof addt)))) (merge prof addt)))
;; --- Attrs Helpers ;; --- Attrs Helpers