Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate email for member models #17532

Open
wants to merge 4 commits into
base: v15/dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/Umbraco.Core/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Copyright (c) Umbraco.

Check notice on line 1 in src/Umbraco.Core/Extensions/StringExtensions.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (v15/dev)

ℹ Getting worse: Primitive Obsession

The ratio of primitive types in function arguments increases from 84.12% to 84.21%, threshold = 30.0%. The functions in this file have too many primitive types (e.g. int, double, float) in their function argument lists. Using many primitive types lead to the code smell Primitive Obsession. Avoid adding more primitive arguments.

Check notice on line 1 in src/Umbraco.Core/Extensions/StringExtensions.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (v15/dev)

ℹ Getting worse: String Heavy Function Arguments

The ratio of strings in function arguments increases from 68.24% to 68.42%, threshold = 39.0%. The functions in this file have a high ratio of strings as arguments. Avoid adding more.
// See LICENSE for more details.

using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Security.Cryptography;
Expand Down Expand Up @@ -1558,6 +1559,14 @@
yield return sb.ToString();
}

/// <summary>
/// Checks whether a string is a valid email address.
/// </summary>
/// <param name="email">The string check</param>
/// <returns>Returns a bool indicating whether the string is an email address.</returns>
public static bool IsEmail(this string? email) =>
email is not null && new EmailAddressAttribute().IsValid(email);

// having benchmarked various solutions (incl. for/foreach, split and LINQ based ones),
// this is by far the fastest way to find string needles in a string haystack
public static int CountOccurrences(this string haystack, string needle)
Expand Down
6 changes: 2 additions & 4 deletions src/Umbraco.Core/Services/UserService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.ComponentModel.DataAnnotations;

Check notice on line 1 in src/Umbraco.Core/Services/UserService.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (v15/dev)

✅ Getting better: Lines of Code in a Single File

The lines of code decreases from 1789 to 1788, improve code health by reducing it to 1000. The number of Lines of Code in a single file. More Lines of Code lowers the code health.

Check notice on line 1 in src/Umbraco.Core/Services/UserService.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (v15/dev)

✅ Getting better: Number of Functions in a Single Module

The number of functions decreases from 94 to 93, threshold = 75. This file contains too many functions. Beyond a certain threshold, more functions lower the code health.

Check notice on line 1 in src/Umbraco.Core/Services/UserService.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (v15/dev)

✅ Getting better: Primitive Obsession

The ratio of primitive types in function arguments decreases from 56.77% to 56.58%, threshold = 30.0%. The functions in this file have too many primitive types (e.g. int, double, float) in their function argument lists. Using many primitive types lead to the code smell Primitive Obsession. Avoid adding more primitive arguments.
using System.Globalization;
using System.Linq.Expressions;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -917,7 +917,7 @@
{
return UserOperationStatus.UserNameIsNotEmail;
}
if (!IsEmailValid(model.Email))
if (model.Email.IsEmail() is false)
{
return UserOperationStatus.InvalidEmail;
}
Expand Down Expand Up @@ -1136,7 +1136,7 @@
return UserOperationStatus.UserNameIsNotEmail;
}

if (IsEmailValid(model.Email) is false)
if (model.Email.IsEmail() is false)
{
return UserOperationStatus.InvalidEmail;
}
Expand Down Expand Up @@ -1164,8 +1164,6 @@
return UserOperationStatus.Success;
}

private static bool IsEmailValid(string email) => new EmailAddressAttribute().IsValid(email);

private List<int>? GetIdsFromKeys(IEnumerable<Guid>? guids, UmbracoObjectTypes type)
{
var keys = guids?
Expand Down
10 changes: 9 additions & 1 deletion src/Umbraco.Infrastructure/Services/MemberEditingService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Identity;
using System.ComponentModel.DataAnnotations;

Check notice on line 1 in src/Umbraco.Infrastructure/Services/MemberEditingService.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (v15/dev)

✅ Getting better: Overall Code Complexity

The mean cyclomatic complexity decreases from 5.38 to 5.14, threshold = 4. This file has many conditional statements (e.g. if, for, while) across its implementation, leading to lower code health. Avoid adding more conditionals.
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Logging;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Models.ContentEditing;
Expand Down Expand Up @@ -225,6 +226,11 @@
return MemberEditingOperationStatus.InvalidUsername;
}

if (IsEmailValid(model.Email) is false)
{
return MemberEditingOperationStatus.InvalidEmail;
}

Check warning on line 233 in src/Umbraco.Infrastructure/Services/MemberEditingService.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (v15/dev)

❌ Getting worse: Complex Method

ValidateMemberDataAsync increases in cyclomatic complexity from 9 to 10, threshold = 9. This function has many conditional statements (e.g. if, for, while), leading to lower code health. Avoid adding more conditionals and code to it without refactoring.
if (password is not null)
{
IdentityResult validatePassword = await _memberManager.ValidatePasswordAsync(password);
Expand Down Expand Up @@ -393,4 +399,6 @@

return true;
}

private static bool IsEmailValid(string email) => new EmailAddressAttribute().IsValid(email);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Umbraco.

Check notice on line 1 in tests/Umbraco.Tests.UnitTests/Umbraco.Core/ShortStringHelper/StringExtensionsTests.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (v15/dev)

ℹ Getting worse: Primitive Obsession

The ratio of primitive types in function arguments increases from 93.33% to 93.62%, threshold = 30.0%. The functions in this file have too many primitive types (e.g. int, double, float) in their function argument lists. Using many primitive types lead to the code smell Primitive Obsession. Avoid adding more primitive arguments.

Check notice on line 1 in tests/Umbraco.Tests.UnitTests/Umbraco.Core/ShortStringHelper/StringExtensionsTests.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (v15/dev)

✅ Getting better: String Heavy Function Arguments

The ratio of strings in function arguments decreases from 80.00% to 78.72%, threshold = 39.0%. The functions in this file have a high ratio of strings as arguments. Avoid adding more.
// See LICENSE for more details.

using System.Collections.Generic;
Expand Down Expand Up @@ -364,6 +364,16 @@
TryIsFullPath(" ", false, false); // technically, a valid filename on Linux
}

[TestCase("test@test.com",true)]
[TestCase("test@test", true)]
[TestCase("testtest.com", false)]
[TestCase("test@test.dk", true)]
[TestCase("test@test.se", true)]
Zeegaan marked this conversation as resolved.
Show resolved Hide resolved
[TestCase(null, false)]
[TestCase("", false)]
[TestCase(" ", false)]
public void IsEmail(string email, bool isEmail) => Assert.AreEqual(isEmail, email.IsEmail());

private static void TryIsFullPath(string path, bool expectedIsFull, bool expectedIsValid = true)
{
Assert.AreEqual(expectedIsFull, path.IsFullPath(), "IsFullPath('" + path + "')");
Expand Down
Loading