mirror of
https://github.com/penpot/penpot.git
synced 2026-11-26 21:00:32 +00:00
🐛 Record org role changes in the admin console audit log (#13342)
When an organization owner changes a member's org role, the admin console audit log missed several entries: - The consumers rework dropped update-organization-member-role and transfer-team-ownership from the admin console allowlist, so no org role change and no team passed to the organization owner reached it. - set-organization-member-viewer turned pending editor and admin team invitations into viewer invitations without leaving an event. - The org role entry identified the member by id and email only. Both names are back on the allowlist. set-organization-member-viewer now records each invitation it downgrades as update-team-invitation-role, the event of the same change made by hand, attributed to the organization owner, inside its transaction. It takes an optional organization-id and puts it in those events, so a team moved to another organization before the event is archived cannot misroute it. The admin console consumer fills :member-name from :member-id for update-organization-member-role only, from the profile it already loads, so the role change waits on no lookup. AI-assisted-by: claude-opus-5-5
This commit is contained in:
parent
e6f3d21dce
commit
d091491880
@ -8,8 +8,9 @@
|
||||
"The admin console transport of the audit log consumer.
|
||||
|
||||
It reads the stream from its own cursor and ingests the allowlisted
|
||||
events, enriched with the name of their team and the emails of the
|
||||
profiles they mention (the admin console shows people, not ids). The
|
||||
events, enriched with the name of their team, the emails of the
|
||||
profiles they mention and, for an org role change, the member's name
|
||||
(the admin console shows people, not ids). The
|
||||
events outside the allowlist, and the telemetry shadows, count as
|
||||
skipped for the cursor: they are passed over and never reach the
|
||||
destination.
|
||||
@ -84,9 +85,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"
|
||||
@ -102,6 +105,20 @@
|
||||
:member-id :member-email
|
||||
:user-who-send-invitation :user-who-send-invitation-email})
|
||||
|
||||
;; Events whose :member-id is also named (:member-name from profile.fullname).
|
||||
;; Kept to the events that need it: the org role entry names its member.
|
||||
(def ^:private member-named-events
|
||||
#{"update-organization-member-role"})
|
||||
|
||||
(defn- collect-member-ids-needing-name
|
||||
[events]
|
||||
(into #{}
|
||||
(keep (fn [{:keys [name props]}]
|
||||
(when (and (contains? member-named-events name)
|
||||
(not (contains? props :member-name)))
|
||||
(uuid/coerce (:member-id props)))))
|
||||
events))
|
||||
|
||||
(defn- collect-team-ids-needing-name
|
||||
[events]
|
||||
(into #{}
|
||||
@ -174,6 +191,18 @@
|
||||
props
|
||||
id->email-prop))))
|
||||
|
||||
(defn- maybe-assoc-member-name
|
||||
"Fill-only :member-name from the member's fullname, for the events in
|
||||
`member-named-events`. Omitted when the profile is absent or unnamed."
|
||||
[event profiles]
|
||||
(let [props (:props event)]
|
||||
(if-let [fullname (and (contains? member-named-events (:name event))
|
||||
(not (contains? props :member-name))
|
||||
(when-let [id (uuid/coerce (:member-id props))]
|
||||
(not-empty (:fullname (get profiles id)))))]
|
||||
(assoc-in event [:props :member-name] fullname)
|
||||
event)))
|
||||
|
||||
(defn- maybe-assoc-actor-profile
|
||||
"Fill-only top-level :profile-email / :profile-name from the event
|
||||
actor (:profile-id). Omits keys when the id is missing, zero, the
|
||||
@ -202,17 +231,20 @@
|
||||
:user-who-send-invitation-email.
|
||||
3. Fill missing top-level :profile-email / :profile-name from the
|
||||
event actor (:profile-id), including soft-deleted profiles; omit
|
||||
when there is no usable actor id or the value is empty."
|
||||
when there is no usable actor id or the value is empty.
|
||||
4. Fill a missing :member-name for the events in `member-named-events`."
|
||||
[{:keys [::db/conn]} events]
|
||||
(let [team-ids (collect-team-ids-needing-name events)
|
||||
profile-ids (into (collect-profile-ids-needing-email events)
|
||||
(collect-actor-ids-needing-profile events))
|
||||
profile-ids (-> (collect-profile-ids-needing-email events)
|
||||
(into (collect-actor-ids-needing-profile events))
|
||||
(into (collect-member-ids-needing-name events)))
|
||||
team-names (load-team-names conn team-ids)
|
||||
profiles (load-profiles conn profile-ids)]
|
||||
(mapv (fn [event]
|
||||
(-> event
|
||||
(maybe-assoc-team-name team-names)
|
||||
(maybe-assoc-emails profiles)
|
||||
(maybe-assoc-member-name profiles)
|
||||
(maybe-assoc-actor-profile profiles)))
|
||||
events)))
|
||||
|
||||
|
||||
@ -948,12 +948,16 @@ RETURNING id, deleted_at;")
|
||||
WHERE p.id = ?
|
||||
AND lower(ti.email_to) = lower(p.email)
|
||||
AND ti.team_id = ANY(?)
|
||||
AND ti.role IN ('editor', 'admin')")
|
||||
AND ti.role IN ('editor', 'admin')
|
||||
RETURNING ti.team_id, ti.email_to")
|
||||
|
||||
(def ^:private schema:set-organization-member-viewer-params
|
||||
[:map
|
||||
[:profile-id ::sm/uuid]
|
||||
[:organization-owner-id ::sm/uuid]
|
||||
;; Optional so an older Nitrate keeps working; its events then reach
|
||||
;; the organization the team is in when they are archived.
|
||||
[:organization-id {:optional true} ::sm/uuid]
|
||||
[:team-ids [:vector ::sm/uuid]]])
|
||||
|
||||
(def ^:private schema:set-organization-member-viewer-result
|
||||
@ -977,6 +981,23 @@ RETURNING id, deleted_at;")
|
||||
:team-id team-id
|
||||
:profile-id owner-id)))))
|
||||
|
||||
(defn- submit-invitation-role-event
|
||||
"Records a pending invitation turned into a viewer invitation with the
|
||||
event and props of the same change made by hand, attributed to the
|
||||
organization owner who asked for it. The organization travels with it:
|
||||
the team could leave it before the event is archived."
|
||||
[cfg {:keys [organization-owner-id organization-id] :as params} {:keys [team-id email-to]}]
|
||||
(when cf/audit-persist?
|
||||
(audit/submit cfg
|
||||
(-> (audit/event-from-rpc-params params)
|
||||
(assoc :name "update-team-invitation-role"
|
||||
:profile-id organization-owner-id
|
||||
:props (d/without-nils
|
||||
{:organization-id organization-id
|
||||
:team-id team-id
|
||||
:email email-to
|
||||
:role :viewer}))))))
|
||||
|
||||
(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
|
||||
@ -989,7 +1010,7 @@ RETURNING id, deleted_at;")
|
||||
::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]}]
|
||||
[{:keys [::db/conn ::mbus/msgbus] :as cfg} {:keys [profile-id organization-owner-id team-ids] :as params}]
|
||||
(when (= profile-id organization-owner-id)
|
||||
(ex/raise :type :validation
|
||||
:code :cant-change-organization-owner-role))
|
||||
@ -1012,7 +1033,8 @@ RETURNING id, deleted_at;")
|
||||
viewer-flags
|
||||
{:team-id id :profile-id profile-id}))
|
||||
|
||||
(db/exec-one! conn [sql:downgrade-member-team-invitations profile-id team-ids-array])
|
||||
(doseq [invitation (db/exec! conn [sql:downgrade-member-team-invitations profile-id team-ids-array])]
|
||||
(submit-invitation-role-event cfg params invitation))
|
||||
|
||||
;; Deferred past the commit: the member's sessions read the team
|
||||
;; again when told, and a rolled back change must not be announced.
|
||||
|
||||
@ -396,6 +396,56 @@
|
||||
(mapv :name (:events params)))))
|
||||
(t/is (= 0 (unread-count consumer/admin-console)))))))
|
||||
|
||||
(t/deftest consumer-admin-console-forwards-organization-role-events
|
||||
;; The rework of the consumers (#12701) dropped these two names that
|
||||
;; #13163 had added: the admin console lost every org role change and
|
||||
;; every team that passed to the organization owner.
|
||||
(let [prof (th/create-profile* 14 {:is-active true})]
|
||||
(with-redefs [cf/flags #{:admin-console}]
|
||||
(with-mocks [admin-console-mock {:target 'app.nitrate/call :return nil}]
|
||||
(insert-audit-row (:id prof) "update-organization-member-role")
|
||||
(insert-audit-row (:id prof) "transfer-team-ownership")
|
||||
(insert-audit-row (:id prof) "unrelated-event")
|
||||
(run-admin-console)
|
||||
(t/is (:called? @admin-console-mock))
|
||||
(let [[_ _ params] (:call-args @admin-console-mock)]
|
||||
(t/is (= ["update-organization-member-role"
|
||||
"transfer-team-ownership"]
|
||||
(mapv :name (:events params)))))
|
||||
(t/is (= 0 (unread-count consumer/admin-console)))))))
|
||||
|
||||
(t/deftest consumer-admin-console-names-the-member-of-an-org-role-change
|
||||
;; The org role entry names its member (#873). Nitrate sends the id; the
|
||||
;; name is filled here, where the profile is loaded anyway, so the role
|
||||
;; change never waits on a lookup. Only that event gets the name.
|
||||
(let [actor (th/create-profile* 15 {:is-active true :fullname "Org Owner"})
|
||||
member (th/create-profile* 16 {:is-active true :fullname "Bea Ortiz"})
|
||||
other (th/create-profile* 17 {:is-active true :fullname "Ana Ruiz"})]
|
||||
(with-redefs [cf/flags #{:admin-console}]
|
||||
(with-mocks [admin-console-mock {:target 'app.nitrate/call :return nil}]
|
||||
(insert-audit-row (:id actor) "update-organization-member-role"
|
||||
{:member-id (str (:id member))
|
||||
:previous-value "viewer"
|
||||
:new-value "editor"})
|
||||
(insert-audit-row (:id actor) "update-organization-member-role"
|
||||
{:member-id (str (:id member))
|
||||
:member-name "Already named"})
|
||||
;; Its email already set, this member is loaded for the name alone.
|
||||
(insert-audit-row (:id actor) "update-organization-member-role"
|
||||
{:member-id (str (:id other))
|
||||
:member-email "kept@example.com"})
|
||||
(insert-audit-row (:id actor) "create-organization-invitation"
|
||||
{:member-id (str (:id member))})
|
||||
(run-admin-console)
|
||||
(let [[_ _ params] (:call-args @admin-console-mock)
|
||||
[named kept with-email invite] (mapv :props (:events params))]
|
||||
(t/is (= "Bea Ortiz" (:member-name named)))
|
||||
(t/is (= (:email member) (:member-email named)))
|
||||
(t/is (= "Already named" (:member-name kept)))
|
||||
(t/is (= "Ana Ruiz" (:member-name with-email)))
|
||||
(t/is (= "kept@example.com" (:member-email with-email)))
|
||||
(t/is (not (contains? invite :member-name))))))))
|
||||
|
||||
(t/deftest consumer-admin-console-advances-over-non-matching-rows
|
||||
;; A selective consumer that finds nothing must still move: otherwise
|
||||
;; the GC would wait for it forever and the buffer would grow.
|
||||
|
||||
@ -15,6 +15,7 @@
|
||||
[app.email :as eml]
|
||||
[app.http :as-alias http]
|
||||
[app.jobs :as jobs]
|
||||
[app.loggers.audit :as audit]
|
||||
[app.media.svg :as svg]
|
||||
[app.msgbus :as mbus]
|
||||
[app.nitrate :as nitrate]
|
||||
@ -2645,6 +2646,71 @@
|
||||
(filter :new-owner-name)
|
||||
(map #(select-keys % [:team-name :new-owner-name])))))))))
|
||||
|
||||
(def ^:private real-audit-submit audit/submit)
|
||||
|
||||
(t/deftest set-organization-member-viewer-records-the-invitations-it-downgrades
|
||||
;; A pending invitation whose role the organization owner changes by hand
|
||||
;; leaves an `update-team-invitation-role` event; the ones this command
|
||||
;; turns into viewer invitations must leave the same event, tagged with
|
||||
;; the organization so a team moved before archiving cannot misroute it.
|
||||
(with-mocks [nitrate-mock {:target 'app.nitrate/call :return nil}
|
||||
mbus-mock {:target 'app.msgbus/pub! :return nil}]
|
||||
(with-redefs [audit/submit real-audit-submit
|
||||
cf/audit-persist? true]
|
||||
(let [org-id (uuid/random)
|
||||
owner (th/create-profile* 1 {:is-active true})
|
||||
member (th/create-profile* 2 {:is-active true :email "member@example.com"})
|
||||
other (th/create-profile* 3 {:is-active true})
|
||||
editor-team (th/create-team* 1 {:profile-id (:id other)})
|
||||
admin-team (th/create-team* 2 {:profile-id (:id other)})
|
||||
viewer-team (th/create-team* 3 {:profile-id (:id other)})
|
||||
outside-team (th/create-team* 4 {:profile-id (:id other)})
|
||||
_ (insert-team-invitation (:id editor-team) "member@example.com" "editor" (:id other))
|
||||
_ (insert-team-invitation (:id admin-team) "member@example.com" "admin" (:id other))
|
||||
_ (insert-team-invitation (:id viewer-team) "member@example.com" "viewer" (:id other))
|
||||
_ (insert-team-invitation (:id outside-team) "member@example.com" "editor" (:id other))
|
||||
out (th/management-command! {::th/type :set-organization-member-viewer
|
||||
::rpc/profile-id (:id owner)
|
||||
:profile-id (:id member)
|
||||
:organization-owner-id (:id owner)
|
||||
:organization-id org-id
|
||||
:team-ids (mapv :id [editor-team admin-team viewer-team])})
|
||||
events (->> (th/db-exec! ["SELECT * FROM audit_log WHERE name = 'update-team-invitation-role'"])
|
||||
(mapv #(update % :props db/decode-transit-pgobject)))]
|
||||
|
||||
(t/is (th/success? out))
|
||||
(t/is (= 2 (count events)))
|
||||
(t/is (every? #(= (:id owner) (:profile-id %)) events))
|
||||
(t/is (= #{{:organization-id org-id :team-id (:id editor-team)
|
||||
:email "member@example.com" :role :viewer}
|
||||
{:organization-id org-id :team-id (:id admin-team)
|
||||
:email "member@example.com" :role :viewer}}
|
||||
(into #{} (map :props) events)))))))
|
||||
|
||||
(t/deftest set-organization-member-viewer-records-invitations-without-an-organization
|
||||
;; An older Nitrate does not send the organization: the event keeps the
|
||||
;; props of the change made by hand.
|
||||
(with-mocks [nitrate-mock {:target 'app.nitrate/call :return nil}
|
||||
mbus-mock {:target 'app.msgbus/pub! :return nil}]
|
||||
(with-redefs [audit/submit real-audit-submit
|
||||
cf/audit-persist? true]
|
||||
(let [owner (th/create-profile* 1 {:is-active true})
|
||||
member (th/create-profile* 2 {:is-active true :email "member@example.com"})
|
||||
other (th/create-profile* 3 {:is-active true})
|
||||
team (th/create-team* 1 {:profile-id (:id other)})
|
||||
_ (insert-team-invitation (:id team) "member@example.com" "editor" (:id other))
|
||||
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 team)]})
|
||||
events (->> (th/db-exec! ["SELECT * FROM audit_log WHERE name = 'update-team-invitation-role'"])
|
||||
(mapv #(update % :props db/decode-transit-pgobject)))]
|
||||
|
||||
(t/is (th/success? out))
|
||||
(t/is (= [{:team-id (:id team) :email "member@example.com" :role :viewer}]
|
||||
(mapv :props events)))))))
|
||||
|
||||
(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}]
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user