/// <summary>
 ///
 /// </summary>
 public SettingsService(
     IMapper mapper,
     IIdentityAppSettings identityAppSettings)
 {
     _mapper = mapper;
     _identityAppSettings = identityAppSettings;
 }
        /// <inheritdoc />
        public CreateUserValidator(
            IUnitOfWork ufw,
            IValidationLocalizer localizer,
            IIdentityAppSettings settings,
            IConfiguration?configuration)
        {
            _ufw = ufw;

            RuleFor(_ => _.Description)
            .Length(0, 250)
            .WithMessage(localizer.Message(ValidationConstants.Max250LengthMessageTemplate))
            .WithName(localizer.Name(FieldNameConstants.Description));

            RuleFor(_ => _.UserName)
            .Length(0, 50)
            .WithMessage(localizer.Message(ValidationConstants.Max50LengthMessageTemplate))
            .NotEmpty()
            .WithMessage(localizer.Message(ValidationConstants.NotEmptyMessageTemplate))
            .Must(Validation.UserNameIsValid)
            .WithMessage(localizer.Message(ValidationConstants.OnlyLatinAndSpecSymbolsTemplate))
            .WithName(localizer.Name(FieldNameConstants.UserName));

            RuleFor(_ => _.UserName)
            .Must(UniqueName)
            .WithMessage(localizer.Message(ValidationConstants.EntityMustBeUniqueTemplate))
            .WithName(localizer.Name(FieldNameConstants.UserName));

            RuleFor(_ => _.RoleId)
            .NotEmpty()
            .WithMessage(localizer.Message(ValidationConstants.NotEmptyMessageTemplate))
            .Must(RoleExists)
            .WithMessage(localizer.Message(ValidationConstants.RoleNotFoundTemplate))
            .WithName(localizer.Name(FieldNameConstants.Role));

            RuleFor(_ => _.Password)
            .NotEmpty()
            .WithMessage(localizer.Message(ValidationConstants.NotEmptyMessageTemplate))
            .WithName(localizer.Name(FieldNameConstants.Password));

            PasswordRulesHelper.PasswordBaseRules(
                this,
                settings.PasswordPolicy,
                _ => _.Password,
                localizer,
                configuration,
                FieldNameConstants.Password);

            RuleFor(_ => _.Password)
            .Length(settings.PasswordPolicy.MinimumLength, settings.PasswordPolicy.MaximumLength)
            .WithMessage(
                localizer.Message(ValidationConstants.PasswordRequiredLength, settings.PasswordPolicy.MinimumLength, settings.PasswordPolicy.MaximumLength))
            .When(_ => !string.IsNullOrWhiteSpace(_.Password))
            .WithName(localizer.Name(FieldNameConstants.Password));
        }
        /// <inheritdoc />
        public ChangePasswordValidator(
            IValidationLocalizer localizer,
            IHttpContextAccessor accessor,
            UserManager <User> userManager,
            IIdentityAppSettings settings,
            IConfiguration configuration)
        {
            _userManager = userManager;

            RuleFor(_ => _.NewPassword)
            .NotEmpty()
            .WithMessage(localizer.Message(ValidationConstants.NotEmptyMessageTemplate))
            .WithName(localizer.Name(FieldNameConstants.NewPassword));

            RuleFor(_ => _.NewPassword)
            .MustAsync((_, x, y) => CheckNewPassword(accessor.HttpContext !.User !.Identity !.Name !, _))
            .WithMessage(localizer.Message(ValidationConstants.OldAndNewPassMatch))
            .WithName(localizer.Name(FieldNameConstants.NewPassword));

            RuleFor(_ => _.ConfirmNewPassword)
            .Must((x, y) => x.ConfirmNewPassword == x.NewPassword)
            .WithMessage(localizer.Message(ValidationConstants.NewPassAndConfirmMustBeEqual))
            .WhenAsync((_, x) => CheckOldPass(accessor.HttpContext !.User.Identity !.Name !, _.OldPassword));

            RuleFor(_ => _.OldPassword)
            .MustAsync((_, x) => CheckOldPass(accessor.HttpContext !.User.Identity !.Name !, _))
            .WithMessage(localizer.Message(ValidationConstants.CurrentPasswordIncorrectTemplate));

            PasswordRulesHelper.PasswordBaseRules(
                this,
                settings.PasswordPolicy,
                _ => _.NewPassword,
                localizer,
                configuration,
                FieldNameConstants.NewPassword);

            RuleFor(_ => _.NewPassword)
            .Length(settings.PasswordPolicy.MinimumLength, settings.PasswordPolicy.MaximumLength)
            .WithMessage(
                localizer.Message(ValidationConstants.PasswordRequiredLength, settings.PasswordPolicy.MinimumLength, settings.PasswordPolicy.MaximumLength))
            .When(_ => !string.IsNullOrWhiteSpace(_.NewPassword))
            .WithName(localizer.Name(FieldNameConstants.NewPassword));
        }
