feat: add colorscheme to user in db (#987)
This commit is contained in:
@@ -4,7 +4,7 @@ import type { NextAuthConfig } from "next-auth";
|
||||
|
||||
import type { Database } from "@homarr/db";
|
||||
import { eq, inArray } from "@homarr/db";
|
||||
import { groupMembers, groupPermissions } from "@homarr/db/schema/sqlite";
|
||||
import { groupMembers, groupPermissions, users } from "@homarr/db/schema/sqlite";
|
||||
import { getPermissionsWithChildren } from "@homarr/definitions";
|
||||
|
||||
import { env } from "./env.mjs";
|
||||
@@ -31,10 +31,18 @@ export const getCurrentUserPermissionsAsync = async (db: Database, userId: strin
|
||||
|
||||
export const createSessionCallback = (db: Database): NextAuthCallbackOf<"session"> => {
|
||||
return async ({ session, user }) => {
|
||||
const additionalProperties = await db.query.users.findFirst({
|
||||
where: eq(users.id, user.id),
|
||||
columns: {
|
||||
colorScheme: true,
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
...session,
|
||||
user: {
|
||||
...session.user,
|
||||
...additionalProperties,
|
||||
id: user.id,
|
||||
name: user.name,
|
||||
permissions: await getCurrentUserPermissionsAsync(db, user.id),
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { headers } from "next/headers";
|
||||
import type { DefaultSession } from "@auth/core/types";
|
||||
|
||||
import type { GroupPermissionKey } from "@homarr/definitions";
|
||||
import type { ColorScheme, GroupPermissionKey } from "@homarr/definitions";
|
||||
|
||||
import { createConfiguration } from "./configuration";
|
||||
|
||||
@@ -12,6 +12,7 @@ declare module "next-auth" {
|
||||
user: {
|
||||
id: string;
|
||||
permissions: GroupPermissionKey[];
|
||||
colorScheme: ColorScheme;
|
||||
} & DefaultSession["user"];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ describe("constructBoardPermissions", () => {
|
||||
user: {
|
||||
id: "1",
|
||||
permissions: [],
|
||||
colorScheme: "light",
|
||||
},
|
||||
expires: new Date().toISOString(),
|
||||
} satisfies Session;
|
||||
@@ -47,6 +48,7 @@ describe("constructBoardPermissions", () => {
|
||||
user: {
|
||||
id: "2",
|
||||
permissions: getPermissionsWithChildren(["board-full-all"]),
|
||||
colorScheme: "light",
|
||||
},
|
||||
expires: new Date().toISOString(),
|
||||
} satisfies Session;
|
||||
@@ -74,6 +76,7 @@ describe("constructBoardPermissions", () => {
|
||||
user: {
|
||||
id: "2",
|
||||
permissions: getPermissionsWithChildren(["board-modify-all"]),
|
||||
colorScheme: "light",
|
||||
},
|
||||
expires: new Date().toISOString(),
|
||||
} satisfies Session;
|
||||
@@ -102,6 +105,7 @@ describe("constructBoardPermissions", () => {
|
||||
user: {
|
||||
id: "2",
|
||||
permissions: [],
|
||||
colorScheme: "light",
|
||||
},
|
||||
expires: new Date().toISOString(),
|
||||
} satisfies Session;
|
||||
@@ -129,6 +133,7 @@ describe("constructBoardPermissions", () => {
|
||||
user: {
|
||||
id: "2",
|
||||
permissions: [],
|
||||
colorScheme: "light",
|
||||
},
|
||||
expires: new Date().toISOString(),
|
||||
} satisfies Session;
|
||||
@@ -156,6 +161,7 @@ describe("constructBoardPermissions", () => {
|
||||
user: {
|
||||
id: "2",
|
||||
permissions: getPermissionsWithChildren(["board-view-all"]),
|
||||
colorScheme: "light",
|
||||
},
|
||||
expires: new Date().toISOString(),
|
||||
} satisfies Session;
|
||||
@@ -183,6 +189,7 @@ describe("constructBoardPermissions", () => {
|
||||
user: {
|
||||
id: "2",
|
||||
permissions: [],
|
||||
colorScheme: "light",
|
||||
},
|
||||
expires: new Date().toISOString(),
|
||||
} satisfies Session;
|
||||
@@ -210,6 +217,7 @@ describe("constructBoardPermissions", () => {
|
||||
user: {
|
||||
id: "2",
|
||||
permissions: [],
|
||||
colorScheme: "light",
|
||||
},
|
||||
expires: new Date().toISOString(),
|
||||
} satisfies Session;
|
||||
@@ -237,6 +245,7 @@ describe("constructBoardPermissions", () => {
|
||||
user: {
|
||||
id: "2",
|
||||
permissions: [],
|
||||
colorScheme: "light",
|
||||
},
|
||||
expires: new Date().toISOString(),
|
||||
} satisfies Session;
|
||||
@@ -264,6 +273,7 @@ describe("constructBoardPermissions", () => {
|
||||
user: {
|
||||
id: "2",
|
||||
permissions: [],
|
||||
colorScheme: "light",
|
||||
},
|
||||
expires: new Date().toISOString(),
|
||||
} satisfies Session;
|
||||
|
||||
@@ -16,6 +16,7 @@ describe("constructIntegrationPermissions", () => {
|
||||
user: {
|
||||
id: "2",
|
||||
permissions: getPermissionsWithChildren(["integration-full-all"]),
|
||||
colorScheme: "light",
|
||||
},
|
||||
expires: new Date().toISOString(),
|
||||
} satisfies Session;
|
||||
@@ -39,6 +40,7 @@ describe("constructIntegrationPermissions", () => {
|
||||
user: {
|
||||
id: "2",
|
||||
permissions: getPermissionsWithChildren(["integration-interact-all"]),
|
||||
colorScheme: "light",
|
||||
},
|
||||
expires: new Date().toISOString(),
|
||||
} satisfies Session;
|
||||
@@ -62,6 +64,7 @@ describe("constructIntegrationPermissions", () => {
|
||||
user: {
|
||||
id: "2",
|
||||
permissions: [],
|
||||
colorScheme: "light",
|
||||
},
|
||||
expires: new Date().toISOString(),
|
||||
} satisfies Session;
|
||||
@@ -85,6 +88,7 @@ describe("constructIntegrationPermissions", () => {
|
||||
user: {
|
||||
id: "2",
|
||||
permissions: [],
|
||||
colorScheme: "light",
|
||||
},
|
||||
expires: new Date().toISOString(),
|
||||
} satisfies Session;
|
||||
@@ -108,6 +112,7 @@ describe("constructIntegrationPermissions", () => {
|
||||
user: {
|
||||
id: "2",
|
||||
permissions: getPermissionsWithChildren(["integration-use-all"]),
|
||||
colorScheme: "light",
|
||||
},
|
||||
expires: new Date().toISOString(),
|
||||
} satisfies Session;
|
||||
@@ -131,6 +136,7 @@ describe("constructIntegrationPermissions", () => {
|
||||
user: {
|
||||
id: "2",
|
||||
permissions: [],
|
||||
colorScheme: "light",
|
||||
},
|
||||
expires: new Date().toISOString(),
|
||||
} satisfies Session;
|
||||
@@ -154,6 +160,7 @@ describe("constructIntegrationPermissions", () => {
|
||||
user: {
|
||||
id: "2",
|
||||
permissions: [],
|
||||
colorScheme: "light",
|
||||
},
|
||||
expires: new Date().toISOString(),
|
||||
} satisfies Session;
|
||||
@@ -177,6 +184,7 @@ describe("constructIntegrationPermissions", () => {
|
||||
user: {
|
||||
id: "2",
|
||||
permissions: [],
|
||||
colorScheme: "light",
|
||||
},
|
||||
expires: new Date().toISOString(),
|
||||
} satisfies Session;
|
||||
@@ -190,40 +198,3 @@ describe("constructIntegrationPermissions", () => {
|
||||
expect(result.hasUseAccess).toBe(false);
|
||||
});
|
||||
});
|
||||
/*
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
test("should return hasViewAccess as true when board is public", () => {
|
||||
// Arrange
|
||||
const board = {
|
||||
creator: {
|
||||
id: "1",
|
||||
},
|
||||
userPermissions: [],
|
||||
groupPermissions: [],
|
||||
isPublic: true,
|
||||
};
|
||||
const session = {
|
||||
user: {
|
||||
id: "2",
|
||||
permissions: [],
|
||||
},
|
||||
expires: new Date().toISOString(),
|
||||
} satisfies Session;
|
||||
|
||||
// Act
|
||||
const result = constructBoardPermissions(board, session);
|
||||
|
||||
// Assert
|
||||
expect(result.hasFullAccess).toBe(false);
|
||||
expect(result.hasChangeAccess).toBe(false);
|
||||
expect(result.hasViewAccess).toBe(true);
|
||||
});
|
||||
});
|
||||
*/
|
||||
|
||||
@@ -20,6 +20,7 @@ const createSession = (user: Partial<Session["user"]>): Session => ({
|
||||
user: {
|
||||
id: "1",
|
||||
permissions: [],
|
||||
colorScheme: "light",
|
||||
...user,
|
||||
},
|
||||
expires: new Date().toISOString(),
|
||||
|
||||
@@ -32,6 +32,7 @@ export const getSessionFromTokenAsync = async (db: Database, token: string | und
|
||||
name: true,
|
||||
email: true,
|
||||
image: true,
|
||||
colorScheme: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
@@ -101,6 +101,7 @@ describe("session callback", () => {
|
||||
email: "no-email",
|
||||
emailVerified: new Date("2023-01-13"),
|
||||
permissions: [],
|
||||
colorScheme: "dark",
|
||||
},
|
||||
expires: "2023-01-13" as Date & string,
|
||||
sessionToken: "token",
|
||||
|
||||
Reference in New Issue
Block a user