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:
@ -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,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user