Skip to content

Commit

Permalink
React to localization workaround removal for data annotations attribu…
Browse files Browse the repository at this point in the history
…tes (#12333)
  • Loading branch information
hishamco authored Sep 8, 2022
1 parent 7956537 commit 793526b
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 150 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Localization;

namespace OrchardCore.Email.ViewModels
{
public class SmtpSettingsViewModel : IValidatableObject
public class SmtpSettingsViewModel
{
[Required(AllowEmptyStrings = false)]
public string To { get; set; }

[EmailAddress(ErrorMessage = "Invalid Email.")]
public string Sender { get; set; }

public string Bcc { get; set; }
Expand All @@ -22,16 +19,5 @@ public class SmtpSettingsViewModel : IValidatableObject
public string Subject { get; set; }

public string Body { get; set; }

public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
var emailAddressValidator = validationContext.GetService<IEmailAddressValidator>();
var S = validationContext.GetService<IStringLocalizer<SmtpSettingsViewModel>>();

if (!String.IsNullOrWhiteSpace(Sender) && !emailAddressValidator.Validate(Sender))
{
yield return new ValidationResult(S["Invalid Email."], new[] { nameof(Sender) });
}
}
}
}
27 changes: 27 additions & 0 deletions src/OrchardCore.Modules/OrchardCore.OpenId/UrlAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.ComponentModel.DataAnnotations;

namespace OrchardCore.OpenId;

public class UrlAttribute : ValidationAttribute
{
private static readonly char[] _urlSeparators = new[] { ' ', ',' };

protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
if (value != null)
{
var urls = value.ToString();

foreach (var url in urls.Split(_urlSeparators, StringSplitOptions.RemoveEmptyEntries))
{
if (!Uri.TryCreate(url, UriKind.Absolute, out var uri) || !uri.IsWellFormedOriginalString())
{
return new ValidationResult(ErrorMessage, new[] { urls });
}
}
}

return ValidationResult.Success;
}
}
Original file line number Diff line number Diff line change
@@ -1,40 +1,51 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Localization;

