Gets #U4-4454 Can't log in a new member created through memberservice completed

This commit is contained in:
Shannon
2014-03-18 18:41:18 +11:00
parent b2aeb835d5
commit 7a7720c460
8 changed files with 191 additions and 68 deletions

View File

@@ -27,6 +27,7 @@ namespace Umbraco.Core.Models.Membership
if (userType == null) throw new ArgumentNullException("userType");
_userType = userType;
_defaultPermissions = _userType.Permissions;
//Groups = new List<object> { userType };
SessionTimeout = 60;
_sectionCollection = new ObservableCollection<string>();
@@ -38,6 +39,8 @@ namespace Umbraco.Core.Models.Membership
_isLockedOut = false;
_startContentId = -1;
_startMediaId = -1;
//cannot be null
_rawPasswordValue = "";
}
public User(string name, string email, string username, string rawPasswordValue, IUserType userType)

View File

@@ -5,11 +5,17 @@ using Umbraco.Core.Persistence.Querying;
namespace Umbraco.Core.Services
{
/// <summary>
/// Defines the MemberService, which is an easy access to operations involving (umbraco) members.
/// </summary>
public interface IMemberService : IMembershipMemberService
{
IMember CreateMember(string username, string email, string name, string memberTypeAlias);
IMember CreateMember(string username, string email, string name, IMemberType memberType);
IMember CreateMemberWithIdentity(string username, string email, string name, string memberTypeAlias);
IMember CreateMemberWithIdentity(string username, string email, string name, IMemberType memberType);
/// <summary>
/// This is simply a helper method which essentially just wraps the MembershipProvider's ChangePassword method
/// </summary>

View File

@@ -13,8 +13,7 @@ namespace Umbraco.Core.Services
/// Idea is to have this is an isolated interface so that it can be easily 'replaced' in the membership provider impl.
/// </remarks>
public interface IMembershipMemberService : IMembershipMemberService<IMember>, IMembershipRoleService<IMember>
{
IMember CreateMember(string username, string email, string memberType);
{
IMember CreateMemberWithIdentity(string username, string email, IMemberType memberType);
}

View File

@@ -10,8 +10,6 @@ namespace Umbraco.Core.Services
/// </remarks>
public interface IMembershipUserService : IMembershipMemberService<IUser>
{
IUser CreateUserWithIdentity(string username, string email, string password, IUserType userType);
IUser CreateUserWithIdentity(string username, string email, IUserType userType);
}
}

View File

@@ -9,6 +9,16 @@ namespace Umbraco.Core.Services
/// </summary>
public interface IUserService : IMembershipUserService
{
/// <summary>
/// This is simply a helper method which essentially just wraps the MembershipProvider's ChangePassword method
/// </summary>
/// <param name="user">The user to save the password for</param>
/// <param name="password"></param>
/// <remarks>
/// This method exists so that Umbraco developers can use one entry point to create/update users if they choose to.
/// </remarks>
void SavePassword(IUser user, string password);
/// <summary>
/// To permanently delete the user pass in true, otherwise they will just be disabled
/// </summary>

View File

@@ -609,23 +609,95 @@ namespace Umbraco.Core.Services
}
}
public IMember CreateMember(string username, string email, string memberTypeAlias)
/// <summary>
/// Creates a member object
/// </summary>
/// <param name="username"></param>
/// <param name="email"></param>
/// <param name="name"></param>
/// <param name="memberTypeAlias"></param>
/// <returns></returns>
public IMember CreateMember(string username, string email, string name, string memberTypeAlias)
{
var memberTypeService = ApplicationContext.Current.Services.MemberTypeService;
var memberType = memberTypeService.Get(memberTypeAlias);
var memberType = FindMemberTypeByAlias(memberTypeAlias);
return CreateMember(username, email, name, memberType);
}
var member = new Member(username, email.ToLower().Trim(), username, memberType);
/// <summary>
/// Creates a new member object
/// </summary>
/// <param name="username"></param>
/// <param name="email"></param>
/// <param name="name"></param>
/// <param name="memberType"></param>
/// <returns></returns>
public IMember CreateMember(string username, string email, string name, IMemberType memberType)
{
var member = new Member(name, email.ToLower().Trim(), username, memberType);
Created.RaiseEvent(new NewEventArgs<IMember>(member, false, memberTypeAlias, -1), this);
Created.RaiseEvent(new NewEventArgs<IMember>(member, false, memberType.Alias, -1), this);
return member;
}
/// <summary>
/// Creates a member with an Id
/// </summary>
/// <param name="username"></param>
/// <param name="email"></param>
/// <param name="name"></param>
/// <param name="memberTypeAlias"></param>
/// <returns></returns>
public IMember CreateMemberWithIdentity(string username, string email, string name, string memberTypeAlias)
{
var memberType = FindMemberTypeByAlias(memberTypeAlias);
return CreateMemberWithIdentity(username, email, name, memberType);
}
/// <summary>
/// Creates a member with an Id, the username will be used as their name
/// </summary>
/// <param name="username"></param>
/// <param name="email"></param>
/// <param name="memberType"></param>
/// <returns></returns>
public IMember CreateMemberWithIdentity(string username, string email, IMemberType memberType)
{
return CreateMemberWithIdentity(username, email, username, memberType);
}
/// <summary>
/// Creates a member with an Id
/// </summary>
/// <param name="username"></param>
/// <param name="email"></param>
/// <param name="name"></param>
/// <param name="memberType"></param>
/// <returns></returns>
public IMember CreateMemberWithIdentity(string username, string email, string name, IMemberType memberType)
{
return CreateMemberWithIdentity(username, email, name, "", memberType);
}
/// <summary>
/// Creates and persists a new Member
/// </summary>
/// <param name="email"></param>
/// <param name="username"></param>
/// <param name="rawPasswordValue"></param>
/// <param name="memberTypeAlias"></param>
/// <returns></returns>
IMember IMembershipMemberService<IMember>.CreateWithIdentity(string username, string email, string rawPasswordValue, string memberTypeAlias)
{
var memberType = FindMemberTypeByAlias(memberTypeAlias);
return CreateMemberWithIdentity(username, email, memberType);
}
private IMember CreateMemberWithIdentity(string username, string email, string name, string rawPasswordValue, IMemberType memberType)
{
if (memberType == null) throw new ArgumentNullException("memberType");
var member = new Member(username, email.ToLower().Trim(), username, memberType);
var member = new Member(name, email.ToLower().Trim(), username, rawPasswordValue, memberType);
if (Saving.IsRaisedEventCancelled(new SaveEventArgs<IMember>(member), this))
{
@@ -649,33 +721,6 @@ namespace Umbraco.Core.Services
return member;
}
/// <summary>
/// Creates and persists a new Member
/// </summary>
/// <param name="email"></param>
/// <param name="username"></param>
/// <param name="rawPasswordValue"></param>
/// <param name="memberTypeAlias"></param>
/// <returns></returns>
IMember IMembershipMemberService<IMember>.CreateWithIdentity(string username, string email, string rawPasswordValue, string memberTypeAlias)
{
var uow = _uowProvider.GetUnitOfWork();
IMemberType memberType;
using (var repository = _repositoryFactory.CreateMemberTypeRepository(uow))
{
var query = Query<IMemberType>.Builder.Where(x => x.Alias == memberTypeAlias);
memberType = repository.GetByQuery(query).FirstOrDefault();
}
if (memberType == null)
{
throw new ArgumentException(string.Format("No MemberType matching the passed in Alias: '{0}' was found", memberTypeAlias));
}
return CreateMemberWithIdentity(username, email, memberType);
}
/// <summary>
/// Gets a Member by its Id
@@ -937,8 +982,33 @@ namespace Umbraco.Core.Services
repository.DissociateRoles(memberIds, roleNames);
}
}
#endregion
private IMemberType FindMemberTypeByAlias(string memberTypeAlias)
{
using (var repository = _repositoryFactory.CreateMemberTypeRepository(_uowProvider.GetUnitOfWork()))
{
var query = Query<IMemberType>.Builder.Where(x => x.Alias == memberTypeAlias);
var types = repository.GetByQuery(query);
if (types.Any() == false)
throw new Exception(
string.Format("No MemberType matching the passed in Alias: '{0}' was found",
memberTypeAlias));
var contentType = types.First();
if (contentType == null)
throw new Exception(string.Format("MemberType matching the passed in Alias: '{0}' was null",
memberTypeAlias));
return contentType;
}
}
/// <summary>
/// Rebuilds all xml content in the cmsContentXml table for all media
/// </summary>

