/// <summary> /// Get the canonical user object from the Users DAL if it exists. Otherwise, creates an empty user with safe default /// values. /// </summary> /// <param name="userId"> /// The ID of the user to load. /// </param> /// <return> /// The User Object /// </return> protected Users.Dal.DataModel.User RetrieveUser(Guid userId) { // Attempt to load the user from the Users DAL. Users.Dal.DataModel.User user = UsersDal.GetUserByUserId(userId); // Set safe default values if canonical user object does not exist or is not fully populated. if (user == null) { user = new Users.Dal.DataModel.User(); } if (user.Name == null) { user.Name = String.Empty; } if (user.Email == null) { user.Email = String.Empty; } // Place only first name in salutation. If there is no name, make sure no space will be added. string[] nameParts = user.Name.Split(' '); SalutationName = String.Format(" {0}", nameParts[0]); if (SalutationName == " ") { SalutationName = String.Empty; } return(user); }
/// <summary> /// Places the User object representing the person making this request to the context. /// </summary> /// <returns> /// The ResultCode corresponding to the result of the operation. /// </returns> /// <remarks> /// If flagged to do so, a user account will be created and associated with the specified e-mail address, if the e-mail /// address has not already been used. /// </remarks> private ResultCode PlaceUserInContext() { ResultCode result = ResultCode.Success; bool createUnauthenticatedAccount = false; if (Context[Key.CreateUnauthenticatedAccount] != null) { createUnauthenticatedAccount = (bool)Context[Key.CreateUnauthenticatedAccount]; } if (createUnauthenticatedAccount == true) { string emailAddress = Context[Key.EmailAddress] as string; if (String.IsNullOrWhiteSpace(emailAddress) == false) { try { // Ensure the e-mail address may be valid. MailAddress mailAddress = new MailAddress(emailAddress); // Attempt to add a user to User Services via Users Dal and obtain its authentication vector. IUsersDal usersDal = PartnerFactory.UsersDal(Context.Config); Users.Dal.DataModel.User fullUser = usersDal.CreateUnauthenticatedUser(mailAddress.Address, (string)Context[Key.ReferrerId], (string)Context[Key.UserLocation]); UnauthenticatedAddCardResponse response = (UnauthenticatedAddCardResponse)Context[Key.Response]; if (String.IsNullOrWhiteSpace(fullUser.MsId) == true) { response.AuthenticationVector = AuthenticationVector.Email.ToString(); } else if (fullUser.MsId.StartsWith("FB-", StringComparison.OrdinalIgnoreCase) == true) { response.AuthenticationVector = AuthenticationVector.Facebook.ToString(); } else { response.AuthenticationVector = AuthenticationVector.MicrosoftAccount.ToString(); } Guid userId = fullUser.Id; Context[Key.GlobalUserId] = userId; // If the user returned by User Services has not already been registered in the Commerce system, register a new Commerce user. User user = SharedUserLogic.RetrieveUser(); if (user == null) { user = new User(userId, Guid.NewGuid()); Context[Key.User] = user; result = SharedUserLogic.AddUser(); if (result == ResultCode.Created) { Analytics.AddRegisterUserEvent(user.GlobalId, user.AnalyticsEventId, Guid.Empty, Context[Key.ReferrerId] as string); result = ResultCode.Success; } } else { Context[Key.User] = user; } // If the user was added or retrieved successfully, proceed. if (result == ResultCode.Success) { // If the user has not already signed up officially with Bing Offers, proceed. if (response.AuthenticationVector == AuthenticationVector.Email.ToString()) { // If the user has not already added a card, proceed. SharedCardLogic sharedCardLogic = new SharedCardLogic(Context, CommerceOperationsFactory.CardOperations(Context)); if (sharedCardLogic.RetrieveUserCards().Count() == 0) { response.ActivationToken = fullUser.ActivationToken; } else { result = ResultCode.UnauthenticatedUserAlreadyExists; } } else { result = ResultCode.UserAlreadyExists; } } } catch (FormatException) { result = ResultCode.InvalidParameter; } } else { result = ResultCode.ParameterCannotBeNull; } } else { Context[Key.User] = SharedUserLogic.RetrieveUser(); } return(result); }