jjbell170 / nest-casl

Casl integration for NestJS
MIT License
0 stars 0 forks source link

Sweep: Update the library to support string as the subject type. CASL by nature supports that. Currently, this library only accepts class types, making typescript complain when providing string types. #1

Open jjbell170 opened 9 months ago

jjbell170 commented 9 months ago

You can define permissions like:

export const userPermissions: Permissions<Roles, Subjects, Actions> = { everyone({ user, can }) { can(Actions.read, 'User', { userId: user.id }); }, }; However, the types of the @UseAbility decorator cannot take a string for the subject parameter so @UseAbility(Actions.read, 'User') shows a type error.

Fix this.

Checklist - [X] Modify `src/interfaces/permissions.interface.ts` ✓ https://github.com/jjbell170/nest-casl/commit/19e309ff8d02021413d8c14e8157aac668a04e43 [Edit](https://github.com/jjbell170/nest-casl/edit/sweep/update_the_library_to_support_string_as_66c1b/src/interfaces/permissions.interface.ts#L30-L41) - [X] Running GitHub Actions for `src/interfaces/permissions.interface.ts` ✓ [Edit](https://github.com/jjbell170/nest-casl/edit/sweep/update_the_library_to_support_string_as_66c1b/src/interfaces/permissions.interface.ts#L30-L41) - [X] Modify `src/__specs__/app/post/post.permissions.ts` ✓ https://github.com/jjbell170/nest-casl/commit/b1e0f7d0bfe0b6ed0c0f4ae822d15505b85c3df6 [Edit](https://github.com/jjbell170/nest-casl/edit/sweep/update_the_library_to_support_string_as_66c1b/src/__specs__/app/post/post.permissions.ts#L5-L5) - [X] Running GitHub Actions for `src/__specs__/app/post/post.permissions.ts` ✓ [Edit](https://github.com/jjbell170/nest-casl/edit/sweep/update_the_library_to_support_string_as_66c1b/src/__specs__/app/post/post.permissions.ts#L5-L5) - [X] Modify `README.md` ✓ https://github.com/jjbell170/nest-casl/commit/2e3085291cece35cc4cfd0008507d6b2b4a92354 [Edit](https://github.com/jjbell170/nest-casl/edit/sweep/update_the_library_to_support_string_as_66c1b/README.md#L88-L90) - [X] Running GitHub Actions for `README.md` ✓ [Edit](https://github.com/jjbell170/nest-casl/edit/sweep/update_the_library_to_support_string_as_66c1b/README.md#L88-L90) - [X] Modify `README.md` ✓ https://github.com/jjbell170/nest-casl/commit/59f26f15d22cf1f77e8603c98aa91cb644f06fee [Edit](https://github.com/jjbell170/nest-casl/edit/sweep/update_the_library_to_support_string_as_66c1b/README.md#L100-L106) - [X] Running GitHub Actions for `README.md` ✓ [Edit](https://github.com/jjbell170/nest-casl/edit/sweep/update_the_library_to_support_string_as_66c1b/README.md#L100-L106)
sweep-ai[bot] commented 9 months ago

🚀 Here's the PR! #5

See Sweep's progress at the progress dashboard!
Sweep Basic Tier: I'm using GPT-4. You have 4 GPT-4 tickets left for the month and 2 for the day. (tracking ID: 3494684b47)

For more GPT-4 tickets, visit our payment portal. For a one week free trial, try Sweep Pro (unlimited GPT-4 tickets).
Install Sweep Configs: Pull Request

[!TIP] I can email you next time I complete a pull request if you set up your email here!


Actions (click)

GitHub Actions✓

Here are the GitHub Actions logs prior to making any changes:

Sandbox logs for a02af35
Checking src/interfaces/permissions.interface.ts for syntax errors... ✅ src/interfaces/permissions.interface.ts has no syntax errors! 1/1 ✓
Checking src/interfaces/permissions.interface.ts for syntax errors...
✅ src/interfaces/permissions.interface.ts has no syntax errors!

