Skip to content

Commit

Permalink
remove multitenancy
Browse files Browse the repository at this point in the history
  • Loading branch information
Brett Adams committed Dec 23, 2024
1 parent 4084611 commit ced4908
Show file tree
Hide file tree
Showing 101 changed files with 620 additions and 1,393 deletions.
3 changes: 1 addition & 2 deletions bases/app/deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
poly/location {:local/root "../../components/location"}
poly/material {:local/root "../../components/material"}
poly/media {:local/root "../../components/media"}
poly/organization {:local/root "../../components/organization"}
poly/postmark {:local/root "../../components/postmark"}
poly/store {:local/root "../../components/store"}
poly/taxon {:local/root "../../components/taxon"}
Expand All @@ -17,7 +16,7 @@
poly/validation {:local/root "../../components/validation"}

babashka/fs {:mvn/version "0.4.19"}
com.github.brettatoms/zodiac {:mvn/version "0.4.45"}
com.github.brettatoms/zodiac {:mvn/version "0.4.48"}
com.github.brettatoms/zodiac-assets {:mvn/version "0.1.18"}
com.github.brettatoms/zodiac-sql {:mvn/version "0.1.7"}
com.fzakaria/slf4j-timbre {:mvn/version "0.4.1"}
Expand Down
8 changes: 4 additions & 4 deletions bases/app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion bases/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"alpinejs": "3.14.1",
"autoprefixer": "10.4.20",
"cssnano": "7.0.6",
"daisyui": "4.12.10",
"daisyui": "4.12.19",
"htmx.org": "1.9.12",
"postcss": "8.4.49",
"postcss-import": "16.1.0",
Expand Down
6 changes: 5 additions & 1 deletion bases/app/src/sepal/app/flash.clj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
(defn error [response text]
(add-message response text category/error))

(defn success [response text]
(add-message response text category/success))

#_(defn error-seq
"Create a validation-seq from humanized malli validation error.
Expand Down Expand Up @@ -47,7 +50,8 @@

(defn banner [messages]
[:div {:class (html/attr "pointer-events-none" "fixed" "inset-x-0" "bottom-0" "sm:flex"
"sm:justify-center" "sm:px-6" "sm:pb-5" "lg:px-8" "z-10")}
"sm:justify-center" "sm:px-6" "sm:pb-5" "lg:px-8" "z-10"
"flex" "flex-col" "gap-4")}
(for [{:keys [text category]} messages]
(let [color (condp = category
category/error "bg-red-600"
Expand Down
1 change: 0 additions & 1 deletion bases/app/src/sepal/app/globals.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
(ns sepal.app.globals)

(def ^:dynamic *organization* nil)
(def ^:dynamic *viewer* nil)
46 changes: 2 additions & 44 deletions bases/app/src/sepal/app/middleware.clj
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
(ns sepal.app.middleware
(:require [sepal.app.globals :as g]
[sepal.app.http-response :as http]
[sepal.app.routes.auth.routes :as auth.routes]
[sepal.error.interface :as error.i]
[sepal.organization.interface :as org.i]
[sepal.user.interface :as user.i]
[taoensso.timbre :as log]
[zodiac.core :as z]))
Expand All @@ -28,22 +28,7 @@
(-> request
(assoc :viewer viewer)
(handler)))
(http/see-other :auth/login)))))