View File

@@ -9,6 +9,7 @@ using Umbraco.Core.Models.Rdbms;
using Umbraco.Core.Persistence;
using Umbraco.Core.Persistence.Querying;
using Umbraco.Core.Persistence.UnitOfWork;
using Umbraco.Core.Security;
namespace Umbraco.Core.Services
{
@@ -75,7 +76,23 @@ namespace Umbraco.Core.Services
}
}
public IUser CreateUserWithIdentity(string username, string email, string password, IUserType userType)
public IUser CreateUserWithIdentity(string username, string email, IUserType userType)
{
return CreateUserWithIdentity(username, email, "", userType);
}
IUser IMembershipMemberService<IUser>.CreateWithIdentity(string username, string email, string rawPasswordValue, string memberTypeAlias)
{
var userType = GetUserTypeByAlias(memberTypeAlias);
if (userType == null)
{
throw new ArgumentException("The user type " + memberTypeAlias + " could not be resolved");
}
return CreateUserWithIdentity(username, email, rawPasswordValue, userType);
}
private IUser CreateUserWithIdentity(string username, string email, string rawPasswordValue, IUserType userType)
{
if (userType == null) throw new ArgumentNullException("userType");
@@ -94,8 +111,7 @@ namespace Umbraco.Core.Services
Email = email,
Language = Configuration.GlobalSettings.DefaultUILanguage,
Name = username,
RawPasswordValue = password,
DefaultPermissions = userType.Permissions,
RawPasswordValue = rawPasswordValue,
Username = username,
StartContentId = -1,
StartMediaId = -1,
@@ -115,17 +131,6 @@ namespace Umbraco.Core.Services
}
}
IUser IMembershipMemberService<IUser>.CreateWithIdentity(string username, string email, string rawPasswordValue, string memberTypeAlias)
{
var userType = GetUserTypeByAlias(memberTypeAlias);
if (userType == null)
{
throw new ArgumentException("The user type " + memberTypeAlias + " could not be resolved");
}
return CreateUserWithIdentity(username, email, rawPasswordValue, userType);
}
public IUser GetById(int id)
{
using (var repository = _repositoryFactory.CreateUserRepository(_uowProvider.GetUnitOfWork()))
@@ -187,6 +192,37 @@ namespace Umbraco.Core.Services
uow.Database.Execute("delete from umbracoUserLogins where userID = @id", new {id = membershipUser.Id});
}
/// <summary>
/// This is simply a helper method which essentially just wraps the MembershipProvider's ChangePassword method
/// </summary>
/// <param name="user">The user to save the password for</param>
/// <param name="password"></param>
/// <remarks>
/// This method exists so that Umbraco developers can use one entry point to create/update users if they choose to.
/// </remarks>
public void SavePassword(IUser user, string password)
{
if (user == null) throw new ArgumentNullException("user");
var provider = MembershipProviderExtensions.GetUsersMembershipProvider();
if (provider.IsUmbracoMembershipProvider())
{
provider.ChangePassword(user.Username, "", password);
}
//go re-fetch the member and update the properties that may have changed
var result = GetByUsername(user.Username);
if (result != null)
{
//should never be null but it could have been deleted by another thread.
user.RawPasswordValue = result.RawPasswordValue;
user.LastPasswordChangeDate = result.LastPasswordChangeDate;
user.UpdateDate = user.UpdateDate;
}
throw new NotSupportedException("When using a non-Umbraco membership provider you must change the user password by using the MembershipProvider.ChangePassword method");
}
/// <summary>
/// To permanently delete the user pass in true, otherwise they will just be disabled
/// </summary>

