-
Notifications
You must be signed in to change notification settings - Fork 11
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
Add reef admin relationships #128
Merged
Merged
Changes from 14 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
dc62aa0
Add ManyToMany reef-user relationship
avalmas-programize 7048a89
Refactor reef service
avalmas-programize 6d1b9bf
Add IsReefManagerGuard
avalmas-programize 4dc7b83
Migrate reef admin relatioships
avalmas-programize a29e271
Add endpoint to fetch administrated reefs of user
avalmas-programize 72de005
Merge branch 'master' into reef-admins
ericboucher 7469749
Override cycle dependencies for eneity files
avalmas-programize 7b74822
Fix typo
avalmas-programize 28deadb
Make users the joinTable for reef ManyToMany relationship
avalmas-programize c33281b
Add public decorator
avalmas-programize 7500856
Merge branch 'master' into reef-admins
ericboucher 725cb58
Merge branch 'master' into reef-admins
ericboucher 9f1eecc
Override user's role
avalmas-programize 37600df
Update users.controller.ts
ericboucher 2b0cc33
Fix reef_manager elevation
ericboucher 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 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 |
---|---|---|
|
@@ -52,6 +52,12 @@ | |
} | ||
] | ||
} | ||
}, | ||
{ | ||
"files": ["src/**/*.entity.{ts,js}"], | ||
"rules": { | ||
"import/no-cycle": "off" | ||
} | ||
} | ||
] | ||
} |
76 changes: 76 additions & 0 deletions
76
packages/api/migration/1599586648784-ReefAdminManyToMany.ts
This file contains 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,76 @@ | ||
import { MigrationInterface, QueryRunner } from 'typeorm'; | ||
|
||
export class ReefAdminManyToMany1599586648784 implements MigrationInterface { | ||
name = 'ReefAdminManyToMany1599586648784'; | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query( | ||
`ALTER TABLE "reef" DROP CONSTRAINT "FK_dc56bfd6bfcd1f221ec83885294"`, | ||
); | ||
await queryRunner.query( | ||
` | ||
CREATE TABLE "users_administered_reefs_reef" ( | ||
"reef_id" integer NOT NULL, | ||
"users_id" integer NOT NULL, | ||
CONSTRAINT "PK_21f162e26e837a19d1e1accd1cd" PRIMARY KEY ("reef_id", "users_id") | ||
) | ||
`, | ||
); | ||
await queryRunner.query( | ||
`CREATE INDEX "IDX_088a629ef23eb9eba6ac857ed6" ON "users_administered_reefs_reef" ("reef_id") `, | ||
); | ||
await queryRunner.query( | ||
`CREATE INDEX "IDX_da52b9542bf7df43f4840ae439" ON "users_administered_reefs_reef" ("users_id") `, | ||
); | ||
await queryRunner.query(`ALTER TABLE "reef" DROP COLUMN "admin_id"`); | ||
await queryRunner.query( | ||
` | ||
ALTER TABLE "users_administered_reefs_reef" ADD CONSTRAINT "FK_088a629ef23eb9eba6ac857ed62" | ||
FOREIGN KEY ("reef_id") REFERENCES "reef"("id") ON DELETE CASCADE ON UPDATE NO ACTION | ||
`, | ||
); | ||
await queryRunner.query( | ||
` | ||
ALTER TABLE "users_administered_reefs_reef" ADD CONSTRAINT "FK_da52b9542bf7df43f4840ae4394" | ||
FOREIGN KEY ("users_id") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE NO ACTION | ||
`, | ||
); | ||
await queryRunner.query( | ||
`ALTER TABLE "reef_application" DROP CONSTRAINT "FK_77d33d9b9602120cd1529312e77"`, | ||
); | ||
await queryRunner.query( | ||
`ALTER TABLE "reef_application" DROP CONSTRAINT "UQ_77d33d9b9602120cd1529312e77"`, | ||
); | ||
await queryRunner.query( | ||
`ALTER TABLE "reef_application" ADD CONSTRAINT "FK_77d33d9b9602120cd1529312e77" FOREIGN KEY ("reef_id") REFERENCES "reef"("id") ON DELETE CASCADE ON UPDATE NO ACTION`, | ||
); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query( | ||
`ALTER TABLE "users_administered_reefs_reef" DROP CONSTRAINT "FK_da52b9542bf7df43f4840ae4394"`, | ||
); | ||
await queryRunner.query( | ||
`ALTER TABLE "users_administered_reefs_reef" DROP CONSTRAINT "FK_088a629ef23eb9eba6ac857ed62"`, | ||
); | ||
await queryRunner.query(`ALTER TABLE "reef" ADD "admin_id" integer`); | ||
await queryRunner.query(`DROP INDEX "IDX_da52b9542bf7df43f4840ae439"`); | ||
await queryRunner.query(`DROP INDEX "IDX_088a629ef23eb9eba6ac857ed6"`); | ||
await queryRunner.query(`DROP TABLE "users_administered_reefs_reef"`); | ||
await queryRunner.query( | ||
` | ||
ALTER TABLE "reef" ADD CONSTRAINT "FK_dc56bfd6bfcd1f221ec83885294" | ||
FOREIGN KEY ("admin_id") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE NO ACTION | ||
`, | ||
); | ||
await queryRunner.query( | ||
`ALTER TABLE "reef_application" DROP CONSTRAINT "FK_77d33d9b9602120cd1529312e77"`, | ||
); | ||
await queryRunner.query( | ||
`ALTER TABLE "reef_application" ADD CONSTRAINT "UQ_77d33d9b9602120cd1529312e77" UNIQUE ("reef_id")`, | ||
); | ||
await queryRunner.query( | ||
`ALTER TABLE "reef_application" ADD CONSTRAINT "FK_77d33d9b9602120cd1529312e77" FOREIGN KEY ("reef_id") REFERENCES "reef"("id") ON DELETE CASCADE ON UPDATE NO ACTION`, | ||
); | ||
} | ||
} |
This file contains 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 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 |
---|---|---|
@@ -1,5 +1,24 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { Injectable, ExecutionContext } from '@nestjs/common'; | ||
import { AuthGuard } from '@nestjs/passport'; | ||
import { Reflector } from '@nestjs/core'; | ||
import { of } from 'rxjs'; | ||
|
||
@Injectable() | ||
export class FirebaseAuthGuard extends AuthGuard('custom') {} | ||
export class FirebaseAuthGuard extends AuthGuard('custom') { | ||
constructor(private reflector: Reflector) { | ||
super(); | ||
} | ||
|
||
canActivate(context: ExecutionContext) { | ||
const isPublic = this.reflector.get<boolean>( | ||
'isPublic', | ||
context.getHandler(), | ||
); | ||
|
||
if (isPublic) { | ||
return of(true); | ||
} | ||
|
||
return super.canActivate(context); | ||
} | ||
} |
This file contains 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,48 @@ | ||
import { CanActivate, ExecutionContext } from '@nestjs/common'; | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
import { Reflector } from '@nestjs/core'; | ||
import { Repository } from 'typeorm'; | ||
import { User, AdminLevel } from '../users/users.entity'; | ||
import { Reef } from '../reefs/reefs.entity'; | ||
|
||
export class IsReefAdminGuard implements CanActivate { | ||
constructor( | ||
@InjectRepository(Reef) | ||
private reefRepository: Repository<Reef>, | ||
|
||
private reflector: Reflector, | ||
) {} | ||
|
||
async canActivate(context: ExecutionContext): Promise<boolean> { | ||
const isPublic = this.reflector.get<boolean>( | ||
'isPublic', | ||
context.getHandler(), | ||
); | ||
|
||
if (isPublic) { | ||
return true; | ||
} | ||
|
||
const request = context.switchToHttp().getRequest(); | ||
const { user }: { user: User } = request; | ||
const reefId = parseInt(request.params.reef_id, 10); | ||
|
||
if (user.adminLevel === AdminLevel.SuperAdmin) { | ||
return true; | ||
} | ||
|
||
if (!Number.isNaN(reefId) && user.adminLevel === AdminLevel.ReefManager) { | ||
const isReefAdmin = await this.reefRepository | ||
.createQueryBuilder('reef') | ||
.innerJoin('reef.admins', 'admins', 'admins.id = :userId', { | ||
userId: user.id, | ||
}) | ||
.andWhere('reef.id = :reefId', { reefId }) | ||
.getOne(); | ||
|
||
return !!isReefAdmin; | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains 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 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,6 @@ | ||
import { applyDecorators, SetMetadata } from '@nestjs/common'; | ||
import { AdminLevel } from '../users/users.entity'; | ||
|
||
export const OverrideLevelAccess = (...levels: AdminLevel[]) => { | ||
return applyDecorators(SetMetadata('levels', levels)); | ||
}; |
This file contains 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,5 @@ | ||
import { applyDecorators, SetMetadata } from '@nestjs/common'; | ||
|
||
export const Public = () => { | ||
return applyDecorators(SetMetadata('isPublic', true)); | ||
}; |
This file contains 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 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 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 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 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
Oops, something went wrong.
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.
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.
is this not needed anymore?
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.
As I see it, this endpoint was using a mock response which is already implemented in the survey details endpoint (GET /reefs/:reef_id/surveys/:id). The only difference was that I fetch all POIs of a survey and this was designed to fetch only one POI of a survey. I don't see any use for it anymore.
Also it was conflicting with the new endpoints (surveys endpoints with the updated prefix)