Scope server with workspace (#157)

* Rename User to AuthUser to avoid naming conflict with user business entity

* Prevent query by workspace in graphql

* Make full user and workspace object available in graphql resolvers

* Add Seed to create companies and people accross two workspace

* Check workspace on all entities findMany, find, create, update)
This commit is contained in:
Charles Bochet
2023-05-30 20:40:04 +02:00
committed by GitHub
parent 0f9c6dede7
commit 3674365e6f
47 changed files with 380 additions and 483 deletions

View File

@ -2,31 +2,39 @@ import { Resolver, Query, Args } from '@nestjs/graphql';
import { PrismaService } from 'src/database/prisma.service';
import { UseGuards } from '@nestjs/common';
import { JwtAuthGuard } from 'src/auth/guards/jwt.auth.guard';
import { User } from '../@generated/user/user.model';
import { FindManyUserArgs } from '../@generated/user/find-many-user.args';
import { FindUniqueUserOrThrowArgs } from '../@generated/user/find-unique-user-or-throw.args';
import { Workspace } from '@prisma/client';
import { AuthWorkspace } from './decorators/auth-workspace.decorator';
import { ArgsService } from './services/args.service';
import { CheckWorkspaceOwnership } from 'src/auth/guards/check-workspace-ownership.guard';
@UseGuards(JwtAuthGuard, CheckWorkspaceOwnership)
@Resolver(() => User)
export class UserResolver {
constructor(private readonly prismaService: PrismaService) {}
constructor(
private readonly prismaService: PrismaService,
private readonly argsService: ArgsService,
) {}
@UseGuards(JwtAuthGuard)
@Query(() => [User], {
nullable: false,
})
async users(@Args() args: FindManyUserArgs): Promise<User[]> {
async findManyUser(
@Args() args: FindManyUserArgs,
@AuthWorkspace() workspace: Workspace,
): Promise<User[]> {
args.where = {
...args.where,
...{
WorkspaceMember: {
is: { workspace: { is: { id: { equals: workspace.id } } } },
},
},
};
return await this.prismaService.user.findMany({
...args,
});
}
@UseGuards(JwtAuthGuard)
@Query(() => User, {
nullable: false,
})
async user(@Args() args: FindUniqueUserOrThrowArgs): Promise<User | null> {
return await this.prismaService.user.findUnique({
...args,
});
}
}