fix: rename checkPermission to be more fitting

This commit is contained in:
sean-brydon 2024-01-05 16:09:57 +10:00
parent 0dddc2224a
commit dd0fd5b5cc
2 changed files with 6 additions and 7 deletions

View File

@ -7,7 +7,7 @@ import {
parseTeamId,
checkSelfImpersonation,
checkUserIdentifier,
checkPermission,
checkGlobalPermission,
} from "./ImpersonationProvider";
const session: Session = {
@ -65,17 +65,17 @@ describe("checkUserIdentifier", () => {
describe("checkPermission", () => {
it("should throw an error if the user is not an admin and team impersonation is disabled", () => {
process.env.NEXT_PUBLIC_TEAM_IMPERSONATION = "false";
expect(() => checkPermission(session)).toThrow();
expect(() => checkGlobalPermission(session)).toThrow();
});
it("should not throw an error if the user is an admin and team impersonation is disabled", () => {
const modifiedSession = { ...session, user: { ...session.user, role: UserPermissionRole.ADMIN } };
process.env.NEXT_PUBLIC_TEAM_IMPERSONATION = "false";
expect(() => checkPermission(modifiedSession)).not.toThrow();
expect(() => checkGlobalPermission(modifiedSession)).not.toThrow();
});
it("should not throw an error if the user is not an admin but team impersonation is enabled", () => {
process.env.NEXT_PUBLIC_TEAM_IMPERSONATION = "true";
expect(() => checkPermission(session)).not.toThrow();
expect(() => checkGlobalPermission(session)).not.toThrow();
});
});

View File

@ -63,7 +63,7 @@ export function checkUserIdentifier(creds: Partial<Credentials>) {
if (!creds?.username) throw new Error("User identifier must be present");
}
export function checkPermission(session: Session | null) {
export function checkGlobalPermission(session: Session | null) {
if (
(session?.user.role !== "ADMIN" && process.env.NEXT_PUBLIC_TEAM_IMPERSONATION === "false") ||
!session?.user
@ -148,8 +148,7 @@ const ImpersonationProvider = CredentialsProvider({
const teamId = parseTeamId(creds);
checkSelfImpersonation(session, creds);
checkUserIdentifier(creds);
checkPermission(session);
checkGlobalPermission(session);
const impersonatedUser = await getImpersonatedUser({ session, teamId, creds });
if (session?.user.role === "ADMIN") {