-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7b00eed
commit 519f97f
Showing
7 changed files
with
79 additions
and
72 deletions.
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
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
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,41 @@ | ||
import { Injectable } from '@nestjs/common' | ||
import { plainToClass } from 'class-transformer' | ||
import mongoose from 'mongoose' | ||
|
||
// Auth | ||
import { ForbiddenError } from '@casl/ability' | ||
import { Action } from '../casl/casl-ability.factory' | ||
import { CaslAbilityFactory } from '../casl/casl-ability.factory' | ||
|
||
// User | ||
import { User } from '../entities/user/user.model' | ||
|
||
@Injectable() | ||
export abstract class BaseService { | ||
abstract dbModel: any // mongoose.Model<mongoose.Document> | ||
abstract modelClass: any //{ new (): any } | ||
abstract context: any //{ new (): any } | ||
|
||
get currentUser(): User | undefined { | ||
return this.context.req?.user | ||
} | ||
|
||
async checkPermissons( | ||
action: Action, | ||
id?: mongoose.Types.ObjectId, | ||
): Promise<mongoose.Model<mongoose.Document>> { | ||
// Checks permissons for a single record | ||
const ability = CaslAbilityFactory.createForUser(this.currentUser) | ||
if (id) { | ||
const doc = await this.dbModel.findById(id).exec() | ||
const model = plainToClass(this.modelClass, doc?.toObject()) as any | ||
ForbiddenError.from(ability).throwUnlessCan(action, model) | ||
return doc | ||
} else { | ||
ForbiddenError.from(ability).throwUnlessCan( | ||
action, | ||
this.modelClass as any, | ||
) | ||
} | ||
} | ||
} |