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

* feat: nested casl abilities

* fix: remove unused packages

* Fixes

* Fix createMany broken

* Fix lint

* Fix lint

* Fix lint

* Fix lint

* Fixes

* Fix CommentThread

* Fix bugs

* Fix lint

* Fix bugs

* Fixed auto routing

* Fixed app path

---------

Co-authored-by: Charles Bochet <charles@twenty.com>
Co-authored-by: Lucas Bordeau <bordeau.lucas@gmail.com>
This commit is contained in:
Jérémy M
2023-07-26 01:37:22 +02:00
committed by GitHub
parent 92b9e987a5
commit 51cfc0d82c
69 changed files with 1192 additions and 883 deletions

View File

@ -1,9 +1,5 @@
import { Test, TestingModule } from '@nestjs/testing';
import { CanActivate } from '@nestjs/common';
import { UpdateOneGuard } from 'src/guards/update-one.guard';
import { DeleteManyGuard } from 'src/guards/delete-many.guard';
import { CreateOneGuard } from 'src/guards/create-one.guard';
import { AbilityFactory } from 'src/ability/ability.factory';
import { CompanyService } from './company.service';
@ -13,8 +9,6 @@ describe('CompanyResolver', () => {
let resolver: CompanyResolver;
beforeEach(async () => {
const mockGuard: CanActivate = { canActivate: jest.fn(() => true) };
const module: TestingModule = await Test.createTestingModule({
providers: [
CompanyResolver,
@ -27,14 +21,7 @@ describe('CompanyResolver', () => {
useValue: {},
},
],
})
.overrideGuard(UpdateOneGuard)
.useValue(mockGuard)
.overrideGuard(DeleteManyGuard)
.useValue(mockGuard)
.overrideGuard(CreateOneGuard)
.useValue(mockGuard)
.compile();
}).compile();
resolver = module.get<CompanyResolver>(CompanyResolver);
});

View File

@ -12,9 +12,6 @@ import { UpdateOneCompanyArgs } from 'src/core/@generated/company/update-one-com
import { CreateOneCompanyArgs } from 'src/core/@generated/company/create-one-company.args';
import { AffectedRows } from 'src/core/@generated/prisma/affected-rows.output';
import { DeleteManyCompanyArgs } from 'src/core/@generated/company/delete-many-company.args';
import { UpdateOneGuard } from 'src/guards/update-one.guard';
import { DeleteManyGuard } from 'src/guards/delete-many.guard';
import { CreateOneGuard } from 'src/guards/create-one.guard';
import {
PrismaSelect,
PrismaSelector,
@ -78,7 +75,6 @@ export class CompanyResolver {
});
}
@UseGuards(UpdateOneGuard)
@Mutation(() => Company, {
nullable: true,
})
@ -96,7 +92,6 @@ export class CompanyResolver {
} as Prisma.CompanyUpdateArgs);
}
@UseGuards(DeleteManyGuard)
@Mutation(() => AffectedRows, {
nullable: false,
})
@ -110,7 +105,6 @@ export class CompanyResolver {
});
}
@UseGuards(CreateOneGuard)
@Mutation(() => Company, {
nullable: false,
})

View File

@ -8,35 +8,35 @@ export class CompanyService {
constructor(private readonly prismaService: PrismaService) {}
// Find
findFirst = this.prismaService.company.findFirst;
findFirstOrThrow = this.prismaService.company.findFirstOrThrow;
findFirst = this.prismaService.client.company.findFirst;
findFirstOrThrow = this.prismaService.client.company.findFirstOrThrow;
findUnique = this.prismaService.company.findUnique;
findUniqueOrThrow = this.prismaService.company.findUniqueOrThrow;
findUnique = this.prismaService.client.company.findUnique;
findUniqueOrThrow = this.prismaService.client.company.findUniqueOrThrow;
findMany = this.prismaService.company.findMany;
findMany = this.prismaService.client.company.findMany;
// Create
create = this.prismaService.company.create;
createMany = this.prismaService.company.createMany;
create = this.prismaService.client.company.create;
createMany = this.prismaService.client.company.createMany;
// Update
update = this.prismaService.company.update;
upsert = this.prismaService.company.upsert;
updateMany = this.prismaService.company.updateMany;
update = this.prismaService.client.company.update;
upsert = this.prismaService.client.company.upsert;
updateMany = this.prismaService.client.company.updateMany;
// Delete
delete = this.prismaService.company.delete;
deleteMany = this.prismaService.company.deleteMany;
delete = this.prismaService.client.company.delete;
deleteMany = this.prismaService.client.company.deleteMany;
// Aggregate
aggregate = this.prismaService.company.aggregate;
aggregate = this.prismaService.client.company.aggregate;
// Count
count = this.prismaService.company.count;
count = this.prismaService.client.company.count;
// GroupBy
groupBy = this.prismaService.company.groupBy;
groupBy = this.prismaService.client.company.groupBy;
async createDefaultCompanies({ workspaceId }: { workspaceId: string }) {
const companies = companiesSeed.map((company) => ({
...company,