Skip to content

Commit

Permalink
Release 0.8.0
Browse files Browse the repository at this point in the history
  • Loading branch information
dennemark committed Oct 18, 2024
1 parent 37f14f8 commit b8fc84e
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 21 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@


## [0.8.0](https://github.com/dennemark/prisma-extension-casl/compare/0.7.6...0.8.0) (2024-10-18)


### Features

* :sparkles: allow querying additional ability actions ([37f14f8](https://github.com/dennemark/prisma-extension-casl/commit/37f14f8a93983db3d507fcf4c20003ab551e787c))

## [0.7.6](https://github.com/dennemark/prisma-extension-casl/compare/0.7.5...0.7.6) (2024-10-15)

## [0.7.5](https://github.com/dennemark/prisma-extension-casl/compare/0.7.4...0.7.5) (2024-10-15)
Expand Down
30 changes: 29 additions & 1 deletion dist/index.d.mts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@ type CreationTree = {
}[];
};

type PrismaExtensionCaslOptions = {
/**
* will add a field on each returned prisma result that stores allowed actions on result (not nested)
* so instead of { id: 0 } it would return { id: 0, [permissionField]: ['create', 'read', 'update', 'delete'] }
*
* to return other actions, please use addPermissionActions
*/
permissionField?: string;
/**
* adds additional permission actions to ['create', 'read', 'update', 'delete']
* that should be returned if permissionField is used.
*/
addPermissionActions?: string[];
};
type PrismaCaslOperation = 'create' | 'createMany' | 'createManyAndReturn' | 'upsert' | 'findFirst' | 'findFirstOrThrow' | 'findMany' | 'findUnique' | 'findUniqueOrThrow' | 'aggregate' | 'count' | 'groupBy' | 'update' | 'updateMany' | 'delete' | 'deleteMany';

/**
Expand Down Expand Up @@ -55,7 +69,21 @@ declare function applyCaslToQuery(operation: PrismaCaslOperation, args: any, abi
* - this is a function call to instantiate abilities on each prisma query to allow adding i.e. context or claims
* @returns enriched prisma client
*/
declare function useCaslAbilities(getAbilityFactory: () => AbilityBuilder<PureAbility<AbilityTuple, PrismaQuery>>, permissionField?: string): (client: any) => {
/**
* enrich a prisma client to check for CASL abilities even in nested queries
*
* `client.$extends(useCaslAbilities(build))`
*
* https://casl.js.org/v6/en/package/casl-prisma
*
*
* @param getAbilityFactory function to return CASL prisma abilities
* - this is a function call to instantiate abilities on each prisma query to allow adding i.e. context or claims
* @param opts additional options: { permissionField, additionalActions }
* @returns enriched prisma client
* @returns
*/
declare function useCaslAbilities(getAbilityFactory: () => AbilityBuilder<PureAbility<AbilityTuple, PrismaQuery>>, opts?: PrismaExtensionCaslOptions): (client: any) => {
$extends: {
extArgs: {
result: {};
Expand Down
30 changes: 29 additions & 1 deletion dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@ type CreationTree = {
}[];
};

type PrismaExtensionCaslOptions = {
/**
* will add a field on each returned prisma result that stores allowed actions on result (not nested)
* so instead of { id: 0 } it would return { id: 0, [permissionField]: ['create', 'read', 'update', 'delete'] }
*
* to return other actions, please use addPermissionActions
*/
permissionField?: string;
/**
* adds additional permission actions to ['create', 'read', 'update', 'delete']
* that should be returned if permissionField is used.
*/
addPermissionActions?: string[];
};
type PrismaCaslOperation = 'create' | 'createMany' | 'createManyAndReturn' | 'upsert' | 'findFirst' | 'findFirstOrThrow' | 'findMany' | 'findUnique' | 'findUniqueOrThrow' | 'aggregate' | 'count' | 'groupBy' | 'update' | 'updateMany' | 'delete' | 'deleteMany';

/**
Expand Down Expand Up @@ -55,7 +69,21 @@ declare function applyCaslToQuery(operation: PrismaCaslOperation, args: any, abi
* - this is a function call to instantiate abilities on each prisma query to allow adding i.e. context or claims
* @returns enriched prisma client
*/
declare function useCaslAbilities(getAbilityFactory: () => AbilityBuilder<PureAbility<AbilityTuple, PrismaQuery>>, permissionField?: string): (client: any) => {
/**
* enrich a prisma client to check for CASL abilities even in nested queries
*
* `client.$extends(useCaslAbilities(build))`
*
* https://casl.js.org/v6/en/package/casl-prisma
*
*
* @param getAbilityFactory function to return CASL prisma abilities
* - this is a function call to instantiate abilities on each prisma query to allow adding i.e. context or claims
* @param opts additional options: { permissionField, additionalActions }
* @returns enriched prisma client
* @returns
*/
declare function useCaslAbilities(getAbilityFactory: () => AbilityBuilder<PureAbility<AbilityTuple, PrismaQuery>>, opts?: PrismaExtensionCaslOptions): (client: any) => {
$extends: {
extArgs: {
result: {};
Expand Down
19 changes: 10 additions & 9 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1337,11 +1337,12 @@ function applyCaslToQuery(operation, args, abilities, model, queryAllRuleRelatio
}

// src/storePermissions.ts
function storePermissions(result, abilities, model, prop) {
if (prop === void 0) {
function storePermissions(result, abilities, model, opts) {
if (!opts?.permissionField) {
return result;
}
const actions = ["create", "read", "update", "delete"];
const prop = opts.permissionField;
const actions = ["create", "read", "update", "delete", ...opts?.addPermissionActions ?? []];
const storeProp = (entry) => {
if (entry) {
entry[prop] = [];
Expand All @@ -1362,7 +1363,7 @@ function storePermissions(result, abilities, model, prop) {
}

// src/filterQueryResults.ts
function filterQueryResults(result, mask, creationTree, abilities, model, permissionField) {
function filterQueryResults(result, mask, creationTree, abilities, model, opts) {
if (typeof result === "number") {
return result;
}
Expand Down Expand Up @@ -1400,7 +1401,7 @@ function filterQueryResults(result, mask, creationTree, abilities, model, permis
}
const permittedFields = getPermittedFields(abilities, "read", model, entry);
let hasKeys = false;
Object.keys(entry).filter((field) => field !== permissionField).forEach((field) => {
Object.keys(entry).filter((field) => field !== opts?.permissionField).forEach((field) => {
const relationField = relationFieldsByModel[model][field];
if (relationField) {
const nestedCreationTree = creationTree && field in creationTree.children ? creationTree.children[field] : void 0;
Expand All @@ -1420,7 +1421,7 @@ function filterQueryResults(result, mask, creationTree, abilities, model, permis
});
return hasKeys && Object.keys(entry).length > 0 ? entry : null;
};
const permissionResult = storePermissions(result, abilities, model, permissionField);
const permissionResult = storePermissions(result, abilities, model, opts);
if (Array.isArray(permissionResult)) {
return permissionResult.map((entry) => filterPermittedFields(entry)).filter((x5) => x5);
} else {
Expand All @@ -1429,7 +1430,7 @@ function filterQueryResults(result, mask, creationTree, abilities, model, permis
}

// src/index.ts
function useCaslAbilities(getAbilityFactory, permissionField) {
function useCaslAbilities(getAbilityFactory, opts) {
return import_client2.Prisma.defineExtension((client) => {
const allOperations = (getAbilities) => ({
async $allOperations({ args, query, model, operation, ...rest }) {
Expand Down Expand Up @@ -1462,7 +1463,7 @@ function useCaslAbilities(getAbilityFactory, permissionField) {
perf?.mark("prisma-casl-extension-1");
function getCaslQuery() {
try {
return applyCaslToQuery(operation, args, abilities, model, permissionField ? true : false);
return applyCaslToQuery(operation, args, abilities, model, opts?.permissionField ? true : false);
} catch (e4) {
if (args.debugCasl || caslOperationDict[operation].action !== "read") {
throw e4;
Expand All @@ -1485,7 +1486,7 @@ function useCaslAbilities(getAbilityFactory, permissionField) {
if (fluentRelationModel && caslQuery.mask) {
caslQuery.mask = fluentRelationModel && fluentRelationModel in caslQuery.mask ? caslQuery.mask[fluentRelationModel] : {};
}
const filteredResult = filterQueryResults(result, caslQuery.mask, caslQuery.creationTree, abilities, fluentModel, permissionField);
const filteredResult = filterQueryResults(result, caslQuery.mask, caslQuery.creationTree, abilities, fluentModel, opts);
if (perf) {
perf.mark("prisma-casl-extension-4");
logger?.log(
Expand Down
19 changes: 10 additions & 9 deletions dist/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1312,11 +1312,12 @@ function applyCaslToQuery(operation, args, abilities, model, queryAllRuleRelatio
}

// src/storePermissions.ts
function storePermissions(result, abilities, model, prop) {
if (prop === void 0) {
function storePermissions(result, abilities, model, opts) {
if (!opts?.permissionField) {
return result;
}
const actions = ["create", "read", "update", "delete"];
const prop = opts.permissionField;
const actions = ["create", "read", "update", "delete", ...opts?.addPermissionActions ?? []];
const storeProp = (entry) => {
if (entry) {
entry[prop] = [];
Expand All @@ -1337,7 +1338,7 @@ function storePermissions(result, abilities, model, prop) {
}

// src/filterQueryResults.ts
function filterQueryResults(result, mask, creationTree, abilities, model, permissionField) {
function filterQueryResults(result, mask, creationTree, abilities, model, opts) {
if (typeof result === "number") {
return result;
}
Expand Down Expand Up @@ -1375,7 +1376,7 @@ function filterQueryResults(result, mask, creationTree, abilities, model, permis
}
const permittedFields = getPermittedFields(abilities, "read", model, entry);
let hasKeys = false;
Object.keys(entry).filter((field) => field !== permissionField).forEach((field) => {
Object.keys(entry).filter((field) => field !== opts?.permissionField).forEach((field) => {
const relationField = relationFieldsByModel[model][field];
if (relationField) {
const nestedCreationTree = creationTree && field in creationTree.children ? creationTree.children[field] : void 0;
Expand All @@ -1395,7 +1396,7 @@ function filterQueryResults(result, mask, creationTree, abilities, model, permis
});
return hasKeys && Object.keys(entry).length > 0 ? entry : null;
};
const permissionResult = storePermissions(result, abilities, model, permissionField);
const permissionResult = storePermissions(result, abilities, model, opts);
if (Array.isArray(permissionResult)) {
return permissionResult.map((entry) => filterPermittedFields(entry)).filter((x5) => x5);
} else {
Expand All @@ -1404,7 +1405,7 @@ function filterQueryResults(result, mask, creationTree, abilities, model, permis
}

// src/index.ts
function useCaslAbilities(getAbilityFactory, permissionField) {
function useCaslAbilities(getAbilityFactory, opts) {
return Prisma2.defineExtension((client) => {
const allOperations = (getAbilities) => ({
async $allOperations({ args, query, model, operation, ...rest }) {
Expand Down Expand Up @@ -1437,7 +1438,7 @@ function useCaslAbilities(getAbilityFactory, permissionField) {
perf?.mark("prisma-casl-extension-1");
function getCaslQuery() {
try {
return applyCaslToQuery(operation, args, abilities, model, permissionField ? true : false);
return applyCaslToQuery(operation, args, abilities, model, opts?.permissionField ? true : false);
} catch (e4) {
if (args.debugCasl || caslOperationDict[operation].action !== "read") {
throw e4;
Expand All @@ -1460,7 +1461,7 @@ function useCaslAbilities(getAbilityFactory, permissionField) {
if (fluentRelationModel && caslQuery.mask) {
caslQuery.mask = fluentRelationModel && fluentRelationModel in caslQuery.mask ? caslQuery.mask[fluentRelationModel] : {};
}
const filteredResult = filterQueryResults(result, caslQuery.mask, caslQuery.creationTree, abilities, fluentModel, permissionField);
const filteredResult = filterQueryResults(result, caslQuery.mask, caslQuery.creationTree, abilities, fluentModel, opts);
if (perf) {
perf.mark("prisma-casl-extension-4");
logger?.log(
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "prisma-extension-casl",
"version": "0.7.6",
"version": "0.8.0",
"description": "Enforce casl abilities on prisma client ",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down

0 comments on commit b8fc84e

Please sign in to comment.