diff --git a/backend/src/app/rpc/management/nitrate.clj b/backend/src/app/rpc/management/nitrate.clj index 7666a637a5..44b92f22bc 100644 --- a/backend/src/app/rpc/management/nitrate.clj +++ b/backend/src/app/rpc/management/nitrate.clj @@ -1095,6 +1095,14 @@ RETURNING id, deleted_at;") AND email_to <> ALL(?) ) AS non_member") +(def ^:private sql:count-non-member-organization-team-invitations + "SELECT team_id, COUNT(*) AS count + FROM team_invitation + WHERE team_id = ANY(?) + AND email_to <> ALL(?) + AND valid_until > now() + GROUP BY team_id") + (def ^:private sql:delete-non-member-organization-team-invitations "DELETE FROM team_invitation WHERE team_id = ANY(?) @@ -1139,6 +1147,29 @@ RETURNING id, deleted_at;") (db/run! cfg (fn [{:keys [::db/conn]}] {:exists (boolean (non-member-organization-team-invitations-exist? conn params))}))) +(def ^:private schema:count-organization-team-invitations-for-non-members-result + [:vector + [:map + [:team-id ::sm/uuid] + [:count ::sm/int]]]) + +(sv/defmethod ::count-organization-team-invitations-for-non-members + "Count, per team, the pending (not expired) team invitations for + emails that are not organization members. Teams without any are left + out." + {::doc/added "3.0" + ::rpc/allowed-shared-keys allowed-shared-key-ids + ::sm/params schema:organization-team-invitations-for-non-members-params + ::sm/result schema:count-organization-team-invitations-for-non-members-result + ::nitrate/sso false} + [cfg params] + (db/run! cfg (fn [{:keys [::db/conn]}] + (let [{:keys [emails-array teams-array]} + (organization-team-invitations-for-non-members-arrays conn params)] + (db/exec! conn [sql:count-non-member-organization-team-invitations + teams-array + emails-array]))))) + (sv/defmethod ::delete-organization-team-invitations-for-non-members "Delete team invitations for emails that are not organization members." {::doc/added "2.18" @@ -1264,20 +1295,33 @@ RETURNING id, deleted_at;") [:map [:organization-id ::sm/uuid]]) +(def ^:private schema:team-detail + [:map + [:id ::sm/uuid] + [:name ::sm/text] + [:photo-url {:optional true} ::sm/uri] + [:created-at ::sm/inst] + [:last-activity-at {:optional true} [:maybe ::sm/inst]] + [:owner-profile-id {:optional true} [:maybe ::sm/uuid]] + [:owner-name {:optional true} [:maybe ::sm/text]] + [:owner-photo-url {:optional true} ::sm/uri] + [:num-projects ::sm/int] + [:num-files ::sm/int] + [:num-members ::sm/int]]) + (def ^:private schema:get-teams-detail-result - [:vector - [:map - [:id ::sm/uuid] - [:name ::sm/text] - [:photo-url {:optional true} ::sm/uri] - [:created-at ::sm/inst] - [:last-activity-at {:optional true} [:maybe ::sm/inst]] - [:owner-profile-id {:optional true} [:maybe ::sm/uuid]] - [:owner-name {:optional true} [:maybe ::sm/text]] - [:owner-photo-url {:optional true} ::sm/uri] - [:num-projects ::sm/int] - [:num-files ::sm/int] - [:num-members ::sm/int]]]) + [:vector schema:team-detail]) + +(defn- get-teams-detail-rows + "Detail rows of the given non-empty team ids, with their photo ids + resolved to public URLs." + [conn team-ids] + (let [ids-array (db/create-array conn "uuid" team-ids)] + (->> (db/exec! conn [sql:get-teams-detail ids-array]) + (mapv (fn [{:keys [photo-id owner-photo-id] :as row}] + (cond-> (dissoc row :photo-id :owner-photo-id) + photo-id (assoc :photo-url (files/resolve-public-uri photo-id)) + owner-photo-id (assoc :owner-photo-url (files/resolve-public-uri owner-photo-id)))))))) (sv/defmethod ::get-teams-detail "Get detailed information for all non-deleted teams in an organization, @@ -1294,12 +1338,51 @@ RETURNING id, deleted_at;") [] (db/run! cfg (fn [{:keys [::db/conn]}] - (let [ids-array (db/create-array conn "uuid" team-ids)] - (->> (db/exec! conn [sql:get-teams-detail ids-array]) - (mapv (fn [{:keys [photo-id owner-photo-id] :as row}] - (cond-> (dissoc row :photo-id :owner-photo-id) - photo-id (assoc :photo-url (files/resolve-public-uri photo-id)) - owner-photo-id (assoc :owner-photo-url (files/resolve-public-uri owner-photo-id)))))))))))) + (get-teams-detail-rows conn team-ids)))))) + +;; ---- API: get-owned-teams-detail + +(def ^:private sql:get-teams-members + "SELECT team_id, profile_id + FROM team_profile_rel + WHERE team_id = ANY(?)") + +(def ^:private schema:get-owned-teams-detail-result + [:vector + (conj schema:team-detail [:member-ids [:vector ::sm/uuid]])]) + +(defn- get-teams-member-ids + "Map of team id to the ids of its members, for the given non-empty + team ids." + [conn team-ids] + (let [ids-array (db/create-array conn "uuid" team-ids)] + (reduce (fn [result {:keys [team-id profile-id]}] + (update result team-id (fnil conj []) profile-id)) + {} + (db/exec! conn [sql:get-teams-members ids-array])))) + +(sv/defmethod ::get-owned-teams-detail + "List the non-default, non-deleted teams the current profile owns, + with the same details as get-teams-detail plus the ids of their + members. Nitrate uses it to offer the teams an organization owner can + bring into an organization." + {::doc/added "3.0" + ::rpc/allowed-shared-keys allowed-shared-key-ids + ::sm/params [:map] + ::sm/result schema:get-owned-teams-detail-result + ::nitrate/sso false} + [cfg {:keys [::rpc/profile-id]}] + (let [current-user-id (-> (profile/get-profile cfg profile-id) :id)] + (db/run! cfg + (fn [{:keys [::db/conn]}] + (let [team-ids (->> (db/exec! conn [sql:get-teams current-user-id]) + (mapv :id))] + (if (empty? team-ids) + [] + (let [member-ids (get-teams-member-ids conn team-ids)] + (mapv (fn [{:keys [id] :as team}] + (assoc team :member-ids (get member-ids id []))) + (get-teams-detail-rows conn team-ids))))))))) ;; ---- API: check-organization-sso @@ -1353,6 +1436,29 @@ RETURNING id, deleted_at;") (neh/send-organization-setup-sso-emails! cfg organization-id)) nil) +;; ---- API: send-organization-setup-sso-emails-for-team + +(def ^:private schema:send-organization-setup-sso-emails-for-team + [:map + [:organization-id ::sm/uuid] + [:team-id ::sm/uuid] + [:organization-member-ids [:vector ::sm/uuid]]]) + +(sv/defmethod ::send-organization-setup-sso-emails-for-team + "Send the SSO email to the people a team brings into an organization: + the team members missing from `organization-member-ids` (the members + before the team joined) and the pending team invitees. Does nothing + when the organization has no active SSO. Nitrate calls it after adding + a team, as add-team-to-organization does when Penpot adds one." + {::doc/added "3.0" + ::rpc/allowed-shared-keys allowed-shared-key-ids + ::sm/params schema:send-organization-setup-sso-emails-for-team + ::rpc/auth false} + [cfg {:keys [organization-id team-id organization-member-ids]}] + (neh/send-organization-setup-sso-emails-for-team! + cfg organization-id team-id (set organization-member-ids)) + nil) + ;; ---- API: bulk-create-profiles (def ^:private schema:bulk-create-profiles-params diff --git a/backend/test/backend_tests/rpc_management_nitrate_test.clj b/backend/test/backend_tests/rpc_management_nitrate_test.clj index bfbfcc1834..4f63a63bbb 100644 --- a/backend/test/backend_tests/rpc_management_nitrate_test.clj +++ b/backend/test/backend_tests/rpc_management_nitrate_test.clj @@ -575,6 +575,58 @@ (t/is (= (:modified-at file-after-update) (:last-activity-at updated-team))) (t/is (not= (:last-activity-at with-file) (:last-activity-at updated-team))))))) +(t/deftest get-owned-teams-detail-returns-owned-teams-with-counts-and-members + (with-mocks [nitrate-mock {:target 'app.nitrate/call :return nil}] + (let [profile (th/create-profile* 1 {:is-active true}) + member (th/create-profile* 2 {:is-active true}) + stranger (th/create-profile* 3 {:is-active true}) + team (th/create-team* 1 {:profile-id (:id profile)}) + _ (th/create-team-role* {:team-id (:id team) + :profile-id (:id member) + :role :editor}) + project (th/create-project* 1 {:profile-id (:id profile) + :team-id (:id team)}) + _ (th/create-file* 1 {:profile-id (:id profile) + :project-id (:id project)}) + empty-team (th/create-team* 2 {:profile-id (:id profile)}) + deleted-team (th/create-team* 3 {:profile-id (:id profile)}) + _ (th/db-update! :team + {:deleted-at (ct/now)} + {:id (:id deleted-team)}) + foreign-team (th/create-team* 4 {:profile-id (:id stranger)}) + _ (th/create-team-role* {:team-id (:id foreign-team) + :profile-id (:id profile) + :role :admin}) + out (th/management-command! {::th/type :get-owned-teams-detail + ::rpc/profile-id (:id profile)}) + teams (d/index-by :id (:result out)) + summarize (fn [team] + (-> (select-keys team [:name :num-projects :num-files :num-members]) + (assoc :member-ids (set (:member-ids team)))))] + + (t/is (th/success? out)) + (t/is (= #{(:id team) (:id empty-team)} (set (keys teams)))) + (t/is (= {:name (:name team) + :num-projects 1 + :num-files 1 + :num-members 2 + :member-ids #{(:id profile) (:id member)}} + (summarize (get teams (:id team))))) + (t/is (= {:name (:name empty-team) + :num-projects 0 + :num-files 0 + :num-members 1 + :member-ids #{(:id profile)}} + (summarize (get teams (:id empty-team)))))))) + +(t/deftest get-owned-teams-detail-returns-empty-when-owning-no-teams + (with-mocks [nitrate-mock {:target 'app.nitrate/call :return nil}] + (let [profile (th/create-profile* 1 {:is-active true}) + out (th/management-command! {::th/type :get-owned-teams-detail + ::rpc/profile-id (:id profile)})] + (t/is (th/success? out)) + (t/is (= [] (:result out)))))) + (t/deftest notify-organization-deletion-prefixes-teams-and-publishes-organization-deleted-event ;; --- Deferred organization-summary: nil during setup, filled before RPC --- (let [organization-summary-ref (atom nil)] @@ -1343,6 +1395,88 @@ :valid-until (ct/in-future "24h")}) (t/is (true? (exist!)))))) +(t/deftest count-organization-team-invitations-for-non-members-counts-pending-per-team + (with-mocks [nitrate-mock {:target 'app.nitrate/call :return nil}] + (let [member (th/create-profile* 1 {:is-active true :email "member1@example.com"}) + profile (th/create-profile* 2 {:is-active true}) + team-1 (th/create-team* 1 {:profile-id (:id profile)}) + team-2 (th/create-team* 2 {:profile-id (:id profile)}) + team-3 (th/create-team* 3 {:profile-id (:id profile)}) + outside-team (th/create-team* 4 {:profile-id (:id profile)}) + invite (fn [team email valid-until] + (th/db-insert! :team-invitation + {:id (uuid/random) + :team-id (:id team) + :org-id nil + :email-to email + :created-by (:id profile) + :role "editor" + :valid-until valid-until}))] + + (invite team-1 "member1@example.com" (ct/in-future "24h")) + (invite team-1 "outsider-a@example.com" (ct/in-future "24h")) + (invite team-1 "outsider-b@example.com" (ct/in-future "24h")) + (invite team-2 "outsider-c@example.com" (ct/in-future "24h")) + (invite team-2 "expired@example.com" (ct/in-past "1h")) + (invite outside-team "outsider-d@example.com" (ct/in-future "24h")) + + (let [out (th/management-command! + {::th/type :count-organization-team-invitations-for-non-members + ::rpc/profile-id (:id profile) + :team-ids [(:id team-1) (:id team-2) (:id team-3)] + :member-ids [(:id member)]})] + (t/is (th/success? out)) + (t/is (= {(:id team-1) 2 + (:id team-2) 1} + (->> (:result out) + (into {} (map (juxt :team-id :count)))))))))) + +(t/deftest count-organization-team-invitations-for-non-members-handles-empty-lists + (with-mocks [nitrate-mock {:target 'app.nitrate/call :return nil}] + (let [profile (th/create-profile* 1 {:is-active true}) + deleted (th/create-profile* 2 {:is-active true :email "deleted@example.com"}) + _ (th/db-update! :profile + {:deleted-at (ct/now)} + {:id (:id deleted)}) + team-1 (th/create-team* 1 {:profile-id (:id profile)}) + team-2 (th/create-team* 2 {:profile-id (:id profile)}) + invite (fn [team email] + (th/db-insert! :team-invitation + {:id (uuid/random) + :team-id (:id team) + :org-id nil + :email-to email + :created-by (:id profile) + :role "editor" + :valid-until (ct/in-future "24h")})) + count! (fn [team-ids member-ids] + (let [out (th/management-command! + {::th/type :count-organization-team-invitations-for-non-members + ::rpc/profile-id (:id profile) + :team-ids team-ids + :member-ids member-ids})] + (t/is (th/success? out)) + (into {} (map (juxt :team-id :count)) (:result out)))) + all-pending {(:id team-1) 2 + (:id team-2) 1}] + + (invite team-1 "deleted@example.com") + (invite team-1 "outsider-a@example.com") + (invite team-2 "outsider-b@example.com") + + (t/testing "no team ids give no counts" + (t/is (= {} (count! [] []))) + (t/is (= {} (count! [] [(:id profile)])))) + + (t/testing "no member ids count every pending invitation" + (t/is (= all-pending + (count! [(:id team-1) (:id team-2)] [])))) + + (t/testing "member ids without an email count every pending invitation" + (t/is (= all-pending + (count! [(:id team-1) (:id team-2)] + [(:id deleted) (uuid/random)]))))))) + (t/deftest delete-organization-team-invitations-for-non-members-removes-non-member-invitations (with-mocks [nitrate-mock {:target 'app.nitrate/call :return nil}] (let [member1 (th/create-profile* 1 {:is-active true :email "member1@example.com"}) @@ -2071,6 +2205,71 @@ (th/management-command! params)) (t/is (empty? @sent)))) +(t/deftest send-organization-setup-sso-emails-for-team-emails-people-new-to-the-organization + (let [sent (atom [])] + (with-mocks [nitrate-mock {:target 'app.nitrate/call + :return (fn [_cfg method _params] + (case method + :get-organization-sso {:active true} + :get-organization-summary {:name "Acme Inc"} + nil))} + email-mock {:target 'app.email/send-template + :return (fn [_cfg params] (swap! sent conj params) nil)}] + (let [owner (th/create-profile* 1 {:is-active true}) + existing-member (th/create-profile* 2 {:is-active true + :email "existing@example.com"}) + new-member (th/create-profile* 3 {:is-active true + :email "new@example.com"}) + team (th/create-team* 1 {:profile-id (:id owner)}) + _ (th/create-team-role* {:team-id (:id team) + :profile-id (:id existing-member) + :role :editor}) + _ (th/create-team-role* {:team-id (:id team) + :profile-id (:id new-member) + :role :editor}) + _ (th/db-insert! :team-invitation + {:id (uuid/random) + :team-id (:id team) + :org-id nil + :email-to "external@example.com" + :created-by (:id owner) + :role "editor" + :valid-until (ct/in-future "24h")}) + out (th/management-command! + {::th/type :send-organization-setup-sso-emails-for-team + :organization-id (uuid/random) + :team-id (:id team) + :organization-member-ids [(:id owner) (:id existing-member)]})] + + (t/is (th/success? out)) + (t/is (= #{"new@example.com" "external@example.com"} + (->> @sent (map :to) set))) + (t/is (every? #(= "Acme Inc" (:organization-name %)) @sent)))))) + +(t/deftest send-organization-setup-sso-emails-for-team-skips-email-when-sso-is-not-active + (let [sent (atom [])] + (with-mocks [nitrate-mock {:target 'app.nitrate/call + :return (fn [_cfg method _params] + (case method + :get-organization-sso {:active false} + nil))} + email-mock {:target 'app.email/send-template + :return (fn [_cfg params] (swap! sent conj params) nil)}] + (let [owner (th/create-profile* 1 {:is-active true}) + new-member (th/create-profile* 2 {:is-active true}) + team (th/create-team* 1 {:profile-id (:id owner)}) + _ (th/create-team-role* {:team-id (:id team) + :profile-id (:id new-member) + :role :editor}) + out (th/management-command! + {::th/type :send-organization-setup-sso-emails-for-team + :organization-id (uuid/random) + :team-id (:id team) + :organization-member-ids [(:id owner)]})] + + (t/is (th/success? out)) + (t/is (empty? @sent)))))) + (t/deftest check-organization-sso-returns-valid-true (let [organization-id (uuid/random) out (with-redefs [ssrf/safe-url? (constantly true)