예제 #1
0
        public async Task <ActionResult> CreateUser(CreateAccountModel model)
        {
            if (ModelState.IsValid)
            {
                //page.310
                AppUser user = new AppUser
                {
                    UserName        = model.Name,
                    Email           = model.Email,
                    PhoneNumber     = model.Phone,
                    CityID          = model.CityID,
                    CountryID       = model.CountryID,
                    ShippingAddress = model.ShippingAddress
                };
                IdentityResult result = await myUserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    //await UserManager.AddToRoleAsync(user.Id, "Users");  //新帳號預設Users權限
                    TempData["message"] = "創建帳號成功,請登入!";
                    return(RedirectToAction("Login", "Account", new { returnUrl = Url.Action("Index", "Home") }));
                }
                else
                {
                    AddErrorsFromResult(result);
                }
            }
            CityAndCountryPorvider.SetSelectListToViewBag(this, repository, model.CityID, model.CountryID);
            if (!CityAndCountryPorvider.CheckIfSelectListOfViewBagCorrect(this))
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            return(View(model));
        }
예제 #2
0
        public async Task <ActionResult> UserCreate(CreateAccountModel model)
        {
            if (ModelState.IsValid)
            {
                //page.310
                AppUser user = new AppUser
                {
                    UserName        = model.Name,
                    Email           = model.Email,
                    PhoneNumber     = model.Phone,
                    CityID          = model.CityID,
                    CountryID       = model.CountryID,
                    ShippingAddress = model.ShippingAddress
                };
                IdentityResult result = await myUserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    return(RedirectToAction("UserIndex"));
                }
                else
                {
                    AddErrorsFromResult(result);
                }
            }
            CityAndCountryPorvider.SetSelectListToViewBag(this, repository, model.CityID, model.CountryID);
            ViewBag.IsAdminAccess = true;
            if (!CityAndCountryPorvider.CheckIfSelectListOfViewBagCorrect(this))
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            return(View("~/Views/Home/CreateUser.cshtml", model));
        }
예제 #3
0
 public ActionResult EditUser()
 {
     CityAndCountryPorvider.SetSelectListToViewBag(this, repository, CurrentUser.CityID, CurrentUser.CountryID);
     ViewBag.IsGoogleAccount = CheckIfGoogleAccount();
     if (!CityAndCountryPorvider.CheckIfSelectListOfViewBagCorrect(this))
     {
         return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
     }
     return(View(GetUserInfoEditModel(CurrentUser)));
 }
예제 #4
0
        public ActionResult UserCreate()
        {
            int defaultCityId = repository.Cities.OrderBy(c => c.ID).First().ID;

            CityAndCountryPorvider.SetSelectListToViewBag(this, repository, defaultCityId);
            ViewBag.IsAdminAccess = true;
            if (!CityAndCountryPorvider.CheckIfSelectListOfViewBagCorrect(this))
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            return(View("~/Views/Home/CreateUser.cshtml"));
        }
예제 #5
0
        public void Can_Set_CityAndCountrySelectListItem_To_ViewBag_And_Check()
        {
            MyMock mock       = new MyMock();
            var    controller = new HomeController(mock.ProductRepository.Object, mock.UserManager.Object);

            CityAndCountryPorvider.SetSelectListToViewBag(controller, mock.ProductRepository.Object, 2);
            SelectListItem[] resultCity    = controller.ViewBag.CitySelectList as SelectListItem[];
            SelectListItem[] resultCountry = controller.ViewBag.CountrySelectList as SelectListItem[];

            Assert.AreEqual(4, resultCity.Length);
            Assert.AreEqual(2, resultCountry.Length);
            Assert.IsTrue(CityAndCountryPorvider.CheckIfSelectListOfViewBagCorrect(controller));
        }
예제 #6
0
 public ActionResult Checkout(Cart cart)
 {
     if (cart.Lines.Count() == 0)
     {
         TempData["message"] = "抱歉, 您的購物車是空的!";
         return(RedirectToAction("Index"));
     }
     CityAndCountryPorvider.SetSelectListToViewBag(this, repository, CurrentUser.CityID, CurrentUser.CountryID);
     if (!CityAndCountryPorvider.CheckIfSelectListOfViewBagCorrect(this))
     {
         return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
     }
     return(View(new ShippingDetails {
         CityID = CurrentUser.CityID,
         CountryID = CurrentUser.CountryID,
         ReceiverName = "",
         Line = CurrentUser.ShippingAddress
     }));
 }
