ApplicationUser.cs 1.7 KB

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