Browse Source

refactor: 重构代码以符合DDD架构 - 将OperationResult迁移到领域层 - 优化服务接口位置 - 更新依赖注入配置

web
root 3 months ago
parent
commit
b0a379f9b2
  1. 1
      src/CellularManagement.Application/Behaviours/ValidationBehaviour.cs
  2. 2
      src/CellularManagement.Application/DependencyInjection.cs
  3. 1
      src/CellularManagement.Application/Features/Auth/Commands/AuthenticateUser/AuthenticateUserCommand.cs
  4. 8
      src/CellularManagement.Application/Features/Auth/Commands/AuthenticateUser/AuthenticateUserCommandHandler.cs
  5. 1
      src/CellularManagement.Application/Features/Auth/Commands/AuthenticateUser/AuthenticateUserResponse.cs
  6. 1
      src/CellularManagement.Application/Features/Auth/Commands/RefreshToken/RefreshTokenCommand.cs
  7. 8
      src/CellularManagement.Application/Features/Auth/Commands/RefreshToken/RefreshTokenCommandHandler.cs
  8. 1
      src/CellularManagement.Application/Features/Auth/Commands/RegisterUser/RegisterUserCommand.cs
  9. 5
      src/CellularManagement.Application/Features/Auth/Commands/RegisterUser/RegisterUserCommandHandler.cs
  10. 2
      src/CellularManagement.Application/Features/Auth/Common/UserInfo.cs
  11. 1
      src/CellularManagement.Application/Features/Permissions/Commands/CreatePermission/CreatePermissionCommand.cs
  12. 2
      src/CellularManagement.Application/Features/Permissions/Commands/CreatePermission/CreatePermissionCommandHandler.cs
  13. 2
      src/CellularManagement.Application/Features/RolePermissions/Commands/AddRolePermissions/AddRolePermissionsCommand.cs
  14. 5
      src/CellularManagement.Application/Features/RolePermissions/Commands/AddRolePermissions/AddRolePermissionsCommandHandler.cs
  15. 2
      src/CellularManagement.Application/Features/RolePermissions/Commands/DeleteRolePermissions/DeleteRolePermissionsCommand.cs
  16. 5
      src/CellularManagement.Application/Features/RolePermissions/Commands/DeleteRolePermissions/DeleteRolePermissionsCommandHandler.cs
  17. 1
      src/CellularManagement.Application/Features/RolePermissions/Queries/GetRolePermissions/GetRolePermissionsQuery.cs
  18. 5
      src/CellularManagement.Application/Features/RolePermissions/Queries/GetRolePermissions/GetRolePermissionsQueryHandler.cs
  19. 1
      src/CellularManagement.Application/Features/RolePermissions/Queries/GetRolePermissions/GetRolePermissionsResponse.cs
  20. 1
      src/CellularManagement.Application/Features/Roles/Commands/CreateRole/CreateRoleCommand.cs
  21. 1
      src/CellularManagement.Application/Features/Roles/Commands/DeleteRole/DeleteRoleCommand.cs
  22. 5
      src/CellularManagement.Application/Features/Roles/Commands/RoleCommandHandler.cs
  23. 3
      src/CellularManagement.Application/Features/Roles/Queries/GetAllRolesQuery.cs
  24. 1
      src/CellularManagement.Application/Features/Roles/Queries/GetRole/GetRoleQuery.cs
  25. 2
      src/CellularManagement.Application/Features/Roles/Queries/GetRole/GetRoleResponse.cs
  26. 5
      src/CellularManagement.Application/Features/Roles/Queries/RoleQueryHandler.cs
  27. 1
      src/CellularManagement.Application/Features/Users/Commands/CreateUser/CreateUserCommand.cs
  28. 5
      src/CellularManagement.Application/Features/Users/Commands/CreateUser/CreateUserCommandHandler.cs
  29. 1
      src/CellularManagement.Application/Features/Users/Commands/DeleteUser/DeleteUserCommand.cs
  30. 5
      src/CellularManagement.Application/Features/Users/Commands/DeleteUser/DeleteUserCommandHandler.cs
  31. 1
      src/CellularManagement.Application/Features/Users/Commands/UpdateUser/UpdateUserCommand.cs
  32. 5
      src/CellularManagement.Application/Features/Users/Commands/UpdateUser/UpdateUserCommandHandler.cs
  33. 1
      src/CellularManagement.Application/Features/Users/Queries/GetAllUsers/GetAllUsersQuery.cs
  34. 5
      src/CellularManagement.Application/Features/Users/Queries/GetAllUsers/GetAllUsersQueryHandler.cs
  35. 2
      src/CellularManagement.Application/Features/Users/Queries/GetAllUsers/GetAllUsersResponse.cs
  36. 1
      src/CellularManagement.Application/Features/Users/Queries/GetUserById/GetUserByIdQuery.cs
  37. 4
      src/CellularManagement.Application/Features/Users/Queries/GetUserById/GetUserByIdQueryHandler.cs
  38. 7
      src/CellularManagement.Domain/Common/OperationResult.cs
  39. 3
      src/CellularManagement.Domain/Services/ICacheService.cs
  40. 33
      src/CellularManagement.Domain/Services/IEmailService.cs
  41. 25
      src/CellularManagement.Domain/Services/IEmailVerificationService.cs
  42. 3
      src/CellularManagement.Domain/Services/IJwtProvider.cs
  43. 3
      src/CellularManagement.Domain/Services/IKeyRotationService.cs
  44. 7
      src/CellularManagement.Infrastructure/DependencyInjection.cs
  45. 42
      src/CellularManagement.Infrastructure/Options/EmailOptions.cs
  46. 3
      src/CellularManagement.Infrastructure/Services/CacheService.cs
  47. 116
      src/CellularManagement.Infrastructure/Services/EmailService.cs
  48. 132
      src/CellularManagement.Infrastructure/Services/EmailVerificationService.cs
  49. 2
      src/CellularManagement.Infrastructure/Services/JwtProvider.cs
  50. 2
      src/CellularManagement.Infrastructure/Services/KeyRotationBackgroundService.cs
  51. 2
      src/CellularManagement.Infrastructure/Services/KeyRotationService.cs
  52. 3
      src/CellularManagement.Presentation/Controllers/AuthController.cs
  53. 1
      src/CellularManagement.Presentation/Controllers/PermissionsController.cs
  54. 1
      src/CellularManagement.Presentation/Controllers/RolePermissionController.cs
  55. 1
      src/CellularManagement.Presentation/Controllers/RolesController.cs
  56. 3
      src/CellularManagement.Presentation/Controllers/UsersController.cs
  57. 17
      src/CellularManagement.Presentation/appsettings.json

