feat(core-shared): add isAdmin access helper
This commit is contained in:
20
packages/core-shared/src/payload/access/is-admin.test.ts
Normal file
20
packages/core-shared/src/payload/access/is-admin.test.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { isAdmin } from "./is-admin";
|
||||
|
||||
describe("isAdmin", () => {
|
||||
it("returns true when user role is 'admin'", () => {
|
||||
expect(isAdmin({ req: { user: { role: "admin" } } })).toBe(true);
|
||||
});
|
||||
|
||||
it("returns false when user role is not 'admin'", () => {
|
||||
expect(isAdmin({ req: { user: { role: "editor" } } })).toBe(false);
|
||||
});
|
||||
|
||||
it("returns false when user has no role", () => {
|
||||
expect(isAdmin({ req: { user: {} } })).toBe(false);
|
||||
});
|
||||
|
||||
it("returns false when there is no user", () => {
|
||||
expect(isAdmin({ req: {} })).toBe(false);
|
||||
});
|
||||
});
|
||||
7
packages/core-shared/src/payload/access/is-admin.ts
Normal file
7
packages/core-shared/src/payload/access/is-admin.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
export function isAdmin({
|
||||
req,
|
||||
}: {
|
||||
req: { user?: { role?: string } };
|
||||
}): boolean {
|
||||
return req.user?.role === "admin";
|
||||
}
|
||||
Reference in New Issue
Block a user