publicizes user stuff, changes the IUser.Id to an int - no need for it to be an object and casted everywhere. Updates the login logic to perform the mapping logic for custom membership providers with an extension method.

This commit is contained in:
Shannon
2014-01-22 14:07:18 +11:00
parent fb9569d914
commit 9bd8d729fa
8 changed files with 109 additions and 50 deletions

View File

@@ -0,0 +1,52 @@
using System;
using System.Web.Security;
using Umbraco.Core.Models.Membership;
namespace Umbraco.Core.Services
{
internal static class UserServiceExtensions
{
/// <summary>
/// Maps a custom provider's information to an umbraco user account
/// </summary>
/// <param name="userService"></param>
/// <param name="member"></param>
/// <remarks>
/// To maintain compatibility we have to check the login name if the provider key lookup fails but otherwise
/// we'll store the provider user key in the login column.
/// </remarks>
public static IUser CreateUserMappingForCustomProvider(this IUserService userService, MembershipUser member)
{
if (member == null) throw new ArgumentNullException("member");
var valToLookup = member.ProviderUserKey == null ? member.UserName : member.ProviderUserKey.ToString();
var found = userService.GetByUsername(valToLookup);
if (found == null && member.ProviderUserKey != null)
{
//try by username
found = userService.GetByUsername(member.UserName);
}
if (found == null)
{
var writer = userService.GetUserTypeByAlias("writer");
if (writer == null)
{
throw new InvalidOperationException("Could not map the custom user to an Umbraco user, no 'writer' user type could be found");
}
var user = new User(
member.UserName,
member.Email ?? Guid.NewGuid().ToString("N") + "@example.com", //email cannot be empty
member.ProviderUserKey == null ? member.UserName : member.ProviderUserKey.ToString(),
Guid.NewGuid().ToString("N"), //pass cannot be empty
writer);
user.AddAllowedSection(Constants.Applications.Content);
userService.Save(user);
return user;
}
return found;
}
}
}