From 90022041e64cf391b4a984294c5b25600180d26c Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Wed, 28 Feb 2024 14:42:55 +0100 Subject: [PATCH] :sparkles: Improve error handling on ldap frontend code --- backend/src/app/rpc/commands/ldap.clj | 3 +- frontend/src/app/main/data/users.cljs | 27 ++++++++++ frontend/src/app/main/ui/auth/login.cljs | 58 ++++++++++----------- frontend/src/app/main/ui/auth/register.cljs | 4 +- 4 files changed, 57 insertions(+), 35 deletions(-) diff --git a/backend/src/app/rpc/commands/ldap.clj b/backend/src/app/rpc/commands/ldap.clj index 780f0e100..dff521500 100644 --- a/backend/src/app/rpc/commands/ldap.clj +++ b/backend/src/app/rpc/commands/ldap.clj @@ -12,7 +12,6 @@ [app.db :as db] [app.http.session :as session] [app.loggers.audit :as-alias audit] - [app.main :as-alias main] [app.rpc :as-alias rpc] [app.rpc.commands.auth :as auth] [app.rpc.commands.profile :as profile] @@ -73,7 +72,7 @@ (rph/with-meta {::audit/props (:props profile) ::audit/profile-id (:id profile)}))) - (-> profile + (-> (profile/strip-private-attrs profile) (rph/with-transform (session/create-fn cfg (:id profile))) (rph/with-meta {::audit/props (:props profile) ::audit/profile-id (:id profile)})))))) diff --git a/frontend/src/app/main/data/users.cljs b/frontend/src/app/main/data/users.cljs index 8a540317f..ff8865c50 100644 --- a/frontend/src/app/main/data/users.cljs +++ b/frontend/src/app/main/data/users.cljs @@ -231,8 +231,35 @@ (rx/observe-on :async))))) (rx/catch on-error)))))) +(def ^:private schema:login-with-ldap + (sm/define + [:map + [:email ::sm/email] + [:password :string]])) + +(defn login-with-ldap + [params] + + (dm/assert! + "expected valid params" + (sm/check! schema:login-with-ldap params)) + + (ptk/reify ::login-with-ldap + ptk/WatchEvent + (watch [_ _ _] + (let [{:keys [on-error on-success] + :or {on-error rx/throw + on-success identity}} (meta params)] + (->> (rp/cmd! :login-with-ldap params) + (rx/tap on-success) + (rx/map (fn [profile] + (-> profile + (with-meta {::ev/source "login-with-ldap"}) + (logged-in)))) + (rx/catch on-error)))))) (defn login-from-token + "Used mainly as flow continuation after token validation." [{:keys [profile] :as tdata}] (ptk/reify ::login-from-token ptk/WatchEvent diff --git a/frontend/src/app/main/ui/auth/login.cljs b/frontend/src/app/main/ui/auth/login.cljs index 95a620a68..ac3f2e470 100644 --- a/frontend/src/app/main/ui/auth/login.cljs +++ b/frontend/src/app/main/ui/auth/login.cljs @@ -42,6 +42,10 @@ {:type :warning :content (tr "auth.demo-warning")}]) +(defn create-demo-profile + [] + (st/emit! (du/create-demo-profile))) + (defn- login-with-oidc [event provider params] (dom/prevent-default event) @@ -60,28 +64,6 @@ :else (st/emit! (msg/error (tr "errors.generic")))))))) -(defn- login-with-ldap - [event params] - (dom/prevent-default event) - (dom/stop-propagation event) - (let [{:keys [on-error]} (meta params)] - (->> (rp/cmd! :login-with-ldap params) - (rx/subs! (fn [profile] - (if-let [token (:invitation-token profile)] - (st/emit! (rt/nav :auth-verify-token {} {:token token})) - (st/emit! (du/login-from-token {:profile profile})))) - (fn [{:keys [type code] :as error}] - (cond - (and (= type :restriction) - (= code :ldap-not-initialized)) - (st/emit! (msg/error (tr "errors.ldap-disabled"))) - - (fn? on-error) - (on-error error) - - :else - (st/emit! (msg/error (tr "errors.generic"))))))))) - (s/def ::email ::us/email) (s/def ::password ::us/not-empty-string) (s/def ::invitation-token ::us/not-empty-string) @@ -114,6 +96,11 @@ (= :profile-blocked (:code cause))) (reset! error (tr "errors.profile-blocked")) + (and (= :restriction (:type cause)) + (= :ldap-not-initialized (:code cause))) + (st/emit! (msg/error (tr "errors.ldap-disabled"))) + + (and (= :restriction (:type cause)) (= :admin-only-profile (:code cause))) (reset! error (tr "errors.profile-blocked")) @@ -130,9 +117,10 @@ (reset! error (tr "errors.generic"))))) on-success-default - (fn [data] - (when-let [token (:invitation-token data)] - (st/emit! (rt/nav :auth-verify-token {} {:token token})))) + (mf/use-fn + (fn [data] + (when-let [token (:invitation-token data)] + (st/emit! (rt/nav :auth-verify-token {} {:token token}))))) on-success (fn [data] @@ -153,11 +141,15 @@ (mf/use-callback (mf/deps form) (fn [event] + (dom/prevent-default event) + (dom/stop-propagation event) + (reset! error nil) - (let [params (:clean-data @form)] - (login-with-ldap event (with-meta params - {:on-error on-error - :on-success on-success}))))) + (let [params (:clean-data @form) + params (with-meta params + {:on-error on-error + :on-success on-success})] + (st/emit! (du/login-with-ldap params))))) on-recovery-request (mf/use-fn @@ -308,5 +300,11 @@ [:& lk/link {:action go-register :class (stl/css :register-link) :data-test "register-submit"} - (tr "auth.register-submit")]])]])) + (tr "auth.register-submit")]])] + (when (contains? cf/flags :demo-users) + [:div {:class (stl/css :link-entry :demo-account)} + [:span (tr "auth.create-demo-profile") " "] + [:& lk/link {:action create-demo-profile + :data-test "demo-account-link"} + (tr "auth.create-demo-account")]])])) diff --git a/frontend/src/app/main/ui/auth/register.cljs b/frontend/src/app/main/ui/auth/register.cljs index 90fa8cd30..61066fb81 100644 --- a/frontend/src/app/main/ui/auth/register.cljs +++ b/frontend/src/app/main/ui/auth/register.cljs @@ -154,7 +154,7 @@ [:* [:hr {:class (stl/css :separator)}] [:div {:class (stl/css :demo-account)} - [:& lk/link {:action #(st/emit! (du/create-demo-profile)) + [:& lk/link {:action login/create-demo-profile :class (stl/css :demo-account-link)} (tr "auth.create-demo-account")]]])]]) @@ -265,5 +265,3 @@ [:div {:class (stl/css :notification-text)} (tr "auth.verification-email-sent")] [:div {:class (stl/css :notification-text-email)} (:email params "")] [:div {:class (stl/css :notification-text)} (tr "auth.check-your-email")]]) - -