Adds more methods to the MembershipHelper, updates the RegisterModel and obsoletes the standard ctor so biz logic is not happening in the model, updates the snippets to use the correct way. Fixes an issue in the membership repo for persisting new members when the member type doesn't contain all the built-in props.

This commit is contained in:
Shannon
2014-01-28 18:59:27 +11:00
parent 3b639e61b2
commit 6fefcacc61
12 changed files with 105 additions and 49 deletions

View File

@@ -237,7 +237,12 @@ namespace Umbraco.Core.Persistence.Repositories
//Create the PropertyData for this version - cmsPropertyData
var propertyFactory = new PropertyFactory(entity.ContentType, entity.Version, entity.Id);
var propertyDataDtos = propertyFactory.BuildDto(((Member)entity).Properties);
//Add Properties
// - don't try to save the property if it doesn't exist (or doesn't have an ID) on the content type
// - this can occur if the member type doesn't contain the built-in properties that the
// - member object contains.
var propsToPersist = entity.Properties.Where(x => x.PropertyType.HasIdentity).ToArray();
var propertyDataDtos = propertyFactory.BuildDto(propsToPersist);
var keyDictionary = new Dictionary<int, int>();
//Add Properties
@@ -248,7 +253,7 @@ namespace Umbraco.Core.Persistence.Repositories
}
//Update Properties with its newly set Id
foreach (var property in ((Member)entity).Properties)
foreach (var property in propsToPersist)
{
property.Id = keyDictionary[property.PropertyTypeId];
}
@@ -332,11 +337,9 @@ namespace Umbraco.Core.Persistence.Repositories
// - don't try to save the property if it doesn't exist (or doesn't have an ID) on the content type
// - this can occur if the member type doesn't contain the built-in properties that the
// - member object contains.
var existingProperties = entity.Properties
.Where(property => entity.ContentType.PropertyTypes.Any(x => x.Alias == property.Alias && x.HasIdentity))
.ToList();
var propsToPersist = entity.Properties.Where(x => x.PropertyType.HasIdentity).ToArray();
var propertyDataDtos = propertyFactory.BuildDto(existingProperties);
var propertyDataDtos = propertyFactory.BuildDto(propsToPersist);
foreach (var propertyDataDto in propertyDataDtos)
{

View File

@@ -16,6 +16,7 @@
Html.RequiresJs("/umbraco_client/Application/JQuery/jquery.validate.unobtrusive.min.js");
}
@*NOTE: This RenderJsHere code should be put on your main template page where the rest of your script tags are placed*@
@Html.RenderJsHere()
@using (Html.BeginUmbracoForm<UmbLoginController>("HandleLogin"))

View File

@@ -5,7 +5,7 @@
@using Umbraco.Web.Controllers
@{
var loginStatusModel = new LoginStatusModel();
var loginStatusModel = Members.GetCurrentLoginStatus();
Html.EnableClientValidation();
Html.EnableUnobtrusiveJavaScript();
@@ -14,6 +14,7 @@
Html.RequiresJs("/umbraco_client/Application/JQuery/jquery.validate.unobtrusive.min.js");
}
@*NOTE: This RenderJsHere code should be put on your main template page where the rest of your script tags are placed*@
@Html.RenderJsHere()
@if (loginStatusModel.IsLoggedIn)

View File

@@ -7,7 +7,7 @@
@using Umbraco.Web.Controllers
@{
var registerModel = new RegisterModel();
var registerModel = Members.CreateRegistrationModel();
@*
Configurable here:
@@ -38,6 +38,9 @@
var success = TempData["FormSuccess"] != null;
}
@*NOTE: This RenderJsHere code should be put on your main template page where the rest of your script tags are placed*@
@Html.RenderJsHere()
@if (success)
{
// This message will show if RedirectOnSucces is set to false (default)
@@ -68,11 +71,11 @@ else
<br />
@if (registerModel.MemberProperties != null) {
for (var i = 0; i < registerModel.MemberProperties.Count; i++)
foreach (var prop in registerModel.MemberProperties)
{
@Html.LabelFor(m => registerModel.MemberProperties[i].Value, registerModel.MemberProperties[i].Name)
@Html.EditorFor(m => registerModel.MemberProperties[i].Value)
@Html.HiddenFor(m => registerModel.MemberProperties[i].Alias)
@Html.LabelFor(m => prop.Value, prop.Name)
@Html.EditorFor(m => prop.Value)
@Html.HiddenFor(m => prop.Alias)
<br />
}
}
@@ -84,7 +87,5 @@ else
<button>Register</button>
</fieldset>
@Html.RenderJsHere()
}
}

View File

@@ -16,6 +16,7 @@
Html.RequiresJs("/umbraco_client/Application/JQuery/jquery.validate.unobtrusive.min.js");
}
@*NOTE: This RenderJsHere code should be put on your main template page where the rest of your script tags are placed*@
@Html.RenderJsHere()
@using (Html.BeginUmbracoForm<UmbLoginController>("HandleLogin"))

View File

@@ -6,7 +6,7 @@
@using Umbraco.Web.Controllers
@{
var loginStatusModel = new LoginStatusModel();
var loginStatusModel = Members.GetCurrentLoginStatus();
Html.EnableClientValidation();
Html.EnableUnobtrusiveJavaScript();
@@ -15,6 +15,7 @@
Html.RequiresJs("/umbraco_client/Application/JQuery/jquery.validate.unobtrusive.min.js");
}
@*NOTE: This RenderJsHere code should be put on your main template page where the rest of your script tags are placed*@
@Html.RenderJsHere()
@if (loginStatusModel.IsLoggedIn)

View File