namespace OrchardCore.OpenId.ViewModels
{
public class CreateOpenIdApplicationViewModel : IValidatableObject
public class CreateOpenIdApplicationViewModel
{
[Required]
public string ClientId { get; set; }

[Required]
public string DisplayName { get; set; }

[Url(ErrorMessage = "{0} is not well-formed")]
public string RedirectUris { get; set; }

[Url(ErrorMessage = "{0} is not well-formed")]
public string PostLogoutRedirectUris { get; set; }

public string Type { get; set; }

public string ConsentType { get; set; }

public string ClientSecret { get; set; }

public List<RoleEntry> RoleEntries { get; } = new List<RoleEntry>();

public List<ScopeEntry> ScopeEntries { get; } = new List<ScopeEntry>();

public bool AllowPasswordFlow { get; set; }

public bool AllowClientCredentialsFlow { get; set; }

public bool AllowAuthorizationCodeFlow { get; set; }

public bool AllowRefreshTokenFlow { get; set; }

public bool AllowHybridFlow { get; set; }

public bool AllowImplicitFlow { get; set; }

public bool AllowLogoutEndpoint { get; set; }

public bool AllowIntrospectionEndpoint { get; set; }

public bool AllowRevocationEndpoint { get; set; }
public bool RequireProofKeyForCodeExchange { get; set; }

public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) => ValidateUrls(validationContext, nameof(RedirectUris), RedirectUris)
.Union(ValidateUrls(validationContext, nameof(PostLogoutRedirectUris), PostLogoutRedirectUris));
public bool RequireProofKeyForCodeExchange { get; set; }

public class RoleEntry
{
Expand All @@ -46,21 +57,5 @@ public class ScopeEntry
public string Name { get; set; }
public bool Selected { get; set; }
}

private IEnumerable<ValidationResult> ValidateUrls(ValidationContext context, string memberName, string member)
{
if (member != null)
{
var S = context.GetRequiredService<IStringLocalizer<CreateOpenIdApplicationViewModel>>();

foreach (var url in member.Split(new[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries))
{
if (!Uri.TryCreate(url, UriKind.Absolute, out var uri) || !uri.IsWellFormedOriginalString())
{
yield return new ValidationResult(S["{0} is not well-formed", url], new[] { memberName });
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Localization;

namespace OrchardCore.OpenId.ViewModels
{
public class EditOpenIdApplicationViewModel : IValidatableObject
public class EditOpenIdApplicationViewModel
{
[HiddenInput]
public string Id { get; set; }
Expand All @@ -19,26 +15,41 @@ public class EditOpenIdApplicationViewModel : IValidatableObject
[Required]
public string DisplayName { get; set; }

[Url(ErrorMessage = "{0} is not well-formed")]
public string RedirectUris { get; set; }

[Url(ErrorMessage = "{0} is not well-formed")]
public string PostLogoutRedirectUris { get; set; }

public string Type { get; set; }

public string ConsentType { get; set; }

public string ClientSecret { get; set; }

public List<RoleEntry> RoleEntries { get; } = new List<RoleEntry>();

public List<ScopeEntry> ScopeEntries { get; } = new List<ScopeEntry>();

public bool AllowPasswordFlow { get; set; }

public bool AllowClientCredentialsFlow { get; set; }

public bool AllowAuthorizationCodeFlow { get; set; }

public bool AllowRefreshTokenFlow { get; set; }

public bool AllowHybridFlow { get; set; }

public bool AllowImplicitFlow { get; set; }

public bool AllowLogoutEndpoint { get; set; }

public bool AllowIntrospectionEndpoint { get; set; }

public bool AllowRevocationEndpoint { get; set; }
public bool RequireProofKeyForCodeExchange { get; set; }

public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) => ValidateUrls(validationContext, nameof(RedirectUris), RedirectUris)
.Union(ValidateUrls(validationContext, nameof(PostLogoutRedirectUris), PostLogoutRedirectUris));
public bool RequireProofKeyForCodeExchange { get; set; }

public class RoleEntry
{
Expand All @@ -51,21 +62,5 @@ public class ScopeEntry
public string Name { get; set; }
public bool Selected { get; set; }
}

private IEnumerable<ValidationResult> ValidateUrls(ValidationContext context, string memberName, string member)
{
if (member != null)
{
var S = context.GetRequiredService<IStringLocalizer<EditOpenIdApplicationViewModel>>();

foreach (var url in member.Split(new[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries))
{
if (!Uri.TryCreate(url, UriKind.Absolute, out var uri) || !uri.IsWellFormedOriginalString())
{
yield return new ValidationResult(S["{0} is not well-formed", url], new[] { memberName });
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,11 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Localization;
using OrchardCore.Email;

namespace OrchardCore.Users.ViewModels
{
public class ChangeEmailViewModel : IValidatableObject
public class ChangeEmailViewModel
{
[Required(ErrorMessage = "Email is required.")]
[Email.EmailAddress(ErrorMessage = "Invalid Email.")]
public string Email { get; set; }

public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
var emailAddressValidator = validationContext.GetService<IEmailAddressValidator>();
var S = validationContext.GetService<IStringLocalizer<ChangeEmailViewModel>>();
if (string.IsNullOrWhiteSpace(Email))
{
yield return new ValidationResult(S["Email is required."], new[] { nameof(Email) });
}
else if (!emailAddressValidator.Validate(Email))
{
yield return new ValidationResult(S["Invalid Email."], new[] { nameof(Email) });
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,32 +1,18 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Localization;
using OrchardCore.Email;

namespace OrchardCore.Users.ViewModels
{
public class EditUserInformationViewModel : IValidatableObject
public class EditUserInformationViewModel
{
[Required]
public string UserName { get; set; }

[Required]
[Email.EmailAddress(ErrorMessage = "Invalid Email.")]
public string Email { get; set; }

[BindNever]
public bool IsEditingDisabled { get; set; }

public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
var emailAddressValidator = validationContext.GetService<IEmailAddressValidator>();
var S = validationContext.GetService<IStringLocalizer<EditUserInformationViewModel>>();

if (!string.IsNullOrEmpty(Email) && !emailAddressValidator.Validate(Email))
{
yield return new ValidationResult(S["Invalid Email."], new[] { "Email" });
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,11 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Localization;
using OrchardCore.Email;

namespace OrchardCore.Users.ViewModels
{
public class ForgotPasswordViewModel : IValidatableObject
public class ForgotPasswordViewModel
{
[Required]
[Required(ErrorMessage = "Email is required.")]
[Email.EmailAddress(ErrorMessage = "Invalid Email.")]
public string Email { get; set; }

public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
var emailAddressValidator = validationContext.GetService<IEmailAddressValidator>();
var S = validationContext.GetService<IStringLocalizer<ForgotPasswordViewModel>>();

if (string.IsNullOrWhiteSpace(Email))
{
yield return new ValidationResult(S["Email is required."], new[] { nameof(Email) });
}
else if (!emailAddressValidator.Validate(Email))
{
yield return new ValidationResult(S["Invalid Email."], new[] { nameof(Email) });
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,11 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Localization;

namespace OrchardCore.Users.ViewModels
{
public class LinkExternalLoginViewModel : IValidatableObject
public class LinkExternalLoginViewModel
{
[DataType(DataType.Password)]
[Required(ErrorMessage = "Password is required.")]
public string Password { get; set; }

public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
var S = validationContext.GetService<IStringLocalizer<LinkExternalLoginViewModel>>();
if (string.IsNullOrWhiteSpace(Password))
{
yield return new ValidationResult(S["Password is required"], new[] { nameof(Password) });
}
}
}
}
Loading

0 comments on commit 793526b

Please sign in to comment.