Merge branch 'master' into dev
This commit is contained in:
Vendored
+3
@@ -5,5 +5,8 @@
|
|||||||
"editor.codeActionsOnSave": {
|
"editor.codeActionsOnSave": {
|
||||||
"source.fixAll": "explicit",
|
"source.fixAll": "explicit",
|
||||||
"source.fixAll.eslint": "explicit"
|
"source.fixAll.eslint": "explicit"
|
||||||
|
},
|
||||||
|
"[typescript]": {
|
||||||
|
"editor.defaultFormatter": "vscode.typescript-language-features"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,66 @@
|
|||||||
|
import { CanActivate, ExecutionContext, Injectable, UnauthorizedException, Inject, Logger } from '@nestjs/common';
|
||||||
|
import { Request } from 'express';
|
||||||
|
import { ConfigService } from '@nestjs/config';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class SuperAdminAuthGuard implements CanActivate {
|
||||||
|
private readonly logger = new Logger(SuperAdminAuthGuard.name);
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
@Inject(ConfigService)
|
||||||
|
private readonly configService: ConfigService,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
canActivate(context: ExecutionContext): boolean {
|
||||||
|
const request = context.switchToHttp().getRequest<Request>();
|
||||||
|
|
||||||
|
try {
|
||||||
|
const credentials = this.extractBasicAuthCredentials(request);
|
||||||
|
if (!credentials) {
|
||||||
|
throw new UnauthorizedException('Basic authentication required');
|
||||||
|
}
|
||||||
|
|
||||||
|
const { username, password } = credentials;
|
||||||
|
const expectedUsername = this.configService.get<string>('SUPER_ADMIN_USERNAME') ?? 'danak@dsc.com';
|
||||||
|
const expectedPassword = this.configService.get<string>('SUPER_ADMIN_PASSWORD') ?? 'DsCdAnAk?@ABC';
|
||||||
|
if (username !== expectedUsername || password !== expectedPassword) {
|
||||||
|
this.logger.warn(`Invalid super admin credentials attempt for username: ${username}`);
|
||||||
|
throw new UnauthorizedException('Invalid credentials');
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
} catch (err) {
|
||||||
|
if (err instanceof UnauthorizedException) {
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
this.logger.error('error in SuperAdminAuthGuard', err);
|
||||||
|
throw new UnauthorizedException('Authentication failed');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private extractBasicAuthCredentials(request: Request): { username: string; password: string } | null {
|
||||||
|
const authHeader = request.headers.authorization;
|
||||||
|
if (!authHeader) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const [type, encoded] = authHeader.split(' ');
|
||||||
|
if (type?.toLowerCase() !== 'basic' || !encoded) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const decoded = Buffer.from(encoded, 'base64').toString('utf-8');
|
||||||
|
const [username, password] = decoded.split(':');
|
||||||
|
|
||||||
|
if (!username || !password) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return { username, password };
|
||||||
|
} catch (error) {
|
||||||
|
this.logger.error('Failed to decode Basic Auth credentials', error);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,19 +8,16 @@ import { UpdateGroupDto } from '../dto/update-group.dto';
|
|||||||
import {
|
import {
|
||||||
ApiTags,
|
ApiTags,
|
||||||
ApiOperation,
|
ApiOperation,
|
||||||
ApiCreatedResponse,
|
|
||||||
ApiOkResponse,
|
|
||||||
ApiNotFoundResponse,
|
|
||||||
ApiParam,
|
ApiParam,
|
||||||
ApiBody,
|
ApiBody,
|
||||||
ApiBearerAuth,
|
ApiBearerAuth,
|
||||||
} from '@nestjs/swagger';
|
} from '@nestjs/swagger';
|
||||||
import { AdminAuthGuard } from 'src/modules/auth/guards/adminAuth.guard';
|
import { SuperAdminAuthGuard } from 'src/modules/auth/guards/superAdminAuth.guard';
|
||||||
import { UseGuards } from '@nestjs/common';
|
import { UseGuards } from '@nestjs/common';
|
||||||
|
import { AdminAuthGuard } from 'src/modules/auth/guards/adminAuth.guard';
|
||||||
|
|
||||||
@ApiTags('icons')
|
@ApiTags('icons')
|
||||||
@Controller('admin/icons')
|
@Controller()
|
||||||
@UseGuards(AdminAuthGuard)
|
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
export class IconsController {
|
export class IconsController {
|
||||||
constructor(
|
constructor(
|
||||||
@@ -28,86 +25,91 @@ export class IconsController {
|
|||||||
private readonly groupService: GroupService,
|
private readonly groupService: GroupService,
|
||||||
) { }
|
) { }
|
||||||
|
|
||||||
// Icon endpoints
|
@Get('admin/groups/icons')
|
||||||
@Post()
|
@UseGuards(AdminAuthGuard)
|
||||||
|
@ApiOperation({ summary: 'Get all icons' })
|
||||||
|
findAllPublicIcons() {
|
||||||
|
return this.groupService.findAllPublic();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Post('super-admin/icons')
|
||||||
|
@UseGuards(SuperAdminAuthGuard)
|
||||||
@ApiOperation({ summary: 'Create icon' })
|
@ApiOperation({ summary: 'Create icon' })
|
||||||
@ApiCreatedResponse({ description: 'Icon created', type: CreateIconDto })
|
|
||||||
@ApiBody({ type: CreateIconDto })
|
@ApiBody({ type: CreateIconDto })
|
||||||
createIcon(@Body() dto: CreateIconDto) {
|
createIcon(@Body() dto: CreateIconDto) {
|
||||||
return this.iconService.create(dto);
|
return this.iconService.create(dto);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Get()
|
@Get('super-admin/icons')
|
||||||
|
@UseGuards(SuperAdminAuthGuard)
|
||||||
@ApiOperation({ summary: 'Get all icons' })
|
@ApiOperation({ summary: 'Get all icons' })
|
||||||
@ApiOkResponse({ description: 'List of all icons' })
|
|
||||||
findAllIcons() {
|
findAllIcons() {
|
||||||
return this.iconService.findAll();
|
return this.iconService.findAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Get(':id')
|
@Get('super-admin/icons/:id')
|
||||||
|
@UseGuards(SuperAdminAuthGuard)
|
||||||
@ApiOperation({ summary: 'Get icon by id' })
|
@ApiOperation({ summary: 'Get icon by id' })
|
||||||
@ApiOkResponse({ description: 'Icon detail', type: CreateIconDto })
|
|
||||||
@ApiNotFoundResponse({ description: 'Icon not found' })
|
|
||||||
@ApiParam({ name: 'id', required: true, type: String })
|
@ApiParam({ name: 'id', required: true, type: String })
|
||||||
findOneIcon(@Param('id') id: string) {
|
findOneIcon(@Param('id') id: string) {
|
||||||
return this.iconService.findOne(id);
|
return this.iconService.findOne(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Patch(':id')
|
@Patch('super-admin/icons/:id')
|
||||||
|
@UseGuards(SuperAdminAuthGuard)
|
||||||
@ApiOperation({ summary: 'Update icon' })
|
@ApiOperation({ summary: 'Update icon' })
|
||||||
@ApiParam({ name: 'id' })
|
@ApiParam({ name: 'id' })
|
||||||
@ApiBody({ type: UpdateIconDto })
|
@ApiBody({ type: UpdateIconDto })
|
||||||
@ApiOkResponse({ description: 'Updated icon', type: UpdateIconDto })
|
|
||||||
updateIcon(@Param('id') id: string, @Body() dto: UpdateIconDto) {
|
updateIcon(@Param('id') id: string, @Body() dto: UpdateIconDto) {
|
||||||
return this.iconService.update(id, dto);
|
return this.iconService.update(id, dto);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Delete(':id')
|
@Delete('super-admin/icons/:id')
|
||||||
|
@UseGuards(SuperAdminAuthGuard)
|
||||||
@ApiOperation({ summary: 'Delete icon' })
|
@ApiOperation({ summary: 'Delete icon' })
|
||||||
@ApiParam({ name: 'id' })
|
@ApiParam({ name: 'id' })
|
||||||
@ApiOkResponse({ description: 'Deleted' })
|
|
||||||
removeIcon(@Param('id') id: string) {
|
removeIcon(@Param('id') id: string) {
|
||||||
return this.iconService.remove(id);
|
return this.iconService.remove(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Group endpoints (must come before :id routes to avoid route conflicts)
|
// Group endpoints (must come before :id routes to avoid route conflicts)
|
||||||
@Post('groups')
|
@Post('super-admin/icons/groups')
|
||||||
|
@UseGuards(SuperAdminAuthGuard)
|
||||||
@ApiOperation({ summary: 'Create icon group' })
|
@ApiOperation({ summary: 'Create icon group' })
|
||||||
@ApiCreatedResponse({ description: 'Group created', type: CreateGroupDto })
|
|
||||||
@ApiBody({ type: CreateGroupDto })
|
@ApiBody({ type: CreateGroupDto })
|
||||||
createGroup(@Body() dto: CreateGroupDto) {
|
createGroup(@Body() dto: CreateGroupDto) {
|
||||||
return this.groupService.create(dto);
|
return this.groupService.create(dto);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Get('groups')
|
@Get('super-admin/icons/groups')
|
||||||
|
@UseGuards(SuperAdminAuthGuard)
|
||||||
@ApiOperation({ summary: 'Get all icon groups' })
|
@ApiOperation({ summary: 'Get all icon groups' })
|
||||||
@ApiOkResponse({ description: 'List of all icon groups' })
|
|
||||||
findAllGroups() {
|
findAllGroups() {
|
||||||
return this.groupService.findAll();
|
return this.groupService.findAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Get('groups/:id')
|
@Get('super-admin/icons/groups/:id')
|
||||||
|
@UseGuards(SuperAdminAuthGuard)
|
||||||
|
|
||||||
@ApiOperation({ summary: 'Get icon group by id' })
|
@ApiOperation({ summary: 'Get icon group by id' })
|
||||||
@ApiOkResponse({ description: 'Group detail', type: CreateGroupDto })
|
|
||||||
@ApiNotFoundResponse({ description: 'Group not found' })
|
|
||||||
@ApiParam({ name: 'id', required: true, type: String })
|
@ApiParam({ name: 'id', required: true, type: String })
|
||||||
findOneGroup(@Param('id') id: string) {
|
findOneGroup(@Param('id') id: string) {
|
||||||
return this.groupService.findOne(id);
|
return this.groupService.findOne(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Patch('groups/:id')
|
@Patch('super-admin/icons/groups/:id')
|
||||||
|
@UseGuards(SuperAdminAuthGuard)
|
||||||
@ApiOperation({ summary: 'Update icon group' })
|
@ApiOperation({ summary: 'Update icon group' })
|
||||||
@ApiParam({ name: 'id' })
|
@ApiParam({ name: 'id' })
|
||||||
@ApiBody({ type: UpdateGroupDto })
|
|
||||||
@ApiOkResponse({ description: 'Updated group', type: UpdateGroupDto })
|
|
||||||
updateGroup(@Param('id') id: string, @Body() dto: UpdateGroupDto) {
|
updateGroup(@Param('id') id: string, @Body() dto: UpdateGroupDto) {
|
||||||
return this.groupService.update(id, dto);
|
return this.groupService.update(id, dto);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Delete('groups/:id')
|
@Delete('super-admin/icons/groups/:id')
|
||||||
|
@UseGuards(SuperAdminAuthGuard)
|
||||||
@ApiOperation({ summary: 'Delete icon group' })
|
@ApiOperation({ summary: 'Delete icon group' })
|
||||||
@ApiParam({ name: 'id' })
|
@ApiParam({ name: 'id' })
|
||||||
@ApiOkResponse({ description: 'Deleted' })
|
|
||||||
removeGroup(@Param('id') id: string) {
|
removeGroup(@Param('id') id: string) {
|
||||||
return this.groupService.remove(id);
|
return this.groupService.remove(id);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { Entity, Index, Property, Collection, OneToMany } from '@mikro-orm/core';
|
import { Entity, Index, Property, Collection, OneToMany, Cascade } from '@mikro-orm/core';
|
||||||
import { BaseEntity } from '../../../common/entities/base.entity';
|
import { BaseEntity } from '../../../common/entities/base.entity';
|
||||||
import { Icon } from './icon.entity';
|
import { Icon } from './icon.entity';
|
||||||
|
|
||||||
@@ -7,6 +7,9 @@ export class Group extends BaseEntity {
|
|||||||
@Property()
|
@Property()
|
||||||
name!: string;
|
name!: string;
|
||||||
|
|
||||||
@OneToMany(() => Icon, icon => icon.group)
|
@OneToMany(() => Icon, icon => icon.group,{
|
||||||
|
cascade: [Cascade.ALL],
|
||||||
|
orphanRemoval: true,
|
||||||
|
})
|
||||||
icons = new Collection<Icon>(this);
|
icons = new Collection<Icon>(this);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -47,12 +47,15 @@ export class GroupService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async remove(id: string): Promise<void> {
|
async remove(id: string): Promise<void> {
|
||||||
const group = await this.groupRepository.findOne({ id });
|
const group = await this.groupRepository.findOne({ id }, { populate: ['icons'] });
|
||||||
if (!group) {
|
if (!group) {
|
||||||
throw new NotFoundException(GroupMessage.NOT_FOUND);
|
throw new NotFoundException(GroupMessage.NOT_FOUND);
|
||||||
}
|
}
|
||||||
group.deletedAt = new Date();
|
await this.em.removeAndFlush(group);
|
||||||
await this.em.persistAndFlush(group);
|
}
|
||||||
|
|
||||||
|
findAllPublic(): Promise<Group[]> {
|
||||||
|
return this.groupRepository.find({ deletedAt: null }, { populate: ['icons'] });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -70,8 +70,7 @@ export class IconService {
|
|||||||
if (!icon) {
|
if (!icon) {
|
||||||
throw new NotFoundException(IconMessage.NOT_FOUND);
|
throw new NotFoundException(IconMessage.NOT_FOUND);
|
||||||
}
|
}
|
||||||
icon.deletedAt = new Date();
|
await this.em.removeAndFlush(icon);
|
||||||
await this.em.persistAndFlush(icon);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user