Skip to content

Commit

Permalink
Merge pull request #1693 from openforis/development
Browse files Browse the repository at this point in the history
Merge develop to main
  • Loading branch information
a-luz authored Nov 21, 2023
2 parents cd7da96 + 7be22b9 commit 8b6b82a
Show file tree
Hide file tree
Showing 27 changed files with 349 additions and 145 deletions.
6 changes: 5 additions & 1 deletion config.example.edn
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@
:triangulum.server/mode "dev"
:triangulum.server/log-dir "logs"

:triangulum.server/handler collect-earth-online.routing/authenticated-routing-handler
:triangulum.server/handler triangulum.handler/authenticated-routing-handler
:triangulum.server/keystore-file "keystore.pkcs12"
:triangulum.server/keystore-type "pkcs12"
:triangulum.server/keystore-password "foobar"

;; handler (server)
:triangulum.handler/not-found-handler triangulum.views/not-found-page
:triangulum.handler/redirect-handler collect-earth-online.handlers/redirect-handler
:triangulum.handler/route-authenticator collect-earth-online.handlers/route-authenticator
:triangulum.handler/routing-tables [collect-earth-online.routing/routes]
:triangulum.handler/session-key "changeme12345678" ; must be 16 characters
:triangulum.handler/bad-tokens #{".php"}
:triangulum.handler/private-request-keys #{:base64Image :plotFileBase64 :sampleFileBase64}
Expand Down
2 changes: 1 addition & 1 deletion deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
org.clojure/data.json {:mvn/version "1.0.0"}
ring/ring {:mvn/version "1.8.2"}
sig-gis/triangulum {:git/url "https://github.com/sig-gis/triangulum"
:git/sha "5cffefc2e8a8027a178d7ff1103cb2e38e174bba"}}
:git/sha "3d41dab63e1bc8ebe046f64db44ae3df986f5bdf"}}

:aliases {:build-db {:main-opts ["-m" "triangulum.build-db"]}
:config {:main-opts ["-m" "triangulum.config"]}
Expand Down
71 changes: 40 additions & 31 deletions src/clj/collect_earth_online/db/doi.clj
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
(defn get-doi-reference
[{:keys [params]}]
(let [project-id (tc/val->int (:projectId params))
doi-path (:doi_path (first (call-sql "select_doi_by_project" project-id)))]
doi-path (:doi_path (last (call-sql "select_doi_by_project" project-id)))]
(data-response {:doiPath doi-path})))

