From 1275bed0330877b94c793e920391bdc10bfe6168 Mon Sep 17 00:00:00 2001 From: xiang <27800734@qq.com> Date: Sun, 11 May 2025 00:14:12 +0800 Subject: [PATCH] 1 --- .../Articles/DeleteArticleCommandHandler.cs | 24 ++++++++++++++++--- .../UpdateConfigurationCommandHandler.cs | 4 +--- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/Yes.Application/Admins/Articles/DeleteArticleCommandHandler.cs b/Yes.Application/Admins/Articles/DeleteArticleCommandHandler.cs index ace5977..033c139 100644 --- a/Yes.Application/Admins/Articles/DeleteArticleCommandHandler.cs +++ b/Yes.Application/Admins/Articles/DeleteArticleCommandHandler.cs @@ -1,18 +1,36 @@ namespace Yes.Application.Admins.Articles { - public record DeleteArticleCommand(int Id) : IRequest; + public record DeleteArticleCommand(int Id) : IRequest; public record DeleteArticleCommandResponse(int Id); - public class DeleteArticleCommandHandler(BlogDbContext db) : IRequestHandler + public class DeleteArticleCommandHandler( + BlogDbContext db, + IIdentityContext identity) : IRequestHandler { private readonly BlogDbContext _db = db; + private readonly IIdentityContext _identity = identity; public async Task Handle(DeleteArticleCommand request, CancellationToken cancellationToken) { var article = await _db.Articles.FindAsync(request.Id); if (article != null) - { + { + var identityUser = await _db.Users.FindAsync(_identity.Id); + if (identityUser == null) + { + throw new UserNotExistsException(_identity.Id); + } + + if (!identityUser.IsSystemUser()) + { + if (article.UserId != identityUser.Id) + { + throw new AccessDeniedException(); + } + } + + article.Delete(); _db.Articles.Update(article); await _db.SaveChangesAsync(); diff --git a/Yes.Application/Admins/Configurations/UpdateConfigurationCommandHandler.cs b/Yes.Application/Admins/Configurations/UpdateConfigurationCommandHandler.cs index aebec5f..ae4f50c 100644 --- a/Yes.Application/Admins/Configurations/UpdateConfigurationCommandHandler.cs +++ b/Yes.Application/Admins/Configurations/UpdateConfigurationCommandHandler.cs @@ -1,6 +1,4 @@ -using static Dapper.SqlMapper; - -namespace Yes.Application.Admins.Configurations +namespace Yes.Application.Admins.Configurations { public record UpdateConfigurationCommand( string Name,