mirror of
https://github.com/penpot/penpot.git
synced 2025-05-20 20:26:14 +02:00
🎉 Add the ability to create demo user on demand.
This commit is contained in:
parent
b0ca6493e3
commit
e42ccf932e
12 changed files with 285 additions and 199 deletions
|
@ -58,6 +58,7 @@
|
|||
:smtp-ssl (lookup-env env :uxbox-smtp-ssl false)
|
||||
:smtp-enabled (lookup-env env :uxbox-smtp-enabled false)
|
||||
|
||||
:allow-demo-users (lookup-env env :uxbox-allow-demo-users true)
|
||||
:registration-enabled (lookup-env env :uxbox-registration-enabled true)})
|
||||
|
||||
(defn read-test-config
|
||||
|
|
|
@ -77,7 +77,6 @@
|
|||
(> user-index 0))
|
||||
(create-additional-project-user conn [project-index user-index])))))
|
||||
|
||||
|
||||
;; --- Create Page Files
|
||||
|
||||
(def create-file-sql
|
||||
|
|
|
@ -22,7 +22,8 @@
|
|||
(derive :logout ::unauthenticated)
|
||||
(derive :register-profile ::unauthenticated)
|
||||
(derive :request-profile-recovery ::unauthenticated)
|
||||
(derive :recover-profile ::unauthenticated)))
|
||||
(derive :recover-profile ::unauthenticated)
|
||||
(derive :create-demo-profile ::unauthenticated)))
|
||||
|
||||
(def query-types-hierarchy
|
||||
(make-hierarchy))
|
||||
|
|
|
@ -19,7 +19,7 @@
|
|||
(require 'uxbox.services.queries.projects)
|
||||
(require 'uxbox.services.queries.project-files)
|
||||
(require 'uxbox.services.queries.project-pages)
|
||||
(require 'uxbox.services.queries.users)
|
||||
(require 'uxbox.services.queries.profile)
|
||||
(require 'uxbox.services.queries.user-attrs))
|
||||
|
||||
(defn- load-mutation-services
|
||||
|
|
52
backend/src/uxbox/services/mutations/demo.clj
Normal file
52
backend/src/uxbox/services/mutations/demo.clj
Normal file
|
@ -0,0 +1,52 @@
|
|||
;; This Source Code Form is subject to the terms of the Mozilla Public
|
||||
;; License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
;;
|
||||
;; This Source Code Form is "Incompatible With Secondary Licenses", as
|
||||
;; defined by the Mozilla Public License, v. 2.0.
|
||||
;;
|
||||
;; Copyright (c) 2016-2020 Andrey Antukh <niwi@niwi.nz>
|
||||
|
||||
(ns uxbox.services.mutations.demo
|
||||
"A demo specific mutations."
|
||||
(:require
|
||||
[clojure.spec.alpha :as s]
|
||||
[datoteka.core :as fs]
|
||||
[datoteka.storages :as ds]
|
||||
[promesa.core :as p]
|
||||
[promesa.exec :as px]
|
||||
[sodi.prng]
|
||||
[sodi.pwhash]
|
||||
[sodi.util]
|
||||
[uxbox.common.exceptions :as ex]
|
||||
[uxbox.common.spec :as us]
|
||||
[uxbox.config :as cfg]
|
||||
[uxbox.db :as db]
|
||||
[uxbox.emails :as emails]
|
||||
[uxbox.images :as images]
|
||||
[uxbox.media :as media]
|
||||
[uxbox.services.mutations :as sm]
|
||||
[uxbox.services.util :as su]
|
||||
[uxbox.services.mutations.profile :as profile]
|
||||
[uxbox.util.blob :as blob]
|
||||
[uxbox.util.uuid :as uuid]
|
||||
[vertx.core :as vc]))
|
||||
|
||||
(su/defstr sql:create-demo-user
|
||||
"insert into users (id, fullname, username, email, password, photo, is_demo)
|
||||
values ($1, $2, $3, $4, $5, '', true) returning *")
|
||||
|
||||
(sm/defmutation ::create-demo-profile
|
||||
[params]
|
||||
(let [id (uuid/next)
|
||||
sem (System/currentTimeMillis)
|
||||
username (str "demo-" sem)
|
||||
email (str username ".demo@uxbox.io")
|
||||
fullname (str "Demo User " sem)
|
||||
password (-> (sodi.prng/random-bytes 12)
|
||||
(sodi.util/bytes->b64s))
|
||||
password' (sodi.pwhash/derive password)]
|
||||
(db/with-atomic [conn db/pool]
|
||||
(db/query-one conn [sql:create-demo-user id fullname username email password'])
|
||||
{:username username
|
||||
:password password})))
|
|
@ -26,10 +26,7 @@
|
|||
[uxbox.media :as media]
|
||||
[uxbox.services.mutations :as sm]
|
||||
[uxbox.services.util :as su]
|
||||
[uxbox.services.queries.users :refer [get-profile
|
||||
decode-profile-row
|
||||
strip-private-attrs
|
||||
resolve-thumbnail]]
|
||||
[uxbox.services.queries.profile :as profile]
|
||||
[uxbox.util.blob :as blob]
|
||||
[uxbox.util.uuid :as uuid]
|
||||
[vertx.core :as vc]))
|
||||
|
@ -125,8 +122,7 @@
|
|||
email fullname (blob/encode metadata)]]
|
||||
(-> (db/query-one conn sqlv)
|
||||
(p/then' su/raise-not-found-if-nil)
|
||||
(p/then' decode-profile-row)
|
||||
(p/then' strip-private-attrs))))
|
||||
(p/then' profile/strip-private-attrs))))
|
||||
|
||||
(s/def ::update-profile
|
||||
(s/keys :req-un [::id ::username ::email ::fullname ::metadata]))
|
||||
|
@ -142,7 +138,7 @@
|
|||
|
||||
(defn- validate-password
|
||||
[conn {:keys [user old-password] :as params}]
|
||||
(p/let [profile (get-profile conn user)
|
||||
(p/let [profile (profile/retrieve-profile conn user)
|
||||
result (sodi.pwhash/verify old-password (:password profile))]
|
||||
(when-not (:valid result)
|
||||
(ex/raise :type :validation
|
||||
|
@ -205,8 +201,7 @@
|
|||
returning id, photo"]
|
||||
(-> (db/query-one db/pool [sql (str path) user])
|
||||
(p/then' su/raise-not-found-if-nil)
|
||||
;; (p/then' strip-private-attrs)
|
||||
(p/then resolve-thumbnail))))]
|
||||
(p/then profile/resolve-thumbnail))))]
|
||||
|
||||
(when-not (valid-image-types? (:mtype file))
|
||||
(ex/raise :type :validation
|
||||
|
@ -218,9 +213,9 @@
|
|||
|
||||
;; --- Mutation: Register Profile
|
||||
|
||||
(def ^:private create-user-sql
|
||||
"insert into users (id, fullname, username, email, password, metadata, photo)
|
||||
values ($1, $2, $3, $4, $5, $6, '') returning *")
|
||||
(su/defstr sql:create-user
|
||||
"insert into users (id, fullname, username, email, password, photo)
|
||||
values ($1, $2, $3, $4, $5, '') returning *")
|
||||
|
||||
(defn- check-profile-existence!
|
||||
[conn {:keys [username email] :as params}]
|
||||
|
@ -239,24 +234,21 @@
|
|||
(defn create-profile
|
||||
"Create the user entry on the database with limited input
|
||||
filling all the other fields with defaults."
|
||||
[conn {:keys [id username fullname email password metadata] :as params}]
|
||||
[conn {:keys [id username fullname email password] :as params}]
|
||||
(let [id (or id (uuid/next))
|
||||
metadata (blob/encode metadata)
|
||||
password (sodi.pwhash/derive password)
|
||||
sqlv [create-user-sql
|
||||
sqlv [sql:create-user
|
||||
id
|
||||
fullname
|
||||
username
|
||||
email
|
||||
password
|
||||
metadata]]
|
||||
(-> (db/query-one conn sqlv)
|
||||
(p/then' decode-profile-row))))
|
||||
password]]
|
||||
(db/query-one conn sqlv)))
|
||||
|
||||
(defn register-profile
|
||||
[conn params]
|
||||
(-> (create-profile conn params)
|
||||
(p/then' strip-private-attrs)
|
||||
(p/then' profile/strip-private-attrs)
|
||||
(p/then (fn [profile]
|
||||
(-> (emails/send! emails/register {:to (:email params)
|
||||
:name (:fullname params)})
|
||||
|
@ -328,3 +320,5 @@
|
|||
(db/with-atomic [conn db/pool]
|
||||
(-> (validate-token conn token)
|
||||
(p/then (fn [user-id] (update-password conn user-id)))))))
|
||||
|
||||
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
;;
|
||||
;; Copyright (c) 2016 Andrey Antukh <niwi@niwi.nz>
|
||||
|
||||
(ns uxbox.services.queries.users
|
||||
(ns uxbox.services.queries.profile
|
||||
(:require
|
||||
[clojure.spec.alpha :as s]
|
||||
[promesa.core :as p]
|
||||
|
@ -18,7 +18,6 @@
|
|||
|
||||
;; --- Helpers & Specs
|
||||
|
||||
(declare decode-profile-row)
|
||||
(declare strip-private-attrs)
|
||||
|
||||
(s/def ::email ::us/email)
|
||||
|
@ -42,28 +41,21 @@
|
|||
(-> (px/submit! #(images/populate-thumbnails user opts))
|
||||
(su/handle-on-context))))
|
||||
|
||||
(defn get-profile
|
||||
(defn retrieve-profile
|
||||
[conn id]
|
||||
(let [sql "select * from users where id=$1 and deleted_at is null"]
|
||||
(-> (db/query-one db/pool [sql id])
|
||||
(p/then' decode-profile-row))))
|
||||
(db/query-one db/pool [sql id])))
|
||||
|
||||
(s/def ::profile
|
||||
(s/keys :req-un [::user]))
|
||||
|
||||
(sq/defquery ::profile
|
||||
[{:keys [user] :as params}]
|
||||
(-> (get-profile db/pool user)
|
||||
(-> (retrieve-profile db/pool user)
|
||||
(p/then' strip-private-attrs)))
|
||||
|
||||
;; --- Attrs Helpers
|
||||
|
||||
(defn decode-profile-row
|
||||
[{:keys [metadata] :as row}]
|
||||
(when row
|
||||
(cond-> row
|
||||
metadata (assoc :metadata (blob/decode metadata)))))
|
||||
|
||||
(defn strip-private-attrs
|
||||
"Only selects a publicy visible user attrs."
|
||||
[profile]
|
Loading…
Add table
Add a link
Reference in a new issue