Exemplo n.º 4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="UserService"/> class.
 /// </summary>
 public UserService(
     IUnitOfWork ufw,
     UserManager <User> userManager,
     RoleManager <Role> roleManager,
     IHttpContextAccessor accessor,
     IMapper mapper,
     IValidationLocalizer localizer,
     IIdentityAppSettings settings,
     ICacheWrapper cache)
 {
     _ufw         = ufw;
     _userManager = userManager;
     _roleManager = roleManager;
     _accessor    = accessor;
     _mapper      = mapper;
     _localizer   = localizer;
     _settings    = settings;
     _cache       = cache;
 }
Exemplo n.º 5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="AuthService"/> class.
 /// </summary>
 public AuthService(
     UserManager <User> userManager,
     SignInManager <User> signInManager,
     RoleManager <Role> roleManager,
     IOptionsSnapshot <JwtOptions> jwtOptions,
     IValidationLocalizer validationLocalizer,
     IIdentityAppSettings settings,
     ILogger logger,
     IOwnSystemLocalizer <UsersConstants> localizer)
 {
     _jwtOptions          = jwtOptions.Value;
     _userManager         = userManager;
     _signInManager       = signInManager;
     _roleManager         = roleManager;
     _validationLocalizer = validationLocalizer;
     _settings            = settings;
     _logger    = logger;
     _localizer = localizer;
 }
 /// <inheritdoc />
 public IdentityUserManager(
     IUserStore <User> store,
     IOptions <IdentityOptions> optionsAccessor,
     IPasswordHasher <User> passwordHasher,
     IEnumerable <IUserValidator <User> > userValidators,
     IEnumerable <IPasswordValidator <User> > passwordValidators,
     ILookupNormalizer keyNormalizer,
     IdentityErrorDescriber errors,
     IServiceProvider services,
     ILogger <UserManager <User> > logger,
     IIdentityAppSettings settings)
     : base(store, optionsAccessor, passwordHasher, userValidators, passwordValidators, keyNormalizer, errors, services, logger)
 {
     try
     {
         Options.Lockout = settings.PasswordPolicy;
     }
     catch
     {
         //
     }
 }
        /// <inheritdoc />
        public ChangePasswordForUserValidator(
            IValidationLocalizer localizer,
            IIdentityAppSettings settings,
            IUnitOfWork ufw,
            IConfiguration?configuration)
        {
            _ufw = ufw;
            RuleFor(_ => _.UserId)
            .NotEmpty()
            .WithMessage(localizer.Message(ValidationConstants.NotEmptyMessageTemplate))
            .WithName(localizer.Name(FieldNameConstants.User));

            RuleFor(_ => _.UserId)
            .Must(UserExists)
            .WithMessage(localizer.Message(ValidationConstants.UserNotFound));

            RuleFor(_ => _.NewPassword)
            .NotEmpty()
            .WithMessage(localizer.Message(ValidationConstants.NotEmptyMessageTemplate))
            .WithName(localizer.Name(FieldNameConstants.Password));

            PasswordRulesHelper.PasswordBaseRules(
                this,
                settings.PasswordPolicy,
                _ => _.NewPassword,
                localizer,
                configuration,
                FieldNameConstants.Password);

            RuleFor(_ => _.NewPassword)
            .Length(settings.PasswordPolicy.MinimumLength, settings.PasswordPolicy.MaximumLength)
            .WithMessage(
                localizer.Message(ValidationConstants.PasswordRequiredLength, settings.PasswordPolicy.MinimumLength, settings.PasswordPolicy.MaximumLength))
            .When(_ => !string.IsNullOrWhiteSpace(_.NewPassword))
            .WithName(localizer.Name(FieldNameConstants.NewPassword));
        }