1
src/CellularManagement.Application/Behaviours/ValidationBehaviour.cs

@ -1,3 +1,4 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;

2
src/CellularManagement.Application/DependencyInjection.cs

@ -2,6 +2,7 @@ using Microsoft.Extensions.DependencyInjection;
using MediatR; using MediatR;
using FluentValidation; using FluentValidation;
using CellularManagement.Application.Behaviours; using CellularManagement.Application.Behaviours;
using CellularManagement.Domain.Services;
namespace CellularManagement.Application; namespace CellularManagement.Application;
@ -33,6 +34,7 @@ public static class DependencyInjection
// 注册验证器 // 注册验证器
services.AddScoped(typeof(IPipelineBehavior<,>), typeof(ValidationBehaviour<,>)); services.AddScoped(typeof(IPipelineBehavior<,>), typeof(ValidationBehaviour<,>));
return services; return services;
} }
} }

1
src/CellularManagement.Application/Features/Auth/Commands/AuthenticateUser/AuthenticateUserCommand.cs

@ -1,3 +1,4 @@
using CellularManagement.Domain.Common;
using MediatR; using MediatR;
namespace CellularManagement.Application.Features.Auth.Commands.AuthenticateUser; namespace CellularManagement.Application.Features.Auth.Commands.AuthenticateUser;

8
src/CellularManagement.Application/Features/Auth/Commands/AuthenticateUser/AuthenticateUserCommandHandler.cs

@ -3,10 +3,16 @@ using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using MediatR; using MediatR;
using CellularManagement.Application.Common; using CellularManagement.Application.Common;
using CellularManagement.Application.Services;
using CellularManagement.Domain.Entities; using CellularManagement.Domain.Entities;
using CellularManagement.Application.Features.Auth.Common; using CellularManagement.Application.Features.Auth.Common;
using CellularManagement.Domain.Repositories; using CellularManagement.Domain.Repositories;
using CellularManagement.Domain.Services;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Threading;
using System.Linq;
using System;
using CellularManagement.Domain.Common;
namespace CellularManagement.Application.Features.Auth.Commands.AuthenticateUser; namespace CellularManagement.Application.Features.Auth.Commands.AuthenticateUser;

1
src/CellularManagement.Application/Features/Auth/Commands/AuthenticateUser/AuthenticateUserResponse.cs

@ -1,3 +1,4 @@
using System;
using CellularManagement.Application.Features.Auth.Common; using CellularManagement.Application.Features.Auth.Common;
namespace CellularManagement.Application.Features.Auth.Commands.AuthenticateUser; namespace CellularManagement.Application.Features.Auth.Commands.AuthenticateUser;

1
src/CellularManagement.Application/Features/Auth/Commands/RefreshToken/RefreshTokenCommand.cs

@ -1,6 +1,7 @@
using MediatR; using MediatR;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using CellularManagement.Application.Features.Auth.Commands.AuthenticateUser; using CellularManagement.Application.Features.Auth.Commands.AuthenticateUser;
using CellularManagement.Domain.Common;
namespace CellularManagement.Application.Features.Auth.Commands.RefreshToken; namespace CellularManagement.Application.Features.Auth.Commands.RefreshToken;

8
src/CellularManagement.Application/Features/Auth/Commands/RefreshToken/RefreshTokenCommandHandler.cs

@ -1,11 +1,17 @@
using MediatR; using MediatR;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using CellularManagement.Application.Common; using CellularManagement.Application.Common;
using CellularManagement.Application.Services;
using System.Security.Claims; using System.Security.Claims;
using CellularManagement.Application.Features.Auth.Commands.AuthenticateUser; using CellularManagement.Application.Features.Auth.Commands.AuthenticateUser;
using CellularManagement.Application.Features.Auth.Common; using CellularManagement.Application.Features.Auth.Common;
using CellularManagement.Domain.Repositories; using CellularManagement.Domain.Repositories;
using CellularManagement.Domain.Services;
using System.Threading.Tasks;
using System.Threading;
using System.Linq;
using System.Collections.Generic;
using System;
using CellularManagement.Domain.Common;
namespace CellularManagement.Application.Features.Auth.Commands.RefreshToken; namespace CellularManagement.Application.Features.Auth.Commands.RefreshToken;

1
src/CellularManagement.Application/Features/Auth/Commands/RegisterUser/RegisterUserCommand.cs

