From bab4c7715d10fa7203c948160ea577caba80c04c Mon Sep 17 00:00:00 2001 From: gmargol Date: Thu, 3 Nov 2016 11:21:53 +0000 Subject: [PATCH 1/4] Adding optional isApproved parameter instead of hardcoding 'true' value --- src/Umbraco.Core/Models/Member.cs | 5 +++-- src/Umbraco.Core/Services/IMembershipMemberService.cs | 3 ++- src/Umbraco.Core/Services/MemberService.cs | 9 +++++---- src/Umbraco.Core/Services/UserService.cs | 7 ++++--- .../Security/Providers/UmbracoMembershipProvider.cs | 4 ++-- 5 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/Umbraco.Core/Models/Member.cs b/src/Umbraco.Core/Models/Member.cs index 02d6b9aece..033bbb49b7 100644 --- a/src/Umbraco.Core/Models/Member.cs +++ b/src/Umbraco.Core/Models/Member.cs @@ -96,7 +96,8 @@ namespace Umbraco.Core.Models /// The password value passed in to this parameter should be the encoded/encrypted/hashed format of the member's password /// /// - public Member(string name, string email, string username, string rawPasswordValue, IMemberType contentType) + /// Optional IsApproved parameter + public Member(string name, string email, string username, string rawPasswordValue, IMemberType contentType, bool isApproved = true) : base(name, -1, contentType, new PropertyCollection()) { Mandate.ParameterNotNull(contentType, "contentType"); @@ -106,7 +107,7 @@ namespace Umbraco.Core.Models _email = email; _username = username; _rawPasswordValue = rawPasswordValue; - IsApproved = true; + IsApproved = isApproved; } private static readonly Lazy Ps = new Lazy(); diff --git a/src/Umbraco.Core/Services/IMembershipMemberService.cs b/src/Umbraco.Core/Services/IMembershipMemberService.cs index 801f88d9ac..7419c33254 100644 --- a/src/Umbraco.Core/Services/IMembershipMemberService.cs +++ b/src/Umbraco.Core/Services/IMembershipMemberService.cs @@ -69,8 +69,9 @@ namespace Umbraco.Core.Services /// Email of the to create /// This value should be the encoded/encrypted/hashed value for the password that will be stored in the database /// Alias of the Type + /// IsApproved of the to create /// - T CreateWithIdentity(string username, string email, string passwordValue, string memberTypeAlias); + T CreateWithIdentity(string username, string email, string passwordValue, string memberTypeAlias, bool isApproved = true); /// /// Gets an by its provider key diff --git a/src/Umbraco.Core/Services/MemberService.cs b/src/Umbraco.Core/Services/MemberService.cs index 094539d66e..36bcbf29d2 100644 --- a/src/Umbraco.Core/Services/MemberService.cs +++ b/src/Umbraco.Core/Services/MemberService.cs @@ -830,10 +830,10 @@ namespace Umbraco.Core.Services /// This value should be the encoded/encrypted/hashed value for the password that will be stored in the database /// Alias of the Type /// - IMember IMembershipMemberService.CreateWithIdentity(string username, string email, string passwordValue, string memberTypeAlias) + IMember IMembershipMemberService.CreateWithIdentity(string username, string email, string passwordValue, string memberTypeAlias, bool isApproved) { var memberType = FindMemberTypeByAlias(memberTypeAlias); - return CreateMemberWithIdentity(username, email, username, passwordValue, memberType); + return CreateMemberWithIdentity(username, email, username, passwordValue, memberType, isApproved); } /// @@ -846,12 +846,13 @@ namespace Umbraco.Core.Services /// Name of the Member to create /// This value should be the encoded/encrypted/hashed value for the password that will be stored in the database /// MemberType the Member should be based on + /// Optional IsApproved of the Member to create /// - private IMember CreateMemberWithIdentity(string username, string email, string name, string passwordValue, IMemberType memberType) + private IMember CreateMemberWithIdentity(string username, string email, string name, string passwordValue, IMemberType memberType, bool isApproved = true) { if (memberType == null) throw new ArgumentNullException("memberType"); - var member = new Member(name, email.ToLower().Trim(), username, passwordValue, memberType); + var member = new Member(name, email.ToLower().Trim(), username, passwordValue, memberType, isApproved); if (Saving.IsRaisedEventCancelled(new SaveEventArgs(member), this)) { diff --git a/src/Umbraco.Core/Services/UserService.cs b/src/Umbraco.Core/Services/UserService.cs index 8e984d1e5d..13e502fa4f 100644 --- a/src/Umbraco.Core/Services/UserService.cs +++ b/src/Umbraco.Core/Services/UserService.cs @@ -100,7 +100,7 @@ namespace Umbraco.Core.Services /// This value should be the encoded/encrypted/hashed value for the password that will be stored in the database /// Alias of the Type /// - IUser IMembershipMemberService.CreateWithIdentity(string username, string email, string passwordValue, string memberTypeAlias) + IUser IMembershipMemberService.CreateWithIdentity(string username, string email, string passwordValue, string memberTypeAlias, bool isApproved = true) { var userType = GetUserTypeByAlias(memberTypeAlias); if (userType == null) @@ -120,8 +120,9 @@ namespace Umbraco.Core.Services /// Email of the Member to create /// This value should be the encoded/encrypted/hashed value for the password that will be stored in the database /// MemberType the Member should be based on + /// Optional IsApproved parameter /// - private IUser CreateUserWithIdentity(string username, string email, string passwordValue, IUserType userType) + private IUser CreateUserWithIdentity(string username, string email, string passwordValue, IUserType userType, bool isApproved = true) { if (userType == null) throw new ArgumentNullException("userType"); @@ -145,7 +146,7 @@ namespace Umbraco.Core.Services StartContentId = -1, StartMediaId = -1, IsLockedOut = false, - IsApproved = true + IsApproved = isApproved }; //adding default sections content and media user.AddAllowedSection("content"); diff --git a/src/Umbraco.Web/Security/Providers/UmbracoMembershipProvider.cs b/src/Umbraco.Web/Security/Providers/UmbracoMembershipProvider.cs index 8482eb72a2..71e2b1d0f3 100644 --- a/src/Umbraco.Web/Security/Providers/UmbracoMembershipProvider.cs +++ b/src/Umbraco.Web/Security/Providers/UmbracoMembershipProvider.cs @@ -166,11 +166,11 @@ namespace Umbraco.Web.Security.Providers username, email, FormatPasswordForStorage(encodedPassword, salt), - memberTypeAlias); + memberTypeAlias, + isApproved); member.PasswordQuestion = passwordQuestion; member.RawPasswordAnswerValue = EncryptString(passwordAnswer); - member.IsApproved = isApproved; member.LastLoginDate = DateTime.Now; member.LastPasswordChangeDate = DateTime.Now; From 26d450ad9a3d9d997cafae93f1651a0881f2ef0b Mon Sep 17 00:00:00 2001 From: gmargol Date: Thu, 3 Nov 2016 11:31:33 +0000 Subject: [PATCH 2/4] Adding optional parameters to tests --- .../UmbracoServiceMembershipProviderTests.cs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Umbraco.Tests/Membership/UmbracoServiceMembershipProviderTests.cs b/src/Umbraco.Tests/Membership/UmbracoServiceMembershipProviderTests.cs index 72900351b2..eb26c9f734 100644 --- a/src/Umbraco.Tests/Membership/UmbracoServiceMembershipProviderTests.cs +++ b/src/Umbraco.Tests/Membership/UmbracoServiceMembershipProviderTests.cs @@ -83,10 +83,10 @@ namespace Umbraco.Tests.Membership mServiceMock.Setup(service => service.GetByEmail("test@test.com")).Returns(() => null); mServiceMock.Setup(service => service.GetDefaultMemberType()).Returns("Member"); mServiceMock.Setup( - service => service.CreateWithIdentity(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())) - .Callback((string u, string e, string p, string m) => + service => service.CreateWithIdentity(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())) + .Callback((string u, string e, string p, string m, bool isApproved) => { - createdMember = new Member("test", e, u, p, memberType); + createdMember = new Member("test", e, u, p, memberType, isApproved); }) .Returns(() => createdMember); var provider = new MembersMembershipProvider(mServiceMock.Object); @@ -114,10 +114,10 @@ namespace Umbraco.Tests.Membership mServiceMock.Setup(service => service.GetByEmail("test@test.com")).Returns(() => null); mServiceMock.Setup(service => service.GetDefaultMemberType()).Returns("Member"); mServiceMock.Setup( - service => service.CreateWithIdentity(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())) - .Callback((string u, string e, string p, string m) => + service => service.CreateWithIdentity(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())) + .Callback((string u, string e, string p, string m, bool isApproved) => { - createdMember = new Member("test", e, u, p, memberType); + createdMember = new Member("test", e, u, p, memberType, isApproved); }) .Returns(() => createdMember); @@ -147,10 +147,10 @@ namespace Umbraco.Tests.Membership mServiceMock.Setup(service => service.GetByEmail("test@test.com")).Returns(() => null); mServiceMock.Setup(service => service.GetDefaultMemberType()).Returns("Member"); mServiceMock.Setup( - service => service.CreateWithIdentity(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())) - .Callback((string u, string e, string p, string m) => + service => service.CreateWithIdentity(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())) + .Callback((string u, string e, bool a, string p, string m, bool isApproved) => { - createdMember = new Member("test", e, u, p, memberType); + createdMember = new Member("test", e, u, p, memberType, isApproved); }) .Returns(() => createdMember); From 1e73bb12ac82487fc77500b9e0b258ed11593011 Mon Sep 17 00:00:00 2001 From: gmargol Date: Thu, 3 Nov 2016 16:26:14 +0000 Subject: [PATCH 3/4] Fixing failing unit tests --- .../Membership/UmbracoServiceMembershipProviderTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Umbraco.Tests/Membership/UmbracoServiceMembershipProviderTests.cs b/src/Umbraco.Tests/Membership/UmbracoServiceMembershipProviderTests.cs index eb26c9f734..8ef1265e09 100644 --- a/src/Umbraco.Tests/Membership/UmbracoServiceMembershipProviderTests.cs +++ b/src/Umbraco.Tests/Membership/UmbracoServiceMembershipProviderTests.cs @@ -148,7 +148,7 @@ namespace Umbraco.Tests.Membership mServiceMock.Setup(service => service.GetDefaultMemberType()).Returns("Member"); mServiceMock.Setup( service => service.CreateWithIdentity(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())) - .Callback((string u, string e, bool a, string p, string m, bool isApproved) => + .Callback((string u, string e, string p, string m, bool isApproved) => { createdMember = new Member("test", e, u, p, memberType, isApproved); }) From f0f25c67c64a1a395c65045c04afbac3db19db73 Mon Sep 17 00:00:00 2001 From: Claus Date: Thu, 29 Jun 2017 10:00:16 +0200 Subject: [PATCH 4/4] refactoring method signatures to prevent breaking changes. --- src/Umbraco.Core/Models/Member.cs | 27 +++++++++++++++++-- .../Services/IMembershipMemberService.cs | 13 ++++++++- src/Umbraco.Core/Services/MemberService.cs | 16 +++++++++++ src/Umbraco.Core/Services/UserService.cs | 25 ++++++++++++++--- .../UmbracoServiceMembershipProviderTests.cs | 4 +-- 5 files changed, 76 insertions(+), 9 deletions(-) diff --git a/src/Umbraco.Core/Models/Member.cs b/src/Umbraco.Core/Models/Member.cs index 033bbb49b7..1ed11af93e 100644 --- a/src/Umbraco.Core/Models/Member.cs +++ b/src/Umbraco.Core/Models/Member.cs @@ -96,8 +96,31 @@ namespace Umbraco.Core.Models /// The password value passed in to this parameter should be the encoded/encrypted/hashed format of the member's password /// /// - /// Optional IsApproved parameter - public Member(string name, string email, string username, string rawPasswordValue, IMemberType contentType, bool isApproved = true) + public Member(string name, string email, string username, string rawPasswordValue, IMemberType contentType) + : base(name, -1, contentType, new PropertyCollection()) + { + Mandate.ParameterNotNull(contentType, "contentType"); + + _contentTypeAlias = contentType.Alias; + _contentType = contentType; + _email = email; + _username = username; + _rawPasswordValue = rawPasswordValue; + IsApproved = true; + } + + /// + /// Constructor for creating a Member object + /// + /// + /// + /// + /// + /// The password value passed in to this parameter should be the encoded/encrypted/hashed format of the member's password + /// + /// + /// + public Member(string name, string email, string username, string rawPasswordValue, IMemberType contentType, bool isApproved) : base(name, -1, contentType, new PropertyCollection()) { Mandate.ParameterNotNull(contentType, "contentType"); diff --git a/src/Umbraco.Core/Services/IMembershipMemberService.cs b/src/Umbraco.Core/Services/IMembershipMemberService.cs index 7419c33254..7736a4f609 100644 --- a/src/Umbraco.Core/Services/IMembershipMemberService.cs +++ b/src/Umbraco.Core/Services/IMembershipMemberService.cs @@ -61,6 +61,17 @@ namespace Umbraco.Core.Services /// True if the Member exists otherwise False bool Exists(string username); + /// + /// Creates and persists a new + /// + /// An can be of type or + /// Username of the to create + /// Email of the to create + /// This value should be the encoded/encrypted/hashed value for the password that will be stored in the database + /// Alias of the Type + /// + T CreateWithIdentity(string username, string email, string passwordValue, string memberTypeAlias); + /// /// Creates and persists a new /// @@ -71,7 +82,7 @@ namespace Umbraco.Core.Services /// Alias of the Type /// IsApproved of the to create /// - T CreateWithIdentity(string username, string email, string passwordValue, string memberTypeAlias, bool isApproved = true); + T CreateWithIdentity(string username, string email, string passwordValue, string memberTypeAlias, bool isApproved); /// /// Gets an by its provider key diff --git a/src/Umbraco.Core/Services/MemberService.cs b/src/Umbraco.Core/Services/MemberService.cs index a962d0554c..26e74f66cc 100644 --- a/src/Umbraco.Core/Services/MemberService.cs +++ b/src/Umbraco.Core/Services/MemberService.cs @@ -820,6 +820,22 @@ namespace Umbraco.Core.Services /// This value should be the encoded/encrypted/hashed value for the password that will be stored in the database /// Alias of the Type /// + IMember IMembershipMemberService.CreateWithIdentity(string username, string email, string passwordValue, string memberTypeAlias) + { + var memberType = FindMemberTypeByAlias(memberTypeAlias); + return CreateMemberWithIdentity(username, email, username, passwordValue, memberType); + } + + /// + /// Creates and persists a new + /// + /// An can be of type or + /// Username of the to create + /// Email of the to create + /// This value should be the encoded/encrypted/hashed value for the password that will be stored in the database + /// Alias of the Type + /// Is the member approved + /// IMember IMembershipMemberService.CreateWithIdentity(string username, string email, string passwordValue, string memberTypeAlias, bool isApproved) { var memberType = FindMemberTypeByAlias(memberTypeAlias); diff --git a/src/Umbraco.Core/Services/UserService.cs b/src/Umbraco.Core/Services/UserService.cs index c6f8676cef..68aa66e231 100644 --- a/src/Umbraco.Core/Services/UserService.cs +++ b/src/Umbraco.Core/Services/UserService.cs @@ -19,7 +19,6 @@ namespace Umbraco.Core.Services /// public class UserService : ScopeRepositoryService, IUserService { - //TODO: We need to change the isUpgrading flag to use an app state enum as described here: http://issues.umbraco.org/issue/U4-6816 // in the meantime, we will use a boolean which we are currently using during upgrades to ensure that a user object is not persisted during this phase, otherwise // exceptions can occur if the db is not in it's correct state. @@ -102,7 +101,7 @@ namespace Umbraco.Core.Services /// This value should be the encoded/encrypted/hashed value for the password that will be stored in the database /// Alias of the Type /// - IUser IMembershipMemberService.CreateWithIdentity(string username, string email, string passwordValue, string memberTypeAlias, bool isApproved = true) + IUser IMembershipMemberService.CreateWithIdentity(string username, string email, string passwordValue, string memberTypeAlias) { var userType = GetUserTypeByAlias(memberTypeAlias); if (userType == null) @@ -113,6 +112,26 @@ namespace Umbraco.Core.Services return CreateUserWithIdentity(username, email, passwordValue, userType); } + /// + /// Creates and persists a new + /// + /// Username of the to create + /// Email of the to create + /// This value should be the encoded/encrypted/hashed value for the password that will be stored in the database + /// Alias of the Type + /// Is the member approved + /// + IUser IMembershipMemberService.CreateWithIdentity(string username, string email, string passwordValue, string memberTypeAlias, bool isApproved) + { + var userType = GetUserTypeByAlias(memberTypeAlias); + if (userType == null) + { + throw new EntityNotFoundException("The user type " + memberTypeAlias + " could not be resolved"); + } + + return CreateUserWithIdentity(username, email, passwordValue, userType, isApproved); + } + /// /// Creates and persists a Member /// @@ -122,7 +141,7 @@ namespace Umbraco.Core.Services /// Email of the Member to create /// This value should be the encoded/encrypted/hashed value for the password that will be stored in the database /// MemberType the Member should be based on - /// Optional IsApproved parameter + /// Is the user approved /// private IUser CreateUserWithIdentity(string username, string email, string passwordValue, IUserType userType, bool isApproved = true) { diff --git a/src/Umbraco.Tests/Membership/UmbracoServiceMembershipProviderTests.cs b/src/Umbraco.Tests/Membership/UmbracoServiceMembershipProviderTests.cs index 8ef1265e09..0f37a4aa26 100644 --- a/src/Umbraco.Tests/Membership/UmbracoServiceMembershipProviderTests.cs +++ b/src/Umbraco.Tests/Membership/UmbracoServiceMembershipProviderTests.cs @@ -1,6 +1,4 @@ -using System; -using System.Collections.Specialized; -using System.Configuration.Provider; +using System.Collections.Specialized; using System.Web.Security; using Moq; using NUnit.Framework;