コード例 #1
0
        public ActionResult Add(RegisterUserViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            string errorMessage = "Unable to register. Please contact us for assistance.";

            try
            {
                if (Roles.IsUserInRole("Admin"))
                {
                    model.IsAdmin = true;
                }

                string userLogin = model.Email.ToLower();

                if (WebSecurity.UserExists(userLogin))
                {
                    ModelState.AddModelError("EmailAlreadyExists", "The Email address is already in use.");
                    return(View(model));
                }

                WebSecurity.CreateUserAndAccount(userLogin, model.Password);

                ApplicationUser user = model.ToApplicationUser();
                user.ApplicationUserId = WebSecurity.GetUserId(userLogin);
                user.Email             = userLogin;

                ResultEnum result = userService.CreateApplicationUser(user);
                switch (result)
                {
                case ResultEnum.Success:
                    if (model.IsProvider && !Roles.IsUserInRole("Admin"))
                    {
                        Roles.AddUserToRoles(userLogin, new string[] { "Provider" });
                    }
                    else if (model.IsProvider && Roles.IsUserInRole("Admin"))
                    {
                        Roles.AddUserToRoles(userLogin, new string[] { "Provider", "ActiveUser" });
                    }
                    else if (!model.IsAdmin)
                    {
                        Roles.AddUserToRoles(userLogin, new string[] { "Customer", "ActiveUser" });
                    }

                    if (!Roles.IsUserInRole("Admin"))
                    {
                        if (WebSecurity.Login(model.Email, model.Password))
                        {
                            // Login successful

                            // Send SMS message to confirm successful registration
                            string phoneNumber = PhoneValidation.ValidateMobileNumber(model.Phone);

                            if (phoneNumber != null)
                            {
                                string message = String.Format(
                                    "Hi {0}, We're just confirming your successful registration with Grande Travel.", model.FirstName);

                                GrandeTravel.Utility.IPhoneService commClient =
                                    UtilityFactory.GetPhoneService(Authentication.GetTwilioAuthentication());

                                Task task = commClient.SendSMSAsync(phoneNumber, message);
                            }

                            // If the customer wants to order a package, redirect to Payment
                            if (model.HasPackage)
                            {
                                return(RedirectToAction("CreateTransaction", "Payment", new { PackageId = model.PackageId }));
                            }

                            // If a provider, show confirmation message
                            if (model.IsProvider && !Roles.IsUserInRole("Admin"))
                            {
                                model.AccountCreatedSuccessfully = true;
                                model.isProviderConfirmed        = true;
                                WebSecurity.Logout();
                                return(View(model));
                            }

                            return(RedirectToAction("Index", "Home"));
                        }
                        else
                        {
                            // Login unsuccessful
                            ModelState.AddModelError("ErrorMessage", errorMessage);
                            return(View(model));
                        }
                    }
                    else
                    {
                        // Admin user - Create user only. Show success message, but do not log in.
                        model.AccountCreatedSuccessfully = true;
                        return(View(model));
                    }

                case ResultEnum.Fail:
                    break;
                }
            }
            catch (Exception)
            {
                ModelState.AddModelError("ErrorMessage", errorMessage);
                return(View(model));
            }

            return(View(model));
        }