Merge pull request 'Auth Check For Decks' (#68) from Backend_Fix into main
Reviewed-on: #68
This commit was merged in pull request #68.
This commit is contained in:
@@ -199,12 +199,13 @@ deckRouter.patch('/:id', authRequired, async (req, res) => {
|
|||||||
try {
|
try {
|
||||||
const deckId = req.params.id;
|
const deckId = req.params.id;
|
||||||
const userId = (req as any).user.userId;
|
const userId = (req as any).user.userId;
|
||||||
|
const authLevel = (req as any).user.authLevel;
|
||||||
logRequest('Update deck endpoint accessed', req, res, { deckId, userId, updateFields: Object.keys(req.body) });
|
logRequest('Update deck endpoint accessed', req, res, { deckId, userId, updateFields: Object.keys(req.body) });
|
||||||
|
|
||||||
// Convert string enum values to integers
|
// Convert string enum values to integers
|
||||||
const updateData = convertEnumValues(req.body);
|
const updateData = convertEnumValues(req.body);
|
||||||
|
|
||||||
const result = await container.updateDeckCommandHandler.execute({ id: deckId, ...updateData });
|
const result = await container.updateDeckCommandHandler.execute({ userid: userId, authLevel: authLevel, id: deckId, ...updateData });
|
||||||
|
|
||||||
logRequest('Deck updated successfully', req, res, { deckId, userId });
|
logRequest('Deck updated successfully', req, res, { deckId, userId });
|
||||||
res.json(result);
|
res.json(result);
|
||||||
@@ -244,9 +245,10 @@ deckRouter.delete('/:id', authRequired, async (req, res) => {
|
|||||||
try {
|
try {
|
||||||
const deckId = req.params.id;
|
const deckId = req.params.id;
|
||||||
const userId = (req as any).user.userId;
|
const userId = (req as any).user.userId;
|
||||||
|
const authLevel = (req as any).user.authLevel;
|
||||||
logRequest('Soft delete deck endpoint accessed', req, res, { deckId, userId });
|
logRequest('Soft delete deck endpoint accessed', req, res, { deckId, userId });
|
||||||
|
|
||||||
const result = await container.deleteDeckCommandHandler.execute({ id: deckId, soft: true });
|
const result = await container.deleteDeckCommandHandler.execute({ userid: userId, authLevel: authLevel, id: deckId, soft: true });
|
||||||
|
|
||||||
logRequest('Deck soft delete successful', req, res, { deckId, userId, success: result });
|
logRequest('Deck soft delete successful', req, res, { deckId, userId, success: result });
|
||||||
res.json({ success: result });
|
res.json({ success: result });
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
export interface DeleteDeckCommand {
|
export interface DeleteDeckCommand {
|
||||||
|
userid: string;
|
||||||
|
authLevel: number;
|
||||||
id: string;
|
id: string;
|
||||||
soft?: boolean;
|
soft?: boolean;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,24 @@
|
|||||||
import { IDeckRepository } from '../../../Domain/IRepository/IDeckRepository';
|
import { IDeckRepository } from '../../../Domain/IRepository/IDeckRepository';
|
||||||
|
import { logAuth, logError } from '../../Services/Logger';
|
||||||
import { DeleteDeckCommand } from './DeleteDeckCommand';
|
import { DeleteDeckCommand } from './DeleteDeckCommand';
|
||||||
|
|
||||||
export class DeleteDeckCommandHandler {
|
export class DeleteDeckCommandHandler {
|
||||||
constructor(private readonly deckRepo: IDeckRepository) {}
|
constructor(private readonly deckRepo: IDeckRepository) {}
|
||||||
|
|
||||||
async execute(cmd: DeleteDeckCommand): Promise<boolean> {
|
async execute(cmd: DeleteDeckCommand): Promise<boolean> {
|
||||||
|
|
||||||
|
//get decks userid
|
||||||
|
const deck = await this.deckRepo.findById(cmd.id);
|
||||||
|
if (!deck) {
|
||||||
|
logError(`Deck not found with ID: ${cmd.id}`);
|
||||||
|
throw new Error('Deck not found');
|
||||||
|
}
|
||||||
|
|
||||||
|
if(cmd.authLevel !==1 && deck.userid !== cmd.userid) {
|
||||||
|
logAuth(`Unauthorized access attempt to deck with ID: ${cmd.id}, UserID: ${cmd.userid}`);
|
||||||
|
throw new Error('Unauthorized');
|
||||||
|
}
|
||||||
|
|
||||||
if (cmd.soft) {
|
if (cmd.soft) {
|
||||||
await this.deckRepo.softDelete(cmd.id);
|
await this.deckRepo.softDelete(cmd.id);
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -1,11 +1,10 @@
|
|||||||
import { n } from "framer-motion/dist/types.d-D0HXPxHm";
|
|
||||||
|
|
||||||
export interface UpdateDeckCommand {
|
export interface UpdateDeckCommand {
|
||||||
|
userid: string;
|
||||||
|
authLevel: number;
|
||||||
id: string;
|
id: string;
|
||||||
userstate?: number;
|
userstate?: number;
|
||||||
name?: string;
|
name?: string;
|
||||||
type?: number;
|
type?: number;
|
||||||
userid?: string;
|
|
||||||
cards?: any[];
|
cards?: any[];
|
||||||
ctype?: number;
|
ctype?: number;
|
||||||
state?: number;
|
state?: number;
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import { UpdateDeckCommand } from './UpdateDeckCommand';
|
|||||||
import { ShortDeckDto } from '../../DTOs/DeckDto';
|
import { ShortDeckDto } from '../../DTOs/DeckDto';
|
||||||
import { DeckMapper } from '../../DTOs/Mappers/DeckMapper';
|
import { DeckMapper } from '../../DTOs/Mappers/DeckMapper';
|
||||||
import { DeckAggregate } from '../../../Domain/Deck/DeckAggregate';
|
import { DeckAggregate } from '../../../Domain/Deck/DeckAggregate';
|
||||||
import { logError } from '../../Services/Logger';
|
import { logAuth, logError } from '../../Services/Logger';
|
||||||
|
|
||||||
export class UpdateDeckCommandHandler {
|
export class UpdateDeckCommandHandler {
|
||||||
constructor(private readonly deckRepo: IDeckRepository) {}
|
constructor(private readonly deckRepo: IDeckRepository) {}
|
||||||
@@ -24,6 +24,11 @@ export class UpdateDeckCommandHandler {
|
|||||||
throw new Error('Deck not found');
|
throw new Error('Deck not found');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(cmd.authLevel !==1 && existingDeck.userid !== cmd.userid) {
|
||||||
|
logAuth(`Unauthorized access attempt to deck with ID: ${cmd.id}, UserID: ${cmd.userid}`);
|
||||||
|
throw new Error('Unauthorized');
|
||||||
|
}
|
||||||
|
|
||||||
const for_update: Partial<DeckAggregate> = {};
|
const for_update: Partial<DeckAggregate> = {};
|
||||||
if(cmd.name !== undefined) for_update.name = cmd.name;
|
if(cmd.name !== undefined) for_update.name = cmd.name;
|
||||||
if(cmd.type !== undefined) for_update.type = cmd.type;
|
if(cmd.type !== undefined) for_update.type = cmd.type;
|
||||||
|
|||||||
Reference in New Issue
Block a user