(defn require-org-membership [handler path-param-key]
;; TODO: This redirects to root but maybe we should return a 404 or 403 or
;; something.
(fn [{:keys [::z/context path-params viewer] :as request}]
(let [{:keys [db]} context
org-id (Long/parseLong (get path-params path-param-key))
org (org.i/get-user-org db (:user/id viewer))]
(if (= (:organization/id org) org-id)
(binding [g/*organization* org]
(-> request
(assoc-in [:session :organization] org)
(assoc-in [::z/context :organization] org)
(handler)))
(http/see-other :root)))))
(http/see-other auth.routes/login)))))

(defn resource-loader
"Accept a getter function that accepts the request and loads the resource and
Expand Down Expand Up @@ -80,30 +65,3 @@
(let [{:keys [db]} context
id (-> path-params path-param-key coercer)]
(getter db id)))))

(defn require-resource-org-membership
"Require a user to be a member of the organization that owns the resource."
[handler organization-id-key]
(fn [{:keys [::z/context viewer] :as request}]
(let [{:keys [db resource]} context
org-id (get resource organization-id-key)
org (org.i/get-user-org db (:user/id viewer))]
(cond
;; Pass through if the resource doesn't have an organization id
(nil? org-id)
(handler request)

(nil? resource)
(do
(log/warn "Could not get the org of the resource: No resource found")
(handler request))

(= (:organization/id org) org-id)
(binding [g/*organization* org]
(-> request
(assoc-in [:session :organization] org)
(assoc-in [::z/context :organization] org)
(handler)))

:else
(http/see-other :root)))))
24 changes: 17 additions & 7 deletions bases/app/src/sepal/app/routes/accession/core.clj
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
(ns sepal.app.routes.accession.core
(:require [sepal.accession.interface :as accession.i]
[sepal.app.middleware :as middleware]
[sepal.app.routes.accession.create :as create]
[sepal.app.routes.accession.detail :as detail]
[sepal.app.routes.accession.detail.general :as detail-general]
[sepal.app.routes.accession.detail.media :as detail-media]))
[sepal.app.routes.accession.detail.media :as detail-media]
[sepal.app.routes.accession.index :as index]
[sepal.app.routes.accession.routes :as routes]))

(def accession-loader
(middleware/default-loader accession.i/get-by-id
Expand All @@ -12,12 +15,19 @@

(defn routes []
["" {:middleware [[middleware/require-viewer]]}
;; TODO: require permission middlware
["/:id" {:middleware [[middleware/resource-loader accession-loader]
[middleware/require-resource-org-membership :accession/organization-id]]}
["/" {:name :accession/detail
["/"
{:name routes/index
:handler #'index/handler}]
["/new/"
{:name routes/new
:handler #'create/handler
:conflicting true}]
["/:id" {:middleware [[middleware/resource-loader accession-loader]]
:parameters {:path {:id nat-int?}}
:conflicting true}
["/" {:name routes/detail
:handler #'detail/handler}]
["/general/" {:name :accession/detail-general
["/general/" {:name routes/detail-general
:handler #'detail-general/handler}]
["/media/" {:name :accession/detail-media
["/media/" {:name routes/detail-media
:handler #'detail-media/handler}]]])
21 changes: 8 additions & 13 deletions bases/app/src/sepal/app/routes/accession/create.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@
[sepal.app.http-response :refer [found see-other]]
[sepal.app.params :as params]
[sepal.app.routes.accession.form :as accession.form]
[sepal.app.routes.org.routes :as org.routes]
[sepal.app.routes.accession.routes :as accession.routes]
[sepal.app.ui.form :as ui.form]
[sepal.app.ui.page :as page]
[sepal.database.interface :as db.i]
[sepal.error.interface :as error.i]
[zodiac.core :as z]))

(defn page-content [& {:keys [errors values org]}]
(accession.form/form :action (z/url-for org.routes/accessions-new {:org-id (:organization/id org)})
(defn page-content [& {:keys [errors values]}]
(accession.form/form :action (z/url-for accession.routes/new)
:errors errors
:org org
:values values))

(defn footer-buttons []
Expand All @@ -28,7 +27,6 @@
(defn render [& {:keys [errors org values]}]
(-> (page/page :attrs {:x-data "accessionFormData"}
:content (page-content :errors errors
:org org
:values values)
:footer (ui.form/footer :buttons (footer-buttons))
:page-title "Create accession")))
Expand All @@ -48,19 +46,16 @@
[:taxon-id :int]])

(defn handler [{:keys [::z/context form-params request-method viewer]}]
(let [{:keys [db]} context
org (:organization context)]
(let [{:keys [db]} context]
(case request-method
:post
(let [data (-> (params/decode FormParams form-params)
(assoc :organization-id (:organization/id org)))
(let [data (params/decode FormParams form-params)
result (create! db (:user/id viewer) data)]
(if-not (error.i/error? result)
;; TODO: Add a success message
(see-other :accession/detail {:id (:accession/id result)})
(-> (found org.routes/accessions-new {:org-id (:organization/id org)})
(see-other accession.routes/detail {:id (:accession/id result)})
(-> (found accession.routes/new)
(assoc :flash {;;:error (error.i/explain result)
:values data}))))

(render :org org
:values form-params))))
(render :values form-params))))
3 changes: 2 additions & 1 deletion bases/app/src/sepal/app/routes/accession/detail.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
(ns sepal.app.routes.accession.detail
(:require [sepal.app.http-response :as http]
[sepal.app.routes.accession.routes :as accession.routes]
[zodiac.core :as z]))

(defn handler [{:keys [::z/context]}]
(let [{:keys [resource]} context]
(http/found :accession/detail-general {:id (:accession/id resource)})))
(http/found accession.routes/detail-general {:id (:accession/id resource)})))
12 changes: 6 additions & 6 deletions bases/app/src/sepal/app/routes/accession/detail/general.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
[sepal.app.http-response :as http]
[sepal.app.params :as params]
[sepal.app.routes.accession.form :as accession.form]
[sepal.app.routes.accession.routes :as accession.routes]
[sepal.app.ui.form :as ui.form]
[sepal.app.ui.page :as page]
[sepal.app.ui.tabs :as tabs]
Expand All @@ -15,16 +16,16 @@
(defn tab-items [& {:keys [accession]}]
[{:label "General"
:key :name
:href (z/url-for :accession/detail-general {:id (:accession/id accession)})}
:href (z/url-for accession.routes/detail-general {:id (:accession/id accession)})}
{:label "Media"
:key :media
:href (z/url-for :accession/detail-media {:id (:accession/id accession)})}])
:href (z/url-for accession.routes/detail-media {:id (:accession/id accession)})}])

(defn page-content [& {:keys [errors org accession values]}]
[:div {:class "flex flex-col gap-2"}
(tabs/tabs :active :name
:items (tab-items :accession accession))
(accession.form/form :action (z/url-for :accession/detail-general {:id (:accession/id accession)})
(accession.form/form :action (z/url-for accession.routes/detail-general {:id (:accession/id accession)})
:errors errors
:org org
:values values)])
Expand Down Expand Up @@ -77,9 +78,8 @@
(let [result (save! db (:accession/id resource) (:user/id viewer) values)]
;; TODO: handle errors
(if-not (error.i/error? result)
(http/found :accession/detail {:org-id (-> organization :organization/id str)
:id (:accession/id resource)})
(-> (http/found :accession/detail)
(http/found accession.routes/detail {:id (:accession/id resource)})
(-> (http/found accession.routes/detail {:id (:accession/id resource)})
;; TODO: The errors needs to be parsed here and return a message
(assoc :flash {:error result
:values form-params}))))
Expand Down
17 changes: 8 additions & 9 deletions bases/app/src/sepal/app/routes/accession/detail/media.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
[sepal.app.html :as html]
[sepal.app.json :as json]
[sepal.app.params :as params]
[sepal.app.routes.accession.routes :as accession.routes]
[sepal.app.routes.media.routes :as media.routes]
[sepal.app.ui.media :as media.ui]
[sepal.app.ui.page :as page]
[sepal.app.ui.tabs :as tabs]
Expand All @@ -19,18 +21,17 @@
"Upload"])

(defn next-page-url [& {:keys [accession current-page]}]
(z/url-for
:accession/detail-media
{:id (:accession/id accession)}
{:page (+ 1 current-page)}))
(z/url-for accession.routes/detail-media
{:id (:accession/id accession)}
{:page (+ 1 current-page)}))

(defn tab-items [& {:keys [accession]}]
[{:label "General"
:key :name
:href (z/url-for :accession/detail-general {:id (:accession/id accession)})}
:href (z/url-for accession.routes/detail-general {:id (:accession/id accession)})}
{:label "Media"
:key :media
:href (z/url-for :accession/detail-media {:id (:accession/id accession)})}])
:href (z/url-for accession.routes/detail-media {:id (:accession/id accession)})}])

(defn page-content [& {:keys [media org page page-size accession]}]
[:div {:x-data (json/js {:selected nil})
Expand All @@ -46,8 +47,7 @@
;; probably store the antiForgeryToken in a separate element and then that
;; element can be updated with the when we get the signing urls
[:div {:x-media-uploader (json/js {:antiForgeryToken (force *anti-forgery-token*)
:signingUrl (z/url-for :media/s3)
:organizationId (:organization/id org)
:signingUrl (z/url-for media.routes/s3)
:linkResourceType "accession"
:linkResourceId (:accession/id accession)
:trigger "#upload-button"})}]
Expand Down Expand Up @@ -82,7 +82,6 @@
media (->> (media.i/get-linked db
"accession"
(:accession/id resource)
(:organization/id organization)
:offset offset
:limit limit)
(mapv #(assoc % :thumbnail-url (media.ui/thumbnail-url imgix-media-domain
Expand Down
6 changes: 3 additions & 3 deletions bases/app/src/sepal/app/routes/accession/form.clj
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
(ns sepal.app.routes.accession.form
(:require [sepal.app.html :as html]
[sepal.app.json :as json]
[sepal.app.routes.org.routes :as org.routes]
[sepal.app.routes.taxon.routes :as taxon.routes]
[sepal.app.ui.form :as form]
[zodiac.core :as z]))

(defn form [& {:keys [action errors org values]}]
(defn form [& {:keys [action errors values]}]
[:div
(form/form
{:action action
Expand All @@ -20,7 +20,7 @@
:value (:code values)
:errors (:code errors))

(let [url (z/url-for org.routes/taxa {:org-id (:organization/id org)})]
(let [url (z/url-for taxon.routes/index)]
(form/field :label "Taxon"
:name "taxon-id"
:input [:select {:x-taxon-field (json/js {:url url})
Expand Down
Loading

0 comments on commit ced4908

Please sign in to comment.