(defn create-contributors-list
Expand Down Expand Up @@ -110,59 +110,68 @@
(update :survey_questions #(tc/jsonb->clj %))
(update :aoi_features #(tc/jsonb->clj %))
(update :created_date #(str %))
(update :closed_date (fn [x] (when x (str x))))
(update :published_date #(str %)))))

(defn upload-deposition-files!
[bucket-url project-id zip-file]
[doi-id zip-file]
(let [headers (req-headers)]
(http/put (str bucket-url "/" project-id)
{:content-type :multipart/form-data
:headers headers
:as :json
:multipart [{:name "Content/type" :content "application/octet-stream"}
{:name "file" :content (io/file zip-file)}]})))
(http/post (str base-url "/deposit/depositions/" doi-id "/files")
{:headers headers
:multipart [{:name "Content/type" :content "application/octet-stream"}
{:name "file" :content (io/file zip-file)}]})))

(defn upload-doi-files!
[doi-id project-id]
(let [project-id project-id
doi (first (call-sql "select_doi_by_id" doi-id))
bucket (-> doi :full_data tc/jsonb->clj :links :bucket)
project-data (json/write-str (get-project-data project-id))
zip-file (create-and-zip-files-for-doi project-id project-data)]
(try
(:body (upload-deposition-files! bucket project-id zip-file))
(catch Exception _
(:body (upload-deposition-files! (:doi_uid doi) zip-file))
(catch Exception e
(throw (ex-info "Failed to upload files."
{:details "Error in file upload to zenodo"}))))))

(defn create-doi!
[{:keys [params session]}]
(let [user-id (:userId session -1)
project-id (:projectId params)
project-name (:projectName params)
institution-name (:name (first (call-sql "select_institution_by_id" (-> params :institution) user-id)))
description (:description params)
creator (first (call-sql "get_user_by_id" user-id))
contributors (call-sql "select_assigned_users_by_project" project-id)]
(try
(->
(create-zenodo-deposition! institution-name project-name creator contributors description)
:body
(insert-doi! project-id user-id)
(upload-doi-files! project-id))
(data-response {:message "DOI created successfully"})
(catch Exception _
(data-response {:message "Failed to create DOI."}
{:status 500})))))
(let [user-id (:userId session -1)
project-id (:projectId params)
project-name (:projectName params)
institution-name (:name (first (call-sql "select_institution_by_id" (-> params :institution) user-id)))
description (:description params)
creator (first (call-sql "get_user_by_id" user-id))
contributors (call-sql "select_assigned_users_by_project" project-id)
project-published? (:availability (first (call-sql "select_project_by_id" project-id)))
doi-published? (:submitted (last (call-sql "select_doi_by_project" project-id)))]
(cond
(and (not= "published" project-published?)
(not= "closed" project-published?)) (data-response {:message "In order to create a DOI, the project must be published"}
{:status 500})
doi-published? (data-response {:message "A DOI for this project has already been published."}
{:status 500})
:else
(try
(->
(create-zenodo-deposition! institution-name project-name creator contributors description)
:body
(insert-doi! project-id user-id)
(upload-doi-files! project-id))
(data-response {:message "DOI created successfully"})
(catch Exception _
(data-response {:message "Failed to create DOI."}
{:status 500}))))))

(defn publish-doi!
"request zenodo to publish the DOI on DataCite"
[{:keys [params]}]
(let [project-id (:projectId params)
doi-id (:doi_uid (first (call-sql "select_doi_by_project" project-id)))]
doi-id (:doi_uid (last (call-sql "select_doi_by_project" project-id)))
req (http/post (str base-url "/deposit/depositions/" doi-id "/actions/publish")
{:as :json
:headers (req-headers)})]
(try
(http/post (str base-url "/deposition/depositions/" doi-id "/actions/publish")
{:headers (req-headers)})
(call-sql "update_doi" doi-id (tc/clj->jsonb (:body req)))
(data-response {})
(catch Exception _
(data-response {:message "Failed to publish DOI"}
Expand Down
26 changes: 14 additions & 12 deletions src/clj/collect_earth_online/db/plots.clj
Original file line number Diff line number Diff line change
Expand Up @@ -256,22 +256,23 @@
;;;

(defn add-user-samples [{:keys [params session]}]
(let [project-id (tc/val->int (:projectId params))
plot-id (tc/val->int (:plotId params))
session-user-id (:userId session -1)
current-user-id (tc/val->int (:currentUserId params -1))
review-mode? (and (tc/val->bool (:inReviewMode params))
(let [project-id (tc/val->int (:projectId params))
plot-id (tc/val->int (:plotId params))
session-user-id (:userId session -1)
current-user-id (tc/val->int (:currentUserId params -1))
review-mode? (and (tc/val->bool (:inReviewMode params))
(pos? current-user-id)
(is-proj-admin? session-user-id project-id nil))
confidence (tc/val->int (:confidence params))
collection-start (tc/val->long (:collectionStart params))
user-samples (:userSamples params)
user-images (:userImages params)
new-plot-samples (:newPlotSamples params)
user-id (if review-mode? current-user-id session-user-id)
confidence (tc/val->int (:confidence params))
confidence-comment (:confidenceComment params)
collection-start (tc/val->long (:collectionStart params))
user-samples (:userSamples params)
user-images (:userImages params)
new-plot-samples (:newPlotSamples params)
user-id (if review-mode? current-user-id session-user-id)
;; Samples created in the UI have IDs starting with 1. When the new sample is created
;; in Postgres, it gets different ID. The user sample ID needs to be updated to match.
id-translation (when new-plot-samples
id-translation (when new-plot-samples
(call-sql "delete_user_plot_by_plot" plot-id user-id)
(call-sql "delete_samples_by_plot" plot-id)
(reduce (fn [acc {:keys [id visibleId sampleGeom]}]
Expand All @@ -288,6 +289,7 @@
plot-id
user-id
(when (pos? confidence) confidence)
(when confidence-comment confidence-comment)
(when-not review-mode? (Timestamp. collection-start))
(tc/clj->jsonb (set/rename-keys user-samples id-translation))
(tc/clj->jsonb (set/rename-keys user-images id-translation)))
Expand Down
2 changes: 1 addition & 1 deletion src/clj/collect_earth_online/generators/external_file.clj
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@
(create-shape-files folder-name "sample" project-id)
(create-data-file folder-name project-data)
(sh-wrapper tmp-dir {}
(str "7z a " folder-name "/files" ".zip " folder-name "/*"))
(str "7z a " folder-name "files" ".zip " folder-name "*"))
(str folder-name "files.zip")))

(defn zip-shape-files
Expand Down
33 changes: 33 additions & 0 deletions src/clj/collect_earth_online/handlers.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
(ns collect-earth-online.handlers
(:require [collect-earth-online.db.institutions :refer [is-inst-admin?]]
[collect-earth-online.db.projects :refer [can-collect? is-proj-admin?]]
[ring.util.codec :refer [url-encode]]
[ring.util.response :refer [redirect]]
[triangulum.response :refer [no-cross-traffic?]]
[triangulum.type-conversion :refer [val->int]]))

(defn route-authenticator [{:keys [session params headers] :as _request} auth-type]
(let [user-id (:userId session -1)
institution-id (val->int (:institutionId params))
project-id (val->int (:projectId params))
token-key (:tokenKey params)]
(condp = auth-type
:user (pos? user-id)
:super (= 1 user-id)
:collect (can-collect? user-id project-id token-key)
:token (can-collect? -99 project-id token-key)
:admin (cond
(pos? project-id) (is-proj-admin? user-id project-id token-key)
(pos? institution-id) (is-inst-admin? user-id institution-id))
:no-cross (no-cross-traffic? headers)
true)))

(defn redirect-handler [{:keys [session query-string uri] :as _request}]
(let [full-url (url-encode (str uri (when query-string (str "?" query-string))))]
(if (:userId session)
(redirect (str "/home?flash_message=You do not have permission to access "
full-url))
(redirect (str "/login?returnurl="
full-url
"&flash_message=You must login to see "
full-url)))))
48 changes: 3 additions & 45 deletions src/clj/collect_earth_online/routing.clj
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
(ns collect-earth-online.routing
(:require [triangulum.views :refer [render-page not-found-page]]
[ring.util.response :refer [redirect]]
[ring.util.codec :refer [url-encode]]
[triangulum.type-conversion :as tc]
[triangulum.response :refer [forbidden-response no-cross-traffic?]]
[collect-earth-online.db.doi :as doi]
(:require [collect-earth-online.db.doi :as doi]
[collect-earth-online.db.geodash :as geodash]
[collect-earth-online.db.imagery :as imagery]
[collect-earth-online.db.institutions :as institutions]
[collect-earth-online.db.plots :as plots]
[collect-earth-online.db.projects :as projects]
[collect-earth-online.db.users :as users]
[collect-earth-online.proxy :as proxy]))
[collect-earth-online.proxy :as proxy]
[triangulum.views :refer [render-page]]))

(def routes
{;; Page Routes
Expand Down Expand Up @@ -194,41 +190,3 @@
[:get "/get-nicfi-tiles"] {:handler proxy/get-nicfi-tiles
:auth-type :no-cross
:auth-action :block}})

(defn- redirect-auth [user-id]
(fn [request]
(let [{:keys [query-string uri]} request
full-url (url-encode (str uri (when query-string (str "?" query-string))))]
(if (pos? user-id)
(redirect (str "/home?flash_message=You do not have permission to access "
full-url))
(redirect (str "/login?returnurl="
full-url
"&flash_message=You must login to see "
full-url))))))

(defn authenticated-routing-handler [{:keys [uri request-method params headers session] :as request}]
(let [{:keys [auth-type auth-action handler] :as route} (get routes [request-method uri])
user-id (:userId session -1)
institution-id (tc/val->int (:institutionId params))
project-id (tc/val->int (:projectId params))
next-handler (if route
(if (condp = auth-type
:user (pos? user-id)
:super (= 1 user-id)
:collect (projects/can-collect? user-id project-id (:tokenKey params))
:token (projects/can-collect? -99 project-id (:tokenKey params))
:admin (cond
(pos? project-id)
(projects/is-proj-admin? user-id project-id (:tokenKey params))

(pos? institution-id)
(institutions/is-inst-admin? user-id institution-id))
:no-cross (no-cross-traffic? headers)
true)
handler
(if (= :redirect auth-action)
(redirect-auth user-id)
forbidden-response))
not-found-page)]
(next-handler request)))
2 changes: 1 addition & 1 deletion src/clj/collect_earth_online/workers.clj
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
file (reverse (file-seq d))]
(io/delete-file file))))

(defn- start-clean-up-service! []
(defn start-clean-up-service! []
(log-str "Starting temp file removal service.")
(future
(while true
Expand Down
5 changes: 4 additions & 1 deletion src/css/custom.css
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,7 @@ a:hover {
}
#lPanel ul {
padding-left: 0;
margin-left: 5px;
list-style: none;
margin-bottom: 0;
}
Expand Down Expand Up @@ -859,5 +860,7 @@ input:checked + .switch-slider:before {
}

body {
padding-top:60px;
margin-left: 10px;
margin-top: 60px;
overflow-x: hidden;
}
Loading

0 comments on commit 8b6b82a

Please sign in to comment.