Add backward compatibility fixes for email whitelisting

This commit is contained in:
Andrey Antukh 2024-06-04 13:20:58 +02:00
parent 70c9314f7f
commit 40f39681ad

View file

@ -14,14 +14,14 @@
[clojure.core :as c] [clojure.core :as c]
[clojure.java.io :as io] [clojure.java.io :as io]
[cuerdas.core :as str] [cuerdas.core :as str]
[datoteka.fs :as fs]
[integrant.core :as ig])) [integrant.core :as ig]))
(defmethod ig/init-key ::email/whitelist (defn- read-whitelist
[_ _] [path]
(when (c/contains? cf/flags :email-whitelist) (when (and path (fs/exists? path))
(try (try
(let [path (cf/get :email-domain-whitelist) (with-open [reader (io/reader path)]
result (with-open [reader (io/reader path)]
(reduce (fn [result line] (reduce (fn [result line]
(if (str/starts-with? line "#") (if (str/starts-with? line "#")
result result
@ -29,16 +29,24 @@
#{} #{}
(line-seq reader))) (line-seq reader)))
;; backward comapatibility with previous way to set a
;; whitelist for email domains
result (into result (cf/get :registration-domain-whitelist))]
(l/inf :hint "initializing email whitelist" :domains (count result))
(not-empty result))
(catch Throwable cause (catch Throwable cause
(l/wrn :hint "unexpected exception on initializing email whitelist" (l/wrn :hint "unexpected exception on reading email whitelist"
:cause cause))))) :cause cause)))))
(defmethod ig/init-key ::email/whitelist
[_ _]
(let [whitelist (or (cf/get :registration-domain-whitelist) #{})
whitelist (if (c/contains? cf/flags :email-whitelist)
(into whitelist (read-whitelist (cf/get :email-domain-whitelist)))
whitelist)
whitelist (not-empty whitelist)]
(when whitelist
(l/inf :hint "initializing email whitelist" :domains (count whitelist)))
whitelist))
(defn contains? (defn contains?
"Check if email is in the whitelist." "Check if email is in the whitelist."
[{:keys [::email/whitelist]} email] [{:keys [::email/whitelist]} email]