@ -1,3 +1,4 @@
using CellularManagement.Domain.Common;
using MediatR; using MediatR;
namespace CellularManagement.Application.Features.Auth.Commands.RegisterUser; namespace CellularManagement.Application.Features.Auth.Commands.RegisterUser;

5
src/CellularManagement.Application/Features/Auth/Commands/RegisterUser/RegisterUserCommandHandler.cs

@ -4,6 +4,11 @@ using MediatR;
using CellularManagement.Domain.Entities; using CellularManagement.Domain.Entities;
using CellularManagement.Domain.Repositories; using CellularManagement.Domain.Repositories;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using CellularManagement.Domain.Common;
using System.Linq;
using System;
using System.Threading.Tasks;
using System.Threading;
namespace CellularManagement.Application.Features.Auth.Commands.RegisterUser; namespace CellularManagement.Application.Features.Auth.Commands.RegisterUser;

2
src/CellularManagement.Application/Features/Auth/Common/UserInfo.cs

@ -1,3 +1,5 @@
using System.Collections.Generic;
namespace CellularManagement.Application.Features.Auth.Common; namespace CellularManagement.Application.Features.Auth.Common;
/// <summary> /// <summary>

1
src/CellularManagement.Application/Features/Permissions/Commands/CreatePermission/CreatePermissionCommand.cs

@ -1,5 +1,6 @@
using MediatR; using MediatR;
using CellularManagement.Application.Common; using CellularManagement.Application.Common;
using CellularManagement.Domain.Common;
namespace CellularManagement.Application.Features.Permissions.Commands.CreatePermission; namespace CellularManagement.Application.Features.Permissions.Commands.CreatePermission;

2
src/CellularManagement.Application/Features/Permissions/Commands/CreatePermission/CreatePermissionCommandHandler.cs

@ -5,6 +5,8 @@ using Microsoft.Extensions.Logging;
using CellularManagement.Application.Common; using CellularManagement.Application.Common;
using CellularManagement.Domain.Entities; using CellularManagement.Domain.Entities;
using CellularManagement.Domain.Repositories; using CellularManagement.Domain.Repositories;
using CellularManagement.Domain.Common;
using System;
namespace CellularManagement.Application.Features.Permissions.Commands.CreatePermission; namespace CellularManagement.Application.Features.Permissions.Commands.CreatePermission;

2
src/CellularManagement.Application/Features/RolePermissions/Commands/AddRolePermissions/AddRolePermissionsCommand.cs

@ -1,3 +1,5 @@
using System.Collections.Generic;
using CellularManagement.Domain.Common;
using MediatR; using MediatR;
namespace CellularManagement.Application.Features.RolePermissions.Commands.AddRolePermissions; namespace CellularManagement.Application.Features.RolePermissions.Commands.AddRolePermissions;

5
src/CellularManagement.Application/Features/RolePermissions/Commands/AddRolePermissions/AddRolePermissionsCommandHandler.cs

@ -1,6 +1,11 @@
using MediatR; using MediatR;
using CellularManagement.Domain.Repositories; using CellularManagement.Domain.Repositories;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using CellularManagement.Domain.Common;
using System.Threading.Tasks;
using System.Threading;
using System;
using System.Linq;
namespace CellularManagement.Application.Features.RolePermissions.Commands.AddRolePermissions; namespace CellularManagement.Application.Features.RolePermissions.Commands.AddRolePermissions;

2
src/CellularManagement.Application/Features/RolePermissions/Commands/DeleteRolePermissions/DeleteRolePermissionsCommand.cs

@ -1,3 +1,5 @@
using System.Collections.Generic;
using CellularManagement.Domain.Common;
using MediatR; using MediatR;
namespace CellularManagement.Application.Features.RolePermissions.Commands.DeleteRolePermissions; namespace CellularManagement.Application.Features.RolePermissions.Commands.DeleteRolePermissions;

5
src/CellularManagement.Application/Features/RolePermissions/Commands/DeleteRolePermissions/DeleteRolePermissionsCommandHandler.cs

@ -1,6 +1,11 @@
using MediatR; using MediatR;
using CellularManagement.Domain.Repositories; using CellularManagement.Domain.Repositories;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using CellularManagement.Domain.Common;
using System.Threading.Tasks;
using System.Threading;
using System.Linq;
using System;
namespace CellularManagement.Application.Features.RolePermissions.Commands.DeleteRolePermissions; namespace CellularManagement.Application.Features.RolePermissions.Commands.DeleteRolePermissions;

1
src/CellularManagement.Application/Features/RolePermissions/Queries/GetRolePermissions/GetRolePermissionsQuery.cs

@ -1,3 +1,4 @@
using CellularManagement.Domain.Common;
using MediatR; using MediatR;
namespace CellularManagement.Application.Features.RolePermissions.Queries.GetRolePermissions; namespace CellularManagement.Application.Features.RolePermissions.Queries.GetRolePermissions;

5
src/CellularManagement.Application/Features/RolePermissions/Queries/GetRolePermissions/GetRolePermissionsQueryHandler.cs

@ -1,6 +1,11 @@
using MediatR; using MediatR;
using CellularManagement.Domain.Repositories; using CellularManagement.Domain.Repositories;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using CellularManagement.Domain.Common;
using System.Threading.Tasks;
using System.Threading;
using System.Linq;
using System;
namespace CellularManagement.Application.Features.RolePermissions.Queries.GetRolePermissions; namespace CellularManagement.Application.Features.RolePermissions.Queries.GetRolePermissions;

