move permission seed and provide autocomplete
This commit is contained in:
parent
6cadcd28a6
commit
a794342480
|
|
@ -1,87 +1,10 @@
|
||||||
import { Permission, PrismaClient } from "@prisma/client";
|
import permissionData from "../../src/modules/permission/data/initialPermissions";
|
||||||
|
import { PrismaClient } from "@prisma/client";
|
||||||
import { log } from "console";
|
import { log } from "console";
|
||||||
|
|
||||||
export default async function permissionSeed(prisma: PrismaClient) {
|
export default async function permissionSeed(prisma: PrismaClient) {
|
||||||
|
|
||||||
log("Seeding permissions...")
|
log("Seeding permissions...")
|
||||||
|
|
||||||
const permissionData: Omit<Permission, "id">[] = [
|
|
||||||
// Permission group
|
|
||||||
{
|
|
||||||
code: "permissions.read",
|
|
||||||
name: "Read permission",
|
|
||||||
description: "Allows reading a single permission",
|
|
||||||
isActive: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
code: "permissions.readAll",
|
|
||||||
name: "Read all permissions",
|
|
||||||
description: "Allows reading all permissions",
|
|
||||||
isActive: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
code: "permissions.update",
|
|
||||||
name: "Update permission",
|
|
||||||
description: "Allows updating a permission",
|
|
||||||
isActive: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
code: "permissions.delete",
|
|
||||||
name: "Delete permission",
|
|
||||||
description: "Allows deleting a permission",
|
|
||||||
isActive: true,
|
|
||||||
},
|
|
||||||
// Role group
|
|
||||||
{
|
|
||||||
code: "roles.read",
|
|
||||||
name: "Read role",
|
|
||||||
description: "Allows reading a single role",
|
|
||||||
isActive: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
code: "roles.readAll",
|
|
||||||
name: "Read all roles",
|
|
||||||
description: "Allows reading all roles",
|
|
||||||
isActive: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
code: "roles.update",
|
|
||||||
name: "Update role",
|
|
||||||
description: "Allows updating a role",
|
|
||||||
isActive: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
code: "roles.delete",
|
|
||||||
name: "Delete role",
|
|
||||||
description: "Allows deleting a role",
|
|
||||||
isActive: true,
|
|
||||||
},
|
|
||||||
// User group
|
|
||||||
{
|
|
||||||
code: "users.read",
|
|
||||||
name: "Read user",
|
|
||||||
description: "Allows reading a single user",
|
|
||||||
isActive: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
code: "users.readAll",
|
|
||||||
name: "Read all users",
|
|
||||||
description: "Allows reading all users",
|
|
||||||
isActive: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
code: "users.update",
|
|
||||||
name: "Update user",
|
|
||||||
description: "Allows updating a user",
|
|
||||||
isActive: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
code: "users.delete",
|
|
||||||
name: "Delete user",
|
|
||||||
description: "Allows deleting a user",
|
|
||||||
isActive: true,
|
|
||||||
},
|
|
||||||
];
|
|
||||||
|
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
permissionData.map(async (permission) => {
|
permissionData.map(async (permission) => {
|
||||||
|
|
|
||||||
40
src/modules/auth/utils/checkMultiplePermissions.ts
Normal file
40
src/modules/auth/utils/checkMultiplePermissions.ts
Normal file
|
|
@ -0,0 +1,40 @@
|
||||||
|
import checkPermission from "@/modules/dashboard/services/checkPermission";
|
||||||
|
import getCurrentUser from "./getCurrentUser";
|
||||||
|
import { PermissionCode } from "@/modules/permission/data/initialPermissions";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks multiple permissions for the current user and returns an object indicating
|
||||||
|
* whether each permission is granted.
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @param permissions - An object with keys as permission names and values as the required roles/permissions.
|
||||||
|
* @returns An object with keys as permission names and boolean values indicating whether the permission is granted.
|
||||||
|
*/
|
||||||
|
async function checkMultiplePermissions<
|
||||||
|
T extends Record<
|
||||||
|
string,
|
||||||
|
| "guest-only"
|
||||||
|
| "authenticated-only"
|
||||||
|
| "*"
|
||||||
|
| PermissionCode
|
||||||
|
| (string & {})
|
||||||
|
>
|
||||||
|
>(permissions: T): Promise<{ [K in keyof T]: boolean }> {
|
||||||
|
const permissionResults: Partial<{ [K in keyof T]: boolean }> = {};
|
||||||
|
const currentUser = await getCurrentUser();
|
||||||
|
|
||||||
|
for (const permissionKey in permissions) {
|
||||||
|
if (permissions.hasOwnProperty(permissionKey)) {
|
||||||
|
const requiredPermission = permissions[permissionKey];
|
||||||
|
const isPermissionGranted = await checkPermission(
|
||||||
|
requiredPermission,
|
||||||
|
currentUser
|
||||||
|
);
|
||||||
|
permissionResults[permissionKey] = isPermissionGranted;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return permissionResults as { [K in keyof T]: boolean };
|
||||||
|
}
|
||||||
|
|
||||||
|
export default checkMultiplePermissions;
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
import getCurrentUser from "./getCurrentUser";
|
import getCurrentUser from "./getCurrentUser";
|
||||||
import "server-only";
|
import "server-only";
|
||||||
import getUserPermissions from "./getUserPermissions";
|
import getUserPermissions from "./getUserPermissions";
|
||||||
|
import { PermissionCode } from "@/modules/permission/data/initialPermissions";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Deprecated. Use dashboard service instead
|
* Deprecated. Use dashboard service instead
|
||||||
|
|
@ -12,7 +13,7 @@ import getUserPermissions from "./getUserPermissions";
|
||||||
* @returns true if the user has the required permission, otherwise false.
|
* @returns true if the user has the required permission, otherwise false.
|
||||||
*/
|
*/
|
||||||
export default async function checkPermission(
|
export default async function checkPermission(
|
||||||
permission: "guest-only" | "authenticated-only" | "*" | (string & {}),
|
permission: "guest-only" | "authenticated-only" | "*" | PermissionCode | (string & {}),
|
||||||
currentUser?: Awaited<ReturnType<typeof getCurrentUser>>
|
currentUser?: Awaited<ReturnType<typeof getCurrentUser>>
|
||||||
): Promise<boolean> {
|
): Promise<boolean> {
|
||||||
// Allow if no specific permission is required.
|
// Allow if no specific permission is required.
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,10 @@ import getCurrentUser from "@/modules/auth/utils/getCurrentUser";
|
||||||
/**
|
/**
|
||||||
* Checks multiple permissions for the current user and returns an object indicating
|
* Checks multiple permissions for the current user and returns an object indicating
|
||||||
* whether each permission is granted.
|
* whether each permission is granted.
|
||||||
|
*
|
||||||
|
* Deprecated. Moved into Auth module
|
||||||
*
|
*
|
||||||
|
* @deprecated
|
||||||
* @param permissions - An object with keys as permission names and values as the required roles/permissions.
|
* @param permissions - An object with keys as permission names and values as the required roles/permissions.
|
||||||
* @returns An object with keys as permission names and boolean values indicating whether the permission is granted.
|
* @returns An object with keys as permission names and boolean values indicating whether the permission is granted.
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
91
src/modules/permission/data/initialPermissions.ts
Normal file
91
src/modules/permission/data/initialPermissions.ts
Normal file
|
|
@ -0,0 +1,91 @@
|
||||||
|
import { Permission } from "@prisma/client";
|
||||||
|
|
||||||
|
const permissionData = [
|
||||||
|
// Permission group
|
||||||
|
{
|
||||||
|
code: "permissions.read",
|
||||||
|
name: "Read permission",
|
||||||
|
description: "Allows reading a single permission",
|
||||||
|
isActive: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
code: "permissions.readAll",
|
||||||
|
name: "Read all permissions",
|
||||||
|
description: "Allows reading all permissions",
|
||||||
|
isActive: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
code: "permissions.update",
|
||||||
|
name: "Update permission",
|
||||||
|
description: "Allows updating a permission",
|
||||||
|
isActive: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
code: "permissions.delete",
|
||||||
|
name: "Delete permission",
|
||||||
|
description: "Allows deleting a permission",
|
||||||
|
isActive: true,
|
||||||
|
},
|
||||||
|
// Role group
|
||||||
|
{
|
||||||
|
code: "roles.read",
|
||||||
|
name: "Read role",
|
||||||
|
description: "Allows reading a single role",
|
||||||
|
isActive: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
code: "roles.readAll",
|
||||||
|
name: "Read all roles",
|
||||||
|
description: "Allows reading all roles",
|
||||||
|
isActive: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
code: "roles.update",
|
||||||
|
name: "Update role",
|
||||||
|
description: "Allows updating a role",
|
||||||
|
isActive: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
code: "roles.delete",
|
||||||
|
name: "Delete role",
|
||||||
|
description: "Allows deleting a role",
|
||||||
|
isActive: true,
|
||||||
|
},
|
||||||
|
// User group
|
||||||
|
{
|
||||||
|
code: "users.read",
|
||||||
|
name: "Read user",
|
||||||
|
description: "Allows reading a single user",
|
||||||
|
isActive: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
code: "users.readAll",
|
||||||
|
name: "Read all users",
|
||||||
|
description: "Allows reading all users",
|
||||||
|
isActive: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
code: "users.update",
|
||||||
|
name: "Update user",
|
||||||
|
description: "Allows updating a user",
|
||||||
|
isActive: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
code: "users.delete",
|
||||||
|
name: "Delete user",
|
||||||
|
description: "Allows deleting a user",
|
||||||
|
isActive: true,
|
||||||
|
},
|
||||||
|
|
||||||
|
//Promos
|
||||||
|
{
|
||||||
|
code: "promos.readAll",
|
||||||
|
name: "Read all promos",
|
||||||
|
}
|
||||||
|
] as const;
|
||||||
|
|
||||||
|
export type PermissionCode = (typeof permissionData)[number]['code'];
|
||||||
|
|
||||||
|
const exportedPermissionData = permissionData as unknown as Omit<Permission, "id">[];
|
||||||
|
|
||||||
|
export default exportedPermissionData
|
||||||
Loading…
Reference in New Issue
Block a user