Skip to content

Commit 772c523

Browse files
Zeegaanelit0451
andcommitted
Validate email for member models (#17532)
* Validate email for member models * Add null check or more test cases * return invalid when not a valid email * Cleanup * remove private method in favor of extension * Remove non used, using statement --------- Co-authored-by: Elitsa <[email protected]> (cherry picked from commit 6b0f8e7)
1 parent 1e9182c commit 772c523

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

src/Umbraco.Core/Extensions/StringExtensions.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// See LICENSE for more details.
33

44
using System.ComponentModel;
5+
using System.ComponentModel.DataAnnotations;
56
using System.Diagnostics.CodeAnalysis;
67
using System.Globalization;
78
using System.Security.Cryptography;
@@ -1558,6 +1559,14 @@ public static IEnumerable<string> EscapedSplit(this string value, char splitChar
15581559
yield return sb.ToString();
15591560
}
15601561

1562+
/// <summary>
1563+
/// Checks whether a string is a valid email address.
1564+
/// </summary>
1565+
/// <param name="email">The string check</param>
1566+
/// <returns>Returns a bool indicating whether the string is an email address.</returns>
1567+
public static bool IsEmail(this string? email) =>
1568+
string.IsNullOrWhiteSpace(email) is false && new EmailAddressAttribute().IsValid(email);
1569+
15611570
// having benchmarked various solutions (incl. for/foreach, split and LINQ based ones),
15621571
// this is by far the fastest way to find string needles in a string haystack
15631572
public static int CountOccurrences(this string haystack, string needle)

src/Umbraco.Core/Services/UserService.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -915,7 +915,7 @@ private async Task<UserOperationStatus> ValidateUserCreateModel(UserCreateModel
915915
{
916916
return UserOperationStatus.UserNameIsNotEmail;
917917
}
918-
if (!IsEmailValid(model.Email))
918+
if (model.Email.IsEmail() is false)
919919
{
920920
return UserOperationStatus.InvalidEmail;
921921
}
@@ -1134,7 +1134,7 @@ private UserOperationStatus ValidateUserUpdateModel(IUser existingUser, UserUpda
11341134
return UserOperationStatus.UserNameIsNotEmail;
11351135
}
11361136

1137-
if (IsEmailValid(model.Email) is false)
1137+
if (model.Email.IsEmail() is false)
11381138
{
11391139
return UserOperationStatus.InvalidEmail;
11401140
}
@@ -1162,8 +1162,6 @@ private UserOperationStatus ValidateUserUpdateModel(IUser existingUser, UserUpda
11621162
return UserOperationStatus.Success;
11631163
}
11641164

1165-
private static bool IsEmailValid(string email) => new EmailAddressAttribute().IsValid(email);
1166-
11671165
private List<int>? GetIdsFromKeys(IEnumerable<Guid>? guids, UmbracoObjectTypes type)
11681166
{
11691167
var keys = guids?

src/Umbraco.Infrastructure/Services/MemberEditingService.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,11 @@ private async Task<MemberEditingOperationStatus> ValidateMemberDataAsync(MemberE
225225
return MemberEditingOperationStatus.InvalidUsername;
226226
}
227227

228+
if (model.Email.IsEmail() is false)
229+
{
230+
return MemberEditingOperationStatus.InvalidEmail;
231+
}
232+
228233
if (password is not null)
229234
{
230235
IdentityResult validatePassword = await _memberManager.ValidatePasswordAsync(password);

tests/Umbraco.Tests.UnitTests/Umbraco.Core/ShortStringHelper/StringExtensionsTests.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,16 @@ public void IsFullPath()
364364
TryIsFullPath(" ", false, false); // technically, a valid filename on Linux
365365
}
366366

367+
[TestCase("[email protected]", true)]
368+
[TestCase("test@test", true)]
369+
[TestCase("testtest.com", false)]
370+
[TestCase("[email protected]", true)]
371+
[TestCase("[email protected]", true)]
372+
[TestCase(null, false)]
373+
[TestCase("", false)]
374+
[TestCase(" ", false)]
375+
public void IsEmail(string? email, bool isEmail) => Assert.AreEqual(isEmail, email.IsEmail());
376+
367377
private static void TryIsFullPath(string path, bool expectedIsFull, bool expectedIsValid = true)
368378
{
369379
Assert.AreEqual(expectedIsFull, path.IsFullPath(), "IsFullPath('" + path + "')");

0 commit comments

Comments
 (0)