mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-27 19:44:54 +00:00
Add user space type guards
This commit is contained in:
70
src/guards/user.building.guard.ts
Normal file
70
src/guards/user.building.guard.ts
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
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, spaceType: { type: 'building' } },
|
||||||
|
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',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
70
src/guards/user.community.guard.ts
Normal file
70
src/guards/user.community.guard.ts
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
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 CheckUserCommunityGuard 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, communityUuid } = req.body;
|
||||||
|
|
||||||
|
await this.checkUserIsFound(userUuid);
|
||||||
|
|
||||||
|
await this.checkCommunityIsFound(communityUuid);
|
||||||
|
|
||||||
|
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 checkCommunityIsFound(spaceUuid: string) {
|
||||||
|
const spaceData = await this.spaceRepository.findOne({
|
||||||
|
where: { uuid: spaceUuid, spaceType: { type: 'community' } },
|
||||||
|
relations: ['spaceType'],
|
||||||
|
});
|
||||||
|
if (!spaceData) {
|
||||||
|
throw new NotFoundException('Community 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 communityUuid',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
70
src/guards/user.floor.guard.ts
Normal file
70
src/guards/user.floor.guard.ts
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
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 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, spaceType: { type: 'floor' } },
|
||||||
|
relations: ['spaceType'],
|
||||||
|
});
|
||||||
|
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',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
70
src/guards/user.room.guard.ts
Normal file
70
src/guards/user.room.guard.ts
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
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, spaceType: { type: 'room' } },
|
||||||
|
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',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
70
src/guards/user.unit.guard.ts
Normal file
70
src/guards/user.unit.guard.ts
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
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, spaceType: { type: 'unit' } },
|
||||||
|
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