sorry guys, got really busy recently as I also have a full time job..
so here starts Part III - code。
The first change is made in the database, (i am using nopcommerce 1.9): its pretty simple
ALTER TABLE [dbo].[Nop_Customer] ADD FacebookID NVARCHAR(255) NULL
then you will need all the supporting changes in the edmx and code:
(My source depot got some problem and now i lost the change track and only have the most up to date code, so the code listed may be not the exact changes)
in nopCommerce_1.90_Source\Libraries\Nop.BusinessLogic\Data\NopModel.edmx
under entity <EntityType Name="Nop_Customer">
add
<Property Name="FacebookID" Type="nvarchar" MaxLength="255" />
so it looks like this:
<code>
<EntityType Name="Nop_Customer">
<Key>
<PropertyRef Name="CustomerID" />
</Key>
<Property Name="CustomerID" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
<Property Name="CustomerGUID" Type="uniqueidentifier" Nullable="false" />
<Property Name="Email" Type="nvarchar" Nullable="false" MaxLength="255" />
<Property Name="Username" Type="nvarchar" Nullable="false" MaxLength="100" />
<Property Name="PasswordHash" Type="nvarchar" Nullable="false" MaxLength="255" />
<Property Name="SaltKey" Type="nvarchar" Nullable="false" MaxLength="255" />
<Property Name="AffiliateID" Type="int" Nullable="false" />
<Property Name="BillingAddressID" Type="int" Nullable="false" />
<Property Name="ShippingAddressID" Type="int" Nullable="false" />
<Property Name="LastPaymentMethodID" Type="int" Nullable="false" />
<Property Name="LastAppliedCouponCode" Type="nvarchar" Nullable="false" MaxLength="100" />
<Property Name="GiftCardCouponCodes" Type="xml" Nullable="false" />
<Property Name="CheckoutAttributes" Type="xml" Nullable="false" />
<Property Name="LanguageID" Type="int" Nullable="false" />
<Property Name="CurrencyID" Type="int" Nullable="false" />
<Property Name="TaxDisplayTypeID" Type="int" Nullable="false" />
<Property Name="IsTaxExempt" Type="bit" Nullable="false" />
<Property Name="IsAdmin" Type="bit" Nullable="false" />
<Property Name="IsGuest" Type="bit" Nullable="false" />
<Property Name="IsForumModerator" Type="bit" Nullable="false" />
<Property Name="TotalForumPosts" Type="int" Nullable="false" />
<Property Name="Signature" Type="nvarchar" Nullable="false" MaxLength="300" />
<Property Name="AdminComment" Type="nvarchar" Nullable="false" />
<Property Name="Active" Type="bit" Nullable="false" />
<Property Name="Deleted" Type="bit" Nullable="false" />
<Property Name="RegistrationDate" Type="datetime" Nullable="false" />
<Property Name="TimeZoneID" Type="nvarchar" Nullable="false" MaxLength="200" />
<Property Name="AvatarID" Type="int" Nullable="false" />
<Property Name="DateOfBirth" Type="datetime" />
<Property Name="FacebookID" Type="nvarchar" MaxLength="255" /> </EntityType>
</code>
under <EntityType Name="Customer">, add
<Property Type="String" Name="FacebookId" Nullable="true" />
under <EntitySetMapping Name="Customers"> add
<EntitySetMapping Name="Customers">
<EntityTypeMapping TypeName="NopSolutions.NopCommerce.BusinessLogic.Data.Customer">
<MappingFragment StoreEntitySet="Nop_Customer">
<ScalarProperty Name="FacebookId" ColumnName="FacebookID" />These are the required changes in .edmx file. pretty simple.
Then need to add some supporting method/property:
in nopCommerce_1.90_Source\Libraries\Nop.BusinessLogic\Customer\Customer.cs, add
/// <summary>
/// Gets or sets facebook id
/// </summary>
public string FacebookId { get; set; }
in nopCommerce_1.90_Source\Libraries\Nop.BusinessLogic\Customer\CustomerService.cs,
/// <summary>
/// Gets a customer by facebookId
/// </summary>
/// <param name="facebookId">Customer facebook Id</param>
/// <returns>A customer</returns>
public Customer GetCustomerByFacebookId(string facebookId)
{
if (string.IsNullOrEmpty(facebookId))
return null;
var query = from c in _context.Customers
orderby c.CustomerId
where c.FacebookId == facebookId
select c;
var customer = query.FirstOrDefault();
return customer;
}
/// <summary>
/// Adds a customer with facebook Id
/// </summary>
/// <param name="customerGuid">The customer identifier</param>
/// <param name="email">The email</param>
/// <param name="facebookId">The facebookId</param>
/// <param name="username">The username</param>
/// <param name="password">The password</param>
/// <param name="affiliateId">The affiliate identifier</param>
/// <param name="billingAddressId">The billing address identifier</param>
/// <param name="shippingAddressId">The shipping address identifier</param>
/// <param name="lastPaymentMethodId">The last payment method identifier</param>
/// <param name="lastAppliedCouponCode">The last applied coupon code</param>
/// <param name="giftCardCouponCodes">The applied gift card coupon code</param>
/// <param name="checkoutAttributes">The selected checkout attributes</param>
/// <param name="languageId">The language identifier</param>
/// <param name="currencyId">The currency identifier</param>
/// <param name="taxDisplayType">The tax display type</param>
/// <param name="isTaxExempt">A value indicating whether the customer is tax exempt</param>
/// <param name="isAdmin">A value indicating whether the customer is administrator</param>
/// <param name="isGuest">A value indicating whether the customer is guest</param>
/// <param name="isForumModerator">A value indicating whether the customer is forum moderator</param>
/// <param name="totalForumPosts">A forum post count</param>
/// <param name="signature">Signature</param>
/// <param name="adminComment">Admin comment</param>
/// <param name="active">A value indicating whether the customer is active</param>
/// <param name="deleted">A value indicating whether the customer has been deleted</param>
/// <param name="registrationDate">The date and time of customer registration</param>
/// <param name="timeZoneId">The time zone identifier</param>
/// <param name="avatarId">The avatar identifier</param>
/// <param name="dateOfBirth">Date of birth</param>
/// <param name="status">Status</param>
/// <returns>A customer</returns>
public Customer AddCustomer(Guid customerGuid, string email, string facebookId, string username,
string password, int affiliateId, int billingAddressId,
int shippingAddressId, int lastPaymentMethodId,
string lastAppliedCouponCode, string giftCardCouponCodes,
string checkoutAttributes, int languageId, int currencyId,
TaxDisplayTypeEnum taxDisplayType, bool isTaxExempt, bool isAdmin, bool isGuest,
bool isForumModerator, int totalForumPosts, string signature, string adminComment,
bool active, bool deleted, DateTime registrationDate,
string timeZoneId, int avatarId, DateTime? dateOfBirth, out MembershipCreateStatus status)
{
Customer customer = null;
if (username == null)
username = string.Empty;
username = username.Trim();
if (email == null)
email = string.Empty;
email = email.Trim();
facebookId = facebookId.Trim();
if (signature == null)
signature = string.Empty;
signature = signature.Trim();
string saltKey = string.Empty;
string passwordHash = string.Empty;
status = MembershipCreateStatus.UserRejected;
if (!isGuest)
{
if (!NopContext.Current.IsAdmin)
{
if (this.CustomerRegistrationType == CustomerRegistrationTypeEnum.Disabled)
{
status = MembershipCreateStatus.ProviderError;
return customer;
}
}
if (this.UsernamesEnabled)
{
if (GetCustomerByUsername(username) != null)
{
status = MembershipCreateStatus.DuplicateUserName;
return customer;
}
if (username.Length > 100)
{
status = MembershipCreateStatus.InvalidUserName;
return customer;
}
}
if (GetCustomerByEmail(email) != null)
{
status = MembershipCreateStatus.DuplicateEmail;
return customer;
}
if (!CommonHelper.IsValidEmail(email))
{
status = MembershipCreateStatus.InvalidEmail;
return customer;
}
if (email.Length > 100)
{
status = MembershipCreateStatus.InvalidEmail;
return customer;
}
if (!NopContext.Current.IsAdmin)
{
if (this.CustomerRegistrationType == CustomerRegistrationTypeEnum.EmailValidation ||
this.CustomerRegistrationType == CustomerRegistrationTypeEnum.AdminApproval)
{
active = false;
}
}
saltKey = CreateSalt(5);
passwordHash = CreatePasswordHash(password, saltKey);
}
customer = AddCustomerForced(customerGuid, email, facebookId, username,
passwordHash, saltKey, affiliateId, billingAddressId,
shippingAddressId, lastPaymentMethodId,
lastAppliedCouponCode, giftCardCouponCodes,
checkoutAttributes, languageId, currencyId, taxDisplayType,
isTaxExempt, isAdmin, isGuest, isForumModerator,
totalForumPosts, signature, adminComment, active,
deleted, registrationDate, timeZoneId, avatarId, dateOfBirth);
if (!isGuest)
{
DateTime lastAccessed = DateTime.UtcNow;
SaveCustomerSession(Guid.NewGuid(), customer.CustomerId, lastAccessed, false);
}
status = MembershipCreateStatus.Success;
if (!isGuest)
{
if (active)
{
IoC.Resolve<IMessageService>().SendCustomerWelcomeMessage(customer, NopContext.Current.WorkingLanguage.LanguageId);
}
else
{
if (this.CustomerRegistrationType == CustomerRegistrationTypeEnum.EmailValidation)
{
Guid accountActivationToken = Guid.NewGuid();
customer.AccountActivationToken = accountActivationToken.ToString();
IoC.Resolve<IMessageService>().SendCustomerEmailValidationMessage(customer, NopContext.Current.WorkingLanguage.LanguageId);
}
}
}
return customer;
}
This is just some kinda duplicate of the normal addcustomer call except that it has a facebookId as an input parameter.
There is also some wrapper:
/// <summary>
/// Adds a customer with faceookId
/// </summary>
/// <param name="email">The email</param>
/// <param name="facebookId">The facebook Id</param>
/// <param name="username">The username</param>
/// <param name="password">The password</param>
/// <param name="isAdmin">A value indicating whether the customer is administrator</param>
/// <param name="isGuest">A value indicating whether the customer is guest</param>
/// <param name="active">A value indicating whether the customer is active</param>
/// <param name="status">Status</param>
/// <returns>A customer</returns>
public Customer AddCustomer(string email, string facebookId, string username, string password,
bool isAdmin, bool isGuest, bool active, out MembershipCreateStatus status)
{
int affiliateId = 0;
HttpCookie affiliateCookie = HttpContext.Current.Request.Cookies.Get("NopCommerce.AffiliateId");
if (affiliateCookie != null)
{
Affiliate affiliate = IoC.Resolve<IAffiliateService>().GetAffiliateById(Convert.ToInt32(affiliateCookie.Value));
if (affiliate != null && affiliate.Active)
affiliateId = affiliate.AffiliateId;
}
var customer = AddCustomer(Guid.NewGuid(), email, facebookId, username, password, affiliateId,
0, 0, 0, string.Empty, string.Empty, string.Empty,
NopContext.Current.WorkingLanguage.LanguageId,
NopContext.Current.WorkingCurrency.CurrencyId,
NopContext.Current.TaxDisplayType, false, isAdmin, isGuest,
false, 0, string.Empty, string.Empty, active,
false, DateTime.UtcNow, string.Empty, 0, null, out status);
if (status == MembershipCreateStatus.Success)
{
if (affiliateCookie != null)
{
affiliateCookie.Expires = DateTime.Now.AddMonths(-1);
if (HttpContext.Current != null)
{
HttpContext.Current.Response.Cookies.Set(affiliateCookie);
}
}
}
return customer;
}
/// <summary>
/// Adds a customer with facebook id without any validations, welcome messages
/// </summary>
/// <param name="customerGuid">The customer identifier</param>
/// <param name="email">The email</param>
/// <param name="facebookId">The facebook Id</param>
/// <param name="username">The username</param>
/// <param name="passwordHash">The password hash</param>
/// <param name="saltKey">The salt key</param>
/// <param name="affiliateId">The affiliate identifier</param>
/// <param name="billingAddressId">The billing address identifier</param>
/// <param name="shippingAddressId">The shipping address identifier</param>
/// <param name="lastPaymentMethodId">The last payment method identifier</param>
/// <param name="lastAppliedCouponCode">The last applied coupon code</param>
/// <param name="giftCardCouponCodes">The applied gift card coupon code</param>
/// <param name="checkoutAttributes">The selected checkout attributes</param>
/// <param name="languageId">The language identifier</param>
/// <param name="currencyId">The currency identifier</param>
/// <param name="taxDisplayType">The tax display type</param>
/// <param name="isTaxExempt">A value indicating whether the customer is tax exempt</param>
/// <param name="isAdmin">A value indicating whether the customer is administrator</param>
/// <param name="isGuest">A value indicating whether the customer is guest</param>
/// <param name="isForumModerator">A value indicating whether the customer is forum moderator</param>
/// <param name="totalForumPosts">A forum post count</param>
/// <param name="signature">Signature</param>
/// <param name="adminComment">Admin comment</param>
/// <param name="active">A value indicating whether the customer is active</param>
/// <param name="deleted">A value indicating whether the customer has been deleted</param>
/// <param name="registrationDate">The date and time of customer registration</param>
/// <param name="timeZoneId">The time zone identifier</param>
/// <param name="avatarId">The avatar identifier</param>
/// <param name="dateOfBirth">Date of birth</param>
/// <returns>A customer</returns>
public Customer AddCustomerForced(Guid customerGuid, string email, string facebookId,
string username, string passwordHash, string saltKey,
int affiliateId, int billingAddressId,
int shippingAddressId, int lastPaymentMethodId,
string lastAppliedCouponCode, string giftCardCouponCodes,
string checkoutAttributes, int languageId,
int currencyId, TaxDisplayTypeEnum taxDisplayType, bool isTaxExempt,
bool isAdmin, bool isGuest, bool isForumModerator,
int totalForumPosts, string signature, string adminComment,
bool active, bool deleted, DateTime registrationDate, string timeZoneId,
int avatarId, DateTime? dateOfBirth)
{
email = CommonHelper.EnsureNotNull(email);
email = email.Trim();
email = CommonHelper.EnsureMaximumLength(email, 255);
username = CommonHelper.EnsureNotNull(username);
username = username.Trim();
username = CommonHelper.EnsureMaximumLength(username, 100);
// facebook
facebookId = facebookId.Trim();
passwordHash = CommonHelper.EnsureNotNull(passwordHash);
passwordHash = CommonHelper.EnsureMaximumLength(passwordHash, 255);
saltKey = CommonHelper.EnsureNotNull(saltKey);
saltKey = CommonHelper.EnsureMaximumLength(saltKey, 255);
lastAppliedCouponCode = CommonHelper.EnsureNotNull(lastAppliedCouponCode);
lastAppliedCouponCode = CommonHelper.EnsureMaximumLength(lastAppliedCouponCode, 100);
signature = CommonHelper.EnsureNotNull(signature);
signature = CommonHelper.EnsureMaximumLength(signature, 300);
adminComment = CommonHelper.EnsureNotNull(adminComment);
adminComment = CommonHelper.EnsureMaximumLength(adminComment, 4000);
timeZoneId = CommonHelper.EnsureNotNull(timeZoneId);
timeZoneId = CommonHelper.EnsureMaximumLength(timeZoneId, 200);
var customer = _context.Customers.CreateObject();
customer.CustomerGuid = customerGuid;
customer.Email = email;
customer.FacebookId = facebookId;
customer.Username = username;
customer.PasswordHash = passwordHash;
customer.SaltKey = saltKey;
customer.AffiliateId = affiliateId;
customer.BillingAddressId = billingAddressId;
customer.ShippingAddressId = shippingAddressId;
customer.LastPaymentMethodId = lastPaymentMethodId;
customer.LastAppliedCouponCode = lastAppliedCouponCode;
customer.GiftCardCouponCodes = giftCardCouponCodes;
customer.CheckoutAttributes = checkoutAttributes;
customer.LanguageId = languageId;
customer.CurrencyId = currencyId;
customer.TaxDisplayTypeId = (int)taxDisplayType;
customer.IsTaxExempt = isTaxExempt;
customer.IsAdmin = isAdmin;
customer.IsGuest = isGuest;
customer.IsForumModerator = isForumModerator;
customer.TotalForumPosts = totalForumPosts;
customer.Signature = signature;
customer.AdminComment = adminComment;
customer.Active = active;
customer.Deleted = deleted;
customer.RegistrationDate = registrationDate;
customer.TimeZoneId = timeZoneId;
customer.AvatarId = avatarId;
customer.DateOfBirth = dateOfBirth;
_context.Customers.AddObject(customer);
_context.SaveChanges();
//reward points
if (!isGuest &&
IoC.Resolve<IOrderService>().RewardPointsEnabled &&
IoC.Resolve<IOrderService>().RewardPointsForRegistration > 0)
{
IoC.Resolve<IOrderService>().InsertRewardPointsHistory(customer.CustomerId, 0,
IoC.Resolve<IOrderService>().RewardPointsForRegistration, decimal.Zero, decimal.Zero,
string.Empty, IoC.Resolve<ILocalizationManager>().GetLocaleResourceString("RewardPoints.Message.EarnedForRegistration"),
DateTime.UtcNow);
}
//raise event
EventContext.Current.OnCustomerCreated(null,
new CustomerEventArgs() { Customer = customer });
return customer;
}
in UpdateCustomer function, add some validation:
/// <summary>
/// Updates the customer
/// </summary>
/// <param name="customer">Customer</param>
public void UpdateCustomer(Customer customer)
{
if (customer == null)
throw new ArgumentNullException("customer");
customer.Email = CommonHelper.EnsureNotNull(customer.Email);
customer.Email = customer.Email.Trim();
customer.Email = CommonHelper.EnsureMaximumLength(customer.Email, 255);
customer.Username = CommonHelper.EnsureNotNull(customer.Username);
customer.Username = customer.Username.Trim();
customer.Username = CommonHelper.EnsureMaximumLength(customer.Username, 100);
if (!string.IsNullOrEmpty(customer.FacebookId))
{
customer.FacebookId = customer.FacebookId.Trim();
}
customer.PasswordHash = CommonHelper.EnsureNotNull(customer.PasswordHash);
customer.PasswordHash = CommonHelper.EnsureMaximumLength(customer.PasswordHash, 255);
customer.SaltKey = CommonHelper.EnsureNotNull(customer.SaltKey);
customer.SaltKey = CommonHelper.EnsureMaximumLength(customer.SaltKey, 255);
customer.LastAppliedCouponCode = CommonHelper.EnsureNotNull(customer.LastAppliedCouponCode);
customer.LastAppliedCouponCode = CommonHelper.EnsureMaximumLength(customer.LastAppliedCouponCode, 255);
customer.Signature = CommonHelper.EnsureNotNull(customer.Signature);
customer.Signature = customer.Signature.Trim();
customer.Signature = CommonHelper.EnsureMaximumLength(customer.Signature, 300);
customer.AdminComment = CommonHelper.EnsureNotNull(customer.AdminComment);
customer.AdminComment = CommonHelper.EnsureMaximumLength(customer.AdminComment, 4000);
customer.TimeZoneId = CommonHelper.EnsureNotNull(customer.TimeZoneId);
customer.TimeZoneId = CommonHelper.EnsureMaximumLength(customer.TimeZoneId, 200);
var subscriptionOld = customer.NewsLetterSubscription;
if (!_context.IsAttached(customer))
_context.Customers.Attach(customer);
_context.SaveChanges();
if (subscriptionOld != null && !customer.Email.ToLower().Equals(subscriptionOld.Email.ToLower()))
{
subscriptionOld.Email = customer.Email;
IoC.Resolve<IMessageService>().UpdateNewsLetterSubscription(subscriptionOld);
}
//raise event
EventContext.Current.OnCustomerUpdated(null,
new CustomerEventArgs() { Customer = customer });
}
and of cause you need the interface definition in
nopCommerce_1.90_Source\Libraries\Nop.BusinessLogic\Customer\ICustomerService.cs
[code]
/// <summary>
/// Gets a customer by facebookId
/// </summary>
/// <param name="facebookId">Customer facebookId</param>
/// <returns>A customer</returns>
Customer GetCustomerByFacebookId(string facebookId);
/// <summary>
/// Adds a customer with facebookId