Skip to content

feat: refactoring casl permission checks for recursive nested operations#778

Merged
charlesBochet merged 16 commits intomainfrom
nested-permissions
Jul 25, 2023
Merged

feat: refactoring casl permission checks for recursive nested operations#778
charlesBochet merged 16 commits intomainfrom
nested-permissions

Conversation

@magrinj
Copy link
Copy Markdown
Contributor

@magrinj magrinj commented Jul 20, 2023

This PR introduces a new capability to the project: checking permissions for Prisma operations involving nested relationships. This new feature ensures that the right permissions are in place when performing complex nested operations in our database.

Key highlights include:

  1. Operations Definition: Operations are categorized into 'create' and 'simple' based on the type of permission checks they require. This classification improves how we process each operation.

  2. Operations Checking: We've introduced 'OperationAbilityCheckers' that conduct permission checks depending on the operation type. 'createAbilityCheck' ensures a user has the ability to create a new entity, while 'simpleAbilityCheck' checks permissions for operations like 'connect', 'disconnect', 'delete', etc.

  3. Recursive Nested Operations: We have introduced a new function, relationAbilityChecker, that handles recursive nested operations. It splits each operation and checks them independently.

Here is an example of how to use these new permission checks:

@Injectable()
export class CreateCompanyAbilityHandler implements IAbilityHandler {
  constructor(private readonly prismaService: PrismaService) {}

  async handle(ability: AppAbility, context: ExecutionContext) {
    const gqlContext = GqlExecutionContext.create(context);
    const args = gqlContext.getArgs();

    const allowed = await relationAbilityChecker(
      'Company',
      ability,
      this.prismaService.client,
      args,
    );

    if (!allowed) {
      return false;
    }

    return ability.can(AbilityAction.Create, 'Company');
  }
}

@magrinj magrinj force-pushed the nested-permissions branch 2 times, most recently from 99d05d4 to 7c03c13 Compare July 25, 2023 13:40
@magrinj magrinj force-pushed the nested-permissions branch from 7c03c13 to aec68ae Compare July 25, 2023 13:40
@magrinj magrinj changed the title poc: nested relation permission feat: refactoring casl permission checks for recursive nested operations Jul 25, 2023
@magrinj magrinj marked this pull request as ready for review July 25, 2023 14:16
@magrinj magrinj linked an issue Jul 25, 2023 that may be closed by this pull request
Copy link
Copy Markdown
Member

@charlesBochet charlesBochet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bravo!

{
create: createAbilityCheck,
createMany: createAbilityCheck,
upsert: simpleAbilityCheck,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that we should find a better naming than "simple". existing?

data,
) => {
// Handle all operations cases
const items = !Array.isArray(data?.data ?? data) ? [data] : data;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const operations

@charlesBochet charlesBochet merged commit 51cfc0d into main Jul 25, 2023
@charlesBochet charlesBochet deleted the nested-permissions branch July 25, 2023 23:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add nested permission layer on Server

3 participants