View File

@@ -37,7 +37,7 @@ namespace Umbraco.Tests.Services
// Arrange
var userService = ServiceContext.UserService;
var userType = userService.GetUserTypeByAlias("admin");
var user = ServiceContext.UserService.CreateUserWithIdentity("test1", "test1@test.com", "123456", userType);
var user = ServiceContext.UserService.CreateUserWithIdentity("test1", "test1@test.com", userType);
var contentType = MockedContentTypes.CreateSimpleContentType();
ServiceContext.ContentTypeService.Save(contentType);
var content = new[]
@@ -64,7 +64,7 @@ namespace Umbraco.Tests.Services
// Arrange
var userService = ServiceContext.UserService;
var userType = userService.GetUserTypeByAlias("admin");
var user = ServiceContext.UserService.CreateUserWithIdentity("test1", "test1@test.com", "123456", userType);
var user = ServiceContext.UserService.CreateUserWithIdentity("test1", "test1@test.com", userType);
var contentType = MockedContentTypes.CreateSimpleContentType();
ServiceContext.ContentTypeService.Save(contentType);
var content = new[]
@@ -98,7 +98,7 @@ namespace Umbraco.Tests.Services
{
var userType = MockedUserType.CreateUserType();
ServiceContext.UserService.SaveUserType(userType);
var user = ServiceContext.UserService.CreateUserWithIdentity("JohnDoe", "john@umbraco.io", "12345", userType);
var user = ServiceContext.UserService.CreateUserWithIdentity("JohnDoe", "john@umbraco.io", userType);
ServiceContext.UserService.Delete(user, true);
var deleted = ServiceContext.UserService.GetUserById(user.Id);
@@ -112,7 +112,7 @@ namespace Umbraco.Tests.Services
{
var userType = MockedUserType.CreateUserType();
ServiceContext.UserService.SaveUserType(userType);
var user = ServiceContext.UserService.CreateUserWithIdentity("JohnDoe", "john@umbraco.io", "12345", userType);
var user = ServiceContext.UserService.CreateUserWithIdentity("JohnDoe", "john@umbraco.io", userType);
ServiceContext.UserService.Delete(user);
var deleted = ServiceContext.UserService.GetUserById(user.Id);
@@ -126,7 +126,7 @@ namespace Umbraco.Tests.Services
{
var userType = MockedUserType.CreateUserType();
ServiceContext.UserService.SaveUserType(userType);
var user = ServiceContext.UserService.CreateUserWithIdentity("JohnDoe", "john@umbraco.io", "12345", userType);
var user = ServiceContext.UserService.CreateUserWithIdentity("JohnDoe", "john@umbraco.io", userType);
Assert.IsTrue(ServiceContext.UserService.Exists("JohnDoe"));
Assert.IsFalse(ServiceContext.UserService.Exists("notFound"));
@@ -137,7 +137,7 @@ namespace Umbraco.Tests.Services
{
var userType = MockedUserType.CreateUserType();
ServiceContext.UserService.SaveUserType(userType);
var user = ServiceContext.UserService.CreateUserWithIdentity("JohnDoe", "john@umbraco.io", "12345", userType);
var user = ServiceContext.UserService.CreateUserWithIdentity("JohnDoe", "john@umbraco.io", userType);
Assert.IsNotNull(ServiceContext.UserService.GetByEmail(user.Email));
Assert.IsNull(ServiceContext.UserService.GetByEmail("do@not.find"));
@@ -148,7 +148,7 @@ namespace Umbraco.Tests.Services
{
var userType = MockedUserType.CreateUserType();
ServiceContext.UserService.SaveUserType(userType);
var user = ServiceContext.UserService.CreateUserWithIdentity("JohnDoe", "john@umbraco.io", "12345", userType);
var user = ServiceContext.UserService.CreateUserWithIdentity("JohnDoe", "john@umbraco.io", userType);
Assert.IsNotNull(ServiceContext.UserService.GetByUsername(user.Username));
Assert.IsNull(ServiceContext.UserService.GetByUsername("notFound"));
@@ -159,7 +159,7 @@ namespace Umbraco.Tests.Services
{
var userType = MockedUserType.CreateUserType();
ServiceContext.UserService.SaveUserType(userType);
var user = ServiceContext.UserService.CreateUserWithIdentity("mydomain\\JohnDoe", "john@umbraco.io", "12345", userType);
var user = ServiceContext.UserService.CreateUserWithIdentity("mydomain\\JohnDoe", "john@umbraco.io", userType);
Assert.IsNotNull(ServiceContext.UserService.GetByUsername(user.Username));
Assert.IsNull(ServiceContext.UserService.GetByUsername("notFound"));
@@ -170,7 +170,7 @@ namespace Umbraco.Tests.Services
{
var userType = MockedUserType.CreateUserType();
ServiceContext.UserService.SaveUserType(userType);
var user = ServiceContext.UserService.CreateUserWithIdentity("JohnDoe", "john@umbraco.io", "12345", userType);
var user = ServiceContext.UserService.CreateUserWithIdentity("JohnDoe", "john@umbraco.io", userType);
Assert.IsNotNull(ServiceContext.UserService.GetUserById(user.Id));
Assert.IsNull(ServiceContext.UserService.GetUserById(9876));
@@ -352,7 +352,7 @@ namespace Umbraco.Tests.Services
var userType = userService.GetUserTypeByAlias("admin");
// Act
var membershipUser = userService.CreateUserWithIdentity("JohnDoe", "john@umbraco.io", "12345", userType);
var membershipUser = userService.CreateUserWithIdentity("JohnDoe", "john@umbraco.io", userType);
// Assert
Assert.That(membershipUser.HasIdentity, Is.True);
@@ -375,7 +375,8 @@ namespace Umbraco.Tests.Services
var hash = new HMACSHA1();
hash.Key = Encoding.Unicode.GetBytes(password);
var encodedPassword = Convert.ToBase64String(hash.ComputeHash(Encoding.Unicode.GetBytes(password)));
var membershipUser = userService.CreateUserWithIdentity("JohnDoe", "john@umbraco.io", encodedPassword, userType);
var membershipUser = new User("JohnDoe", "john@umbraco.io", encodedPassword, encodedPassword, userType);
userService.Save(membershipUser);
// Assert
Assert.That(membershipUser.HasIdentity, Is.True);
@@ -391,8 +392,8 @@ namespace Umbraco.Tests.Services
{
var userType = ServiceContext.UserService.GetUserTypeByAlias("admin");
var user1 = ServiceContext.UserService.CreateUserWithIdentity("test1", "test1@test.com", "test1", userType);
var user2 = ServiceContext.UserService.CreateUserWithIdentity("test2", "test2@test.com", "test2", userType);
var user1 = ServiceContext.UserService.CreateUserWithIdentity("test1", "test1@test.com", userType);
var user2 = ServiceContext.UserService.CreateUserWithIdentity("test2", "test2@test.com", userType);
//adds some allowed sections
user1.AddAllowedSection("test");
@@ -416,7 +417,7 @@ namespace Umbraco.Tests.Services
{
// Arrange
var userType = ServiceContext.UserService.GetUserTypeByAlias("admin");
var user = ServiceContext.UserService.CreateUserWithIdentity("test1", "test1@test.com", "test1", userType);
var user = ServiceContext.UserService.CreateUserWithIdentity("test1", "test1@test.com", userType);
// Act
@@ -433,7 +434,7 @@ namespace Umbraco.Tests.Services
{
// Arrange
var userType = ServiceContext.UserService.GetUserTypeByAlias("admin");
var user = (IUser)ServiceContext.UserService.CreateUserWithIdentity("test1", "test1@test.com", "test1", userType);
var user = (IUser)ServiceContext.UserService.CreateUserWithIdentity("test1", "test1@test.com", userType);
// Act
@@ -450,7 +451,7 @@ namespace Umbraco.Tests.Services
{
// Arrange
var userType = ServiceContext.UserService.GetUserTypeByAlias("admin");
var originalUser = (User)ServiceContext.UserService.CreateUserWithIdentity("test1", "test1@test.com", "test1", userType);
var originalUser = (User)ServiceContext.UserService.CreateUserWithIdentity("test1", "test1@test.com", userType);
// Act