Skip to content

Instantly share code, notes, and snippets.

@javafun
Created November 25, 2019 09:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save javafun/84ef6c4d7e027a78f2549646cca1d35b to your computer and use it in GitHub Desktop.
Save javafun/84ef6c4d7e027a78f2549646cca1d35b to your computer and use it in GitHub Desktop.
Commerce Manager Membership Account Edit
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="~/Overrides/MembershipAccountEdit.ascx.cs" Inherits="Web.Commerce.Overrides.MembershipAccountEdit" %>
<%@ Register Src="~/Apps/Core/Controls/BooleanEditControl.ascx" TagName="BooleanEditControl"
TagPrefix="ecf" %>
<%@ Register TagPrefix="mc2" Assembly="Mediachase.BusinessFoundation" Namespace="Mediachase.BusinessFoundation" %>
<div style="padding: 5px;">
<asp:Label runat="server" ID="lblErrorInfo" Style="color: Red"></asp:Label>
<table width="100%" class="DataForm">
<tr>
<td class="FormLabelCell">
<asp:Label runat="server" ID="UserNameLabel" Text="<%$ Resources:SharedStrings, User_Name %>"></asp:Label>:
</td>
<td class="FormFieldCell">
<asp:TextBox runat="server" ID="UserNameTextBox" Width="250" MaxLength="256"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" ID="UserNameRequired" ControlToValidate="UserNameTextBox"
ErrorMessage="<%$ Resources:SharedStrings, User_Name_Required %>" >*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td colspan="2" class="FormSpacerCell">
</td>
</tr>
<tr runat="server" id="TrPassword">
<td class="FormLabelCell">
<asp:Label runat="server" ID="LabelPassword" Text="<%$ Resources:SharedStrings, Password %>"></asp:Label>:
</td>
<td class="FormFieldCell">
<asp:TextBox runat="server" ID="TbPassword" TextMode="Password" Width="250"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" ID="PasswordRequired" ControlToValidate="TbPassword"
ErrorMessage="<%$ Resources:SharedStrings, RequiredField %>">*</asp:RequiredFieldValidator>
<asp:CustomValidator ID="PasswordCustomValidator" runat="server" ControlToValidate="TbPassword"
OnServerValidate="UserPassword_ServerValidate" dispaly="dynamic" />
</td>
</tr>
<tr>
<td class="FormLabelCell">
<asp:Label ID="Label14" runat="server" Text="<%$ Resources:SharedStrings, Description %>"></asp:Label>:
</td>
<td class="FormFieldCell">
<asp:TextBox runat="server" Width="250" ID="tbDescription"></asp:TextBox><br />
</td>
</tr>
<tr>
<td colspan="2" class="FormSpacerCell">
</td>
</tr>
<tr>
<td class="FormLabelCell">
<asp:Label runat="server" ID="Label5" Text="<%$ Resources:SharedStrings, Email %>"></asp:Label>:
</td>
<td class="FormFieldCell">
<asp:TextBox runat="server" ID="tbEmailText" Width="250" MaxLength="256"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator1" ControlToValidate="tbEmailText"
ErrorMessage="<%$ Resources:SharedStrings, RequiredField %>">*</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator id="RegularExpressionValidator1"
runat="server" ControlToValidate="tbEmailText"
display="Dynamic"
ErrorMessage="<%$ Resources:SharedStrings, Valid_Email_Required%>"
ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">
</asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td colspan="2" class="FormSpacerCell">
</td>
</tr>
<tr>
<td class="FormLabelCell">
<asp:Label runat="server" ID="Label6" Text="<%$ Resources:SharedStrings, Approved %>"></asp:Label>:
</td>
<td class="FormFieldCell">
<ecf:BooleanEditControl ID="IsApproved" runat="server"></ecf:BooleanEditControl>
</td>
</tr>
<tr>
<td colspan="2" class="FormSpacerCell">
</td>
</tr>
<tr>
<td align="right" colspan="2" style="padding-top: 10px; padding-right: 10px;">
<mc2:IMButton ID="btnSave" runat="server" style="width: 105px;" OnServerClick="btnSave_ServerClick">
</mc2:IMButton>
&nbsp;
<mc2:IMButton ID="btnCancel" runat="server" style="width: 105px;" CausesValidation="false">
</mc2:IMButton>
</td>
</tr>
</table>
</div>
using EPiServer.Cms.UI.AspNetIdentity;
using EPiServer.ServiceLocation;
using Mediachase.BusinessFoundation;
using Mediachase.BusinessFoundation.Data;
using Mediachase.Commerce.Customers;
using Mediachase.Commerce.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Web.Commerce.Overrides
{
public partial class MembershipAccountEdit : MCDataBoundControl
{
public ApplicationUserManager<ApplicationUser> UserManager => ServiceLocator.Current.GetInstance<ApplicationUserManager<ApplicationUser>>();
public ApplicationSignInManager<ApplicationUser> SignInManager => ServiceLocator.Current.GetInstance<ApplicationSignInManager<ApplicationUser>>();
public PrimaryKeyId ContactId
{
get
{
PrimaryKeyId empty = PrimaryKeyId.Empty;
if (this.Request.QueryString[nameof(ContactId)] != null)
empty = PrimaryKeyId.Parse(this.Request.QueryString[nameof(ContactId)]);
if (empty == PrimaryKeyId.Empty)
{
var user = UserManager.FindByNameAsync(UserName).Result;
if (user != null)
{
CustomerContact contactForUser = CustomerContext.Current.GetContactByUserId(UserName);
if (contactForUser != null)
empty = contactForUser.PrimaryKeyId.Value;
}
}
return empty;
}
}
protected string UserName => Request.QueryString[nameof(UserName)];
public ApplicationUser GetCurrentApplicationUser()
{
ApplicationUser applicationUser = (ApplicationUser)null;
if (!string.IsNullOrEmpty(UserName))
applicationUser = UserManager.FindByNameAsync(UserName).Result;
else if (ContactId != PrimaryKeyId.Empty)
applicationUser = UserManager.FindByIdAsync(ContactId.ToString()).Result;
return applicationUser;
}
protected string CommandName
{
get
{
string empty = string.Empty;
if (Request.QueryString["commandName"] != null)
empty = Request.QueryString["commandName"];
return empty;
}
}
protected bool IsEditMode => CustomerContext.Current.GetUserForContactId(ContactId) != null;
private void BindData()
{
ApplicationUser currentMembershipUser = GetCurrentApplicationUser();
if (currentMembershipUser != null)
{
this.UserNameTextBox.Text = currentMembershipUser.UserName;
this.UserNameTextBox.Enabled = false;
this.tbDescription.Text = currentMembershipUser.Comment;
this.tbEmailText.Text = currentMembershipUser.Email;
this.IsApproved.IsSelected = currentMembershipUser.IsApproved;
}
else
{
CustomerContact contactById = CustomerContext.Current.GetContactById((Guid)ContactId);
if (contactById == null)
return;
tbEmailText.Text = contactById.GetHtmlEncodedEmail();
}
}
private void BindButtons()
{
btnSave.Text = this.GetGlobalResourceObject("Common", "btnOK").ToString();
btnSave.CustomImage = this.Page.ResolveUrl("~/Apps/MetaDataBase/images/ok-button.gif");
btnCancel.Text = this.GetGlobalResourceObject("Common", "btnCancel").ToString();
btnCancel.CustomImage = this.Page.ResolveUrl("~/Apps/MetaDataBase/images/cancel-button.gif");
btnCancel.Attributes.Add("onclick", CommandHandler.GetCloseOpenedFrameScript(this.Page, string.Empty, false, true));
}
protected void Page_Load(object sender, EventArgs e)
{
lblErrorInfo.Text = string.Empty;
if (!IsPostBack)
{
TrPassword.Visible = !IsEditMode;
BindData();
}
BindButtons();
}
public void UserName_ServerValidator(object sender, ServerValidateEventArgs args)
{
ApplicationUser user = UserManager.FindByNameAsync(UserNameTextBox.Text).Result;
if (user == null || user != null && !this.UserNameTextBox.Enabled)
args.IsValid = true;
else
args.IsValid = false;
}
public void UserPassword_ServerValidate(object source, ServerValidateEventArgs args)
{
//args.IsValid = false;
//if (SecurityContext.ValidateMembershipUserPassword(this.TbPassword.Text) != UserPasswordValidationResult.PasswordOk)
//{
// if (Membership.MinRequiredNonAlphanumericCharacters > 0)
// this.PasswordCustomValidator.ErrorMessage = string.Format(SharedStrings.Pasword_NonAlphabetChars_Num_ValidationError, (object)Membership.MinRequiredPasswordLength, (object)Membership.MinRequiredNonAlphanumericCharacters);
// else
// this.PasswordCustomValidator.ErrorMessage = string.Format(SharedStrings.Password_Min_Length_ValidationError, (object)Membership.MinRequiredPasswordLength);
//}
//else
args.IsValid = true;
}
protected void btnSave_ServerClick(object sender, EventArgs e)
{
Page.Validate();
if (!Page.IsValid)
return;
ApplicationUser user = this.GetCurrentApplicationUser();
if (user == null)
{
CustomerContact contact = CustomerContext.Current.GetContactByUserId(UserNameTextBox.Text);
user = new ApplicationUser()
{
UserName = UserNameTextBox.Text,
Email = tbEmailText.Text,
};
var result = UserManager.CreateAsync(user, TbPassword.Text).Result;
if (result.Succeeded)
{
CustomerContact contactById = CustomerContext.Current.GetContactById((Guid)this.ContactId);
contactById.UserId = new MapUserKey().ToTypedString((object)user.UserName);
contactById.SaveChanges();
//TODO: Assign default role
//TODO: Send Email
}
else
{
lblErrorInfo.Text = "Create user operation error : " + string.Join(". ", result.Errors.ToArray());
return;
}
}
user.Comment = this.tbDescription.Text;
if (!user.Email.Equals(this.tbEmailText.Text, StringComparison.OrdinalIgnoreCase))
user.Email = this.tbEmailText.Text;
user.IsApproved = this.IsApproved.IsSelected;
try
{
var updateResult = UserManager.UpdateAsync(user).Result;
}
catch (Exception ex)
{
this.lblErrorInfo.Text = ex.Message;
return;
}
string empty = string.Empty;
if (!string.IsNullOrEmpty(this.CommandName))
empty = new CommandParameters(this.CommandName).ToString();
CommandHandler.RegisterCloseOpenedFrameScript(this.Page, empty, true);
}
}
}
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace Web.Commerce.Overrides
{
public partial class MembershipAccountEdit
{
/// <summary>
/// lblErrorInfo control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.Label lblErrorInfo;
/// <summary>
/// UserNameLabel control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.Label UserNameLabel;
/// <summary>
/// UserNameTextBox control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.TextBox UserNameTextBox;
/// <summary>
/// UserNameRequired control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.RequiredFieldValidator UserNameRequired;
/// <summary>
/// TrPassword control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.HtmlControls.HtmlTableRow TrPassword;
/// <summary>
/// LabelPassword control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.Label LabelPassword;
/// <summary>
/// TbPassword control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.TextBox TbPassword;
/// <summary>
/// PasswordRequired control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.RequiredFieldValidator PasswordRequired;
/// <summary>
/// PasswordCustomValidator control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.CustomValidator PasswordCustomValidator;
/// <summary>
/// Label14 control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.Label Label14;
/// <summary>
/// tbDescription control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.TextBox tbDescription;
/// <summary>
/// Label5 control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.Label Label5;
/// <summary>
/// tbEmailText control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.TextBox tbEmailText;
/// <summary>
/// RequiredFieldValidator1 control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
/// <summary>
/// RegularExpressionValidator1 control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.RegularExpressionValidator RegularExpressionValidator1;
/// <summary>
/// Label6 control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.Label Label6;
/// <summary>
/// IsApproved control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::Mediachase.Commerce.Manager.Core.Controls.BooleanEditControl IsApproved;
/// <summary>
/// btnSave control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::Mediachase.BusinessFoundation.IMButton btnSave;
/// <summary>
/// btnCancel control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::Mediachase.BusinessFoundation.IMButton btnCancel;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment