LoginValidator.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using FluentValidation;
  2. using GreenTree.Maschinenbestellungen.Core.Helper;
  3. using GreenTree.Maschinenbestellungen.Domain.Model;
  4. using GreenTree.Maschinenbestellungen.Services.Authorization;
  5. using GreenTree.Maschinenbestellungen.Web.Models.Account;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Threading.Tasks;
  10. namespace GreenTree.Maschinenbestellungen.Web.Validators
  11. {
  12. public class LoginValidator : AbstractValidator<LoginModel>
  13. {
  14. #region DI fields
  15. // The global DbContext
  16. private readonly OrderDbContext _eRPDbContext;
  17. // The global user helper
  18. private readonly IUserHelper _userHelper;
  19. // The global administration options
  20. private readonly AdministrationOptions _administrationOptions;
  21. #endregion
  22. #region Ctor
  23. /// <summary>
  24. /// Initializes a new instance of the LoginValidator class
  25. /// </summary>
  26. /// <param name="eRPDbContext">Global DbContext.</param>
  27. /// <param name="userHelper">Global user helper.</param>
  28. public LoginValidator(
  29. OrderDbContext eRPDbContext,
  30. IUserHelper userHelper,
  31. AdministrationOptions administrationOptions)
  32. {
  33. _eRPDbContext = eRPDbContext;
  34. _userHelper = userHelper;
  35. _administrationOptions = administrationOptions;
  36. RuleFor(x => x.Username)
  37. .NotEmpty()
  38. .WithMessage("Benutzername erforderlich.")
  39. .Custom((a, context) =>
  40. {
  41. if (a == _administrationOptions.Administrator)
  42. return;
  43. if (!_eRPDbContext.Users.Any(u => u.Accountname == a || u.MailAddress == a))
  44. {
  45. context.AddFailure("Benutzername nicht gefunden.");
  46. return;
  47. }
  48. });
  49. RuleFor(x => x.Password)
  50. .NotEmpty()
  51. .WithMessage("Passwort erforderlich.")
  52. .Custom((p, context) =>
  53. {
  54. if (String.IsNullOrEmpty(p))
  55. return;
  56. var model = context.InstanceToValidate as LoginModel;
  57. if (model == null)
  58. {
  59. context.AddFailure("Unbekannter Fehler.");
  60. return;
  61. }
  62. if (model.Username == administrationOptions.Administrator)
  63. {
  64. if (_userHelper.HashString(p, false) == _administrationOptions.Password)
  65. return;
  66. else
  67. {
  68. context.AddFailure("Kennwort falsch.");
  69. return;
  70. }
  71. }
  72. var user = _eRPDbContext.Users
  73. .FirstOrDefault(u => u.Accountname == model.Username || u.MailAddress == model.Username);
  74. if (user == null)
  75. {
  76. context.AddFailure("Unbekannter Fehler.");
  77. return;
  78. }
  79. if (user.Password != _userHelper.HashString(p, true))
  80. context.AddFailure("Kennwort falsch.");
  81. return;
  82. });
  83. }
  84. #endregion
  85. }
  86. }