public ActionResult EditUser(vmUser_EditUser vm)
        {
            OnlyOwnerAccess(vm.User.UserId);
            var target = new MemoryStream();

            if (ModelState.IsValid)
            {
                if (vm.Image != null)
                {
                    vm.Image.InputStream.CopyTo(target);
                    vm.User.Image = target.ToArray();
                    vm.User.UseFacebookImage = false;
                }

                var result = vm.User.UserId <= 0 ? UserService.CreateUser(vm.User) : UserService.UpdateUser(vm.User, true);

                if (result.Success)
                {
                    DisplayMessageToUser(new DisplayMessage(DisplayMessageType.SuccessMessage, "User profile has been saved successfully"));
                    return RedirectToAction("ViewUser", new { id = vm.User.UserId });
                }

                Utilities.AddModelStateErrors(ModelState, result.GetServiceErrors());
            }
            FillEditUserEnums(ref vm);

            return View(vm);
        }
        public ActionResult EditUser(int? id)
        {
            var vm = new vmUser_EditUser();

            if (id.HasValue)
            {
                OnlyOwnerAccess(id.Value);
                vm.User = UserService.GetUserById(id.Value);
                vm.EmailAddressVerification = vm.User.EmailAddress;
                vm.AllExistingRoles = Roles.GetAllRoles();
                vm.UsersRoles = Roles.GetRolesForUser();
            }
            if (vm.User == null)
            {
                vm.User = new User();
            }

            FillEditUserEnums(ref vm);

            return View(vm);
        }
 public ActionResult ViewUser(int id = 0, string username = "")
 {
     vmUser_EditUser vm;
     if (id > 0)
     {
         OnlyOwnerAccess(id);
         vm = new vmUser_EditUser {User = UserService.GetUserById(id)};
     }
     else
     {
         OnlyOwnerAccess(username);
         vm = new vmUser_EditUser { User = UserService.GetUserByUsername(username) };
     }
     FillEditUserEnums(ref vm);
     HttpContext.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
     HttpContext.Response.Cache.SetValidUntilExpires(false);
     HttpContext.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
     HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
     HttpContext.Response.Cache.SetNoStore();
     return View(vm);
 }
 private void FillEditUserEnums(ref vmUser_EditUser model)
 {
     model.Regions = UserService.GetRegionsForCountry((int)CountryCodes.USA);
     model.Regions.AddRange(UserService.GetRegionsForCountry((int)CountryCodes.Canada));
 }
        public ActionResult CreateUser(string returnUrl)
        {
            var vm = new vmUser_EditUser
                {
                    returnUrl = returnUrl
                };

            Debug.Assert(TempData != null, "TempData != null");
            var user = TempData["FacebookUser"] as FacebookUser;
            if (user != null)
            {
                var facebookUser = user;
                vm.User.FirstName = facebookUser.FirstName;
                vm.User.LastName = facebookUser.LastName;
                vm.User.EmailAddress = facebookUser.Email;
                vm.EmailAddressVerification = facebookUser.Email;
                vm.User.FacebookId = long.Parse(facebookUser.Id);
                vm.User.UseFacebookImage = true;
                DisplayMessageToUser(new DisplayMessage(DisplayMessageType.General,
                    "Thank you for logging in in with Facebook, please provide some additional information to complete your Dirty Girl account registration.”."));
            }

            FillEditUserEnums(vm);

            return View(vm);
        }
        private void FillEditUserEnums(vmUser_EditUser model)
        {
            model.Regions = UserService.GetRegionsForCountry((int)CountryCodes.USA);
            model.Regions.AddRange(UserService.GetRegionsForCountry((int)CountryCodes.Canada));

            model.Months = DirtyGirlExtensions.ConvertToSelectList<Months>();

            model.Days = new List<int>();
            for (int i = 1; i <= 31; i++)
            {
                model.Days.Add(i);
            }

            model.Years = new List<int>();
            for (int i = DateTime.Now.Year; i >= DateTime.Now.Year - 100; i--)
            {
                model.Years.Add(i);
            }
        }
        public ActionResult EditUser(vmUser_EditUser vm)
        {
            OnlyOwnerAccess(vm.User.UserId);
            var target = new MemoryStream();

            if (ModelState.IsValid)
            {
                if (vm.Image != null)
                {
                    vm.Image.InputStream.CopyTo(target);
                    vm.User.Image = target.ToArray();
                    vm.User.UseFacebookImage = false;
                }

                ServiceResult result = UserService.UpdateUser(vm.User, false);

                if (result.Success)
                {
                    DisplayMessageToUser(new DisplayMessage(DisplayMessageType.SuccessMessage, "User profile has been updated successfully"));
                    if (!string.IsNullOrEmpty(vm.returnUrl))
                    {
                        Response.Redirect(vm.returnUrl);
                    }
                    else
                    {
                        return RedirectToAction("ViewUser", new { userId = vm.User.UserId });
                    }

                }
                Utilities.AddModelStateErrors(ModelState, result.GetServiceErrors());
            }

            FillEditUserEnums(vm);

            return View(vm);
        }
        public ActionResult EditUser(int userId, string returnUrl = "")
        {
            OnlyOwnerAccess(userId);

            var vm = new vmUser_EditUser { User = UserService.GetUserById(userId) };
            vm.EmailAddressVerification = vm.EmailAddress;
            vm.returnUrl = returnUrl;

            FillEditUserEnums(vm);

            return View(vm);
        }
        public ActionResult CreateUser(vmUser_EditUser vm)
        {
            Regex regExUserName = new Regex(@"^[a-zA-Z0-9][a-zA-Z0-9\+@._-]+$");
            Regex regExName = new Regex(@"^[a-zA-Z].*$");

            //bool validImageFile = true;
            if (!vm.ImAGirl)
                ModelState.AddModelError("ImAGirl", "You must confirm you are a female.");

            if (vm.User.UserName.Length < 3)
                ModelState.AddModelError("UserName", "Usernames must be 3 or more characters long.");

            if (!regExUserName.IsMatch(vm.User.UserName))
                ModelState.AddModelError("UserName", "Invalid Username.");

            if (vm.User.FacebookId == null)
            {
                if (string.IsNullOrWhiteSpace(vm.User.Password))
                {
                    ModelState.AddModelError("UserName", "Password is required.");
                }
                else if (vm.User.UserName.ToLower() == vm.Password.ToLower())
                    ModelState.AddModelError("Password", "Your password cannot be the same as your username.");
            }

            if (!regExName.IsMatch(vm.User.FirstName))
                ModelState.AddModelError("FirstName", "Please enter a valid first name.");

            if (!regExName.IsMatch(vm.User.LastName))
                ModelState.AddModelError("LastName", "Please enter a valid last name.");

            if (ModelState.IsValid)
            {
                if (vm.Image != null)
                {
                    MemoryStream m = new MemoryStream();
                    vm.Image.InputStream.CopyTo(m);
                    vm.User.Image = m.ToArray();
                }

                ServiceResult result = UserService.CreateUser(vm.User);
                /*
                if (!validImageFile)
                    result.AddServiceError("Images must be .jpg, .png, .gif, and less than 2 megabytes in size");
                */

                if (result.Success)
                {
                    FormsAuthentication.SetAuthCookie(vm.User.UserName, false);

                    if (!string.IsNullOrEmpty(vm.returnUrl))
                        return Redirect(vm.returnUrl);

                    DisplayMessageToUser(new DisplayMessage(DisplayMessageType.SuccessMessage, "We’ve setup your profile and you’re ready to register for your Dirty Girl run."));

                    return RedirectToAction("ViewUser", new { userId = vm.User.UserId });
                }
                Utilities.AddModelStateErrors(ModelState, result.GetServiceErrors());
            }

            FillEditUserEnums(vm);

            return View(vm);
        }