1
src/CellularManagement.Application/Features/RolePermissions/Queries/GetRolePermissions/GetRolePermissionsResponse.cs

@ -1,3 +1,4 @@
using System.Collections.Generic;
using CellularManagement.Domain.Entities; using CellularManagement.Domain.Entities;
namespace CellularManagement.Application.Features.RolePermissions.Queries.GetRolePermissions; namespace CellularManagement.Application.Features.RolePermissions.Queries.GetRolePermissions;

1
src/CellularManagement.Application/Features/Roles/Commands/CreateRole/CreateRoleCommand.cs

@ -1,3 +1,4 @@
using CellularManagement.Domain.Common;
using MediatR; using MediatR;
namespace CellularManagement.Application.Features.Roles.Commands.CreateRole; namespace CellularManagement.Application.Features.Roles.Commands.CreateRole;

1
src/CellularManagement.Application/Features/Roles/Commands/DeleteRole/DeleteRoleCommand.cs

@ -1,3 +1,4 @@
using CellularManagement.Domain.Common;
using MediatR; using MediatR;
namespace CellularManagement.Application.Features.Roles.Commands.DeleteRole; namespace CellularManagement.Application.Features.Roles.Commands.DeleteRole;

5
src/CellularManagement.Application/Features/Roles/Commands/RoleCommandHandler.cs

@ -4,6 +4,11 @@ using MediatR;
using CellularManagement.Domain.Entities; using CellularManagement.Domain.Entities;
using CellularManagement.Application.Features.Roles.Commands.CreateRole; using CellularManagement.Application.Features.Roles.Commands.CreateRole;
using CellularManagement.Application.Features.Roles.Commands.DeleteRole; using CellularManagement.Application.Features.Roles.Commands.DeleteRole;
using CellularManagement.Domain.Common;
using System.Linq;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace CellularManagement.Application.Features.Roles.Commands; namespace CellularManagement.Application.Features.Roles.Commands;

3
src/CellularManagement.Application/Features/Roles/Queries/GetAllRolesQuery.cs

@ -1,5 +1,8 @@
using MediatR; using MediatR;
using CellularManagement.Application.Common; using CellularManagement.Application.Common;
using System;
using CellularManagement.Domain.Common;
using System.Collections.Generic;
namespace CellularManagement.Application.Features.Roles.Queries; namespace CellularManagement.Application.Features.Roles.Queries;

1
src/CellularManagement.Application/Features/Roles/Queries/GetRole/GetRoleQuery.cs

@ -1,3 +1,4 @@
using CellularManagement.Domain.Common;
using MediatR; using MediatR;
namespace CellularManagement.Application.Features.Roles.Queries.GetRole; namespace CellularManagement.Application.Features.Roles.Queries.GetRole;

2
src/CellularManagement.Application/Features/Roles/Queries/GetRole/GetRoleResponse.cs

@ -1,3 +1,5 @@
using System;
namespace CellularManagement.Application.Features.Roles.Queries.GetRole; namespace CellularManagement.Application.Features.Roles.Queries.GetRole;
/// <summary> /// <summary>

5
src/CellularManagement.Application/Features/Roles/Queries/RoleQueryHandler.cs

@ -5,6 +5,11 @@ using CellularManagement.Domain.Entities;
using CellularManagement.Application.Features.Roles.Queries.GetRole; using CellularManagement.Application.Features.Roles.Queries.GetRole;
using CellularManagement.Application.Features.Roles.Queries; using CellularManagement.Application.Features.Roles.Queries;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using CellularManagement.Domain.Common;
using System.Threading.Tasks;
using System.Threading;
using System;
using System.Linq;
namespace CellularManagement.Application.Features.Roles.Queries; namespace CellularManagement.Application.Features.Roles.Queries;

1
src/CellularManagement.Application/Features/Users/Commands/CreateUser/CreateUserCommand.cs

@ -1,3 +1,4 @@
using CellularManagement.Domain.Common;
using MediatR; using MediatR;
namespace CellularManagement.Application.Features.Users.Commands.CreateUser; namespace CellularManagement.Application.Features.Users.Commands.CreateUser;

5
src/CellularManagement.Application/Features/Users/Commands/CreateUser/CreateUserCommandHandler.cs

@ -3,6 +3,11 @@ using Microsoft.Extensions.Logging;
using MediatR; using MediatR;
using CellularManagement.Domain.Entities; using CellularManagement.Domain.Entities;
using CellularManagement.Domain.Repositories; using CellularManagement.Domain.Repositories;
using CellularManagement.Domain.Common;
using System.Threading.Tasks;
using System.Threading;
using System;
using System.Linq;
namespace CellularManagement.Application.Features.Users.Commands.CreateUser; namespace CellularManagement.Application.Features.Users.Commands.CreateUser;

1
src/CellularManagement.Application/Features/Users/Commands/DeleteUser/DeleteUserCommand.cs

@ -1,3 +1,4 @@
using CellularManagement.Domain.Common;
using MediatR; using MediatR;
namespace CellularManagement.Application.Features.Users.Commands.DeleteUser; namespace CellularManagement.Application.Features.Users.Commands.DeleteUser;

5
src/CellularManagement.Application/Features/Users/Commands/DeleteUser/DeleteUserCommandHandler.cs

@ -3,6 +3,11 @@ using Microsoft.Extensions.Logging;
using MediatR; using MediatR;
using CellularManagement.Domain.Entities; using CellularManagement.Domain.Entities;
using CellularManagement.Domain.Repositories; using CellularManagement.Domain.Repositories;
using CellularManagement.Domain.Common;
using System.Threading.Tasks;
using System.Threading;
using System.Linq;
using System;
namespace CellularManagement.Application.Features.Users.Commands.DeleteUser; namespace CellularManagement.Application.Features.Users.Commands.DeleteUser;

