/// <summary> /// Validates the user ID in the context. /// </summary> /// <param name="crud"> /// The CRUD operation for which a token is being created. /// </param> /// <returns> /// The ResultCode corresponding to the result of the operation. /// </returns> /// <remarks> /// Authenticated user is automatically created within the system if necessary when validating the user ID in the /// context while obtaining a token for Create operations. /// </remarks> private ResultCode ValidateUserId(Crud crud) { ResultCode result = ResultCode.Success; // Attempt to retrieve the user. SharedUserLogic sharedUserLogic = new SharedUserLogic(Context, CommerceOperationsFactory.UserOperations(Context)); User user = sharedUserLogic.RetrieveUser(); // If the user is null and the CRUD operation is Create, implicitly create the user. if (user == null) { if (crud == Crud.Create) { if (Context.ContainsKey(Key.CorrelationId) == true) { Guid userId = (Guid)Context[Key.GlobalUserId]; user = new User(userId, Guid.NewGuid()); Context[Key.User] = user; sharedUserLogic.AddUser(); // Update analytics. Analytics.AddRegisterUserEvent(userId, user.AnalyticsEventId, (Guid)Context[Key.CorrelationId], Context[Key.ReferrerId] as string); // Add referral, if any. SharedReferralLogic sharedReferralLogic = new SharedReferralLogic(Context, CommerceOperationsFactory.ReferralOperations(Context)); sharedReferralLogic.AddReferral((string)Context[Key.ReferredUserId]); } else { Context.Log.Warning("No correlation ID could be found in the context."); result = ResultCode.ParameterCannotBeNull; } } else { result = ResultCode.UnexpectedUnregisteredUser; } } return(result); }
/// <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); }