-
Notifications
You must be signed in to change notification settings - Fork 1
chore: User to Organization Mapping #48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import { Injectable, ExecutionContext, CanActivate } from '@nestjs/common'; | ||
| import { GqlExecutionContext } from '@nestjs/graphql'; | ||
| import { OrganizationService } from './organization.service'; | ||
| import { UserOrgService } from '../userorg/userorg.service'; | ||
|
|
||
| @Injectable() | ||
| export class OrganizationGuard implements CanActivate { | ||
| constructor( | ||
| private readonly organizationService: OrganizationService, | ||
| private readonly userOrgService: UserOrgService | ||
| ) {} | ||
|
|
||
| async canActivate(context: ExecutionContext): Promise<boolean> { | ||
| const ctx = GqlExecutionContext.create(context); | ||
|
|
||
| // Check for the organization in the headers | ||
| const organizationID = ctx.getContext().req.headers.organization; | ||
| if (organizationID == undefined || organizationID == 'undefined') { | ||
| return false; | ||
| } | ||
| if (typeof organizationID !== 'string') { | ||
| return false; | ||
| } | ||
|
|
||
| // Check if the organization exists | ||
| const organization = await this.organizationService.findOne(organizationID); | ||
| if (!organization) { | ||
| return false; | ||
| } | ||
|
|
||
| // Check to see if the user is in the organization | ||
| const user = ctx.getContext().req.user; | ||
| if (!user) { | ||
| return false; | ||
| } | ||
| const userOrg = await this.userOrgService.userIsInOrg(user.user_id, organizationID); | ||
| if (!userOrg) { | ||
| return false; | ||
| } | ||
|
|
||
| // Add the organization to the request | ||
| ctx.getContext().req.organization = organization; | ||
|
|
||
| return true; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import { Schema, Prop, SchemaFactory } from '@nestjs/mongoose'; | ||
|
|
||
| @Schema() | ||
| export class UserOrg { | ||
| @Prop() | ||
| user: string; | ||
|
|
||
| @Prop() | ||
| org: string; | ||
| } | ||
|
|
||
| export type UserOrgDocument = UserOrg & Document; | ||
| export const UserOrgSchema = SchemaFactory.createForClass(UserOrg); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import { Module } from '@nestjs/common'; | ||
| import { UserOrgService } from './userorg.service'; | ||
| import { UserOrg, UserOrgSchema } from './userorg.model'; | ||
| import { MongooseModule } from '@nestjs/mongoose'; | ||
| import { UserOrgResolver } from './userorg.resolver'; | ||
|
|
||
| @Module({ | ||
| imports: [MongooseModule.forFeature([{ name: UserOrg.name, schema: UserOrgSchema }])], | ||
| providers: [UserOrgService, UserOrgResolver], | ||
| exports: [UserOrgService] | ||
| }) | ||
| export class UserOrgModule {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| import { ID, Resolver, Query, Mutation, Args } from '@nestjs/graphql'; | ||
| import { UserOrgService } from './userorg.service'; | ||
| import { TokenContext } from '../jwt/token.context'; | ||
| import { TokenPayload } from '../jwt/token.dto'; | ||
| import { JwtAuthGuard } from '../jwt/jwt.guard'; | ||
| import { UseGuards } from '@nestjs/common'; | ||
|
|
||
| @Resolver() | ||
| export class UserOrgResolver { | ||
| constructor(private readonly userOrgService: UserOrgService) {} | ||
|
|
||
| @Query(() => Boolean) | ||
| async userIsInOrg(@Args('user') user: string, @Args('org') org: string): Promise<boolean> { | ||
| return this.userOrgService.userIsInOrg(user, org); | ||
| } | ||
|
|
||
| @UseGuards(JwtAuthGuard) | ||
| @Mutation(() => Boolean) | ||
| async addUserToOrg( | ||
| @Args('organization', { type: () => ID }) organization: string, | ||
| @TokenContext() user: TokenPayload | ||
| ): Promise<boolean> { | ||
| await this.userOrgService.create(user.user_id, organization); | ||
| return true; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import { Injectable } from '@nestjs/common'; | ||
| import { InjectModel } from '@nestjs/mongoose'; | ||
| import { Model } from 'mongoose'; | ||
| import { UserOrg, UserOrgDocument } from './userorg.model'; | ||
|
|
||
| @Injectable() | ||
| export class UserOrgService { | ||
| constructor(@InjectModel(UserOrg.name) private userOrgModel: Model<UserOrgDocument>) {} | ||
|
|
||
| async create(user: string, org: string): Promise<UserOrg> { | ||
| const existing = await this.find(user, org); | ||
| if (existing) { | ||
| return existing; | ||
| } | ||
| return this.userOrgModel.create({ user, org }); | ||
| } | ||
|
|
||
| async findUserForOrg(org: string): Promise<UserOrg[]> { | ||
| return this.userOrgModel.find({ org }); | ||
| } | ||
|
|
||
| async userIsInOrg(user: string, org: string): Promise<boolean> { | ||
| return !!(await this.find(user, org)); | ||
| } | ||
|
|
||
| async find(user: string, org: string): Promise<UserOrg | null> { | ||
| return this.userOrgModel.findOne({ user, org }); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe you wanna add a TODO comment of what the function "validate" is supposed to do?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With the organization logic shifted, it is actually all it has to do now. It seems funky, but you'll see similar functionality in NestJS's official documentation. The
validationis just for additional logic beyond the JWT validation.