icydns/src/modules/icynet/icynet.controller.ts

83 lines
2.0 KiB
TypeScript

import {
Controller,
Delete,
Get,
HttpException,
Param,
Post,
Put,
Query,
UseGuards,
} from '@nestjs/common';
import { ApiExcludeController } from '@nestjs/swagger';
import { IcynetGuard } from 'src/guards/icynet.guard';
import { ManagerService } from '../objects/manager/manager.service';
import { ZoneEntity } from '../objects/manager/zone.entity';
@ApiExcludeController()
@UseGuards(IcynetGuard)
@Controller({
path: 'api/v1/icynet',
})
export class IcynetController {
constructor(private service: ManagerService) {}
@Get('zones')
async getZoneList(@Query('uuid') uuid?: string) {
let list: ZoneEntity[] = [];
if (uuid) {
list = await this.service.getZonesByIcynetUUID(uuid);
} else {
list = await this.service.getAllZones();
}
return list.map(({ zone }) => zone);
}
@Get('access')
async getAccessList() {
return this.service.getZonesWithIcynet();
}
@Put('zone/:domain/:uuid')
async addZoneAccess(
@Param('uuid') uuid: string,
@Param('domain') domain: string,
) {
const success = await this.service.authorizeIcynetUser(uuid, domain);
return { success };
}
@Delete('zone/:domain/:uuid')
async removeZoneAccess(
@Param('uuid') uuid: string,
@Param('domain') domain: string,
) {
const success = await this.service.revokeIcynetUser(uuid, domain);
return { success };
}
@Post('zone/:domain/:uuid')
async getAccess(
@Param('uuid') uuid: string,
@Param('domain') domain: string,
) {
const added = await this.service.createIcynetAccessKey(uuid, domain);
if (!added) throw new HttpException('Invalid request', 400);
return { token: added.key };
}
@Put('zone/:domain')
async addZone(@Param('domain') domain: string) {
const added = await this.service.addZone(domain);
return { success: !!added };
}
@Delete('zone/:domain')
async removeZone(@Param('domain') domain: string) {
const success = await this.service.removeZone(domain);
return { success };
}
}