1
src/CellularManagement.Application/Features/Users/Commands/UpdateUser/UpdateUserCommand.cs

@ -1,3 +1,4 @@
using CellularManagement.Domain.Common;
using MediatR; using MediatR;
namespace CellularManagement.Application.Features.Users.Commands.UpdateUser; namespace CellularManagement.Application.Features.Users.Commands.UpdateUser;

5
src/CellularManagement.Application/Features/Users/Commands/UpdateUser/UpdateUserCommandHandler.cs

@ -3,6 +3,11 @@ using Microsoft.Extensions.Logging;
using MediatR; using MediatR;
using CellularManagement.Domain.Entities; using CellularManagement.Domain.Entities;
using CellularManagement.Domain.Repositories; using CellularManagement.Domain.Repositories;
using CellularManagement.Domain.Common;
using System.Threading.Tasks;
using System.Threading;
using System.Linq;
using System;
namespace CellularManagement.Application.Features.Users.Commands.UpdateUser; namespace CellularManagement.Application.Features.Users.Commands.UpdateUser;

1
src/CellularManagement.Application/Features/Users/Queries/GetAllUsers/GetAllUsersQuery.cs

@ -1,3 +1,4 @@
using CellularManagement.Domain.Common;
using MediatR; using MediatR;
namespace CellularManagement.Application.Features.Users.Queries.GetAllUsers; namespace CellularManagement.Application.Features.Users.Queries.GetAllUsers;

5
src/CellularManagement.Application/Features/Users/Queries/GetAllUsers/GetAllUsersQueryHandler.cs

@ -4,6 +4,11 @@ using MediatR;
using CellularManagement.Domain.Entities; using CellularManagement.Domain.Entities;
using CellularManagement.Application.Features.Users.Queries.Dtos; using CellularManagement.Application.Features.Users.Queries.Dtos;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using CellularManagement.Domain.Common;
using System.Threading.Tasks;
using System.Threading;
using System.Linq;
using System;
namespace CellularManagement.Application.Features.Users.Queries.GetAllUsers; namespace CellularManagement.Application.Features.Users.Queries.GetAllUsers;

2
src/CellularManagement.Application/Features/Users/Queries/GetAllUsers/GetAllUsersResponse.cs

@ -1,3 +1,5 @@
using System;
using System.Collections.Generic;
using CellularManagement.Application.Features.Users.Queries.Dtos; using CellularManagement.Application.Features.Users.Queries.Dtos;
namespace CellularManagement.Application.Features.Users.Queries.GetAllUsers; namespace CellularManagement.Application.Features.Users.Queries.GetAllUsers;

1
src/CellularManagement.Application/Features/Users/Queries/GetUserById/GetUserByIdQuery.cs

@ -1,3 +1,4 @@
using CellularManagement.Domain.Common;
using MediatR; using MediatR;
namespace CellularManagement.Application.Features.Users.Queries.GetUserById; namespace CellularManagement.Application.Features.Users.Queries.GetUserById;

4
src/CellularManagement.Application/Features/Users/Queries/GetUserById/GetUserByIdQueryHandler.cs

@ -3,6 +3,10 @@ using Microsoft.Extensions.Logging;
using MediatR; using MediatR;
using CellularManagement.Domain.Entities; using CellularManagement.Domain.Entities;
using CellularManagement.Application.Features.Users.Queries.Dtos; using CellularManagement.Application.Features.Users.Queries.Dtos;
using CellularManagement.Domain.Common;
using System.Threading;
using System.Threading.Tasks;
using System;
namespace CellularManagement.Application.Features.Users.Queries.GetUserById; namespace CellularManagement.Application.Features.Users.Queries.GetUserById;

7
src/CellularManagement.Application/Common/OperationResult.cs → src/CellularManagement.Domain/Common/OperationResult.cs

@ -1,3 +1,8 @@
using System.Collections.Generic;
using System.Linq;
namespace CellularManagement.Domain.Common;
/// <summary> /// <summary>
/// 泛型结果类,用于封装操作结果 /// 泛型结果类,用于封装操作结果
/// </summary> /// </summary>
@ -73,4 +78,4 @@ public sealed record OperationResult<T>(
{ {
return new OperationResult<T>(null, errorMessages, default); return new OperationResult<T>(null, errorMessages, default);
} }
} }

3
src/CellularManagement.Application/Services/ICacheService.cs → src/CellularManagement.Domain/Services/ICacheService.cs