예제 #7
0
 public ActionResult CreateUser()
 {
     if (IsAuthenticated)
     {
         return(RedirectToAction("Index"));
     }
     try
     {
         int defaultCityId = repository.Cities.OrderBy(c => c.ID).FirstOrDefault().ID;
         CityAndCountryPorvider.SetSelectListToViewBag(this, repository, defaultCityId);
         if (!CityAndCountryPorvider.CheckIfSelectListOfViewBagCorrect(this))
         {
             return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
         }
         return(View(new CreateAccountModel()));
     }
     catch (NullReferenceException e)
     {
         return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
     }
 }
예제 #8
0
 public ActionResult Checkout(Cart cart, ShippingDetails shippingDetails)
 {
     if (cart.Lines.Count() == 0)
     {
         ModelState.AddModelError("", "抱歉, 您的購物車是空的!");
     }
     CityAndCountryPorvider.SetSelectListToViewBag(this, repository, shippingDetails.CityID, shippingDetails.CountryID);
     if (!CityAndCountryPorvider.CheckIfSelectListOfViewBagCorrect(this))
     {
         return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
     }
     if (ModelState.IsValid)
     {
         //orderProcessor.ProcessOrder(cart, shippingDetails);
         orderLogger.RecordOrder(cart, CurrentUser.Id, shippingDetails.ReceiverName,
                                 repository.GetCityAndCountryName(shippingDetails.CountryID) + shippingDetails.Line);
         cart.Clear();
         return(View("Completed"));
     }
     else
     {
         return(View(shippingDetails));
     }
 }
예제 #9
0
        public async Task <ActionResult> EditUser(UserInfoEditModel userInfo)
        {
            userInfo.Name  = CurrentUser.UserName;
            userInfo.Email = CurrentUser.Email;
            CityAndCountryPorvider.SetSelectListToViewBag(this, repository, userInfo.CityID, userInfo.CountryID);
            ViewBag.IsGoogleAccount = CheckIfGoogleAccount();
            if (!CityAndCountryPorvider.CheckIfSelectListOfViewBagCorrect(this))
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            if (!ModelState.IsValid)
            {
                return(View(userInfo));
            }
            // 確認phone有符合所訂要求
            IdentityResult validPhone = null;

            if (userInfo.Phone != string.Empty)
            {
                CurrentUser.PhoneNumber = userInfo.Phone; //因ValidateAsync只接受User物件,因此這邊直接更改user的屬性
                validPhone = await myUserManager.UserValidator_ValidateAsync(CurrentUser);

                if (!validPhone.Succeeded)
                {
                    AddErrorsFromResult(validPhone);
                }
            }
            // 確認密碼
            IdentityResult validPass = null;

            if (userInfo.Password != string.Empty && userInfo.Password != null)
            {
                validPass = await myUserManager.PasswordValidator_ValidateAsync(userInfo.Password);

                if (validPass.Succeeded)
                {
                    CurrentUser.PasswordHash = myUserManager.PasswordHasher_HashPassword(userInfo.Password);
                }
                else
                {
                    AddErrorsFromResult(validPass);
                }
            }
            if ((validPhone.Succeeded && validPass == null) ||
                (validPhone.Succeeded && userInfo.Password != string.Empty && validPass.Succeeded))
            {
                CurrentUser.CityID          = userInfo.CityID;
                CurrentUser.CountryID       = userInfo.CountryID;
                CurrentUser.ShippingAddress = userInfo.ShippingAddress;
                IdentityResult result = await myUserManager.UpdateAsync(CurrentUser);

                if (result.Succeeded)
                {
                    TempData["message"] = "修改資料成功!";
                    return(RedirectToAction("EditUser"));
                }
                else
                {
                    AddErrorsFromResult(result);
                }
            }
            return(View(userInfo));
        }