diff --git a/backend/src/app/loggers/audit/consumer_task.clj b/backend/src/app/loggers/audit/consumer_task.clj index 9d1146bf22..ef35c7b99e 100644 --- a/backend/src/app/loggers/audit/consumer_task.clj +++ b/backend/src/app/loggers/audit/consumer_task.clj @@ -255,9 +255,11 @@ "sso-config-tested" "success-activate-organization-sso" "success-apply-organization-sso-changes" + "transfer-team-ownership" "unassign-organization-attribute" "update-organization-attribute" "update-organization-invitation" + "update-organization-member-role" "update-organization-permissions" "update-team-invitation" "update-team-invitation-role" diff --git a/backend/src/app/rpc/management/nitrate.clj b/backend/src/app/rpc/management/nitrate.clj index da70a0d639..7666a637a5 100644 --- a/backend/src/app/rpc/management/nitrate.clj +++ b/backend/src/app/rpc/management/nitrate.clj @@ -29,6 +29,7 @@ [app.loggers.audit :as audit] [app.media.svg :as svg] [app.media.validation :as media.v] + [app.msgbus :as mbus] [app.nitrate :as nitrate] [app.rpc :as rpc] [app.rpc.commands.auth :as auth] @@ -921,6 +922,124 @@ RETURNING id, deleted_at;") (count valid-teams-to-transfer) (count valid-teams-to-exit)))) +;; API: set-organization-member-viewer + +;; The teams of the organization where the member can still edit: the +;; ones whose role has to drop to viewer. Default teams stay out: an +;; organization's "Your Penpot" team is not flagged `is_default` here, +;; so Nitrate leaves it out of `team-ids`, and this check covers the +;; profile's own default team. +(def ^:private sql:get-member-editable-teams + "SELECT t.id, t.name, t.photo_id, tpr.is_owner + FROM team_profile_rel AS tpr + JOIN team AS t ON t.id = tpr.team_id + WHERE tpr.profile_id = ? + AND t.id = ANY(?) + AND t.deleted_at IS NULL + AND t.is_default IS FALSE + AND tpr.can_edit IS TRUE + FOR UPDATE OF tpr") + +(def ^:private sql:downgrade-member-team-invitations + "UPDATE team_invitation AS ti + SET role = 'viewer', + updated_at = now() + FROM profile AS p + WHERE p.id = ? + AND lower(ti.email_to) = lower(p.email) + AND ti.team_id = ANY(?) + AND ti.role IN ('editor', 'admin')") + +(def ^:private schema:set-organization-member-viewer-params + [:map + [:profile-id ::sm/uuid] + [:organization-owner-id ::sm/uuid] + [:team-ids [:vector ::sm/uuid]]]) + +(def ^:private schema:set-organization-member-viewer-result + [:map + [:transferred-teams + [:vector [:map + [:id ::sm/uuid] + [:name ::sm/text] + [:photo-url {:optional true} ::sm/uri]]]]]) + +(defn- assign-team-owner + "Makes `owner-id` the owner of the team, adding them to it when they + are not a member yet." + [{:keys [::db/conn] :as cfg} team-id owner-id] + (let [owner-flags (get ctt/permissions-for-role :owner)] + (if (db/get* conn :team-profile-rel {:team-id team-id :profile-id owner-id}) + (db/update! conn :team-profile-rel + owner-flags + {:team-id team-id :profile-id owner-id}) + (teams/add-profile-to-team! cfg (assoc owner-flags + :team-id team-id + :profile-id owner-id))))) + +(sv/defmethod ::set-organization-member-viewer + "Limits a member of an organization to viewer in every team of the + organization: their team roles drop to viewer, the teams they own pass + to the organization owner and their pending editor or admin team + invitations become viewer invitations. Answers the teams whose + ownership moved." + {::doc/added "2.20" + ::rpc/allowed-shared-keys allowed-shared-key-ids + ::sm/params schema:set-organization-member-viewer-params + ::sm/result schema:set-organization-member-viewer-result + ::db/transaction true + ::nitrate/sso false} + [{:keys [::db/conn ::mbus/msgbus] :as cfg} {:keys [profile-id organization-owner-id team-ids]}] + (when (= profile-id organization-owner-id) + (ex/raise :type :validation + :code :cant-change-organization-owner-role)) + + (let [team-ids-array (db/create-array conn "uuid" team-ids) + teams (db/exec! conn [sql:get-member-editable-teams profile-id team-ids-array]) + owned-teams (filterv :is-owner teams) + owner-name (when (seq owned-teams) + (:fullname (db/get conn :profile {:id organization-owner-id}))) + viewer-flags (get ctt/permissions-for-role :viewer)] + + ;; The organization owner asked for this from the admin console, so + ;; no message goes to their sessions: the dashboard reads the team + ;; again the next time they open it. + (doseq [{:keys [id]} owned-teams] + (assign-team-owner cfg id organization-owner-id)) + + (doseq [{:keys [id]} teams] + (db/update! conn :team-profile-rel + viewer-flags + {:team-id id :profile-id profile-id})) + + (db/exec-one! conn [sql:downgrade-member-team-invitations profile-id team-ids-array]) + + ;; Deferred past the commit: the member's sessions read the team + ;; again when told, and a rolled back change must not be announced. + ;; The former owner learns who owns the team now; everybody else + ;; gets the plain role change. + (db/after-commit + (fn [] + (doseq [{:keys [id name is-owner]} teams] + (mbus/pub! msgbus + :topic profile-id + :message (cond-> {:type :team-role-change + :topic profile-id + :team-id id + :role :viewer} + is-owner + (assoc :team-name name + :new-owner-name owner-name)))))) + + (when (seq teams) + (notifications/notify-permissions-changed cfg profile-id)) + + {:transferred-teams + (mapv (fn [{:keys [id name photo-id]}] + (cond-> {:id id :name name} + photo-id (assoc :photo-url (files/resolve-public-uri photo-id)))) + owned-teams)})) + ;; API: send-renewal-email (def ^:private schema:send-renewal-email-params diff --git a/backend/test/backend_tests/rpc_management_nitrate_test.clj b/backend/test/backend_tests/rpc_management_nitrate_test.clj index 60eae1c707..0afe4cd663 100644 --- a/backend/test/backend_tests/rpc_management_nitrate_test.clj +++ b/backend/test/backend_tests/rpc_management_nitrate_test.clj @@ -2354,3 +2354,126 @@ out (th/multipart-management-command! params)] (t/is (th/ex-of-type? (:error out) :restriction)) (t/is (th/ex-of-code? (:error out) :media-max-file-size-reached)))))))) + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; Tests: set-organization-member-viewer +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +(defn- get-team-role + [team-id profile-id] + (when-let [rel (th/db-get :team-profile-rel {:team-id team-id :profile-id profile-id})] + (cond + (:is-owner rel) :owner + (:is-admin rel) :admin + (:can-edit rel) :editor + :else :viewer))) + +(defn- insert-team-invitation + [team-id email role created-by] + (th/db-insert! :team-invitation + {:id (uuid/random) + :team-id team-id + :email-to email + :created-by created-by + :role role + :valid-until (ct/in-future "24h")})) + +(t/deftest set-organization-member-viewer-limits-the-member-in-every-organization-team + (with-mocks [nitrate-mock {:target 'app.nitrate/call :return nil}] + (let [calls (atom []) + owner (th/create-profile* 1 {:is-active true :fullname "Org Owner"}) + member (th/create-profile* 2 {:is-active true :email "member@example.com"}) + other (th/create-profile* 3 {:is-active true}) + owned-team (th/create-team* 1 {:profile-id (:id member)}) + admin-team (th/create-team* 2 {:profile-id (:id other)}) + editor-team (th/create-team* 3 {:profile-id (:id other)}) + viewer-team (th/create-team* 4 {:profile-id (:id other)}) + outside-team (th/create-team* 5 {:profile-id (:id other)}) + invited-team (th/create-team* 6 {:profile-id (:id other)}) + _ (doseq [[team role] [[admin-team :admin] + [editor-team :editor] + [viewer-team :viewer] + [outside-team :editor]]] + (th/create-team-role* {:team-id (:id team) + :profile-id (:id member) + :role role})) + _ (insert-team-invitation (:id invited-team) "member@example.com" "admin" (:id other)) + _ (insert-team-invitation (:id outside-team) "member@example.com" "editor" (:id other)) + team-ids (mapv :id [owned-team admin-team editor-team viewer-team invited-team + {:id (:default-team-id member)}]) + out (with-redefs [mbus/pub! (fn [_msgbus & {:keys [topic message]}] + (swap! calls conj {:topic topic :message message}))] + (th/management-command! {::th/type :set-organization-member-viewer + ::rpc/profile-id (:id owner) + :profile-id (:id member) + :organization-owner-id (:id owner) + :team-ids team-ids}))] + + (t/is (th/success? out)) + (t/is (= [{:id (:id owned-team) :name (:name owned-team)}] + (-> out :result :transferred-teams))) + + ;; Every organization team the member could edit in now has them as viewer. + (t/is (= :viewer (get-team-role (:id owned-team) (:id member)))) + (t/is (= :viewer (get-team-role (:id admin-team) (:id member)))) + (t/is (= :viewer (get-team-role (:id editor-team) (:id member)))) + (t/is (= :viewer (get-team-role (:id viewer-team) (:id member)))) + + ;; The team they owned passes to the organization owner, who joins it. + (t/is (= :owner (get-team-role (:id owned-team) (:id owner)))) + + ;; Teams outside the organization and the personal team stay as they were. + (t/is (= :editor (get-team-role (:id outside-team) (:id member)))) + (t/is (= :owner (get-team-role (:default-team-id member) (:id member)))) + + ;; Pending invitations to organization teams become viewer invitations. + (t/is (= "viewer" (:role (th/db-get :team-invitation {:team-id (:id invited-team) + :email-to "member@example.com"})))) + (t/is (= "editor" (:role (th/db-get :team-invitation {:team-id (:id outside-team) + :email-to "member@example.com"})))) + + ;; The member's sessions learn about each change; only the former + ;; owner message names the team and its new owner. + (let [role-changes (->> @calls + (filter #(= :team-role-change (-> % :message :type))) + (map :message))] + (t/is (= #{(:id owned-team) (:id admin-team) (:id editor-team)} + (into #{} (map :team-id) role-changes))) + (t/is (every? #(= (:id member) (:topic %)) role-changes)) + (t/is (every? #(= :viewer (:role %)) role-changes)) + (t/is (= [{:team-name (:name owned-team) :new-owner-name "Org Owner"}] + (->> role-changes + (filter :new-owner-name) + (map #(select-keys % [:team-name :new-owner-name]))))))))) + +(t/deftest set-organization-member-viewer-promotes-an-organization-owner-already-in-the-team + (with-mocks [nitrate-mock {:target 'app.nitrate/call :return nil} + mbus-mock {:target 'app.msgbus/pub! :return nil}] + (let [owner (th/create-profile* 1 {:is-active true}) + member (th/create-profile* 2 {:is-active true}) + owned-team (th/create-team* 1 {:profile-id (:id member)}) + _ (th/create-team-role* {:team-id (:id owned-team) + :profile-id (:id owner) + :role :viewer}) + out (th/management-command! {::th/type :set-organization-member-viewer + ::rpc/profile-id (:id owner) + :profile-id (:id member) + :organization-owner-id (:id owner) + :team-ids [(:id owned-team)]})] + (t/is (th/success? out)) + (t/is (= :owner (get-team-role (:id owned-team) (:id owner)))) + (t/is (= :viewer (get-team-role (:id owned-team) (:id member)))) + (t/is (= 2 (count (th/db-query :team-profile-rel {:team-id (:id owned-team)}))))))) + +(t/deftest set-organization-member-viewer-rejects-the-organization-owner + (with-mocks [nitrate-mock {:target 'app.nitrate/call :return nil}] + (let [owner (th/create-profile* 1 {:is-active true}) + owned-team (th/create-team* 1 {:profile-id (:id owner)}) + out (th/management-command! {::th/type :set-organization-member-viewer + ::rpc/profile-id (:id owner) + :profile-id (:id owner) + :organization-owner-id (:id owner) + :team-ids [(:id owned-team)]})] + (t/is (not (th/success? out))) + (t/is (th/ex-of-code? (:error out) :cant-change-organization-owner-role)) + (t/is (= :owner (get-team-role (:id owned-team) (:id owner))))))) diff --git a/frontend/src/app/main/data/common.cljs b/frontend/src/app/main/data/common.cljs index 229f5852cc..122be2bb5e 100644 --- a/frontend/src/app/main/data/common.cljs +++ b/frontend/src/app/main/data/common.cljs @@ -197,7 +197,10 @@ :owner (tr "dashboard.permissions-change.owner"))) (defn change-team-role - [{:keys [team-id role]}] + "Applies a role change pushed by the backend. When the organization + takes a team away from its owner, the message names the team and its + new owner, and the former owner is told wherever they are." + [{:keys [team-id role team-name new-owner-name]}] (dm/assert! (uuid? team-id)) (dm/assert! (contains? ctt/valid-roles role)) @@ -205,16 +208,24 @@ ptk/WatchEvent (watch [_ state _] (let [current-team-id (:current-team-id state)] - (when (= team-id current-team-id) + (cond + (some? new-owner-name) + (rx/of (ntf/info (tr "dashboard.team-owner-changed-by-organization" + team-name + new-owner-name))) + + (= team-id current-team-id) (rx/of (ntf/info (get-change-role-msg role)))))) ptk/UpdateEvent (update [_ state] (let [permissions (get ctt/permissions-for-role role)] - (-> state - (update :permissions merge permissions) - (update-in [:team :permissions] merge permissions) - (d/update-in-when [:teams team-id :permissions] merge permissions)))))) + ;; The session's own permissions follow the team it is in; a + ;; change to any other team only updates that team's entry + (cond-> (d/update-in-when state [:teams team-id :permissions] merge permissions) + (= team-id (:current-team-id state)) + (-> (update :permissions merge permissions) + (update-in [:team :permissions] merge permissions))))))) (defn team-membership-change [{:keys [team-id team-name change]}] diff --git a/frontend/src/app/main/data/dashboard.cljs b/frontend/src/app/main/data/dashboard.cljs index c46b56806c..fe1c7143be 100644 --- a/frontend/src/app/main/data/dashboard.cljs +++ b/frontend/src/app/main/data/dashboard.cljs @@ -700,12 +700,17 @@ ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (defn- handle-change-team-role - [params] + [{:keys [team-id] :as params}] (ptk/reify ::handle-change-team-role ptk/WatchEvent - (watch [_ _ _] - (rx/of (dcm/change-team-role params) - (modal/hide))))) + (watch [_ state _] + (rx/concat + (rx/of (dcm/change-team-role params) + (modal/hide)) + ;; The open members list shows the old role (and, after an + ;; ownership transfer, the old owner) until it is fetched again + (when (= team-id (:current-team-id state)) + (rx/of (dtm/fetch-members team-id))))))) (defn- handle-user-organization-change "Handle :user-organization-change websocket messages. diff --git a/frontend/src/app/main/data/workspace/notifications.cljs b/frontend/src/app/main/data/workspace/notifications.cljs index 480c8c9d55..1abcb0b55a 100644 --- a/frontend/src/app/main/data/workspace/notifications.cljs +++ b/frontend/src/app/main/data/workspace/notifications.cljs @@ -131,23 +131,27 @@ (rx/of (dws/send endmsg))))))))) (defn- handle-change-team-role - [{:keys [role] :as msg}] + [{:keys [team-id role] :as msg}] (ptk/reify ::handle-change-team-role ptk/WatchEvent - (watch [_ _ _] - (rx/concat - (rx/of :interrupt - (dwe/clear-edition-mode) - (dwc/set-workspace-read-only false)) - (->> (rx/of (dc/change-team-role msg) - ::dwt/update-editor-state) - ;; Delay so anything that launched :interrupt can finish - (rx/delay 100)) - (if (= :viewer role) - (rx/of (modal/hide) - (dwly/set-options-mode :inspect) - (dpl/close-current-plugin {:close-only-edition-plugins? true})) - (rx/of (dwly/set-options-mode :design))))))) + (watch [_ state _] + ;; A change to another team must not touch the open file: only + ;; the team that owns it changes what can be done here + (if (not= team-id (:current-team-id state)) + (rx/of (dc/change-team-role msg)) + (rx/concat + (rx/of :interrupt + (dwe/clear-edition-mode) + (dwc/set-workspace-read-only false)) + (->> (rx/of (dc/change-team-role msg) + ::dwt/update-editor-state) + ;; Delay so anything that launched :interrupt can finish + (rx/delay 100)) + (if (= :viewer role) + (rx/of (modal/hide) + (dwly/set-options-mode :inspect) + (dpl/close-current-plugin {:close-only-edition-plugins? true})) + (rx/of (dwly/set-options-mode :design)))))))) (defn- process-message [{:keys [type] :as msg}] diff --git a/frontend/test/frontend_tests/data/common_test.cljs b/frontend/test/frontend_tests/data/common_test.cljs index b0c43bd4c8..2b596725a7 100644 --- a/frontend/test/frontend_tests/data/common_test.cljs +++ b/frontend/test/frontend_tests/data/common_test.cljs @@ -14,6 +14,7 @@ id from the argument, the initialized team, the team of the route already on screen, and the profile default, in that order." (:require + [app.common.types.team :as ctt] [app.common.uuid :as uuid] [app.main.data.common :as dcm] [app.main.router :as rt] @@ -172,3 +173,37 @@ (let [events (await (emitted (dcm/go-to-dashboard-members) (state {})))] (t/is (some? (nav-params events :dashboard-members))) (t/is (nil? (:team-id (nav-params events :dashboard-members)))))) + +;; --- Team role changes + +(defn- role-state + "A session inside `current-team-id` with editor rights in it and in + `other-team-id`." + [current-team-id other-team-id] + (let [editor (get ctt/permissions-for-role :editor)] + {:current-team-id current-team-id + :permissions editor + :team {:id current-team-id :permissions editor} + :teams {current-team-id {:permissions editor} + other-team-id {:permissions editor}}})) + +(t/deftest change-team-role-of-another-team-keeps-the-session-permissions + (let [current-team-id (uuid/next) + other-team-id (uuid/next) + editor (get ctt/permissions-for-role :editor) + state (ptk/update (dcm/change-team-role {:team-id other-team-id :role :viewer}) + (role-state current-team-id other-team-id))] + (t/is (= editor (:permissions state))) + (t/is (= editor (get-in state [:team :permissions]))) + (t/is (= editor (get-in state [:teams current-team-id :permissions]))) + (t/is (false? (get-in state [:teams other-team-id :permissions :can-edit]))))) + +(t/deftest change-team-role-of-the-current-team-limits-the-session + (let [current-team-id (uuid/next) + other-team-id (uuid/next) + state (ptk/update (dcm/change-team-role {:team-id current-team-id :role :viewer}) + (role-state current-team-id other-team-id))] + (t/is (false? (get-in state [:permissions :can-edit]))) + (t/is (false? (get-in state [:team :permissions :can-edit]))) + (t/is (false? (get-in state [:teams current-team-id :permissions :can-edit]))) + (t/is (true? (get-in state [:teams other-team-id :permissions :can-edit]))))) diff --git a/frontend/translations/en.po b/frontend/translations/en.po index 7828a52549..faffb0b35f 100644 --- a/frontend/translations/en.po +++ b/frontend/translations/en.po @@ -1359,6 +1359,10 @@ msgstr "This team is not part of any organization" msgid "dashboard.team-organization.remove" msgstr "Remove team from organization" +#: src/app/main/data/common.cljs:213 +msgid "dashboard.team-owner-changed-by-organization" +msgstr "You are now a viewer in %s. %s is its new owner." + #: src/app/main/ui/dashboard/team.cljs:1744 msgid "dashboard.team-projects" msgstr "Team projects" diff --git a/frontend/translations/es.po b/frontend/translations/es.po index 5645657ffb..4e5e230f54 100644 --- a/frontend/translations/es.po +++ b/frontend/translations/es.po @@ -1379,6 +1379,10 @@ msgstr "Este equipo no pertenece a ninguna organización" msgid "dashboard.team-organization.remove" msgstr "Eliminar equipo de la organización" +#: src/app/main/data/common.cljs:213 +msgid "dashboard.team-owner-changed-by-organization" +msgstr "Ahora eres lector en %s. %s es su nuevo propietario." + #: src/app/main/ui/dashboard/team.cljs:1744 msgid "dashboard.team-projects" msgstr "Proyectos del equipo"