Exemplo n.º 1
0
        /// <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);
        }