UserIdentifier.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using System;
  2. using System.Reflection;
  3. using Abp.Extensions;
  4. namespace Abp
  5. {
  6. /// <summary>
  7. /// Used to identify a user.
  8. /// </summary>
  9. [Serializable]
  10. public class UserIdentifier : IUserIdentifier
  11. {
  12. /// <summary>
  13. /// Tenant Id of the user.
  14. /// Can be null for host users in a multi tenant application.
  15. /// </summary>
  16. public int? TenantId { get; protected set; }
  17. /// <summary>
  18. /// Id of the user.
  19. /// </summary>
  20. public long UserId { get; protected set; }
  21. /// <summary>
  22. /// Initializes a new instance of the <see cref="UserIdentifier"/> class.
  23. /// </summary>
  24. protected UserIdentifier()
  25. {
  26. }
  27. /// <summary>
  28. /// Initializes a new instance of the <see cref="UserIdentifier"/> class.
  29. /// </summary>
  30. /// <param name="tenantId">Tenant Id of the user.</param>
  31. /// <param name="userId">Id of the user.</param>
  32. public UserIdentifier(int? tenantId, long userId)
  33. {
  34. TenantId = tenantId;
  35. UserId = userId;
  36. }
  37. /// <summary>
  38. /// Parses given string and creates a new <see cref="UserIdentifier"/> object.
  39. /// </summary>
  40. /// <param name="userIdentifierString">
  41. /// Should be formatted one of the followings:
  42. ///
  43. /// - "userId@tenantId". Ex: "42@3" (for tenant users).
  44. /// - "userId". Ex: 1 (for host users)
  45. /// </param>
  46. public static UserIdentifier Parse(string userIdentifierString)
  47. {
  48. if (userIdentifierString.IsNullOrEmpty())
  49. {
  50. throw new ArgumentNullException(nameof(userIdentifierString), "userAtTenant can not be null or empty!");
  51. }
  52. var splitted = userIdentifierString.Split('@');
  53. if (splitted.Length == 1)
  54. {
  55. return new UserIdentifier(null, splitted[0].To<long>());
  56. }
  57. if (splitted.Length == 2)
  58. {
  59. return new UserIdentifier(splitted[1].To<int>(), splitted[0].To<long>());
  60. }
  61. throw new ArgumentException("userAtTenant is not properly formatted", nameof(userIdentifierString));
  62. }
  63. /// <summary>
  64. /// Creates a string represents this <see cref="UserIdentifier"/> instance.
  65. /// Formatted one of the followings:
  66. ///
  67. /// - "userId@tenantId". Ex: "42@3" (for tenant users).
  68. /// - "userId". Ex: 1 (for host users)
  69. ///
  70. /// Returning string can be used in <see cref="Parse"/> method to re-create identical <see cref="UserIdentifier"/> object.
  71. /// </summary>
  72. public string ToUserIdentifierString()
  73. {
  74. if (TenantId == null)
  75. {
  76. return UserId.ToString();
  77. }
  78. return UserId + "@" + TenantId;
  79. }
  80. public override bool Equals(object obj)
  81. {
  82. if (obj == null || !(obj is UserIdentifier))
  83. {
  84. return false;
  85. }
  86. //Same instances must be considered as equal
  87. if (ReferenceEquals(this, obj))
  88. {
  89. return true;
  90. }
  91. //Transient objects are not considered as equal
  92. var other = (UserIdentifier)obj;
  93. //Must have a IS-A relation of types or must be same type
  94. var typeOfThis = GetType();
  95. var typeOfOther = other.GetType();
  96. if (!typeOfThis.GetTypeInfo().IsAssignableFrom(typeOfOther) && !typeOfOther.GetTypeInfo().IsAssignableFrom(typeOfThis))
  97. {
  98. return false;
  99. }
  100. return TenantId == other.TenantId && UserId == other.UserId;
  101. }
  102. /// <inheritdoc/>
  103. public override int GetHashCode()
  104. {
  105. return TenantId == null ? (int)UserId : (int)(TenantId.Value ^ UserId);
  106. }
  107. /// <inheritdoc/>
  108. public static bool operator ==(UserIdentifier left, UserIdentifier right)
  109. {
  110. if (Equals(left, null))
  111. {
  112. return Equals(right, null);
  113. }
  114. return left.Equals(right);
  115. }
  116. /// <inheritdoc/>
  117. public static bool operator !=(UserIdentifier left, UserIdentifier right)
  118. {
  119. return !(left == right);
  120. }
  121. public override string ToString()
  122. {
  123. return ToUserIdentifierString();
  124. }
  125. }
  126. }