@@ -7,7 +7,7 @@
@using Umbraco.Web.Controllers
@{
var registerModel = new RegisterModel();
var registerModel = Members.CreateRegistrationModel();
@*
Configurable here:
@@ -38,6 +38,9 @@
var success = TempData["FormSuccess"] != null;
}
@*NOTE: This RenderJsHere code should be put on your main template page where the rest of your script tags are placed*@
@Html.RenderJsHere()
@if (success)
{
// This message will show if RedirectOnSucces is set to false (default)
@@ -68,11 +71,11 @@ else
<br />
@if (registerModel.MemberProperties != null) {
for (var i = 0; i < registerModel.MemberProperties.Count; i++)
foreach (var prop in registerModel.MemberProperties)
{
@Html.LabelFor(m => registerModel.MemberProperties[i].Value, registerModel.MemberProperties[i].Name)
@Html.EditorFor(m => registerModel.MemberProperties[i].Value)
@Html.HiddenFor(m => registerModel.MemberProperties[i].Alias)
@Html.LabelFor(m => prop.Value, prop.Name)
@Html.EditorFor(m => prop.Value)
@Html.HiddenFor(m => prop.Alias)
<br />
}
}
@@ -84,7 +87,5 @@ else
<button>Register</button>
</fieldset>
@Html.RenderJsHere()
}
}

View File

@@ -24,7 +24,7 @@ namespace Umbraco.Web.Controllers
MembershipCreateStatus status;
var member = Membership.CreateUser(model.Username, model.Password, model.Email,
//TODO: Support q/a http://issues.umbraco.org/issue/U4-3213
"", "",
null, null,
true, out status);
switch (status)

View File

@@ -39,7 +39,7 @@ namespace Umbraco.Web.Models
/// <summary>
/// This will construct a new LoginStatusModel and perform a lookup for hte curently logged in member
/// </summary>
[Obsolete("Do not use this ctor as it will perform business logic lookups. Use the MembershipHelper.GetLoginStatusModel or the static LoginStatusModel.CreateModel() to create an empty model.")]
[Obsolete("Do not use this ctor as it will perform business logic lookups. Use the MembershipHelper.GetCurrentLoginStatus or the static LoginStatusModel.CreateModel() to create an empty model.")]
public LoginStatusModel()
: this(true)
{

View File

@@ -1,46 +1,55 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using umbraco.cms.businesslogic.member;
using Umbraco.Core;
using Umbraco.Web.Security;
namespace Umbraco.Web.Models
{
public class RegisterModel
{
public RegisterModel()
/// <summary>
/// Creates a new empty RegisterModel
/// </summary>
/// <returns></returns>
public static RegisterModel CreateModel()
{
this.MemberTypeAlias = Constants.Conventions.MemberTypes.Member;
var model = new RegisterModel(false);
return model;
}
this.RedirectOnSucces = false;
private RegisterModel(bool doLookup)
{
MemberTypeAlias = Constants.Conventions.MemberTypes.Member;
RedirectOnSucces = false;
RedirectUrl = "/";
UsernameIsEmail = true;
this.RedirectUrl = "/";
this.UsernameIsEmail = true;
var memberType = MemberType.GetByAlias(this.MemberTypeAlias);
if (memberType != null)
if (doLookup && HttpContext.Current != null && ApplicationContext.Current != null)
{
this.MemberProperties = new List<UmbracoProperty>();
foreach (var prop in memberType.PropertyTypes.Where(memberType.MemberCanEdit))
{
this.MemberProperties.Add(new UmbracoProperty
{
Alias = prop.Alias,
Name = prop.Name,
Value = string.Empty
});
}
var helper = new MembershipHelper(ApplicationContext.Current, new HttpContextWrapper(HttpContext.Current));
var model = helper.CreateRegistrationModel(MemberTypeAlias);
MemberProperties = model.MemberProperties;
}
}
[Obsolete("Do not use this ctor as it will perform business logic lookups. Use the MembershipHelper.CreateRegistrationModel or the static RegisterModel.CreateModel() to create an empty model.")]
public RegisterModel()
: this(true)
{
}
[Required]
[RegularExpression(@"[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?",
ErrorMessage = "Please enter a valid e-mail address")]
public string Email { get; set; }
/// <summary>
/// Returns the member properties
/// </summary>
public List<UmbracoProperty> MemberProperties { get; set; }
public string MemberTypeAlias { get; set; }

View File

@@ -1,5 +1,8 @@
namespace Umbraco.Web.Models
{
/// <summary>
/// A simple representation of an Umbraco property
/// </summary>
public class UmbracoProperty
{
public string Alias { get; set; }

View File

@@ -37,6 +37,41 @@ namespace Umbraco.Web.Security
}
#endregion
/// <summary>
/// Creates a model to use for registering new members with custom member properties
/// </summary>
/// <param name="memberTypeAlias"></param>
/// <returns></returns>
public RegisterModel CreateRegistrationModel(string memberTypeAlias = null)
{
if (Membership.Provider.IsUmbracoMembershipProvider())
{
memberTypeAlias = memberTypeAlias ?? Constants.Conventions.MemberTypes.Member;
var memberType = _applicationContext.Services.MemberTypeService.Get(memberTypeAlias);
if (memberType == null)
throw new InvalidOperationException("Could not find a member type with alias " + memberTypeAlias);
var props = memberType.PropertyTypes
.Where(x => memberType.MemberCanEditProperty(x.Alias))
.Select(prop => new UmbracoProperty
{
Alias = prop.Alias,
Name = prop.Name,
Value = string.Empty
}).ToList();
var model = RegisterModel.CreateModel();
model.MemberProperties = props;
return model;
}
else
{
var model = RegisterModel.CreateModel();
model.MemberTypeAlias = string.Empty;
return model;
}
}
/// <summary>
/// Returns the login status model of the currently logged in member, if no member is logged in it returns null;
/// </summary>