Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

fix(admin): skip repeated bans and unchanged locales in the audit log

Tin7c0a5394bf6c
diff --git a/apps/api/src/admin/audit/audit-user-snapshot.ts b/apps/api/src/admin/audit/audit-user-snapshot.ts
index 3d8d109a9..a13ce4898 100644
--- a/apps/api/src/admin/audit/audit-user-snapshot.ts
+++ b/apps/api/src/admin/audit/audit-user-snapshot.ts
@@ -9,4 +9,5 @@ export type AuditUserSnapshot = {
banExpires: Date | null;
emailVerified: boolean;
image: string | null;
+ locale: string | null;
};
diff --git a/apps/api/src/admin/audit/capture-auth-admin-audit.ts b/apps/api/src/admin/audit/capture-auth-admin-audit.ts
index dabc8312d..0267845f2 100644
--- a/apps/api/src/admin/audit/capture-auth-admin-audit.ts
+++ b/apps/api/src/admin/audit/capture-auth-admin-audit.ts
@@ -47,6 +47,7 @@ const userColumns = {
banExpires: schema.userTable.banExpires,
emailVerified: schema.userTable.emailVerified,
image: schema.userTable.image,
+ locale: schema.userTable.locale,
};
async function findUser(userId: unknown) {
diff --git a/apps/api/src/admin/audit/describe-auth-admin-action.test.ts b/apps/api/src/admin/audit/describe-auth-admin-action.test.ts
index 1f2cdc4ba..25631618d 100644
--- a/apps/api/src/admin/audit/describe-auth-admin-action.test.ts
+++ b/apps/api/src/admin/audit/describe-auth-admin-action.test.ts
@@ -12,6 +12,7 @@ const target: AuditUserSnapshot = {
banExpires: null,
emailVerified: true,
image: null,
+ locale: "en-US",
};
const update = (data: Record<string, unknown>, returned: object) =>
@@ -59,6 +60,23 @@ describe("describeAuthAdminAction", () => {
]);
});
+ it("shows a verification cleared by an email change", () => {
+ expect(
+ update(
+ { email: "[email protected]" },
+ { email: "[email protected]", emailVerified: false },
+ ).map((entry) => entry.metadata),
+ ).toEqual([
+ {
+ fields: ["email", "emailVerified"],
+ changes: {
+ email: { from: "[email protected]", to: "[email protected]" },
+ emailVerified: { from: true, to: false },
+ },
+ },
+ ]);
+ });
+
it("treats a ban through update-user as a deactivation", () => {
expect(
update(
@@ -69,7 +87,15 @@ describe("describeAuthAdminAction", () => {
});
it("records nothing for unchanged updates, roles, or reactivations", () => {
- expect(update({ name: "Tom" }, {})).toEqual([]);
+ expect(update({ name: "Tom", locale: "en-US" }, {})).toEqual([]);
+ expect(
+ describeAuthAdminAction({
+ path: "/admin/ban-user",
+ body: { userId: "u1", banReason: "Spam" },
+ target: { ...target, banned: true, banReason: "Spam" },
+ returned: { user: { ...target, banned: true, banReason: "Spam" } },
+ }),
+ ).toEqual([]);
expect(
describeAuthAdminAction({
path: "/admin/unban-user",
diff --git a/apps/api/src/admin/audit/describe-auth-admin-action.ts b/apps/api/src/admin/audit/describe-auth-admin-action.ts
index 250e72297..012896c2b 100644
--- a/apps/api/src/admin/audit/describe-auth-admin-action.ts
+++ b/apps/api/src/admin/audit/describe-auth-admin-action.ts
@@ -43,7 +43,10 @@ function describeUpdate(
) {
const read = (source: object, key: string) =>
(source as Record<string, unknown>)[key];
- const changed = Object.keys(data).filter(
+ // Also compare fields hooks may change as a side effect, such as an email
+ // change clearing its verification.
+ const keys = new Set([...Object.keys(data), ...SHOWN_FIELDS]);
+ const changed = [...keys].filter(
(key) =>
key !== "updatedAt" && !same(read(target, key), read(updated, key)),
);
@@ -144,7 +147,9 @@ export function describeAuthAdminAction({
? []
: [about("user.role_changed", { from: target.role, to: user?.role })];
case "/admin/ban-user":
- return [about("user.deactivated", deactivation(user))];
+ return target.banned && same(deactivation(target), deactivation(user))
+ ? []
+ : [about("user.deactivated", deactivation(user))];
case "/admin/unban-user":
return target.banned ? [about("user.reactivated")] : [];
case "/admin/remove-user":
diff --git a/apps/api/src/admin/audit/record-auth-admin-audit.ts b/apps/api/src/admin/audit/record-auth-admin-audit.ts
index 9fb21a1e3..bd4ac3ded 100644
--- a/apps/api/src/admin/audit/record-auth-admin-audit.ts
+++ b/apps/api/src/admin/audit/record-auth-admin-audit.ts
@@ -26,7 +26,9 @@ export async function recordAuthAdminAudit(ctx: AuthContext) {
// The change already committed; failing the response would not undo it.
console.error("Admin audit entry could not be recorded:", {
action: entries[0]?.action,
+ // Not the message: database errors embed the query's parameters.
error: error instanceof Error ? error.name : typeof error,
+ code: (error as { cause?: { code?: unknown } })?.cause?.code,
});
}
}