From 43b124664ee67b79e867a9658c7d06082470e112 Mon Sep 17 00:00:00 2001 From: mahan Date: Mon, 5 Jan 2026 16:06:35 +0330 Subject: [PATCH] feat: integrate authentication checks in message command handlers --- .../Commands/DeleteMessage/DeleteMessageCommand.cs | 9 ++++++--- .../TaskChat/Commands/EditMessage/EditMessageCommand.cs | 9 ++++++--- .../TaskChat/Commands/PinMessage/PinMessageCommand.cs | 9 ++++++--- .../TaskChat/Commands/SendMessage/SendMessageCommand.cs | 8 ++++++-- .../Commands/UnpinMessage/UnpinMessageCommand.cs | 9 ++++++--- .../Services/FileManagement/LocalFileStorageService.cs | 1 - 6 files changed, 30 insertions(+), 15 deletions(-) diff --git a/ProgramManager/src/Application/GozareshgirProgramManager.Application/Modules/TaskChat/Commands/DeleteMessage/DeleteMessageCommand.cs b/ProgramManager/src/Application/GozareshgirProgramManager.Application/Modules/TaskChat/Commands/DeleteMessage/DeleteMessageCommand.cs index 4837a3f4..68aaabef 100644 --- a/ProgramManager/src/Application/GozareshgirProgramManager.Application/Modules/TaskChat/Commands/DeleteMessage/DeleteMessageCommand.cs +++ b/ProgramManager/src/Application/GozareshgirProgramManager.Application/Modules/TaskChat/Commands/DeleteMessage/DeleteMessageCommand.cs @@ -1,5 +1,6 @@ using GozareshgirProgramManager.Application._Common.Models; using GozareshgirProgramManager.Application._Common.Interfaces; +using GozareshgirProgramManager.Domain._Common.Exceptions; using GozareshgirProgramManager.Domain.TaskChatAgg.Repositories; namespace GozareshgirProgramManager.Application.Modules.TaskChat.Commands.DeleteMessage; @@ -9,16 +10,18 @@ public record DeleteMessageCommand(Guid MessageId) : IBaseCommand; public class DeleteMessageCommandHandler : IBaseCommandHandler { private readonly ITaskChatMessageRepository _repository; + private readonly IAuthHelper _authHelper; - public DeleteMessageCommandHandler(ITaskChatMessageRepository repository) + public DeleteMessageCommandHandler(ITaskChatMessageRepository repository, IAuthHelper authHelper) { _repository = repository; + _authHelper = authHelper; } public async Task Handle(DeleteMessageCommand request, CancellationToken cancellationToken) { - // TODO: Get current user - var currentUserId = 1L; + var currentUserId = _authHelper.GetCurrentUserId()?? + throw new UnAuthorizedException("کاربر احراز هویت نشده است"); var message = await _repository.GetByIdAsync(request.MessageId); if (message == null) diff --git a/ProgramManager/src/Application/GozareshgirProgramManager.Application/Modules/TaskChat/Commands/EditMessage/EditMessageCommand.cs b/ProgramManager/src/Application/GozareshgirProgramManager.Application/Modules/TaskChat/Commands/EditMessage/EditMessageCommand.cs index c56be3bb..2066a769 100644 --- a/ProgramManager/src/Application/GozareshgirProgramManager.Application/Modules/TaskChat/Commands/EditMessage/EditMessageCommand.cs +++ b/ProgramManager/src/Application/GozareshgirProgramManager.Application/Modules/TaskChat/Commands/EditMessage/EditMessageCommand.cs @@ -1,5 +1,6 @@ using GozareshgirProgramManager.Application._Common.Interfaces; using GozareshgirProgramManager.Application._Common.Models; +using GozareshgirProgramManager.Domain._Common.Exceptions; using GozareshgirProgramManager.Domain.TaskChatAgg.Repositories; using MediatR; @@ -13,16 +14,18 @@ public record EditMessageCommand( public class EditMessageCommandHandler : IBaseCommandHandler { private readonly ITaskChatMessageRepository _repository; + private readonly IAuthHelper _authHelper; - public EditMessageCommandHandler(ITaskChatMessageRepository repository) + public EditMessageCommandHandler(ITaskChatMessageRepository repository, IAuthHelper authHelper) { _repository = repository; + _authHelper = authHelper; } public async Task Handle(EditMessageCommand request, CancellationToken cancellationToken) { - // TODO: Get current user - var currentUserId = 1L; + var currentUserId = _authHelper.GetCurrentUserId()?? + throw new UnAuthorizedException("کاربر احراز هویت نشده است"); var message = await _repository.GetByIdAsync(request.MessageId); if (message == null) diff --git a/ProgramManager/src/Application/GozareshgirProgramManager.Application/Modules/TaskChat/Commands/PinMessage/PinMessageCommand.cs b/ProgramManager/src/Application/GozareshgirProgramManager.Application/Modules/TaskChat/Commands/PinMessage/PinMessageCommand.cs index e0a49a7c..9841df53 100644 --- a/ProgramManager/src/Application/GozareshgirProgramManager.Application/Modules/TaskChat/Commands/PinMessage/PinMessageCommand.cs +++ b/ProgramManager/src/Application/GozareshgirProgramManager.Application/Modules/TaskChat/Commands/PinMessage/PinMessageCommand.cs @@ -1,5 +1,6 @@ using GozareshgirProgramManager.Application._Common.Interfaces; using GozareshgirProgramManager.Application._Common.Models; +using GozareshgirProgramManager.Domain._Common.Exceptions; using GozareshgirProgramManager.Domain.TaskChatAgg.Repositories; using MediatR; @@ -10,16 +11,18 @@ public record PinMessageCommand(Guid MessageId) : IBaseCommand; public class PinMessageCommandHandler : IBaseCommandHandler { private readonly ITaskChatMessageRepository _repository; + private readonly IAuthHelper _authHelper; - public PinMessageCommandHandler(ITaskChatMessageRepository repository) + public PinMessageCommandHandler(ITaskChatMessageRepository repository, IAuthHelper authHelper) { _repository = repository; + _authHelper = authHelper; } public async Task Handle(PinMessageCommand request, CancellationToken cancellationToken) { - // TODO: Get current user - var currentUserId = 1L; + var currentUserId = _authHelper.GetCurrentUserId()?? + throw new UnAuthorizedException("کاربر احراز هویت نشده است"); var message = await _repository.GetByIdAsync(request.MessageId); if (message == null) diff --git a/ProgramManager/src/Application/GozareshgirProgramManager.Application/Modules/TaskChat/Commands/SendMessage/SendMessageCommand.cs b/ProgramManager/src/Application/GozareshgirProgramManager.Application/Modules/TaskChat/Commands/SendMessage/SendMessageCommand.cs index b8b2a32c..a086b83f 100644 --- a/ProgramManager/src/Application/GozareshgirProgramManager.Application/Modules/TaskChat/Commands/SendMessage/SendMessageCommand.cs +++ b/ProgramManager/src/Application/GozareshgirProgramManager.Application/Modules/TaskChat/Commands/SendMessage/SendMessageCommand.cs @@ -2,6 +2,7 @@ using GozareshgirProgramManager.Application._Common.Interfaces; using GozareshgirProgramManager.Application._Common.Models; using GozareshgirProgramManager.Application.Modules.TaskChat.DTOs; using GozareshgirProgramManager.Application.Services.FileManagement; +using GozareshgirProgramManager.Domain._Common.Exceptions; using GozareshgirProgramManager.Domain.TaskChatAgg.Entities; using GozareshgirProgramManager.Domain.TaskChatAgg.Repositories; using GozareshgirProgramManager.Domain.TaskChatAgg.Enums; @@ -29,24 +30,27 @@ public class SendMessageCommandHandler : IBaseCommandHandler> Handle(SendMessageCommand request, CancellationToken cancellationToken) { - var currentUserId = 1L; + var currentUserId = _authHelper.GetCurrentUserId() + ?? throw new UnAuthorizedException("کاربر احراز هویت نشده است"); var task = await _taskRepository.GetByIdAsync(request.TaskId, cancellationToken); if (task == null) diff --git a/ProgramManager/src/Application/GozareshgirProgramManager.Application/Modules/TaskChat/Commands/UnpinMessage/UnpinMessageCommand.cs b/ProgramManager/src/Application/GozareshgirProgramManager.Application/Modules/TaskChat/Commands/UnpinMessage/UnpinMessageCommand.cs index 7d11beb4..664087cd 100644 --- a/ProgramManager/src/Application/GozareshgirProgramManager.Application/Modules/TaskChat/Commands/UnpinMessage/UnpinMessageCommand.cs +++ b/ProgramManager/src/Application/GozareshgirProgramManager.Application/Modules/TaskChat/Commands/UnpinMessage/UnpinMessageCommand.cs @@ -1,5 +1,6 @@ using GozareshgirProgramManager.Application._Common.Interfaces; using GozareshgirProgramManager.Application._Common.Models; +using GozareshgirProgramManager.Domain._Common.Exceptions; using GozareshgirProgramManager.Domain.TaskChatAgg.Repositories; using MediatR; @@ -10,16 +11,18 @@ public record UnpinMessageCommand(Guid MessageId) : IBaseCommand; public class UnpinMessageCommandHandler : IBaseCommandHandler { private readonly ITaskChatMessageRepository _repository; + private readonly IAuthHelper _authHelper; - public UnpinMessageCommandHandler(ITaskChatMessageRepository repository) + public UnpinMessageCommandHandler(ITaskChatMessageRepository repository, IAuthHelper authHelper) { _repository = repository; + _authHelper = authHelper; } public async Task Handle(UnpinMessageCommand request, CancellationToken cancellationToken) { - // TODO: Get current user - var currentUserId = 1L; + var currentUserId = _authHelper.GetCurrentUserId()?? + throw new UnAuthorizedException("کاربر احراز هویت نشده است"); var message = await _repository.GetByIdAsync(request.MessageId); if (message == null) diff --git a/ProgramManager/src/Infrastructure/GozareshgirProgramManager.Infrastructure/Services/FileManagement/LocalFileStorageService.cs b/ProgramManager/src/Infrastructure/GozareshgirProgramManager.Infrastructure/Services/FileManagement/LocalFileStorageService.cs index dd2b8c8a..57fb7bdc 100644 --- a/ProgramManager/src/Infrastructure/GozareshgirProgramManager.Infrastructure/Services/FileManagement/LocalFileStorageService.cs +++ b/ProgramManager/src/Infrastructure/GozareshgirProgramManager.Infrastructure/Services/FileManagement/LocalFileStorageService.cs @@ -1,5 +1,4 @@ using GozareshgirProgramManager.Application.Services.FileManagement; -using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting;