ApplicationUser.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using Microsoft.AspNetCore.Identity;
  2. using System.ComponentModel.DataAnnotations.Schema;
  3. namespace Infrastructure.Persistence.Identity
  4. {
  5. public sealed class ApplicationUser : IdentityUser
  6. {
  7. [NotMapped]
  8. public string ID
  9. {
  10. get => Id;
  11. private set => Id = value;
  12. }
  13. public string? FullName { get; private set; }
  14. public bool IsDeleted { get; private set; } = false;
  15. // EF Core / Identity requires a public parameterless ctor
  16. public ApplicationUser() { }
  17. private ApplicationUser(string id)
  18. {
  19. if (string.IsNullOrWhiteSpace(id))
  20. {
  21. throw new ArgumentException("ID is required.", nameof(id));
  22. }
  23. ID = id;
  24. }
  25. public static ApplicationUser Create(string id, string? fullName = null)
  26. {
  27. var user = new ApplicationUser(id);
  28. user.FullName = fullName;
  29. return user;
  30. }
  31. public void SetFullName(string? fullName)
  32. {
  33. FullName = string.IsNullOrWhiteSpace(fullName) ? null : fullName.Trim();
  34. }
  35. public void SetEmail(string? email)
  36. {
  37. Email = string.IsNullOrWhiteSpace(email) ? null : email.Trim();
  38. NormalizedEmail = Email?.ToUpperInvariant();
  39. }
  40. public void SetPhoneNumber(string? phoneNumber)
  41. {
  42. PhoneNumber = string.IsNullOrWhiteSpace(phoneNumber) ? null : phoneNumber.Trim();
  43. }
  44. public void SetDeleted(bool isDeleted)
  45. {
  46. IsDeleted = isDeleted;
  47. }
  48. public void SetEmailConfirmed(bool emailConfirmed)
  49. {
  50. EmailConfirmed = emailConfirmed;
  51. }
  52. public void SetLockoutEnd(bool lockoutEnd)
  53. {
  54. LockoutEnabled = lockoutEnd;
  55. LockoutEnd = lockoutEnd ? DateTimeOffset.MaxValue : null;
  56. }
  57. }
  58. }