-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Decorator for checking if admin has a specific permissions
- Loading branch information
1 parent
475592e
commit 853d33e
Showing
3 changed files
with
52 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { CanActivate, ExecutionContext, Injectable } from "@nestjs/common"; | ||
import { GqlExecutionContext } from "@nestjs/graphql"; | ||
import { AuthorizationAdminSessionsService } from "@/plugins/core/admin/sessions/authorization/authorization.service"; | ||
import { DatabaseService } from "@/database/database.service"; | ||
import { eq } from "drizzle-orm"; | ||
import { core_users } from "@/plugins/core/admin/database/schema/users"; | ||
import { Reflector } from "@nestjs/core"; | ||
|
||
@Injectable() | ||
export class AdminPermissionGuards implements CanActivate { | ||
constructor( | ||
private readonly reflector: Reflector, | ||
private readonly service: AuthorizationAdminSessionsService, | ||
private readonly databaseService: DatabaseService | ||
) {} | ||
|
||
protected async getAuth(ctx: any) { | ||
const { req, res } = ctx; | ||
const data = await this.service.authorization({ req, res }); | ||
return data.user.id; | ||
} | ||
|
||
async canActivate(context: ExecutionContext): Promise<boolean> { | ||
const ctx = GqlExecutionContext.create(context).getContext(); | ||
const permission = this.reflector.get<string>( | ||
"permission", | ||
context.getHandler() | ||
); | ||
const userId = await this.getAuth(ctx); | ||
|
||
const user = await this.databaseService.db.query.core_users.findFirst({ | ||
where: eq(core_users.id, userId) | ||
}); | ||
|
||
if (!user) return false; | ||
|
||
const admin = | ||
await this.databaseService.db.query.core_admin_permissions.findFirst({ | ||
where: (table, { or, eq }) => | ||
or(eq(table.user_id, user.id), eq(table.group_id, user.group_id)) | ||
}); | ||
|
||
if (!admin || !admin.permissions) return false; | ||
const permissions = admin.permissions; | ||
if (permissions[permission] === true) return true; | ||
return false; | ||
} | ||
} |