2014-01-28 13:35:43 +11:00
using System ;
using System.Linq ;
2013-08-28 17:52:06 +02:00
using System.Web.Mvc ;
2014-01-28 13:35:43 +11:00
using System.Web.Security ;
2013-08-28 17:52:06 +02:00
using System.Xml ;
using umbraco.cms.businesslogic.member ;
using Umbraco.Web.Models ;
using Umbraco.Web.Mvc ;
2014-01-28 13:35:43 +11:00
using Umbraco.Core.Security ;
2013-08-28 17:52:06 +02:00
namespace Umbraco.Web.Controllers
{
public class ProfileController : SurfaceController
{
[HttpPost]
2014-01-28 13:35:43 +11:00
public ActionResult HandleUpdateProfile ( [ Bind ( Prefix = "profileModel" ) ] ProfileModel model )
2013-08-28 17:52:06 +02:00
{
2014-01-28 13:35:43 +11:00
if ( Membership . Provider . IsUmbracoMembershipProvider ( ) = = false )
2013-08-28 17:52:06 +02:00
{
2014-01-28 13:35:43 +11:00
throw new NotSupportedException ( "Profile editing with the " + typeof ( ProfileController ) + " is not supported when not using the default Umbraco membership provider" ) ;
}
2013-08-28 17:52:06 +02:00
2014-01-28 13:35:43 +11:00
if ( ModelState . IsValid = = false )
{
return CurrentUmbracoPage ( ) ;
}
2013-08-28 17:52:06 +02:00
2014-01-28 13:35:43 +11:00
if ( HttpContext . User = = null | | HttpContext . User . Identity . IsAuthenticated = = false )
{
throw new NotSupportedException ( "No member is currently logged in" ) ;
}
2013-08-28 17:52:06 +02:00
2014-01-28 13:35:43 +11:00
var member = Services . MemberService . GetByUsername ( HttpContext . User . Identity . Name ) ;
if ( member = = null )
{
//this should never happen
throw new InvalidOperationException ( "No member found with username: " + HttpContext . User . Identity . Name ) ;
}
2013-08-28 17:52:06 +02:00
2014-01-28 13:35:43 +11:00
if ( model . Name ! = null )
{
member . Name = model . Name ;
}
member . Email = model . Email ;
member . Username = model . Email ;
if ( model . MemberProperties ! = null )
{
//TODO: Shouldn't we ensure that none of these properties are flagged as non-editable to the member??
foreach ( var property in model . MemberProperties . Where ( p = > p . Value ! = null ) )
{
if ( member . Properties . Contains ( property . Alias ) )
{
member . Properties [ property . Alias ] . Value = property . Value ;
}
2013-08-28 17:52:06 +02:00
}
}
2014-01-28 13:35:43 +11:00
Services . MemberService . Save ( member ) ;
//reset the FormsAuth cookie since the username might have changed
FormsAuthentication . SetAuthCookie ( member . Username , true ) ;
//TODO: Why are we redirecting to home again here??
return Redirect ( "/" ) ;
2013-08-28 17:52:06 +02:00
}
}
}