Sandbox passed on the latest master, so sandbox checks will be enabled for this issue.


Step 1: 🔎 Searching

I found the following snippets in your repository. I will now analyze these snippets and come up with a plan.

Some code snippets I think are relevant in decreasing order of relevance (click to expand). If some file is missing from here, you can mention the path in the ticket description. https://github.com/jjbell170/nest-casl/blob/a02af356bb974a8bc67b0a2ba40a33039ade57b0/src/interfaces/permissions.interface.ts#L1-L48 https://github.com/jjbell170/nest-casl/blob/a02af356bb974a8bc67b0a2ba40a33039ade57b0/README.md#L77-L107 https://github.com/jjbell170/nest-casl/blob/a02af356bb974a8bc67b0a2ba40a33039ade57b0/README.md#L127-L171 https://github.com/jjbell170/nest-casl/blob/a02af356bb974a8bc67b0a2ba40a33039ade57b0/src/__specs__/app/post/post.permissions.ts#L1-L19

Step 2: ⌨️ Coding

--- 
+++ 
@@ -29,14 +29,14 @@
 }

 export type DefinePermissions<
-  Subjects extends Subject = Subject,
+  Subjects extends Subject | string = Subject | string,
   Actions extends string = DefaultActions,
   User extends AuthorizableUser = AuthorizableUser,
 > = (builder: UserAbilityBuilder) => void;

 export type Permissions<
   Roles extends string,
-  Subjects extends Subject = Subject,
+  Subjects extends Subject | string = Subject | string,
   Actions extends string = DefaultActions,
   User extends AuthorizableUser = AuthorizableUser,
 > = Partial>>;

Ran GitHub Actions for 19e309ff8d02021413d8c14e8157aac668a04e43:

--- 
+++ 
@@ -3,7 +3,7 @@
 import { Roles } from '../app.roles';
 import { Post } from './dtos/post.dto';

-type Subjects = InferSubjects;
+type Subjects = InferSubjects | string;

 export const permissions: Permissions = {
   everyone({ can }) {

Ran GitHub Actions for b1e0f7d0bfe0b6ed0c0f4ae822d15505b85c3df6:

--- 
+++ 
@@ -85,12 +85,12 @@
 import { Post } from './dtos/post.dto';
 import { Comment } from './dtos/comment.dto';

-export type Subjects = InferSubjects;
+export type Subjects = InferSubjects | string;

 export const permissions: Permissions = {
   everyone({ can }) {
-    can(Actions.read, Post);
-    can(Actions.create, Post);
+    can(Actions.read, 'Post');
+    can(Actions.create, 'Post');
   },

   customer({ user, can }) {

Ran GitHub Actions for 2e3085291cece35cc4cfd0008507d6b2b4a92354:

--- 
+++ 
@@ -85,12 +85,12 @@
 import { Post } from './dtos/post.dto';
 import { Comment } from './dtos/comment.dto';

-export type Subjects = InferSubjects;
+export type Subjects = InferSubjects | string;

 export const permissions: Permissions = {
   everyone({ can }) {
-    can(Actions.read, Post);
-    can(Actions.create, Post);
+    can(Actions.read, 'Post'); // Subjects can now be specified as string literals in addition to class types
+    can(Actions.create, 'Post'); // This flexibility allows for defining permissions without a corresponding DTO
   },

   customer({ user, can }) {

Ran GitHub Actions for 59f26f15d22cf1f77e8603c98aa91cb644f06fee:


Step 3: 🔁 Code Review

I have finished reviewing the code for completeness. I did not find errors for sweep/update_the_library_to_support_string_as_66c1b.


🎉 Latest improvements to Sweep:


💡 To recreate the pull request edit the issue title or description. To tweak the pull request, leave a comment on the pull request. Join Our Discord

This is an automated message generated by Sweep AI.