mirror of
https://github.com/penpot/penpot.git
synced 2026-11-18 16:12:24 +00:00
* 🐛 Build the SSO redirect target on the server The organization SSO flow took the destination URL from the client and redirected to it after the identity provider round-trip, so any authenticated profile could point that redirect at another domain: the server never checked where the URL came from. The RPC now takes the route token of the client (?screen=...¶ms) and the server builds the destination out of it: the public URI plus the query string, parsed and rendered back. The destination can no longer leave the public URI, and the callback checks it again before redirecting, which covers the state tokens minted before the change. The state token also carries the profile that started the flow, so only that session is granted the organization SSO entry: a state prepared by someone else cannot mark the session of the one that completes it. Whenever the RPC receives an organization id (it always comes from the client), it also checks that the profile is a member of the organization or its owner, instead of letting any profile start a flow for an organization it does not belong to. Tests cover the destination builder, a callback that ignores a destination outside the public URI, a state that does not grant the SSO entry to another session, and the organization access check. AI-assisted-by: minimax-m3.1-flash * 🐛 Check the SSO redirect target against the public URI The callback compared the destination with a string prefix, so the two bypasses the advisory documents (a userinfo prefix and a look-alike host) were closed only by the trailing slash of the public URI. The check now compares the parsed origin (scheme, host case-insensitive, port with the default made explicit) and requires the path to be under the base path of the public URI, so neither bypass passes. The client sent `location.search`, the history token, whose leading `?` is URL syntax and no part of a query string. It sends the query string proper now (`screen=…¶ms`) through `rt/get-current-query-string`, and `rt/token->query-string` is the single bridge between the two shapes: every other consumer concatenates the token over the base path. The server puts the separator back when it joins the destination, and trims one that still arrives, so a browser holding the previous bundle keeps its destination while the new one rolls out. Building the destination can no longer break the flow: it catches its own errors and lands on the public URI, which matters in the deployments where the browser origin differs from the configured one. A value that is not a route is ignored with a warning, decided by the keys it parses into (a URL becomes a single key holding the whole URL), and a route with no `screen`, like the root params of the application (`template=…`), stays a usable destination. The organization membership lookup is now shared by the check that gates the SSO flow and the one that raises, so the two cannot drift apart, and the docstring of the RPC lists `:no-organization-access` next to `:no-team-access`. `rt/nav-raw` returns its `:href`/`:uri` through `@event`, as its sibling `rt/navigate` already did. Nothing in the app listens to `::rt/nav-raw`, so the callers behave the same; the SSO test now reads the destination from the event instead of intercepting the helper. Tests cover the two bypasses and the base-path rule, the RPC with an unusable destination, the value that is not a route, the request that carries both ids, an organization that does not exist, the query string the frontend sends, and the two router helpers. AI-assisted-by: minimax-m3.1-flash * ♻️ Deduplicate SSO destination URL building The backend had an unreachable blank check and a one-line wrapper around the URI helper in build-dest-url. On the client, the SSO retry and error flows each assembled the destination from public-uri and the route params. Flatten build-dest-url with a cond and call u/query-string->map directly. Add rt/token->url as the single client helper for absolute route URLs, with tests for both token shapes and for a blank token. Update the routing memory with the new helper contract. AI-assisted-by: hy4-flash-free
617 lines
30 KiB
Clojure
617 lines
30 KiB
Clojure
(ns app.main.data.nitrate
|
|
(:require
|
|
[app.common.data :as d]
|
|
[app.common.data.macros :as dm]
|
|
[app.common.types.organization :as cto]
|
|
[app.common.uri :as u]
|
|
[app.common.uuid :as uuid]
|
|
[app.config :as cf]
|
|
[app.main.data.common :as dcm]
|
|
[app.main.data.event :as ev]
|
|
[app.main.data.modal :as modal]
|
|
[app.main.data.nitrate-audit :as nitrate-audit]
|
|
[app.main.data.notifications :as ntf]
|
|
[app.main.data.team :as dt]
|
|
[app.main.repo :as rp]
|
|
[app.main.router :as rt]
|
|
[app.main.store :as st]
|
|
[app.util.dom :as dom]
|
|
[app.util.i18n :refer [tr]]
|
|
[app.util.session-state :as ss]
|
|
[app.util.storage :as storage]
|
|
[beicon.v2.core :as rx]
|
|
[potok.v2.core :as ptk]))
|
|
|
|
(def ^:private nitrate-entry-pending-popup-key ::nitrate-entry-pending-popup)
|
|
(defn account-age-days
|
|
[profile]
|
|
(nitrate-audit/age-days (:created-at profile)))
|
|
|
|
(defn activate-nitrate-entry-popup!
|
|
[]
|
|
(binding [storage/*sync* true]
|
|
(swap! storage/storage assoc
|
|
nitrate-entry-pending-popup-key true)))
|
|
|
|
(defn nitrate-entry-popup-pending?
|
|
[]
|
|
(true? (get storage/storage nitrate-entry-pending-popup-key)))
|
|
|
|
(defn consume-nitrate-entry-popup!
|
|
[]
|
|
(binding [storage/*sync* true]
|
|
(swap! storage/storage dissoc
|
|
nitrate-entry-pending-popup-key)))
|
|
|
|
(def ^:private offline-connectivity
|
|
{:licenses false})
|
|
|
|
(defn- air-gapped?
|
|
[]
|
|
(contains? cf/flags :air-gapped-conf))
|
|
|
|
(defn show-nitrate-popup
|
|
([popup-type] (show-nitrate-popup popup-type {}))
|
|
([popup-type extra-props]
|
|
(ptk/reify ::show-nitrate-popup
|
|
ptk/WatchEvent
|
|
(watch [_ _ _]
|
|
(if (air-gapped?)
|
|
(rx/of (modal/show popup-type (merge offline-connectivity extra-props)))
|
|
(->> (rp/cmd! ::get-nitrate-connectivity {})
|
|
(rx/map (fn [connectivity]
|
|
(modal/show popup-type
|
|
(merge (or connectivity {}) extra-props))))))))))
|
|
|
|
(defn build-admin-console-url
|
|
([path]
|
|
(build-admin-console-url cf/public-uri path nil))
|
|
([path query-params]
|
|
(build-admin-console-url cf/public-uri path query-params))
|
|
([public-uri path query-params]
|
|
(dm/str
|
|
(cond-> (u/join public-uri "admin-console/" path)
|
|
(seq query-params) (assoc :query (u/map->query-string query-params))))))
|
|
|
|
(defn build-admin-console-href
|
|
([]
|
|
(build-admin-console-url ""))
|
|
([{:keys [organization-id organization-slug]}]
|
|
(if (and organization-id organization-slug)
|
|
(let [path (dm/str "organization/"
|
|
(u/percent-encode organization-slug)
|
|
"/"
|
|
(u/percent-encode (str organization-id))
|
|
"/people/")]
|
|
(build-admin-console-url path))
|
|
(build-admin-console-url ""))))
|
|
|
|
(defn go-to-nitrate-ac
|
|
([]
|
|
(st/emit! (rt/nav-raw :href (build-admin-console-href))))
|
|
([options]
|
|
(st/emit! (rt/nav-raw :href (build-admin-console-href options)))))
|
|
|
|
(defn go-to-nitrate-ac-create-organization
|
|
[event-origin]
|
|
(let [href (build-admin-console-url "" {:action "create-organization"
|
|
:origin event-origin})]
|
|
(st/emit! (rt/nav-raw :href href))))
|
|
|
|
(defn can-send-invitations?
|
|
[{:keys [organization profile-id team-permissions]}]
|
|
(cto/can-send-invitations?
|
|
{:organization organization
|
|
:profile-id profile-id
|
|
:team-permissions team-permissions}))
|
|
|
|
(def go-to-subscription-url (dm/str cf/public-uri "?screen=settings-subscription"))
|
|
|
|
(def go-to-ac-url (build-admin-console-url ""))
|
|
|
|
(defn go-to-nitrate-billing
|
|
[]
|
|
(let [href (build-admin-console-url "licenses/billing"
|
|
{:callback go-to-subscription-url})]
|
|
(st/emit! (rt/nav-raw :href href))))
|
|
|
|
(def nitrate-checkout-error-token "nitrate-checkout-error")
|
|
(def nitrate-checkout-finish-error-token "nitrate-checkout-finish-error")
|
|
(def nitrate-checkout-cancelled-token "nitrate-checkout-cancelled")
|
|
|
|
(defn build-nitrate-callback-urls
|
|
"Build checkout callback URLs from base URLs by appending a `subscription`
|
|
query param identifying the outcome."
|
|
[base-url base-error-url]
|
|
(let [build (fn [url token]
|
|
(dom/append-query-param url :subscription token))]
|
|
{:success-callback (build base-url "subscribed-to-penpot-nitrate")
|
|
:error-callback (build base-error-url nitrate-checkout-error-token)
|
|
:finish-error-callback (build base-error-url nitrate-checkout-finish-error-token)
|
|
:cancel-callback (build base-url nitrate-checkout-cancelled-token)}))
|
|
|
|
(defn go-to-buy-nitrate-license
|
|
[subscription base-url base-error-url event-origin subscription-mode subscription-start-origin]
|
|
(let [{:keys [success-callback error-callback finish-error-callback cancel-callback]}
|
|
(build-nitrate-callback-urls base-url base-error-url)
|
|
params {:subscription subscription
|
|
:callback success-callback
|
|
:error_callback error-callback
|
|
:finish_error_callback finish-error-callback
|
|
:cancel_callback cancel-callback}
|
|
href (build-admin-console-url "licenses/start" params)
|
|
event (ev/event {::ev/name "start-nitrate-checkout"
|
|
::ev/origin event-origin
|
|
:product "nitrate:enterprise"
|
|
:billing-period subscription
|
|
:subscription-mode subscription-mode
|
|
:subscription-start-origin subscription-start-origin})]
|
|
(->> st/stream
|
|
(rx/filter (ptk/type? ::ev/chunk-persisted))
|
|
(rx/take 1)
|
|
(rx/timeout 2000 (rx/of :timeout))
|
|
(rx/subs! (fn [_]
|
|
(st/emit! (rt/nav-raw :href href)))))
|
|
(st/emit! event (ptk/data-event ::ev/force-persist {}))))
|
|
|
|
(defn fetch-connectivity
|
|
[]
|
|
(rp/cmd! ::get-nitrate-connectivity {}))
|
|
|
|
(defn fetch-subscription-warning
|
|
[]
|
|
(rp/cmd! ::get-subscription-warning {}))
|
|
|
|
(defn is-valid-license?
|
|
[profile]
|
|
(and (contains? cf/flags :admin-console)
|
|
;; Possible values: "active" "canceled" "incomplete" "incomplete_expired" "past_due" "paused" "trialing" "unpaid"
|
|
(contains? #{"active" "past_due" "trialing"}
|
|
(dm/get-in profile [:subscription :status]))))
|
|
|
|
(defn organization-teams
|
|
"Teams belonging to `organization-id`, out of the full team map."
|
|
[teams organization-id]
|
|
(->> teams
|
|
vals
|
|
(filter #(= (dm/get-in % [:organization :id]) organization-id))))
|
|
|
|
(defn organization-leave-info
|
|
"The default team id and the not-owned teams of an organization.
|
|
Owned teams come from `::get-leave-organization-summary`."
|
|
[org-teams]
|
|
{:default-team-id (->> org-teams (filter :is-default) first :id)
|
|
:not-owned-teams (->> org-teams
|
|
(remove :is-default)
|
|
(remove #(dm/get-in % [:permissions :is-owner])))})
|
|
|
|
(defn- team-leave-message
|
|
[code]
|
|
(case code
|
|
:only-owner-can-delete-team (tr "errors.team-leave.only-owner-can-delete")
|
|
:no-enough-members-for-leave (tr "errors.team-leave.insufficient-members")
|
|
:member-does-not-exist (tr "errors.team-leave.member-does-not-exists")
|
|
:owner-cant-leave-team (tr "errors.team-leave.owner-cant-leave")
|
|
nil))
|
|
|
|
(defn team-leave-on-error
|
|
[error]
|
|
(let [code (-> error ex-data :code)]
|
|
(if-let [message (team-leave-message code)]
|
|
(rx/of (ntf/error message))
|
|
(rx/throw error))))
|
|
|
|
(defn org-leave-on-error
|
|
[error]
|
|
(let [code (-> error ex-data :code)
|
|
message (or (team-leave-message code)
|
|
(case code
|
|
:not-valid-teams (tr "errors.organization-leave.no-valid-teams")
|
|
:organization-owner-cannot-leave (tr "errors.organization-leave.organization-owner-cannot-leave")
|
|
nil))]
|
|
(if (some? message)
|
|
(rx/of (dt/fetch-teams)
|
|
(modal/hide)
|
|
(ntf/error message))
|
|
(rx/throw error))))
|
|
|
|
(defn leave-organization
|
|
[{:keys [id
|
|
name
|
|
default-team-id
|
|
teams-to-delete
|
|
teams-to-leave
|
|
member-added-at
|
|
organization-member-count-before
|
|
on-error]}]
|
|
|
|
(ptk/reify ::leave-organization
|
|
ptk/WatchEvent
|
|
(watch [_ state _]
|
|
(let [profile-id (dm/get-in state [:profile :id])
|
|
profile-team-id (dm/get-in state [:profile :default-team-id])
|
|
subscription-status
|
|
(if (= "trialing" (dm/get-in state [:profile :subscription :status]))
|
|
"trial"
|
|
"active")
|
|
audit-event
|
|
(nitrate-audit/delete-organization-member-event
|
|
{:organization-id id
|
|
:user-id profile-id
|
|
:user-who-delete-member profile-id
|
|
:deleted-by-role "organization-member"
|
|
:member-added-at member-added-at
|
|
:organization-member-count-before organization-member-count-before
|
|
:subscription-status subscription-status})]
|
|
(rx/concat
|
|
(rx/of audit-event)
|
|
(->> (rp/cmd! ::leave-organization {:id id
|
|
:name name
|
|
:default-team-id default-team-id
|
|
:teams-to-delete teams-to-delete
|
|
:teams-to-leave teams-to-leave})
|
|
(rx/mapcat
|
|
(fn [_]
|
|
(rx/of
|
|
(dt/fetch-teams)
|
|
(dcm/go-to-dashboard-recent :team-id profile-team-id)
|
|
(modal/hide)
|
|
(ntf/show {:content (tr "dashboard.leave-organization.toast" name)
|
|
:type :toast
|
|
:level :success}))))
|
|
(rx/catch on-error)))))))
|
|
|
|
(defn leave-organization-fn
|
|
"Builds the accept callback used by `show-leave-organization-modal`:
|
|
folds transferred teams into `:teams-to-leave` and emits
|
|
`leave-organization`."
|
|
[{:keys [organization default-team-id not-owned-teams on-error]}]
|
|
(fn [{:keys [teams-to-transfer teams-to-delete member-added-at organization-member-count-before]}]
|
|
(let [teams-to-leave
|
|
(cond->> not-owned-teams
|
|
:always (map #(select-keys % [:id]))
|
|
(seq teams-to-transfer) (concat teams-to-transfer))]
|
|
(st/emit! (leave-organization {:id (:id organization)
|
|
:name (:name organization)
|
|
:default-team-id default-team-id
|
|
:teams-to-delete teams-to-delete
|
|
:teams-to-leave teams-to-leave
|
|
:member-added-at member-added-at
|
|
:organization-member-count-before organization-member-count-before
|
|
:on-error on-error})))))
|
|
|
|
(defn show-leave-organization-modal
|
|
[{:keys [organization profile default-team-id leave-fn on-error]}]
|
|
(ptk/reify ::show-leave-organization-modal
|
|
ptk/WatchEvent
|
|
(watch [_ _ _]
|
|
(->> (rp/cmd! ::get-leave-organization-summary {:id (:id organization)
|
|
:default-team-id default-team-id})
|
|
(rx/mapcat
|
|
(fn [summary]
|
|
(let [num-teams-to-delete (:teams-to-delete summary)
|
|
num-teams-to-transfer (:teams-to-transfer summary)
|
|
num-teams-to-exit (:teams-to-exit summary)
|
|
num-teams-to-detach (:teams-to-detach summary)
|
|
leave-fn
|
|
(fn [params]
|
|
(leave-fn
|
|
(assoc params
|
|
:teams-to-delete (:team-ids-to-delete summary)
|
|
:member-added-at (:member-added-at summary)
|
|
:organization-member-count-before
|
|
(:organization-member-count-before summary))))]
|
|
(cond
|
|
(pos? num-teams-to-transfer)
|
|
(rx/of
|
|
(modal/show
|
|
{:type :leave-and-reassign-organization
|
|
:profile profile
|
|
:teams-to-transfer (:transferable-teams summary)
|
|
:num-teams-to-delete num-teams-to-delete
|
|
:accept leave-fn}))
|
|
|
|
(or (pos? num-teams-to-delete)
|
|
(pos? num-teams-to-exit)
|
|
(pos? num-teams-to-detach))
|
|
(rx/of (modal/show
|
|
{:type :confirm
|
|
:title (tr "modals.before-leave-organization.title" (:name organization))
|
|
:message (tr "modals.before-leave-organization.message")
|
|
:accept-label (tr "modals.leave-organization-confirm.accept")
|
|
:on-accept leave-fn
|
|
:error-msg (tr "modals.before-leave-organization.warning")}))
|
|
|
|
:else
|
|
(rx/of (modal/show
|
|
{:type :confirm
|
|
:title (tr "modals.leave-organization-confirm.title" (:name organization))
|
|
:message (tr "modals.leave-organization-confirm.message")
|
|
:accept-label (tr "modals.leave-organization-confirm.accept")
|
|
:on-accept leave-fn}))))))
|
|
(rx/catch on-error)))))
|
|
|
|
|
|
(defn remove-team-from-organization
|
|
[{:keys [team-id organization-id organization-name] :as params}]
|
|
(ptk/reify ::remove-team-from-organization
|
|
ptk/WatchEvent
|
|
(watch [_ _ _]
|
|
(->> (rp/cmd! ::remove-team-from-organization {:team-id team-id :organization-id organization-id :organization-name organization-name})
|
|
(rx/mapcat
|
|
(fn [_]
|
|
(rx/of (dt/fetch-teams)
|
|
(modal/hide))))
|
|
(rx/catch
|
|
(fn [cause]
|
|
(let [code (-> cause ex-data :code)]
|
|
(if (= code :not-allowed)
|
|
(rx/of (modal/show :no-permission-modal {:type :no-organizations-change}))
|
|
(rx/throw cause)))))))))
|
|
|
|
(defn show-remove-team-from-organization-modal
|
|
"Fetches fresh team/organization data, then shows the remove-from-organization confirmation
|
|
modal or the no-permission modal if the move-team permission blocks it."
|
|
[{:keys [team-id]}]
|
|
(ptk/reify ::show-remove-team-from-organization-modal
|
|
ptk/WatchEvent
|
|
(watch [_ state _]
|
|
(let [profile-id (dm/get-in state [:profile :id])]
|
|
(dt/with-refreshed-team team-id
|
|
(fn [team]
|
|
(let [source-organization (:organization team)
|
|
can-move? (cto/allowed?
|
|
:move-team
|
|
{:organization-perms source-organization
|
|
:profile-id profile-id
|
|
:team-perms (:permissions team)
|
|
:target-organization-same-owner? false})]
|
|
(rx/of (if can-move?
|
|
(modal/show
|
|
{:type :confirm
|
|
:title (tr "modals.remove-team-organization.title")
|
|
:message (tr "modals.remove-team-organization.text" (:name team) (:name source-organization))
|
|
:hint (tr "modals.remove-team-organization.info")
|
|
:hint-level :default
|
|
:accept-label (tr "modals.remove-team-organization.accept")
|
|
:on-accept #(st/emit! (remove-team-from-organization {:team-id team-id
|
|
:organization-id (:id source-organization)
|
|
:organization-name (:name source-organization)}))
|
|
:accept-style :danger})
|
|
(modal/show :no-permission-modal {:type :no-organizations-change}))))))))))
|
|
|
|
|
|
(defn add-team-to-organization
|
|
"Adds a team to an organization after checking whether the target
|
|
organization requires a fresh Nitrate SSO session. When SSO is required,
|
|
stores the pending action and redirects to the provider so the dashboard
|
|
can resume the operation after the callback."
|
|
[{:keys [team-id organization-id skip-audit?]}]
|
|
(ptk/reify ::add-team-to-organization
|
|
ptk/WatchEvent
|
|
(watch [_ state _]
|
|
(let [team (dm/get-in state [:teams team-id])
|
|
organization-team-count-before
|
|
(nitrate-audit/organization-team-count
|
|
(vals (:teams state))
|
|
organization-id)
|
|
team-previous-organization-status
|
|
(if (or (:organization-id team)
|
|
(get-in team [:organization :id]))
|
|
"other-organization"
|
|
"no-organization")
|
|
subscription-status
|
|
(or (dm/get-in state [:profile :subscription :status])
|
|
"active")
|
|
audit-event
|
|
(nitrate-audit/add-team-to-organization-event
|
|
{:team team
|
|
:organization-id organization-id
|
|
:organization-team-count-before organization-team-count-before
|
|
:team-previous-organization-status team-previous-organization-status
|
|
:add-method "move-existing-team-to-organization"
|
|
:subscription-status subscription-status})
|
|
pending-id (str (uuid/next))
|
|
;; The callback travels as a query string; the token is kept in
|
|
;; its own shape because `append-query-param` works on URLs.
|
|
callback-query-string (-> (rt/get-current-token)
|
|
(dom/append-query-param :pending-action-id pending-id)
|
|
rt/token->query-string)
|
|
;; The permissions may have changed in Nitrate since the modal
|
|
;; was opened: refresh them and explain why the action failed.
|
|
on-error
|
|
(fn [cause]
|
|
(if (= :not-allowed (-> cause ex-data :code))
|
|
(rx/of (dt/fetch-teams)
|
|
(modal/show :no-permission-modal
|
|
{:type (if (= team-previous-organization-status "other-organization")
|
|
:no-organizations-change
|
|
:no-organizations-create)}))
|
|
(rx/throw cause)))]
|
|
(rx/concat
|
|
(when-not skip-audit?
|
|
(rx/of audit-event))
|
|
(->> (rp/cmd! :check-nitrate-sso {:organization-id organization-id
|
|
:query-string callback-query-string})
|
|
(rx/mapcat
|
|
(fn [{:keys [authorized redirect-uri]}]
|
|
(if authorized
|
|
(->> (rp/cmd! ::add-team-to-organization {:team-id team-id :organization-id organization-id})
|
|
(rx/map (fn [_] (modal/hide)))
|
|
(rx/catch on-error))
|
|
(if redirect-uri
|
|
(do
|
|
(ss/save-pending-action! pending-id {:type :add-team-to-organization
|
|
:team-id team-id
|
|
:organization-id organization-id})
|
|
(rx/of (rt/nav-raw :uri (str redirect-uri))))
|
|
(rx/empty)))))))))))
|
|
|
|
|
|
(defn check-organization-sso
|
|
"Asks the backend whether the organization SSO gate can be satisfied for
|
|
`query-string` (the route params of the client, `screen=…¶ms`, without
|
|
the separator), returning an observable of the raw `:check-nitrate-sso`
|
|
result: `:authorized` with a `:reason` of `:sso-satisfied`,
|
|
`:no-team-access` or `:no-organization-access`, or `:authorized false` with
|
|
a `:redirect-uri` (nil when SSO is required but the provider is unusable).
|
|
Failures are not caught, so a network blip stays a network error for the
|
|
caller to handle instead of masquerading as an answer."
|
|
[{:keys [team-id organization-id query-string]}]
|
|
(rp/cmd! :check-nitrate-sso (d/without-nils {:team-id team-id
|
|
:organization-id organization-id
|
|
:query-string query-string})))
|
|
|
|
(defn retry-organization-sso
|
|
"Retries the organization SSO login flow after a failed attempt, reusing
|
|
the same check-nitrate-sso RPC used elsewhere to move the user through
|
|
the organization's identity provider. Passing `team-id` enables the
|
|
backend's non-member short-circuit. Falls back to navigating straight
|
|
to the route of `query-string` when no fresh SSO redirect is needed or
|
|
available."
|
|
[{:keys [query-string] :as params}]
|
|
(let [dest-url (rt/token->url query-string)]
|
|
(ptk/reify ::retry-organization-sso
|
|
ptk/WatchEvent
|
|
(watch [_ _ _]
|
|
(->> (check-organization-sso params)
|
|
(rx/map (fn [{:keys [redirect-uri]}]
|
|
(rt/nav-raw :uri (or redirect-uri dest-url))))
|
|
(rx/catch (fn [_]
|
|
(rx/of (rt/nav-raw :uri dest-url)))))))))
|
|
|
|
(defn- fetch-organizations-allowed
|
|
"Returns an rx observable of an `organizations-allowed` map (organization-id -> boolean).
|
|
Organizations where :add-anybody-to-team is permitted are pre-approved;
|
|
the rest are verified via :all-team-members-in-organizations."
|
|
[team-id organizations]
|
|
(let [add-anybody-organizations (filterv #(cto/allowed? :add-anybody-to-team {:organization-perms %}) organizations)
|
|
organizations-to-check (filterv #(not (cto/allowed? :add-anybody-to-team {:organization-perms %})) organizations)
|
|
organization-ids-to-check (mapv :id organizations-to-check)]
|
|
(if (empty? organization-ids-to-check)
|
|
(rx/of (into {} (map (fn [organization] [(:id organization) true])) organizations))
|
|
(->> (rp/cmd! :all-team-members-in-organizations {:team-id team-id :organization-ids organization-ids-to-check})
|
|
(rx/map (fn [checked-organizations]
|
|
(merge (into {} (map (fn [organization] [(:id organization) true])) add-anybody-organizations)
|
|
checked-organizations)))))))
|
|
|
|
(defn show-add-team-to-organization-modal
|
|
"Fetches fresh team/organization data, then shows the add-to-organization modal
|
|
restricted to organizations where the user has permission, or the no-permission
|
|
modal if none qualify."
|
|
[{:keys [team-id]}]
|
|
(ptk/reify ::show-add-team-to-organization-modal
|
|
ptk/WatchEvent
|
|
(watch [_ state _]
|
|
(let [profile-id (dm/get-in state [:profile :id])]
|
|
(->> (rp/cmd! :get-teams)
|
|
(rx/mapcat
|
|
(fn [teams]
|
|
(let [all-organizations (map dt/team->organization
|
|
(filter #(and (:is-default %) (:organization %)) teams))
|
|
organizations (filter #(cto/allowed? :create-team
|
|
{:organization-perms %
|
|
:profile-id profile-id}) all-organizations)
|
|
team (first (filter #(= (:id %) team-id) teams))
|
|
current-organization (:organization team)
|
|
on-confirm (fn [organization-id]
|
|
(st/emit! (add-team-to-organization {:team-id team-id
|
|
:organization-id organization-id})))
|
|
show-select-modal
|
|
(fn [organizations-allowed]
|
|
(let [has-filtered? (< (count organizations) (count all-organizations))
|
|
extra-props (when has-filtered?
|
|
{:info-message (tr "dashboard.select-organization-modal.permission-info-add")})]
|
|
(modal/show :select-organization-modal
|
|
(merge {:organizations organizations
|
|
:organizations-allowed organizations-allowed
|
|
:current-organization current-organization
|
|
:on-confirm on-confirm
|
|
:team-id team-id
|
|
:title (tr "dashboard.select-organization-modal.title")
|
|
:choose (tr "dashboard.select-organization-modal.choose")
|
|
:placeholder (tr "dashboard.select-organization-modal.select")
|
|
:accept (tr "dashboard.select-organization-modal.accept")
|
|
:cancel (tr "labels.cancel")}
|
|
extra-props))))]
|
|
(if (empty? organizations)
|
|
(rx/of (dt/teams-fetched teams)
|
|
(modal/show :no-permission-modal {:type :no-organizations-create}))
|
|
(->> (fetch-organizations-allowed team-id organizations)
|
|
(rx/mapcat
|
|
(fn [organizations-allowed]
|
|
(let [valid-organizations (filterv #(true? (get organizations-allowed (:id %))) organizations)]
|
|
(rx/of
|
|
(dt/teams-fetched teams)
|
|
(if (empty? valid-organizations)
|
|
(modal/show
|
|
{:type :alert
|
|
:hide-actions? true
|
|
:message (tr "dashboard.team-organization.add.no-valid-organizations")
|
|
:title (tr "dashboard.select-organization-modal.title")})
|
|
(show-select-modal organizations-allowed))))))))))))))))
|
|
|
|
(defn show-change-team-organization-modal
|
|
"Fetches fresh team/organization data, then shows the change-organization modal
|
|
restricted to organizations where the user has permission, or the no-permission
|
|
modal if none qualify."
|
|
[{:keys [team-id]}]
|
|
(ptk/reify ::show-change-team-organization-modal
|
|
ptk/WatchEvent
|
|
(watch [_ state _]
|
|
(let [profile-id (dm/get-in state [:profile :id])]
|
|
(->> (rp/cmd! :get-teams)
|
|
(rx/mapcat
|
|
(fn [teams]
|
|
(let [all-organizations (map dt/team->organization
|
|
(filter #(and (:is-default %) (:organization %)) teams))
|
|
team (first (filter #(= (:id %) team-id) teams))
|
|
source-organization (:organization team)
|
|
current-organization-id (:id source-organization)
|
|
source-owner-id (:owner-id source-organization)
|
|
can-move? (fn [organization]
|
|
(cto/allowed? :move-team
|
|
{:organization-perms source-organization
|
|
:profile-id profile-id
|
|
:target-organization-same-owner? (= source-owner-id (:owner-id organization))}))
|
|
can-create? (fn [organization]
|
|
(cto/allowed? :create-team
|
|
{:organization-perms organization
|
|
:profile-id profile-id}))
|
|
organizations (filter #(and (can-move? %) (can-create? %)) all-organizations)
|
|
selectable-organizations (remove #(= current-organization-id (:id %)) organizations)
|
|
on-confirm (fn [organization-id]
|
|
(st/emit! (add-team-to-organization {:team-id team-id
|
|
:organization-id organization-id})))]
|
|
(if (empty? selectable-organizations)
|
|
(rx/of (dt/teams-fetched teams)
|
|
(modal/show :no-permission-modal {:type :no-organizations-change}))
|
|
(->> (fetch-organizations-allowed team-id selectable-organizations)
|
|
(rx/mapcat
|
|
(fn [organizations-allowed]
|
|
(let [valid-organizations (filterv #(true? (get organizations-allowed (:id %))) selectable-organizations)
|
|
has-filtered? (< (count organizations) (count all-organizations))
|
|
extra-props (when has-filtered?
|
|
{:info-message (tr "dashboard.select-organization-modal.permission-info")})]
|
|
(rx/of
|
|
(dt/teams-fetched teams)
|
|
(if (empty? valid-organizations)
|
|
(modal/show
|
|
{:type :alert
|
|
:hide-actions? true
|
|
:message (tr "dashboard.team-organization.add.no-valid-organizations")
|
|
:title (tr "dashboard.change-organization-modal.title")})
|
|
(modal/show :select-organization-modal
|
|
(merge {:organizations selectable-organizations
|
|
:organizations-allowed organizations-allowed
|
|
:current-organization source-organization
|
|
:on-confirm on-confirm
|
|
:team-id team-id
|
|
:title (tr "dashboard.change-organization-modal.title")
|
|
:description (tr "dashboard.change-organization-modal.description")
|
|
:choose (tr "dashboard.change-organization-modal.choose")
|
|
:placeholder (tr "dashboard.change-organization-modal.select")
|
|
:accept (tr "dashboard.change-organization-modal.accept")
|
|
:cancel (tr "labels.cancel")}
|
|
extra-props)))))))))))))))))
|