Skip to content

Commit

Permalink
enable caching in authorization
Browse files Browse the repository at this point in the history
  • Loading branch information
bahram1249 committed Feb 20, 2024
1 parent 80a4dff commit bf192b5
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 39 deletions.
4 changes: 4 additions & 0 deletions apps/e-commerce/src/admin/product/product.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -766,11 +766,15 @@ export class ProductService {

await transaction.commit();

const keepJobs = this.config.get<number>(
'PRODUCT_INVENTORY_STATUS_KEEPJOBS',
);
await this.productInventoryQueue.add(
Constants.productInventoryStatusJob(product.id.toString()),
{
productId: product.id,
},
{ removeOnComplete: keepJobs },
);
} catch (error) {
await transaction.rollback();
Expand Down
1 change: 1 addition & 0 deletions libs/auth/src/strategy/jwt.strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ export class JwtStrategy extends PassportStrategy(Strategy, 'jwt') {
],
});
user.menus = menus;
await this.cacheManager.set(`userid:${payload.sub}`, user);
}
return user;
}
Expand Down
101 changes: 62 additions & 39 deletions libs/permission-checker/src/guard/permission.guard.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { Injectable, CanActivate, ExecutionContext } from '@nestjs/common';
import {
Injectable,
CanActivate,
ExecutionContext,
Inject,
} from '@nestjs/common';
import { Reflector } from '@nestjs/core';
import { InjectModel } from '@nestjs/sequelize';
import { Permission } from '@rahino/database/models/core/permission.entity';
Expand All @@ -7,6 +12,8 @@ import { Role } from '@rahino/database/models/core/role.entity';
import { UserRole } from '@rahino/database/models/core/userRole.entity';
import { User } from '@rahino/database/models/core/user.entity';
import { PermissionReflector } from '../interface';
import { CACHE_MANAGER } from '@nestjs/cache-manager';
import { Cache } from 'cache-manager';

@Injectable()
export class PermissionGuard implements CanActivate {
Expand All @@ -16,6 +23,8 @@ export class PermissionGuard implements CanActivate {
private userRepository: typeof User,
@InjectModel(Permission)
private permissionRepository: typeof Permission,
@Inject(CACHE_MANAGER)
private cacheManager: Cache,
) {}

async canActivate(context: ExecutionContext) {
Expand All @@ -35,45 +44,59 @@ export class PermissionGuard implements CanActivate {
permission: PermissionReflector,
userId: bigint,
) {
const permissionFinded = await this.permissionRepository.findOne({
where: {
permissionSymbol: permission.permissionSymbol,
},
});
if (!permissionFinded) {
return false;
}
const item = await this.userRepository.findOne({
where: {
id: userId,
},
include: [
{
model: UserRole,
as: 'userRoles',
required: true,
include: [
{
model: Role,
as: 'role',
required: true,
include: [
{
model: RolePermission,
as: 'rolePermissions',
required: true,
where: {
permissionId: permissionFinded.id,
},
},
],
},
],
},
],
});
let access = true;
if (!item) access = false;
let defined = await this.cacheManager.get(
`userid:${userId}->permission:${permission.permissionSymbol}`,
);
if (defined == false) {
access = false;
} else if (defined == true) {
access = true;
} else {
const permissionFinded = await this.permissionRepository.findOne({
where: {
permissionSymbol: permission.permissionSymbol,
},
});
if (!permissionFinded) {
return false;
}
const item = await this.userRepository.findOne({
where: {
id: userId,
},
include: [
{
model: UserRole,
as: 'userRoles',
required: true,
include: [
{
model: Role,
as: 'role',
required: true,
include: [
{
model: RolePermission,
as: 'rolePermissions',
required: true,
where: {
permissionId: permissionFinded.id,
},
},
],
},
],
},
],
});

if (!item) access = false;
await this.cacheManager.set(
`userid:${userId}->permission:${permission.permissionSymbol}`,
access,
);
}
return access;
}
}

0 comments on commit bf192b5

Please sign in to comment.