mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-16 18:56:22 +00:00
Merge branch 'dev' into SP-201-be-handle-roles-new
This commit is contained in:
@ -1,11 +1,14 @@
|
|||||||
import { Global, Module } from '@nestjs/common';
|
import { Global, Module } from '@nestjs/common';
|
||||||
import { HelperHashService } from './services';
|
import { HelperHashService } from './services';
|
||||||
|
import { SpacePermissionService } from './services/space.permission.service';
|
||||||
|
import { SpaceRepository } from '../modules/space/repositories';
|
||||||
|
import { SpaceRepositoryModule } from '../modules/space/space.repository.module';
|
||||||
|
|
||||||
@Global()
|
@Global()
|
||||||
@Module({
|
@Module({
|
||||||
providers: [HelperHashService],
|
providers: [HelperHashService, SpacePermissionService, SpaceRepository],
|
||||||
exports: [HelperHashService],
|
exports: [HelperHashService, SpacePermissionService],
|
||||||
controllers: [],
|
controllers: [],
|
||||||
imports: [],
|
imports: [SpaceRepositoryModule],
|
||||||
})
|
})
|
||||||
export class HelperModule {}
|
export class HelperModule {}
|
||||||
|
@ -1 +1,2 @@
|
|||||||
export * from './helper.hash.service';
|
export * from './helper.hash.service';
|
||||||
|
export * from './space.permission.service';
|
||||||
|
39
libs/common/src/helper/services/space.permission.service.ts
Normal file
39
libs/common/src/helper/services/space.permission.service.ts
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
import { Injectable } from '@nestjs/common';
|
||||||
|
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||||
|
import { BadRequestException } from '@nestjs/common';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class SpacePermissionService {
|
||||||
|
constructor(private readonly spaceRepository: SpaceRepository) {}
|
||||||
|
|
||||||
|
async checkUserPermission(
|
||||||
|
spaceUuid: string,
|
||||||
|
userUuid: string,
|
||||||
|
type: string,
|
||||||
|
): Promise<void> {
|
||||||
|
try {
|
||||||
|
const spaceData = await this.spaceRepository.findOne({
|
||||||
|
where: {
|
||||||
|
uuid: spaceUuid,
|
||||||
|
spaceType: {
|
||||||
|
type: type,
|
||||||
|
},
|
||||||
|
userSpaces: {
|
||||||
|
user: {
|
||||||
|
uuid: userUuid,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
relations: ['spaceType', 'userSpaces', 'userSpaces.user'],
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!spaceData) {
|
||||||
|
throw new BadRequestException(
|
||||||
|
`You do not have permission to access this ${type}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
throw new BadRequestException(err.message || 'Invalid UUID');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -19,6 +19,7 @@ import { CheckCommunityTypeGuard } from 'src/guards/community.type.guard';
|
|||||||
import { CheckUserBuildingGuard } from 'src/guards/user.building.guard';
|
import { CheckUserBuildingGuard } from 'src/guards/user.building.guard';
|
||||||
import { AdminRoleGuard } from 'src/guards/admin.role.guard';
|
import { AdminRoleGuard } from 'src/guards/admin.role.guard';
|
||||||
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
|
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
|
||||||
|
import { BuildingPermissionGuard } from 'src/guards/building.permission.guard';
|
||||||
|
|
||||||
@ApiTags('Building Module')
|
@ApiTags('Building Module')
|
||||||
@Controller({
|
@Controller({
|
||||||
@ -34,7 +35,12 @@ export class BuildingController {
|
|||||||
async addBuilding(@Body() addBuildingDto: AddBuildingDto) {
|
async addBuilding(@Body() addBuildingDto: AddBuildingDto) {
|
||||||
try {
|
try {
|
||||||
const building = await this.buildingService.addBuilding(addBuildingDto);
|
const building = await this.buildingService.addBuilding(addBuildingDto);
|
||||||
return { message: 'Building added successfully', uuid: building.uuid };
|
return {
|
||||||
|
statusCode: HttpStatus.CREATED,
|
||||||
|
success: true,
|
||||||
|
message: 'Building added successfully',
|
||||||
|
data: building,
|
||||||
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
error.message || 'Internal server error',
|
error.message || 'Internal server error',
|
||||||
@ -44,7 +50,7 @@ export class BuildingController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(JwtAuthGuard)
|
@UseGuards(JwtAuthGuard, BuildingPermissionGuard)
|
||||||
@Get(':buildingUuid')
|
@Get(':buildingUuid')
|
||||||
async getBuildingByUuid(@Param('buildingUuid') buildingUuid: string) {
|
async getBuildingByUuid(@Param('buildingUuid') buildingUuid: string) {
|
||||||
try {
|
try {
|
||||||
@ -60,7 +66,7 @@ export class BuildingController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(JwtAuthGuard)
|
@UseGuards(JwtAuthGuard, BuildingPermissionGuard)
|
||||||
@Get('child/:buildingUuid')
|
@Get('child/:buildingUuid')
|
||||||
async getBuildingChildByUuid(
|
async getBuildingChildByUuid(
|
||||||
@Param('buildingUuid') buildingUuid: string,
|
@Param('buildingUuid') buildingUuid: string,
|
||||||
@ -80,7 +86,7 @@ export class BuildingController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(JwtAuthGuard)
|
@UseGuards(JwtAuthGuard, BuildingPermissionGuard)
|
||||||
@Get('parent/:buildingUuid')
|
@Get('parent/:buildingUuid')
|
||||||
async getBuildingParentByUuid(@Param('buildingUuid') buildingUuid: string) {
|
async getBuildingParentByUuid(@Param('buildingUuid') buildingUuid: string) {
|
||||||
try {
|
try {
|
||||||
@ -100,7 +106,11 @@ export class BuildingController {
|
|||||||
async addUserBuilding(@Body() addUserBuildingDto: AddUserBuildingDto) {
|
async addUserBuilding(@Body() addUserBuildingDto: AddUserBuildingDto) {
|
||||||
try {
|
try {
|
||||||
await this.buildingService.addUserBuilding(addUserBuildingDto);
|
await this.buildingService.addUserBuilding(addUserBuildingDto);
|
||||||
return { message: 'user building added successfully' };
|
return {
|
||||||
|
statusCode: HttpStatus.CREATED,
|
||||||
|
success: true,
|
||||||
|
message: 'user building added successfully',
|
||||||
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
error.message || 'Internal server error',
|
error.message || 'Internal server error',
|
||||||
@ -123,7 +133,7 @@ export class BuildingController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(JwtAuthGuard)
|
@UseGuards(JwtAuthGuard, BuildingPermissionGuard)
|
||||||
@Put('rename/:buildingUuid')
|
@Put('rename/:buildingUuid')
|
||||||
async renameBuildingByUuid(
|
async renameBuildingByUuid(
|
||||||
@Param('buildingUuid') buildingUuid: string,
|
@Param('buildingUuid') buildingUuid: string,
|
||||||
|
@ -10,6 +10,7 @@ import { UserSpaceRepositoryModule } from '@app/common/modules/user-space/user.s
|
|||||||
import { UserSpaceRepository } from '@app/common/modules/user-space/repositories';
|
import { UserSpaceRepository } from '@app/common/modules/user-space/repositories';
|
||||||
import { UserRepositoryModule } from '@app/common/modules/user/user.repository.module';
|
import { UserRepositoryModule } from '@app/common/modules/user/user.repository.module';
|
||||||
import { UserRepository } from '@app/common/modules/user/repositories';
|
import { UserRepository } from '@app/common/modules/user/repositories';
|
||||||
|
import { SpacePermissionService } from '@app/common/helper/services';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
@ -26,7 +27,8 @@ import { UserRepository } from '@app/common/modules/user/repositories';
|
|||||||
SpaceTypeRepository,
|
SpaceTypeRepository,
|
||||||
UserSpaceRepository,
|
UserSpaceRepository,
|
||||||
UserRepository,
|
UserRepository,
|
||||||
|
SpacePermissionService,
|
||||||
],
|
],
|
||||||
exports: [CommunityService],
|
exports: [CommunityService, SpacePermissionService],
|
||||||
})
|
})
|
||||||
export class CommunityModule {}
|
export class CommunityModule {}
|
||||||
|
@ -21,6 +21,7 @@ import { UpdateCommunityNameDto } from '../dtos/update.community.dto';
|
|||||||
import { CheckUserCommunityGuard } from 'src/guards/user.community.guard';
|
import { CheckUserCommunityGuard } from 'src/guards/user.community.guard';
|
||||||
import { AdminRoleGuard } from 'src/guards/admin.role.guard';
|
import { AdminRoleGuard } from 'src/guards/admin.role.guard';
|
||||||
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
|
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
|
||||||
|
import { CommunityPermissionGuard } from 'src/guards/community.permission.guard';
|
||||||
|
|
||||||
@ApiTags('Community Module')
|
@ApiTags('Community Module')
|
||||||
@Controller({
|
@Controller({
|
||||||
@ -37,7 +38,12 @@ export class CommunityController {
|
|||||||
try {
|
try {
|
||||||
const community =
|
const community =
|
||||||
await this.communityService.addCommunity(addCommunityDto);
|
await this.communityService.addCommunity(addCommunityDto);
|
||||||
return { message: 'Community added successfully', uuid: community.uuid };
|
return {
|
||||||
|
statusCode: HttpStatus.CREATED,
|
||||||
|
success: true,
|
||||||
|
message: 'Community added successfully',
|
||||||
|
data: community,
|
||||||
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
error.message || 'Internal server error',
|
error.message || 'Internal server error',
|
||||||
@ -47,7 +53,7 @@ export class CommunityController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(JwtAuthGuard)
|
@UseGuards(JwtAuthGuard, CommunityPermissionGuard)
|
||||||
@Get(':communityUuid')
|
@Get(':communityUuid')
|
||||||
async getCommunityByUuid(@Param('communityUuid') communityUuid: string) {
|
async getCommunityByUuid(@Param('communityUuid') communityUuid: string) {
|
||||||
try {
|
try {
|
||||||
@ -63,7 +69,7 @@ export class CommunityController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(JwtAuthGuard)
|
@UseGuards(JwtAuthGuard, CommunityPermissionGuard)
|
||||||
@Get('child/:communityUuid')
|
@Get('child/:communityUuid')
|
||||||
async getCommunityChildByUuid(
|
async getCommunityChildByUuid(
|
||||||
@Param('communityUuid') communityUuid: string,
|
@Param('communityUuid') communityUuid: string,
|
||||||
@ -102,7 +108,11 @@ export class CommunityController {
|
|||||||
async addUserCommunity(@Body() addUserCommunityDto: AddUserCommunityDto) {
|
async addUserCommunity(@Body() addUserCommunityDto: AddUserCommunityDto) {
|
||||||
try {
|
try {
|
||||||
await this.communityService.addUserCommunity(addUserCommunityDto);
|
await this.communityService.addUserCommunity(addUserCommunityDto);
|
||||||
return { message: 'user community added successfully' };
|
return {
|
||||||
|
statusCode: HttpStatus.CREATED,
|
||||||
|
success: true,
|
||||||
|
message: 'user community added successfully',
|
||||||
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
error.message || 'Internal server error',
|
error.message || 'Internal server error',
|
||||||
@ -111,7 +121,7 @@ export class CommunityController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(JwtAuthGuard)
|
@UseGuards(JwtAuthGuard, CommunityPermissionGuard)
|
||||||
@Put('rename/:communityUuid')
|
@Put('rename/:communityUuid')
|
||||||
async renameCommunityByUuid(
|
async renameCommunityByUuid(
|
||||||
@Param('communityUuid') communityUuid: string,
|
@Param('communityUuid') communityUuid: string,
|
||||||
|
@ -61,7 +61,15 @@ export class DeviceController {
|
|||||||
@Post('room')
|
@Post('room')
|
||||||
async addDeviceInRoom(@Body() addDeviceInRoomDto: AddDeviceInRoomDto) {
|
async addDeviceInRoom(@Body() addDeviceInRoomDto: AddDeviceInRoomDto) {
|
||||||
try {
|
try {
|
||||||
return await this.deviceService.addDeviceInRoom(addDeviceInRoomDto);
|
const device =
|
||||||
|
await this.deviceService.addDeviceInRoom(addDeviceInRoomDto);
|
||||||
|
|
||||||
|
return {
|
||||||
|
statusCode: HttpStatus.CREATED,
|
||||||
|
success: true,
|
||||||
|
message: 'device added in room successfully',
|
||||||
|
data: device,
|
||||||
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
error.message || 'Internal server error',
|
error.message || 'Internal server error',
|
||||||
|
@ -27,6 +27,7 @@ import { convertKeysToCamelCase } from '@app/common/helper/camelCaseConverter';
|
|||||||
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
import { DeviceRepository } from '@app/common/modules/device/repositories';
|
||||||
import { GroupDeviceRepository } from '@app/common/modules/group-device/repositories';
|
import { GroupDeviceRepository } from '@app/common/modules/group-device/repositories';
|
||||||
import { PermissionType } from '@app/common/constants/permission-type.enum';
|
import { PermissionType } from '@app/common/constants/permission-type.enum';
|
||||||
|
import { In } from 'typeorm';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class DeviceService {
|
export class DeviceService {
|
||||||
@ -57,7 +58,7 @@ export class DeviceService {
|
|||||||
permission: {
|
permission: {
|
||||||
userUuid,
|
userUuid,
|
||||||
permissionType: {
|
permissionType: {
|
||||||
type: PermissionType.READ || PermissionType.CONTROLLABLE,
|
type: In([PermissionType.READ, PermissionType.CONTROLLABLE]),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -81,6 +82,7 @@ export class DeviceService {
|
|||||||
} as GetDeviceDetailsInterface;
|
} as GetDeviceDetailsInterface;
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
return devicesData;
|
return devicesData;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// Handle the error here
|
// Handle the error here
|
||||||
@ -147,12 +149,11 @@ export class DeviceService {
|
|||||||
throw new Error('Product UUID is missing for the device.');
|
throw new Error('Product UUID is missing for the device.');
|
||||||
}
|
}
|
||||||
|
|
||||||
await this.deviceRepository.save({
|
return await this.deviceRepository.save({
|
||||||
deviceTuyaUuid: addDeviceInRoomDto.deviceTuyaUuid,
|
deviceTuyaUuid: addDeviceInRoomDto.deviceTuyaUuid,
|
||||||
spaceDevice: { uuid: addDeviceInRoomDto.roomUuid },
|
spaceDevice: { uuid: addDeviceInRoomDto.roomUuid },
|
||||||
productDevice: { uuid: device.productUuid },
|
productDevice: { uuid: device.productUuid },
|
||||||
});
|
});
|
||||||
return { message: 'device added in room successfully' };
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error.code === '23505') {
|
if (error.code === '23505') {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
|
@ -19,6 +19,7 @@ import { CheckBuildingTypeGuard } from 'src/guards/building.type.guard';
|
|||||||
import { CheckUserFloorGuard } from 'src/guards/user.floor.guard';
|
import { CheckUserFloorGuard } from 'src/guards/user.floor.guard';
|
||||||
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
|
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
|
||||||
import { AdminRoleGuard } from 'src/guards/admin.role.guard';
|
import { AdminRoleGuard } from 'src/guards/admin.role.guard';
|
||||||
|
import { FloorPermissionGuard } from 'src/guards/floor.permission.guard';
|
||||||
|
|
||||||
@ApiTags('Floor Module')
|
@ApiTags('Floor Module')
|
||||||
@Controller({
|
@Controller({
|
||||||
@ -34,7 +35,12 @@ export class FloorController {
|
|||||||
async addFloor(@Body() addFloorDto: AddFloorDto) {
|
async addFloor(@Body() addFloorDto: AddFloorDto) {
|
||||||
try {
|
try {
|
||||||
const floor = await this.floorService.addFloor(addFloorDto);
|
const floor = await this.floorService.addFloor(addFloorDto);
|
||||||
return { message: 'Floor added successfully', uuid: floor.uuid };
|
return {
|
||||||
|
statusCode: HttpStatus.CREATED,
|
||||||
|
success: true,
|
||||||
|
message: 'Floor added successfully',
|
||||||
|
data: floor,
|
||||||
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
error.message || 'Internal server error',
|
error.message || 'Internal server error',
|
||||||
@ -44,7 +50,7 @@ export class FloorController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(JwtAuthGuard)
|
@UseGuards(JwtAuthGuard, FloorPermissionGuard)
|
||||||
@Get(':floorUuid')
|
@Get(':floorUuid')
|
||||||
async getFloorByUuid(@Param('floorUuid') floorUuid: string) {
|
async getFloorByUuid(@Param('floorUuid') floorUuid: string) {
|
||||||
try {
|
try {
|
||||||
@ -59,7 +65,7 @@ export class FloorController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(JwtAuthGuard)
|
@UseGuards(JwtAuthGuard, FloorPermissionGuard)
|
||||||
@Get('child/:floorUuid')
|
@Get('child/:floorUuid')
|
||||||
async getFloorChildByUuid(
|
async getFloorChildByUuid(
|
||||||
@Param('floorUuid') floorUuid: string,
|
@Param('floorUuid') floorUuid: string,
|
||||||
@ -79,7 +85,7 @@ export class FloorController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(JwtAuthGuard)
|
@UseGuards(JwtAuthGuard, FloorPermissionGuard)
|
||||||
@Get('parent/:floorUuid')
|
@Get('parent/:floorUuid')
|
||||||
async getFloorParentByUuid(@Param('floorUuid') floorUuid: string) {
|
async getFloorParentByUuid(@Param('floorUuid') floorUuid: string) {
|
||||||
try {
|
try {
|
||||||
@ -99,7 +105,11 @@ export class FloorController {
|
|||||||
async addUserFloor(@Body() addUserFloorDto: AddUserFloorDto) {
|
async addUserFloor(@Body() addUserFloorDto: AddUserFloorDto) {
|
||||||
try {
|
try {
|
||||||
await this.floorService.addUserFloor(addUserFloorDto);
|
await this.floorService.addUserFloor(addUserFloorDto);
|
||||||
return { message: 'user floor added successfully' };
|
return {
|
||||||
|
statusCode: HttpStatus.CREATED,
|
||||||
|
success: true,
|
||||||
|
message: 'user floor added successfully',
|
||||||
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
error.message || 'Internal server error',
|
error.message || 'Internal server error',
|
||||||
@ -123,7 +133,7 @@ export class FloorController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(JwtAuthGuard)
|
@UseGuards(JwtAuthGuard, FloorPermissionGuard)
|
||||||
@Put('rename/:floorUuid')
|
@Put('rename/:floorUuid')
|
||||||
async renameFloorByUuid(
|
async renameFloorByUuid(
|
||||||
@Param('floorUuid') floorUuid: string,
|
@Param('floorUuid') floorUuid: string,
|
||||||
|
35
src/guards/building.permission.guard.ts
Normal file
35
src/guards/building.permission.guard.ts
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
import { SpacePermissionService } from '@app/common/helper/services/space.permission.service';
|
||||||
|
import {
|
||||||
|
BadRequestException,
|
||||||
|
CanActivate,
|
||||||
|
ExecutionContext,
|
||||||
|
Injectable,
|
||||||
|
} from '@nestjs/common';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class BuildingPermissionGuard implements CanActivate {
|
||||||
|
constructor(private readonly permissionService: SpacePermissionService) {}
|
||||||
|
|
||||||
|
async canActivate(context: ExecutionContext): Promise<boolean> {
|
||||||
|
const req = context.switchToHttp().getRequest();
|
||||||
|
|
||||||
|
try {
|
||||||
|
const { buildingUuid } = req.params;
|
||||||
|
const { user } = req;
|
||||||
|
|
||||||
|
if (!buildingUuid) {
|
||||||
|
throw new BadRequestException('buildingUuid is required');
|
||||||
|
}
|
||||||
|
|
||||||
|
await this.permissionService.checkUserPermission(
|
||||||
|
buildingUuid,
|
||||||
|
user.uuid,
|
||||||
|
'building',
|
||||||
|
);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
} catch (error) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
35
src/guards/community.permission.guard.ts
Normal file
35
src/guards/community.permission.guard.ts
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
import { SpacePermissionService } from '@app/common/helper/services/space.permission.service';
|
||||||
|
import {
|
||||||
|
BadRequestException,
|
||||||
|
CanActivate,
|
||||||
|
ExecutionContext,
|
||||||
|
Injectable,
|
||||||
|
} from '@nestjs/common';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class CommunityPermissionGuard implements CanActivate {
|
||||||
|
constructor(private readonly permissionService: SpacePermissionService) {}
|
||||||
|
|
||||||
|
async canActivate(context: ExecutionContext): Promise<boolean> {
|
||||||
|
const req = context.switchToHttp().getRequest();
|
||||||
|
|
||||||
|
try {
|
||||||
|
const { communityUuid } = req.params;
|
||||||
|
const { user } = req;
|
||||||
|
|
||||||
|
if (!communityUuid) {
|
||||||
|
throw new BadRequestException('communityUuid is required');
|
||||||
|
}
|
||||||
|
|
||||||
|
await this.permissionService.checkUserPermission(
|
||||||
|
communityUuid,
|
||||||
|
user.uuid,
|
||||||
|
'community',
|
||||||
|
);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
} catch (error) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
35
src/guards/floor.permission.guard.ts
Normal file
35
src/guards/floor.permission.guard.ts
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
import { SpacePermissionService } from '@app/common/helper/services/space.permission.service';
|
||||||
|
import {
|
||||||
|
BadRequestException,
|
||||||
|
CanActivate,
|
||||||
|
ExecutionContext,
|
||||||
|
Injectable,
|
||||||
|
} from '@nestjs/common';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class FloorPermissionGuard implements CanActivate {
|
||||||
|
constructor(private readonly permissionService: SpacePermissionService) {}
|
||||||
|
|
||||||
|
async canActivate(context: ExecutionContext): Promise<boolean> {
|
||||||
|
const req = context.switchToHttp().getRequest();
|
||||||
|
|
||||||
|
try {
|
||||||
|
const { floorUuid } = req.params;
|
||||||
|
const { user } = req;
|
||||||
|
|
||||||
|
if (!floorUuid) {
|
||||||
|
throw new BadRequestException('floorUuid is required');
|
||||||
|
}
|
||||||
|
|
||||||
|
await this.permissionService.checkUserPermission(
|
||||||
|
floorUuid,
|
||||||
|
user.uuid,
|
||||||
|
'floor',
|
||||||
|
);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
} catch (error) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
35
src/guards/room.permission.guard.ts
Normal file
35
src/guards/room.permission.guard.ts
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
import { SpacePermissionService } from '@app/common/helper/services/space.permission.service';
|
||||||
|
import {
|
||||||
|
BadRequestException,
|
||||||
|
CanActivate,
|
||||||
|
ExecutionContext,
|
||||||
|
Injectable,
|
||||||
|
} from '@nestjs/common';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class RoomPermissionGuard implements CanActivate {
|
||||||
|
constructor(private readonly permissionService: SpacePermissionService) {}
|
||||||
|
|
||||||
|
async canActivate(context: ExecutionContext): Promise<boolean> {
|
||||||
|
const req = context.switchToHttp().getRequest();
|
||||||
|
|
||||||
|
try {
|
||||||
|
const { roomUuid } = req.params;
|
||||||
|
const { user } = req;
|
||||||
|
|
||||||
|
if (!roomUuid) {
|
||||||
|
throw new BadRequestException('roomUuid is required');
|
||||||
|
}
|
||||||
|
|
||||||
|
await this.permissionService.checkUserPermission(
|
||||||
|
roomUuid,
|
||||||
|
user.uuid,
|
||||||
|
'room',
|
||||||
|
);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
} catch (error) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
35
src/guards/unit.permission.guard.ts
Normal file
35
src/guards/unit.permission.guard.ts
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
import { SpacePermissionService } from '@app/common/helper/services/space.permission.service';
|
||||||
|
import {
|
||||||
|
BadRequestException,
|
||||||
|
CanActivate,
|
||||||
|
ExecutionContext,
|
||||||
|
Injectable,
|
||||||
|
} from '@nestjs/common';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class UnitPermissionGuard implements CanActivate {
|
||||||
|
constructor(private readonly permissionService: SpacePermissionService) {}
|
||||||
|
|
||||||
|
async canActivate(context: ExecutionContext): Promise<boolean> {
|
||||||
|
const req = context.switchToHttp().getRequest();
|
||||||
|
|
||||||
|
try {
|
||||||
|
const { unitUuid } = req.params;
|
||||||
|
const { user } = req;
|
||||||
|
|
||||||
|
if (!unitUuid) {
|
||||||
|
throw new BadRequestException('unitUuid is required');
|
||||||
|
}
|
||||||
|
|
||||||
|
await this.permissionService.checkUserPermission(
|
||||||
|
unitUuid,
|
||||||
|
user.uuid,
|
||||||
|
'unit',
|
||||||
|
);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
} catch (error) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -17,6 +17,7 @@ import { CheckUnitTypeGuard } from 'src/guards/unit.type.guard';
|
|||||||
import { CheckUserRoomGuard } from 'src/guards/user.room.guard';
|
import { CheckUserRoomGuard } from 'src/guards/user.room.guard';
|
||||||
import { AdminRoleGuard } from 'src/guards/admin.role.guard';
|
import { AdminRoleGuard } from 'src/guards/admin.role.guard';
|
||||||
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
|
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
|
||||||
|
import { RoomPermissionGuard } from 'src/guards/room.permission.guard';
|
||||||
|
|
||||||
@ApiTags('Room Module')
|
@ApiTags('Room Module')
|
||||||
@Controller({
|
@Controller({
|
||||||
@ -32,7 +33,12 @@ export class RoomController {
|
|||||||
async addRoom(@Body() addRoomDto: AddRoomDto) {
|
async addRoom(@Body() addRoomDto: AddRoomDto) {
|
||||||
try {
|
try {
|
||||||
const room = await this.roomService.addRoom(addRoomDto);
|
const room = await this.roomService.addRoom(addRoomDto);
|
||||||
return { message: 'Room added successfully', uuid: room.uuid };
|
return {
|
||||||
|
statusCode: HttpStatus.CREATED,
|
||||||
|
success: true,
|
||||||
|
message: 'Room added successfully',
|
||||||
|
data: room,
|
||||||
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
error.message || 'Internal server error',
|
error.message || 'Internal server error',
|
||||||
@ -42,7 +48,7 @@ export class RoomController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(JwtAuthGuard)
|
@UseGuards(JwtAuthGuard, RoomPermissionGuard)
|
||||||
@Get(':roomUuid')
|
@Get(':roomUuid')
|
||||||
async getRoomByUuid(@Param('roomUuid') roomUuid: string) {
|
async getRoomByUuid(@Param('roomUuid') roomUuid: string) {
|
||||||
try {
|
try {
|
||||||
@ -57,7 +63,7 @@ export class RoomController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(JwtAuthGuard)
|
@UseGuards(JwtAuthGuard, RoomPermissionGuard)
|
||||||
@Get('parent/:roomUuid')
|
@Get('parent/:roomUuid')
|
||||||
async getRoomParentByUuid(@Param('roomUuid') roomUuid: string) {
|
async getRoomParentByUuid(@Param('roomUuid') roomUuid: string) {
|
||||||
try {
|
try {
|
||||||
@ -76,7 +82,11 @@ export class RoomController {
|
|||||||
async addUserRoom(@Body() addUserRoomDto: AddUserRoomDto) {
|
async addUserRoom(@Body() addUserRoomDto: AddUserRoomDto) {
|
||||||
try {
|
try {
|
||||||
await this.roomService.addUserRoom(addUserRoomDto);
|
await this.roomService.addUserRoom(addUserRoomDto);
|
||||||
return { message: 'user room added successfully' };
|
return {
|
||||||
|
statusCode: HttpStatus.CREATED,
|
||||||
|
success: true,
|
||||||
|
message: 'user room added successfully',
|
||||||
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
error.message || 'Internal server error',
|
error.message || 'Internal server error',
|
||||||
@ -99,7 +109,7 @@ export class RoomController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(JwtAuthGuard)
|
@UseGuards(JwtAuthGuard, RoomPermissionGuard)
|
||||||
@Put('rename/:roomUuid')
|
@Put('rename/:roomUuid')
|
||||||
async renameRoomByUuid(
|
async renameRoomByUuid(
|
||||||
@Param('roomUuid') roomUuid: string,
|
@Param('roomUuid') roomUuid: string,
|
||||||
|
@ -19,6 +19,7 @@ import { CheckFloorTypeGuard } from 'src/guards/floor.type.guard';
|
|||||||
import { CheckUserUnitGuard } from 'src/guards/user.unit.guard';
|
import { CheckUserUnitGuard } from 'src/guards/user.unit.guard';
|
||||||
import { AdminRoleGuard } from 'src/guards/admin.role.guard';
|
import { AdminRoleGuard } from 'src/guards/admin.role.guard';
|
||||||
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
|
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
|
||||||
|
import { UnitPermissionGuard } from 'src/guards/unit.permission.guard';
|
||||||
|
|
||||||
@ApiTags('Unit Module')
|
@ApiTags('Unit Module')
|
||||||
@Controller({
|
@Controller({
|
||||||
@ -34,7 +35,12 @@ export class UnitController {
|
|||||||
async addUnit(@Body() addUnitDto: AddUnitDto) {
|
async addUnit(@Body() addUnitDto: AddUnitDto) {
|
||||||
try {
|
try {
|
||||||
const unit = await this.unitService.addUnit(addUnitDto);
|
const unit = await this.unitService.addUnit(addUnitDto);
|
||||||
return { message: 'Unit added successfully', uuid: unit.uuid };
|
return {
|
||||||
|
statusCode: HttpStatus.CREATED,
|
||||||
|
success: true,
|
||||||
|
message: 'Unit added successfully',
|
||||||
|
data: unit,
|
||||||
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
error.message || 'Internal server error',
|
error.message || 'Internal server error',
|
||||||
@ -44,7 +50,7 @@ export class UnitController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(JwtAuthGuard)
|
@UseGuards(JwtAuthGuard, UnitPermissionGuard)
|
||||||
@Get(':unitUuid')
|
@Get(':unitUuid')
|
||||||
async getUnitByUuid(@Param('unitUuid') unitUuid: string) {
|
async getUnitByUuid(@Param('unitUuid') unitUuid: string) {
|
||||||
try {
|
try {
|
||||||
@ -59,7 +65,7 @@ export class UnitController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(JwtAuthGuard)
|
@UseGuards(JwtAuthGuard, UnitPermissionGuard)
|
||||||
@Get('child/:unitUuid')
|
@Get('child/:unitUuid')
|
||||||
async getUnitChildByUuid(
|
async getUnitChildByUuid(
|
||||||
@Param('unitUuid') unitUuid: string,
|
@Param('unitUuid') unitUuid: string,
|
||||||
@ -76,7 +82,7 @@ export class UnitController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(JwtAuthGuard)
|
@UseGuards(JwtAuthGuard, UnitPermissionGuard)
|
||||||
@Get('parent/:unitUuid')
|
@Get('parent/:unitUuid')
|
||||||
async getUnitParentByUuid(@Param('unitUuid') unitUuid: string) {
|
async getUnitParentByUuid(@Param('unitUuid') unitUuid: string) {
|
||||||
try {
|
try {
|
||||||
@ -95,7 +101,11 @@ export class UnitController {
|
|||||||
async addUserUnit(@Body() addUserUnitDto: AddUserUnitDto) {
|
async addUserUnit(@Body() addUserUnitDto: AddUserUnitDto) {
|
||||||
try {
|
try {
|
||||||
await this.unitService.addUserUnit(addUserUnitDto);
|
await this.unitService.addUserUnit(addUserUnitDto);
|
||||||
return { message: 'user unit added successfully' };
|
return {
|
||||||
|
statusCode: HttpStatus.CREATED,
|
||||||
|
success: true,
|
||||||
|
message: 'user unit added successfully',
|
||||||
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
error.message || 'Internal server error',
|
error.message || 'Internal server error',
|
||||||
@ -118,7 +128,7 @@ export class UnitController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(JwtAuthGuard)
|
@UseGuards(JwtAuthGuard, UnitPermissionGuard)
|
||||||
@Put('rename/:unitUuid')
|
@Put('rename/:unitUuid')
|
||||||
async renameUnitByUuid(
|
async renameUnitByUuid(
|
||||||
@Param('unitUuid') unitUuid: string,
|
@Param('unitUuid') unitUuid: string,
|
||||||
|
Reference in New Issue
Block a user