IwbIdentityResultExtensions.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Abp.Collections.Extensions;
  5. using Abp.Localization;
  6. using Abp.Localization.Sources;
  7. using Abp.Logging;
  8. using Abp.Text;
  9. using Abp.UI;
  10. using Microsoft.AspNet.Identity;
  11. namespace IwbZero.IdentityFramework
  12. {
  13. public static class IwbIdentityResultExtensions
  14. {
  15. private static readonly Dictionary<string, string> IdentityLocalizations
  16. = new Dictionary<string, string>
  17. {
  18. {"User already in role.", "Identity.UserAlreadyInRole"},
  19. {"User is not in role.", "Identity.UserNotInRole"},
  20. {"Role {0} does not exist.", "Identity.RoleNotFound"},
  21. {"Incorrect password.", "Identity.PasswordMismatch"},
  22. {"User name {0} is invalid, can only contain letters or digits.", "Identity.InvalidUserName"},
  23. {"Passwords must be at least {0} characters.", "Identity.PasswordTooShort"},
  24. {"{0} cannot be null or empty.", "Identity.PropertyTooShort"},
  25. {"Name {0} is already taken.", "Identity.DuplicateUserName"},
  26. {"User already has a password set.", "Identity.UserAlreadyHasPassword"},
  27. {"Passwords must have at least one non letter or digit character.", "Identity.PasswordRequireNonLetterOrDigit"},
  28. {"UserId not found.", "Identity.UserIdNotFound"},
  29. {"Invalid token.", "Identity.InvalidToken"},
  30. {"Email '{0}' is invalid.", "Identity.InvalidEmail"},
  31. {"User {0} does not exist.", "Identity.UserNameNotFound"},
  32. {"Lockout is not enabled for this user.", "Identity.LockoutNotEnabled"},
  33. {"Passwords must have at least one uppercase ('A'-'Z').", "Identity.PasswordRequireUpper"},
  34. {"Passwords must have at least one digit ('0'-'9').", "Identity.PasswordRequireDigit"},
  35. {"Passwords must have at least one lowercase ('a'-'z').", "Identity.PasswordRequireLower"},
  36. {"Email '{0}' is already taken.", "Identity.DuplicateEmail"},
  37. {"A user with that external login already exists.", "Identity.ExternalLoginExists"},
  38. {"An unknown failure has occured.", "Identity.DefaultError"}
  39. };
  40. /// <summary>
  41. /// Checks errors of given <see cref="IdentityResult"/> and throws <see cref="UserFriendlyException"/> if it's not succeeded.
  42. /// </summary>
  43. /// <param name="identityResult">Identity result to check</param>
  44. /// <param name="severity">Exception severity</param>
  45. public static void CheckErrors(this IdentityResult identityResult,LogSeverity severity = LogSeverity.Warn)
  46. {
  47. if (identityResult.Succeeded)
  48. {
  49. return;
  50. }
  51. throw new UserFriendlyException(identityResult.Errors.JoinAsString(", "), severity);
  52. }
  53. /// <summary>
  54. /// Checks errors of given <see cref="IdentityResult"/> and throws <see cref="UserFriendlyException"/> if it's not succeeded.
  55. /// </summary>
  56. /// <param name="identityResult">Identity result to check</param>
  57. /// <param name="localizationManager">Localization manager to localize error messages</param>
  58. /// <param name="localizationSourceName"></param>
  59. /// <param name="severity">Exception severity</param>
  60. public static void CheckErrors(this IdentityResult identityResult, ILocalizationManager localizationManager, string localizationSourceName = null,LogSeverity severity = LogSeverity.Warn)
  61. {
  62. if (identityResult.Succeeded)
  63. {
  64. return;
  65. }
  66. throw new UserFriendlyException(identityResult.LocalizeErrors(localizationManager, localizationSourceName), severity);
  67. }
  68. public static string LocalizeErrors(this IdentityResult identityResult, ILocalizationManager localizationManager, string localizationSourceName = null)
  69. {
  70. if (identityResult.Succeeded)
  71. {
  72. throw new ArgumentException("identityResult.Succeeded should be false in order to localize errors.");
  73. }
  74. if (identityResult.Errors == null)
  75. {
  76. throw new ArgumentException("identityResult.Errors should not be null.");
  77. }
  78. if (identityResult is IwbIdentityResult)
  79. {
  80. return identityResult.Errors.JoinAsString(" ");
  81. }
  82. return identityResult.Errors.Select(err => LocalizeErrorMessage(err, localizationManager, localizationSourceName)).JoinAsString(" ");
  83. }
  84. private static string LocalizeErrorMessage(string identityErrorMessage, ILocalizationManager localizationManager, string localizationSourceName = null)
  85. {
  86. localizationSourceName=localizationSourceName?? IwbZeroConsts.IwbZeroLocalizationSourceName;
  87. var localizationSource = localizationManager.GetSource(localizationSourceName);
  88. foreach (var identityLocalization in IdentityLocalizations)
  89. {
  90. string[] values;
  91. if (FormattedStringValueExtracter.IsMatch(identityErrorMessage, identityLocalization.Key, out values))
  92. {
  93. return localizationSource.GetString(identityLocalization.Value, values.Cast<object>().ToArray());
  94. }
  95. }
  96. return localizationSource.GetString("Identity.DefaultError");
  97. }
  98. }
  99. }