Exemplo n.º 1
0
        public ActionResult Index(string searchStringUserNameOrEmail, string currentFilter, int?page)
        {
            try
            {
                int intPage           = 1;
                int intPageSize       = 5;
                int intTotalPageCount = 0;
                if (searchStringUserNameOrEmail != null)
                {
                    intPage = 1;
                }
                else
                {
                    if (currentFilter != null)
                    {
                        searchStringUserNameOrEmail = currentFilter;
                        intPage = page ?? 1;
                    }
                    else
                    {
                        searchStringUserNameOrEmail = "";
                        intPage = page ?? 1;
                    }
                }
                ViewBag.CurrentFilter = searchStringUserNameOrEmail;

                List <ExpandedUserViewModel> col_UserViewModel = new List <ExpandedUserViewModel>();
                int intSkip = (intPage - 1) * intPageSize;
                intTotalPageCount = UserManager.Users
                                    .Where(x => x.UserName.Contains(searchStringUserNameOrEmail))
                                    .Count();
                var result = UserManager.Users
                             .Where(x => x.UserName.Contains(searchStringUserNameOrEmail))
                             .OrderBy(x => x.UserName)
                             .Skip(intSkip)
                             .Take(intPageSize)
                             .ToList();
                foreach (var item in result)
                {
                    ExpandedUserViewModel objUserViewModel = new ExpandedUserViewModel();
                    objUserViewModel.UserName          = item.UserName;
                    objUserViewModel.Email             = item.Email;
                    objUserViewModel.LockoutEndDateGMT = item.LockoutEndDateUtc;
                    col_UserViewModel.Add(objUserViewModel);
                }
                // Set the number of pages
                var _UserViewModelsIPagedList =
                    new StaticPagedList <ExpandedUserViewModel>
                    (
                        col_UserViewModel, intPage, intPageSize, intTotalPageCount
                    );
                return(View(_UserViewModelsIPagedList));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(string.Empty, "Error: " + ex);
                List <ExpandedUserViewModel> col_UserViewModel = new List <ExpandedUserViewModel>();
                return(View(col_UserViewModel.ToPagedList(1, 25)));
            }
        }
Exemplo n.º 2
0
        public ActionResult EditUser(string UserName)
        {
            if (UserName == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            ExpandedUserViewModel objExpandedUserViewModel = GetUser(UserName);

            if (objExpandedUserViewModel == null)
            {
                return(HttpNotFound());
            }
            return(View(objExpandedUserViewModel));
        }
Exemplo n.º 3
0
        private ExpandedUserViewModel UpdateViewModelUser(ExpandedUserViewModel paramExpandedUserViewModel)
        {
            ApplicationUser result =
                UserManager.FindByName(paramExpandedUserViewModel.UserName);

            // If we could not find the user, throw an exception
            if (result == null)
            {
                throw new Exception("Could not find the User");
            }

            result.Email = paramExpandedUserViewModel.Email;

            // Lets check if the account needs to be unlocked
            if (UserManager.IsLockedOut(result.Id))
            {
                // Unlock user
                UserManager.ResetAccessFailedCountAsync(result.Id);
            }

            UserManager.Update(result);

            // Was a password sent across?
            if (!string.IsNullOrEmpty(paramExpandedUserViewModel.Password))
            {
                // Remove current password
                var removePassword = UserManager.RemovePassword(result.Id);
                if (removePassword.Succeeded)
                {
                    // Add new password
                    var AddPassword =
                        UserManager.AddPassword(
                            result.Id,
                            paramExpandedUserViewModel.Password
                            );

                    if (AddPassword.Errors.Count() > 0)
                    {
                        throw new Exception(AddPassword.Errors.FirstOrDefault());
                    }
                }
            }

            return(paramExpandedUserViewModel);
        }
Exemplo n.º 4
0
        private ExpandedUserViewModel GetUser(string paramUserName)
        {
            ExpandedUserViewModel objExpandedUserViewModel = new ExpandedUserViewModel();

            var result = UserManager.FindByName(paramUserName);

            // If we could not find the user, throw an exception
            if (result == null)
            {
                throw new Exception("Could not find the User");
            }

            objExpandedUserViewModel.UserName          = result.UserName;
            objExpandedUserViewModel.Email             = result.Email;
            objExpandedUserViewModel.LockoutEndDateGMT = result.LockoutEndDateUtc;
            objExpandedUserViewModel.AccessFailedCount = result.AccessFailedCount;
            objExpandedUserViewModel.PhoneNumber       = result.PhoneNumber;

            return(objExpandedUserViewModel);
        }
Exemplo n.º 5
0
 public ActionResult EditUser(ExpandedUserViewModel paramExpandedUserViewModel)
 {
     try
     {
         if (paramExpandedUserViewModel == null)
         {
             return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
         }
         ExpandedUserViewModel objExpandedUserViewModel = UpdateViewModelUser(paramExpandedUserViewModel);
         if (objExpandedUserViewModel == null)
         {
             return(HttpNotFound());
         }
         return(Redirect("~/Admin"));
     }
     catch (Exception ex)
     {
         ModelState.AddModelError(string.Empty, "Error: " + ex);
         return(View("EditUser", GetUser(paramExpandedUserViewModel.UserName)));
     }
 }