mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-26 22:24:55 +00:00
remove unused guard
This commit is contained in:
@ -9,7 +9,6 @@ export class SpacePermissionService {
|
||||
async checkUserPermission(
|
||||
spaceUuid: string,
|
||||
userUuid: string,
|
||||
type: string,
|
||||
): Promise<void> {
|
||||
try {
|
||||
const spaceData = await this.spaceRepository.findOne({
|
||||
@ -21,12 +20,12 @@ export class SpacePermissionService {
|
||||
},
|
||||
},
|
||||
},
|
||||
relations: ['spaceType', 'userSpaces', 'userSpaces.user'],
|
||||
relations: ['userSpaces', 'userSpaces.user'],
|
||||
});
|
||||
|
||||
if (!spaceData) {
|
||||
throw new BadRequestException(
|
||||
`You do not have permission to access this ${type}`,
|
||||
`You do not have permission to access this space`,
|
||||
);
|
||||
}
|
||||
} catch (err) {
|
||||
|
||||
@ -4,7 +4,6 @@ import { CommunityController } from './controllers/community.controller';
|
||||
import { ConfigModule } from '@nestjs/config';
|
||||
import { SpaceRepositoryModule } from '@app/common/modules/space/space.repository.module';
|
||||
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||
import { SpaceTypeRepository } from '@app/common/modules/space/repositories';
|
||||
import { UserSpaceRepository } from '@app/common/modules/user/repositories';
|
||||
import { UserRepositoryModule } from '@app/common/modules/user/user.repository.module';
|
||||
import { SpacePermissionService } from '@app/common/helper/services';
|
||||
@ -17,7 +16,6 @@ import { TuyaService } from '@app/common/integrations/tuya/tuya.service';
|
||||
providers: [
|
||||
CommunityService,
|
||||
SpaceRepository,
|
||||
SpaceTypeRepository,
|
||||
UserSpaceRepository,
|
||||
TuyaService,
|
||||
CommunityRepository,
|
||||
|
||||
@ -1,36 +0,0 @@
|
||||
import { SpaceType } from '@app/common/constants/space-type.enum';
|
||||
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,
|
||||
SpaceType.BUILDING,
|
||||
);
|
||||
|
||||
return true;
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,62 +0,0 @@
|
||||
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||
import {
|
||||
Injectable,
|
||||
CanActivate,
|
||||
HttpStatus,
|
||||
BadRequestException,
|
||||
ExecutionContext,
|
||||
} from '@nestjs/common';
|
||||
|
||||
@Injectable()
|
||||
export class CheckBuildingTypeGuard implements CanActivate {
|
||||
constructor(private readonly spaceRepository: SpaceRepository) {}
|
||||
|
||||
async canActivate(context: ExecutionContext): Promise<boolean> {
|
||||
const req = context.switchToHttp().getRequest();
|
||||
|
||||
try {
|
||||
const { floorName, buildingUuid } = req.body;
|
||||
|
||||
if (!floorName) {
|
||||
throw new BadRequestException('floorName is required');
|
||||
}
|
||||
|
||||
if (!buildingUuid) {
|
||||
throw new BadRequestException('buildingUuid is required');
|
||||
}
|
||||
|
||||
await this.checkBuildingIsBuildingType(buildingUuid);
|
||||
|
||||
return true;
|
||||
} catch (error) {
|
||||
this.handleGuardError(error, context);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async checkBuildingIsBuildingType(buildingUuid: string) {
|
||||
const buildingData = await this.spaceRepository.findOne({
|
||||
where: { uuid: buildingUuid },
|
||||
relations: ['spaceType'],
|
||||
});
|
||||
if (!buildingData) {
|
||||
throw new BadRequestException('Invalid building UUID');
|
||||
}
|
||||
}
|
||||
|
||||
private handleGuardError(error: Error, context: ExecutionContext) {
|
||||
const response = context.switchToHttp().getResponse();
|
||||
console.error(error);
|
||||
|
||||
if (error instanceof BadRequestException) {
|
||||
response
|
||||
.status(HttpStatus.BAD_REQUEST)
|
||||
.json({ statusCode: HttpStatus.BAD_REQUEST, message: error.message });
|
||||
} else {
|
||||
response.status(HttpStatus.NOT_FOUND).json({
|
||||
statusCode: HttpStatus.NOT_FOUND,
|
||||
message: 'Building not found',
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,63 +0,0 @@
|
||||
import {
|
||||
CanActivate,
|
||||
ExecutionContext,
|
||||
Injectable,
|
||||
HttpStatus,
|
||||
} from '@nestjs/common';
|
||||
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||
import { BadRequestException } from '@nestjs/common';
|
||||
|
||||
@Injectable()
|
||||
export class CheckCommunityTypeGuard implements CanActivate {
|
||||
constructor(private readonly spaceRepository: SpaceRepository) {}
|
||||
|
||||
async canActivate(context: ExecutionContext): Promise<boolean> {
|
||||
const req = context.switchToHttp().getRequest();
|
||||
|
||||
try {
|
||||
const { buildingName, communityUuid } = req.body;
|
||||
|
||||
if (!buildingName) {
|
||||
throw new BadRequestException('buildingName is required');
|
||||
}
|
||||
|
||||
if (!communityUuid) {
|
||||
throw new BadRequestException('communityUuid is required');
|
||||
}
|
||||
|
||||
await this.checkCommunityIsCommunityType(communityUuid);
|
||||
|
||||
return true;
|
||||
} catch (error) {
|
||||
this.handleGuardError(error, context);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private async checkCommunityIsCommunityType(communityUuid: string) {
|
||||
const communityData = await this.spaceRepository.findOne({
|
||||
where: { uuid: communityUuid },
|
||||
relations: ['spaceType'],
|
||||
});
|
||||
|
||||
if (!communityData) {
|
||||
throw new BadRequestException('Invalid community UUID');
|
||||
}
|
||||
}
|
||||
|
||||
private handleGuardError(error: Error, context: ExecutionContext) {
|
||||
const response = context.switchToHttp().getResponse();
|
||||
console.error(error);
|
||||
|
||||
if (error instanceof BadRequestException) {
|
||||
response
|
||||
.status(HttpStatus.BAD_REQUEST)
|
||||
.json({ statusCode: HttpStatus.BAD_REQUEST, message: error.message });
|
||||
} else {
|
||||
response.status(HttpStatus.NOT_FOUND).json({
|
||||
statusCode: HttpStatus.NOT_FOUND,
|
||||
message: 'Community not found',
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,36 +0,0 @@
|
||||
import { SpaceType } from '@app/common/constants/space-type.enum';
|
||||
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,
|
||||
SpaceType.FLOOR,
|
||||
);
|
||||
|
||||
return true;
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,36 +0,0 @@
|
||||
import { SpaceType } from '@app/common/constants/space-type.enum';
|
||||
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,
|
||||
SpaceType.ROOM,
|
||||
);
|
||||
|
||||
return true;
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,4 +1,3 @@
|
||||
import { SpaceType } from '@app/common/constants/space-type.enum';
|
||||
import { SpacePermissionService } from '@app/common/helper/services/space.permission.service';
|
||||
import {
|
||||
BadRequestException,
|
||||
@ -22,11 +21,7 @@ export class UnitPermissionGuard implements CanActivate {
|
||||
throw new BadRequestException('unitUuid is required');
|
||||
}
|
||||
|
||||
await this.permissionService.checkUserPermission(
|
||||
unitUuid,
|
||||
user.uuid,
|
||||
SpaceType.UNIT,
|
||||
);
|
||||
await this.permissionService.checkUserPermission(unitUuid, user.uuid);
|
||||
|
||||
return true;
|
||||
} catch (error) {
|
||||
|
||||
@ -1,70 +0,0 @@
|
||||
import {
|
||||
CanActivate,
|
||||
ExecutionContext,
|
||||
Injectable,
|
||||
HttpStatus,
|
||||
} from '@nestjs/common';
|
||||
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||
import { BadRequestException, NotFoundException } from '@nestjs/common';
|
||||
import { UserRepository } from '@app/common/modules/user/repositories';
|
||||
|
||||
@Injectable()
|
||||
export class CheckUserBuildingGuard implements CanActivate {
|
||||
constructor(
|
||||
private readonly spaceRepository: SpaceRepository,
|
||||
private readonly userRepository: UserRepository,
|
||||
) {}
|
||||
|
||||
async canActivate(context: ExecutionContext): Promise<boolean> {
|
||||
const req = context.switchToHttp().getRequest();
|
||||
|
||||
try {
|
||||
const { userUuid, buildingUuid } = req.body;
|
||||
|
||||
await this.checkUserIsFound(userUuid);
|
||||
|
||||
await this.checkBuildingIsFound(buildingUuid);
|
||||
|
||||
return true;
|
||||
} catch (error) {
|
||||
this.handleGuardError(error, context);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private async checkUserIsFound(userUuid: string) {
|
||||
const userData = await this.userRepository.findOne({
|
||||
where: { uuid: userUuid },
|
||||
});
|
||||
if (!userData) {
|
||||
throw new NotFoundException('User not found');
|
||||
}
|
||||
}
|
||||
|
||||
private async checkBuildingIsFound(spaceUuid: string) {
|
||||
const spaceData = await this.spaceRepository.findOne({
|
||||
where: { uuid: spaceUuid },
|
||||
relations: ['spaceType'],
|
||||
});
|
||||
if (!spaceData) {
|
||||
throw new NotFoundException('Building not found');
|
||||
}
|
||||
}
|
||||
|
||||
private handleGuardError(error: Error, context: ExecutionContext) {
|
||||
const response = context.switchToHttp().getResponse();
|
||||
if (
|
||||
error instanceof BadRequestException ||
|
||||
error instanceof NotFoundException
|
||||
) {
|
||||
response
|
||||
.status(HttpStatus.NOT_FOUND)
|
||||
.json({ statusCode: HttpStatus.NOT_FOUND, message: error.message });
|
||||
} else {
|
||||
response.status(HttpStatus.BAD_REQUEST).json({
|
||||
statusCode: HttpStatus.BAD_REQUEST,
|
||||
message: 'invalid userUuid or buildingUuid',
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,70 +0,0 @@
|
||||
import {
|
||||
CanActivate,
|
||||
ExecutionContext,
|
||||
Injectable,
|
||||
HttpStatus,
|
||||
} from '@nestjs/common';
|
||||
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||
import { BadRequestException, NotFoundException } from '@nestjs/common';
|
||||
import { UserRepository } from '@app/common/modules/user/repositories';
|
||||
import { SpaceType } from '@app/common/constants/space-type.enum';
|
||||
|
||||
@Injectable()
|
||||
export class CheckUserFloorGuard implements CanActivate {
|
||||
constructor(
|
||||
private readonly spaceRepository: SpaceRepository,
|
||||
private readonly userRepository: UserRepository,
|
||||
) {}
|
||||
|
||||
async canActivate(context: ExecutionContext): Promise<boolean> {
|
||||
const req = context.switchToHttp().getRequest();
|
||||
|
||||
try {
|
||||
const { userUuid, floorUuid } = req.body;
|
||||
|
||||
await this.checkUserIsFound(userUuid);
|
||||
|
||||
await this.checkFloorIsFound(floorUuid);
|
||||
|
||||
return true;
|
||||
} catch (error) {
|
||||
this.handleGuardError(error, context);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private async checkUserIsFound(userUuid: string) {
|
||||
const userData = await this.userRepository.findOne({
|
||||
where: { uuid: userUuid },
|
||||
});
|
||||
if (!userData) {
|
||||
throw new NotFoundException('User not found');
|
||||
}
|
||||
}
|
||||
|
||||
private async checkFloorIsFound(spaceUuid: string) {
|
||||
const spaceData = await this.spaceRepository.findOne({
|
||||
where: { uuid: spaceUuid },
|
||||
});
|
||||
if (!spaceData) {
|
||||
throw new NotFoundException('Floor not found');
|
||||
}
|
||||
}
|
||||
|
||||
private handleGuardError(error: Error, context: ExecutionContext) {
|
||||
const response = context.switchToHttp().getResponse();
|
||||
if (
|
||||
error instanceof BadRequestException ||
|
||||
error instanceof NotFoundException
|
||||
) {
|
||||
response
|
||||
.status(HttpStatus.NOT_FOUND)
|
||||
.json({ statusCode: HttpStatus.NOT_FOUND, message: error.message });
|
||||
} else {
|
||||
response.status(HttpStatus.BAD_REQUEST).json({
|
||||
statusCode: HttpStatus.BAD_REQUEST,
|
||||
message: 'invalid userUuid or floorUuid',
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,70 +0,0 @@
|
||||
import {
|
||||
CanActivate,
|
||||
ExecutionContext,
|
||||
Injectable,
|
||||
HttpStatus,
|
||||
} from '@nestjs/common';
|
||||
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||
import { BadRequestException, NotFoundException } from '@nestjs/common';
|
||||
import { UserRepository } from '@app/common/modules/user/repositories';
|
||||
|
||||
@Injectable()
|
||||
export class CheckUserRoomGuard implements CanActivate {
|
||||
constructor(
|
||||
private readonly spaceRepository: SpaceRepository,
|
||||
private readonly userRepository: UserRepository,
|
||||
) {}
|
||||
|
||||
async canActivate(context: ExecutionContext): Promise<boolean> {
|
||||
const req = context.switchToHttp().getRequest();
|
||||
|
||||
try {
|
||||
const { userUuid, roomUuid } = req.body;
|
||||
|
||||
await this.checkUserIsFound(userUuid);
|
||||
|
||||
await this.checkRoomIsFound(roomUuid);
|
||||
|
||||
return true;
|
||||
} catch (error) {
|
||||
this.handleGuardError(error, context);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private async checkUserIsFound(userUuid: string) {
|
||||
const userData = await this.userRepository.findOne({
|
||||
where: { uuid: userUuid },
|
||||
});
|
||||
if (!userData) {
|
||||
throw new NotFoundException('User not found');
|
||||
}
|
||||
}
|
||||
|
||||
private async checkRoomIsFound(spaceUuid: string) {
|
||||
const spaceData = await this.spaceRepository.findOne({
|
||||
where: { uuid: spaceUuid },
|
||||
relations: ['spaceType'],
|
||||
});
|
||||
if (!spaceData) {
|
||||
throw new NotFoundException('Room not found');
|
||||
}
|
||||
}
|
||||
|
||||
private handleGuardError(error: Error, context: ExecutionContext) {
|
||||
const response = context.switchToHttp().getResponse();
|
||||
if (
|
||||
error instanceof BadRequestException ||
|
||||
error instanceof NotFoundException
|
||||
) {
|
||||
response
|
||||
.status(HttpStatus.NOT_FOUND)
|
||||
.json({ statusCode: HttpStatus.NOT_FOUND, message: error.message });
|
||||
} else {
|
||||
response.status(HttpStatus.BAD_REQUEST).json({
|
||||
statusCode: HttpStatus.BAD_REQUEST,
|
||||
message: 'invalid userUuid or roomUuid',
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,70 +0,0 @@
|
||||
import {
|
||||
CanActivate,
|
||||
ExecutionContext,
|
||||
Injectable,
|
||||
HttpStatus,
|
||||
} from '@nestjs/common';
|
||||
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||
import { BadRequestException, NotFoundException } from '@nestjs/common';
|
||||
import { UserRepository } from '@app/common/modules/user/repositories';
|
||||
|
||||
@Injectable()
|
||||
export class CheckUserUnitGuard implements CanActivate {
|
||||
constructor(
|
||||
private readonly spaceRepository: SpaceRepository,
|
||||
private readonly userRepository: UserRepository,
|
||||
) {}
|
||||
|
||||
async canActivate(context: ExecutionContext): Promise<boolean> {
|
||||
const req = context.switchToHttp().getRequest();
|
||||
|
||||
try {
|
||||
const { userUuid, unitUuid } = req.body;
|
||||
|
||||
await this.checkUserIsFound(userUuid);
|
||||
|
||||
await this.checkUnitIsFound(unitUuid);
|
||||
|
||||
return true;
|
||||
} catch (error) {
|
||||
this.handleGuardError(error, context);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private async checkUserIsFound(userUuid: string) {
|
||||
const userData = await this.userRepository.findOne({
|
||||
where: { uuid: userUuid },
|
||||
});
|
||||
if (!userData) {
|
||||
throw new NotFoundException('User not found');
|
||||
}
|
||||
}
|
||||
|
||||
private async checkUnitIsFound(spaceUuid: string) {
|
||||
const spaceData = await this.spaceRepository.findOne({
|
||||
where: { uuid: spaceUuid },
|
||||
relations: ['spaceType'],
|
||||
});
|
||||
if (!spaceData) {
|
||||
throw new NotFoundException('Unit not found');
|
||||
}
|
||||
}
|
||||
|
||||
private handleGuardError(error: Error, context: ExecutionContext) {
|
||||
const response = context.switchToHttp().getResponse();
|
||||
if (
|
||||
error instanceof BadRequestException ||
|
||||
error instanceof NotFoundException
|
||||
) {
|
||||
response
|
||||
.status(HttpStatus.NOT_FOUND)
|
||||
.json({ statusCode: HttpStatus.NOT_FOUND, message: error.message });
|
||||
} else {
|
||||
response.status(HttpStatus.BAD_REQUEST).json({
|
||||
statusCode: HttpStatus.BAD_REQUEST,
|
||||
message: 'invalid userUuid or unitUuid',
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user