@ -1,9 +1,10 @@
using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Caching.Memory;
namespace CellularManagement.Application.Services; namespace CellularManagement.Domain.Services;
/// <summary> /// <summary>
/// 缓存服务接口 /// 缓存服务接口
/// 定义缓存操作的核心业务逻辑
/// </summary> /// </summary>
public interface ICacheService public interface ICacheService
{ {

33
src/CellularManagement.Domain/Services/IEmailService.cs

@ -0,0 +1,33 @@
using System.Threading.Tasks;
namespace CellularManagement.Domain.Services;
/// <summary>
/// 邮箱服务接口
/// 定义发送邮件和验证邮箱的基本功能
/// </summary>
public interface IEmailService
{
/// <summary>
/// 发送验证码邮件
/// </summary>
/// <param name="to">收件人邮箱</param>
/// <param name="verificationCode">验证码</param>
/// <returns>发送结果</returns>
Task<bool> SendVerificationCodeAsync(string to, string verificationCode);
/// <summary>
/// 发送注册成功邮件
/// </summary>
/// <param name="to">收件人邮箱</param>
/// <param name="username">用户名</param>
/// <returns>发送结果</returns>
Task<bool> SendRegistrationSuccessAsync(string to, string username);
/// <summary>
/// 验证邮箱格式
/// </summary>
/// <param name="email">邮箱地址</param>
/// <returns>验证结果</returns>
bool ValidateEmail(string email);
}

25
src/CellularManagement.Domain/Services/IEmailVerificationService.cs

@ -0,0 +1,25 @@
using System.Threading.Tasks;
namespace CellularManagement.Domain.Services;
/// <summary>
/// 邮箱验证码服务接口
/// 定义验证码生成、发送和验证的核心业务逻辑
/// </summary>
public interface IEmailVerificationService
{
/// <summary>
/// 生成并发送验证码
/// </summary>
/// <param name="email">邮箱地址</param>
/// <returns>是否发送成功</returns>
Task<bool> GenerateAndSendVerificationCodeAsync(string email);
/// <summary>
/// 验证验证码
/// </summary>
/// <param name="email">邮箱地址</param>
/// <param name="code">验证码</param>
/// <returns>验证结果</returns>
Task<bool> VerifyCodeAsync(string email, string code);
}

3
src/CellularManagement.Application/Services/IJwtProvider.cs → src/CellularManagement.Domain/Services/IJwtProvider.cs

@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Security.Claims; using System.Security.Claims;
namespace CellularManagement.Application.Services; namespace CellularManagement.Domain.Services;
/// <summary> /// <summary>
/// JWT 令牌提供者接口 /// JWT 令牌提供者接口

3
src/CellularManagement.Application/Services/IKeyRotationService.cs → src/CellularManagement.Domain/Services/IKeyRotationService.cs

@ -1,9 +1,10 @@
using System.Threading.Tasks; using System.Threading.Tasks;
namespace CellularManagement.Application.Services; namespace CellularManagement.Domain.Services;
/// <summary> /// <summary>
/// 密钥轮换服务接口 /// 密钥轮换服务接口
/// 定义密钥轮换的核心业务逻辑
/// </summary> /// </summary>
public interface IKeyRotationService public interface IKeyRotationService
{ {

7
src/CellularManagement.Infrastructure/DependencyInjection.cs

@ -6,13 +6,13 @@ using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using CellularManagement.Domain.Entities; using CellularManagement.Domain.Entities;
using CellularManagement.Domain.Repositories; using CellularManagement.Domain.Repositories;
using CellularManagement.Application.Services;
using CellularManagement.Infrastructure.Services; using CellularManagement.Infrastructure.Services;
using Scrutor; using Scrutor;
using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens; using Microsoft.IdentityModel.Tokens;
using CellularManagement.Infrastructure.Configurations; using CellularManagement.Infrastructure.Configurations;
using CellularManagement.Domain.Services;
namespace CellularManagement.Infrastructure; namespace CellularManagement.Infrastructure;
@ -49,6 +49,11 @@ public static class DependencyInjection
// 添加 JWT 服务 // 添加 JWT 服务
services.AddJwtServices(configuration); services.AddJwtServices(configuration);
// 添加邮箱服务
services.Configure<EmailOptions>(configuration.GetSection(EmailOptions.SectionName));
services.AddScoped<IEmailService, EmailService>();
services.AddScoped<IEmailVerificationService, EmailVerificationService>();
// 配置数据库选项 // 配置数据库选项
services.Configure<DatabaseOptions>(configuration.GetSection(DatabaseOptions.SectionName)); services.Configure<DatabaseOptions>(configuration.GetSection(DatabaseOptions.SectionName));
var databaseOptionsConfig = configuration.GetSection(DatabaseOptions.SectionName).Get<DatabaseOptions>(); var databaseOptionsConfig = configuration.GetSection(DatabaseOptions.SectionName).Get<DatabaseOptions>();

42
src/CellularManagement.Infrastructure/Options/EmailOptions.cs

@ -0,0 +1,42 @@
namespace CellularManagement.Infrastructure.Options;
/// <summary>
/// 邮箱配置选项
/// </summary>
public class EmailOptions
{
/// <summary>
/// 配置节点名称
/// </summary>
public const string SectionName = "Email";
/// <summary>
/// SMTP服务器地址
/// </summary>
public string SmtpServer { get; set; } = string.Empty;
/// <summary>
/// SMTP服务器端口
/// </summary>
public int SmtpPort { get; set; }
/// <summary>
/// 发件人邮箱地址
/// </summary>
public string FromEmail { get; set; } = string.Empty;
/// <summary>
/// 发件人显示名称
/// </summary>
public string FromName { get; set; } = string.Empty;
/// <summary>
/// 邮箱密码或授权码
/// </summary>
public string Password { get; set; } = string.Empty;
/// <summary>
/// 是否启用SSL
/// </summary>
public bool EnableSsl { get; set; }
}

3
src/CellularManagement.Infrastructure/Services/CacheService.cs

@ -1,4 +1,5 @@
using CellularManagement.Application.Services;
using CellularManagement.Domain.Services;
using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Caching.Memory;
namespace CellularManagement.Infrastructure.Services; namespace CellularManagement.Infrastructure.Services;

116
src/CellularManagement.Infrastructure/Services/EmailService.cs

@ -0,0 +1,116 @@
using System;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using CellularManagement.Domain.Services;
using CellularManagement.Infrastructure.Options;
using MailKit.Net.Smtp;
using MailKit.Security;
using Microsoft.Extensions.Options;
using MimeKit;
namespace CellularManagement.Infrastructure.Services;
/// <summary>
/// 邮箱服务实现类
/// </summary>
public class EmailService : IEmailService
{
private readonly EmailOptions _emailOptions;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="emailOptions">邮箱配置选项</param>
public EmailService(IOptions<EmailOptions> emailOptions)
{
_emailOptions = emailOptions.Value;
}
/// <summary>
/// 发送验证码邮件
/// </summary>
public async Task<bool> SendVerificationCodeAsync(string to, string verificationCode)
{
try
{
var message = new MimeMessage();
message.From.Add(new MailboxAddress(_emailOptions.FromName, _emailOptions.FromEmail));
message.To.Add(new MailboxAddress("", to));
message.Subject = "邮箱验证码";
var bodyBuilder = new BodyBuilder
{
HtmlBody = $@"
<h2></h2>
<p><strong>{verificationCode}</strong></p>
<p>5使</p>
<p></p>"
};
message.Body = bodyBuilder.ToMessageBody();
using var client = new SmtpClient();
await client.ConnectAsync(_emailOptions.SmtpServer, _emailOptions.SmtpPort, _emailOptions.EnableSsl ? SecureSocketOptions.SslOnConnect : SecureSocketOptions.StartTls);
await client.AuthenticateAsync(_emailOptions.FromEmail, _emailOptions.Password);
await client.SendAsync(message);
await client.DisconnectAsync(true);
return true;
}
catch (Exception)
{
return false;
}
}
/// <summary>
/// 发送注册成功邮件
/// </summary>
public async Task<bool> SendRegistrationSuccessAsync(string to, string username)
{
try
{
var message = new MimeMessage();
message.From.Add(new MailboxAddress(_emailOptions.FromName, _emailOptions.FromEmail));
message.To.Add(new MailboxAddress(username, to));
message.Subject = "注册成功通知";
var bodyBuilder = new BodyBuilder
{
HtmlBody = $@"
<h2></h2>
<p> {username}</p>
<p></p>
<p>使</p>
<p></p>"
};
message.Body = bodyBuilder.ToMessageBody();
using var client = new SmtpClient();
await client.ConnectAsync(_emailOptions.SmtpServer, _emailOptions.SmtpPort, _emailOptions.EnableSsl ? SecureSocketOptions.SslOnConnect : SecureSocketOptions.StartTls);
await client.AuthenticateAsync(_emailOptions.FromEmail, _emailOptions.Password);
await client.SendAsync(message);
await client.DisconnectAsync(true);
return true;
}
catch (Exception)
{
return false;
}
}
/// <summary>
/// 验证邮箱格式
/// </summary>
public bool ValidateEmail(string email)
{
if (string.IsNullOrWhiteSpace(email))
return false;
// 使用正则表达式验证邮箱格式
const string pattern = @"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$";
return Regex.IsMatch(email, pattern);
}
}

132
src/CellularManagement.Infrastructure/Services/EmailVerificationService.cs

@ -0,0 +1,132 @@
using System;
using System.Threading.Tasks;
using CellularManagement.Domain.Services;
using Microsoft.Extensions.Caching.Distributed;
using System.Text.Json;
namespace CellularManagement.Infrastructure.Services;
/// <summary>
/// 邮箱验证码服务
/// 提供验证码生成、发送和验证的具体实现
/// </summary>
public class EmailVerificationService : IEmailVerificationService
{
private readonly IEmailService _emailService;
private readonly IDistributedCache _cache;
private const string CacheKeyPrefix = "EmailVerification_";
private const int VerificationCodeLength = 6;
private const int VerificationCodeExpirationMinutes = 5;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="emailService">邮箱服务</param>
/// <param name="cache">分布式缓存</param>
public EmailVerificationService(IEmailService emailService, IDistributedCache cache)
{
_emailService = emailService;
_cache = cache;
}
/// <summary>
/// 生成并发送验证码
/// </summary>
/// <param name="email">邮箱地址</param>
/// <returns>是否发送成功</returns>
public async Task<bool> GenerateAndSendVerificationCodeAsync(string email)
{
if (!_emailService.ValidateEmail(email))
{
return false;
}
// 生成6位数字验证码
var verificationCode = GenerateVerificationCode();
// 将验证码保存到缓存
var cacheKey = $"{CacheKeyPrefix}{email}";
var cacheValue = JsonSerializer.Serialize(new
{
Code = verificationCode,
CreatedAt = DateTime.UtcNow
});
await _cache.SetStringAsync(
cacheKey,
cacheValue,
new DistributedCacheEntryOptions
{
AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(VerificationCodeExpirationMinutes)
});
// 发送验证码邮件
return await _emailService.SendVerificationCodeAsync(email, verificationCode);
}
/// <summary>
/// 验证验证码
/// </summary>
/// <param name="email">邮箱地址</param>
/// <param name="code">验证码</param>
/// <returns>验证结果</returns>
public async Task<bool> VerifyCodeAsync(string email, string code)
{
var cacheKey = $"{CacheKeyPrefix}{email}";
var cachedValue = await _cache.GetStringAsync(cacheKey);
if (string.IsNullOrEmpty(cachedValue))
{
return false;
}
var verificationData = JsonSerializer.Deserialize<VerificationData>(cachedValue);
if (verificationData == null)
{
return false;
}
// 验证码是否过期
if (DateTime.UtcNow - verificationData.CreatedAt > TimeSpan.FromMinutes(VerificationCodeExpirationMinutes))
{
await _cache.RemoveAsync(cacheKey);
return false;
}
// 验证码是否匹配
if (verificationData.Code != code)
{
return false;
}
// 验证成功后删除缓存
await _cache.RemoveAsync(cacheKey);
return true;
}
/// <summary>
/// 生成验证码
/// </summary>
private string GenerateVerificationCode()
{
var random = new Random();
var code = string.Empty;
for (int i = 0; i < VerificationCodeLength; i++)
{
code += random.Next(0, 10).ToString();
}
return code;
}
/// <summary>
/// 验证数据
/// </summary>
private class VerificationData
{
public string Code { get; set; } = string.Empty;
public DateTime CreatedAt { get; set; }
}
}

2
src/CellularManagement.Infrastructure/Services/JwtProvider.cs

@ -1,13 +1,13 @@
using System.IdentityModel.Tokens.Jwt; using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims; using System.Security.Claims;
using System.Text; using System.Text;
using CellularManagement.Application.Services;
using CellularManagement.Infrastructure.Options; using CellularManagement.Infrastructure.Options;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens; using Microsoft.IdentityModel.Tokens;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Caching.Memory;
using System.Text.Json; using System.Text.Json;
using CellularManagement.Domain.Services;
namespace CellularManagement.Infrastructure.Services; namespace CellularManagement.Infrastructure.Services;

2
src/CellularManagement.Infrastructure/Services/KeyRotationBackgroundService.cs

@ -1,7 +1,7 @@
using System; using System;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using CellularManagement.Application.Services; using CellularManagement.Domain.Services;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;

2
src/CellularManagement.Infrastructure/Services/KeyRotationService.cs

@ -2,10 +2,10 @@ using System;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using CellularManagement.Application.Services;
using CellularManagement.Infrastructure.Options; using CellularManagement.Infrastructure.Options;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using CellularManagement.Domain.Services;
namespace CellularManagement.Infrastructure.Services; namespace CellularManagement.Infrastructure.Services;

3
src/CellularManagement.Presentation/Controllers/AuthController.cs

@ -5,7 +5,7 @@ using CellularManagement.Application.Features.Auth.Commands.RegisterUser;
using CellularManagement.Application.Features.Auth.Commands.RefreshToken; using CellularManagement.Application.Features.Auth.Commands.RefreshToken;
using CellularManagement.Application.Common; using CellularManagement.Application.Common;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using CellularManagement.Application.Services; using CellularManagement.Domain.Services;
using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Caching.Memory;
using CellularManagement.Infrastructure.Configurations; using CellularManagement.Infrastructure.Configurations;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
@ -13,6 +13,7 @@ using Microsoft.AspNetCore.Http;
using CellularManagement.Presentation.Abstractions; using CellularManagement.Presentation.Abstractions;
using CellularManagement.Application.Features.Auth.Commands.Login; using CellularManagement.Application.Features.Auth.Commands.Login;
using Swashbuckle.AspNetCore.Filters; using Swashbuckle.AspNetCore.Filters;
using CellularManagement.Domain.Common;
namespace CellularManagement.Presentation.Controllers; namespace CellularManagement.Presentation.Controllers;

1
src/CellularManagement.Presentation/Controllers/PermissionsController.cs

@ -6,6 +6,7 @@ using CellularManagement.Application.Common;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using CellularManagement.Presentation.Abstractions; using CellularManagement.Presentation.Abstractions;
using CellularManagement.Domain.Common;
namespace CellularManagement.Presentation.Controllers; namespace CellularManagement.Presentation.Controllers;

1
src/CellularManagement.Presentation/Controllers/RolePermissionController.cs

@ -7,6 +7,7 @@ using CellularManagement.Application.Features.RolePermissions.Queries.GetRolePer
using CellularManagement.Application.Common; using CellularManagement.Application.Common;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using CellularManagement.Presentation.Abstractions; using CellularManagement.Presentation.Abstractions;
using CellularManagement.Domain.Common;
namespace CellularManagement.Presentation.Controllers; namespace CellularManagement.Presentation.Controllers;

1
src/CellularManagement.Presentation/Controllers/RolesController.cs

@ -9,6 +9,7 @@ using CellularManagement.Application.Common;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using CellularManagement.Presentation.Abstractions; using CellularManagement.Presentation.Abstractions;
using CellularManagement.Domain.Common;
namespace CellularManagement.Presentation.Controllers; namespace CellularManagement.Presentation.Controllers;

3
src/CellularManagement.Presentation/Controllers/UsersController.cs

@ -8,9 +8,10 @@ using CellularManagement.Application.Features.Users.Queries.GetAllUsers;
using CellularManagement.Presentation.Abstractions; using CellularManagement.Presentation.Abstractions;
using CellularManagement.Application.Common; using CellularManagement.Application.Common;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using CellularManagement.Application.Services; using CellularManagement.Domain.Services;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using CellularManagement.Domain.Common;
namespace CellularManagement.Presentation.Controllers; namespace CellularManagement.Presentation.Controllers;

17
src/CellularManagement.Presentation/appsettings.json

@ -0,0 +1,17 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"Email": {
"SmtpServer": "smtp.example.com",
"SmtpPort": 587,
"FromEmail": "noreply@example.com",
"FromName": "系统通知",
"Password": "your-email-password",
"EnableSsl": true
}
}
Loading…
Cancel
Save