Esempio n. 1
0
        public ActionResult CreateUser(ExpandedUser paramExpandedUser)
        {
            try
            {
                if (paramExpandedUser == null)
                {
                    return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
                }

                var Email    = paramExpandedUser.Email.Trim();
                var UserName = paramExpandedUser.Email.Trim();
                var Password = paramExpandedUser.Password.Trim();

                if (Email == "")
                {
                    throw new Exception("No Email");
                }

                if (Password == "")
                {
                    throw new Exception("No Password");
                }

                // UserName is LowerCase of the Email
                UserName = Email.ToLower();

                // Create user

                var objNewAdminUser = new ApplicationUser {
                    UserName = UserName, Email = Email
                };
                var AdminUserCreateResult = UserManager.Create(objNewAdminUser, Password);

                if (AdminUserCreateResult.Succeeded == true)
                {
                    string strNewRole = Convert.ToString(Request.Form["Roles"]);

                    if (strNewRole != "0")
                    {
                        // Put user in role
                        UserManager.AddToRole(objNewAdminUser.Id, strNewRole);
                    }

                    return(Redirect("~/Admin"));
                }
                else
                {
                    ViewBag.Roles = GetAllRolesAsSelectList();
                    ModelState.AddModelError(string.Empty,
                                             "Error: Failed to create the user. Check password requirements.");
                    return(View(paramExpandedUser));
                }
            }
            catch (Exception ex)
            {
                ViewBag.Roles = GetAllRolesAsSelectList();
                ModelState.AddModelError(string.Empty, "Error: " + ex);
                return(View("CreateUser"));
            }
        }
Esempio n. 2
0
 public ActionResult DeleteUser(string UserName)
 {
     try
     {
         if (UserName == null)
         {
             return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
         }
         if (UserName.ToLower() == this.User.Identity.Name.ToLower())
         {
             ModelState.AddModelError(
                 string.Empty, "Error: Cannot delete the current user");
             return(View("EditUser"));
         }
         ExpandedUser objExpandedUser = GetUser(UserName);
         if (objExpandedUser == null)
         {
             return(HttpNotFound());
         }
         else
         {
             DeleteUser(objExpandedUser);
         }
         return(Redirect("~/Admin"));
     }
     catch (Exception ex)
     {
         ModelState.AddModelError(string.Empty, "Error: " + ex);
         return(View("EditUser", GetUser(UserName)));
     }
 }
Esempio n. 3
0
        public ActionResult Create()
        {
            var expandedUser = new ExpandedUser();

            ViewBag.Roles = GetAllRolesAsSelectList();

            return(View(expandedUser));
        }
Esempio n. 4
0
        private void DeleteUser(ExpandedUser paramExpandedUser)
        {
            ApplicationUser user =
                UserManager.FindByName(paramExpandedUser.UserName);

            // If we could not find the user, throw an exception
            if (user == null)
            {
                throw new Exception("Could not find the User");
            }
            UserManager.RemoveFromRoles(user.Id, UserManager.GetRoles(user.Id).ToArray());
            UserManager.Update(user);
            UserManager.Delete(user);
        }
Esempio n. 5
0
        public ActionResult EditUser(string username)
        {
            if (username == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            ExpandedUser expUser = GetUser(username);

            if (expUser == null)
            {
                return(HttpNotFound());
            }
            return(View(expUser));
        }
Esempio n. 6
0
        // GET: /Admin/Edit/TestUser
        //[Authorize(Roles = "Administrator")]
        #region public ActionResult EditUser(string UserName)
        public ActionResult EditUser(string UserName)
        {
            if (UserName == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            ExpandedUser objExpandedUser = GetUser(UserName);

            if (objExpandedUser == null)
            {
                return(HttpNotFound());
            }
            return(View(objExpandedUser));
        }
Esempio n. 7
0
        // DELETE: /Admin/DeleteRole?UserName="******"Administrator")]
        #region public ActionResult DeleteRole(string UserName, string RoleName)
        public ActionResult DeleteRole(string UserName, string RoleName)
        {
            try
            {
                if ((UserName == null) || (RoleName == null))
                {
                    return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
                }

                UserName = UserName.ToLower();

                // Check that we have an actual user
                ExpandedUser objExpandedUser = GetUser(UserName);

                if (objExpandedUser == null)
                {
                    return(HttpNotFound());
                }

                if (UserName.ToLower() ==
                    this.User.Identity.Name.ToLower() && RoleName == "Administrator")
                {
                    ModelState.AddModelError(string.Empty,
                                             "Error: Cannot delete Administrator Role for the current user");
                }

                // Go get the User
                ApplicationUser user = UserManager.FindByName(UserName);
                // Remove User from role
                UserManager.RemoveFromRoles(user.Id, RoleName);
                UserManager.Update(user);

                ViewBag.AddRole = new SelectList(RolesUserIsNotIn(UserName));

                return(RedirectToAction("EditRoles", new { UserName = UserName }));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(string.Empty, "Error: " + ex);

                ViewBag.AddRole = new SelectList(RolesUserIsNotIn(UserName));

                UserAndRoles objUserAndRoles =
                    GetUserAndRoles(UserName);

                return(View("EditRoles", objUserAndRoles));
            }
        }
Esempio n. 8
0
        private ExpandedUser UpdateUser(ExpandedUser paramExpandedUser)
        {
            ApplicationUser result =
                UserManager.FindByName(paramExpandedUser.UserName);

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

            result.Email = paramExpandedUser.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(paramExpandedUser.Password))
            {
                // Remove current password
                var removePassword = UserManager.RemovePassword(result.Id);
                if (removePassword.Succeeded)
                {
                    // Add new password
                    var AddPassword =
                        UserManager.AddPassword(
                            result.Id,
                            paramExpandedUser.Password
                            );

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

            return(paramExpandedUser);
        }
Esempio n. 9
0
        public ActionResult EditRoles(string username)
        {
            if (username == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            username = username.ToLower();

            ExpandedUser expUser = GetUser(username);

            if (expUser == null)
            {
                return(HttpNotFound());
            }

            var userAndRoles = GetUserAndRoles(username);

            return(View(userAndRoles));
        }
Esempio n. 10
0
        private ExpandedUser GetUser(string username)
        {
            var expUser = new ExpandedUser();

            var result = UserManager.FindByName(username);

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

            expUser.UserName          = result.UserName;
            expUser.Email             = result.Email;
            expUser.LockoutEndDateUtc = result.LockoutEndDateUtc;
            expUser.AccessFailedCount = result.AccessFailedCount;
            expUser.PhoneNumber       = result.PhoneNumber;

            return(expUser);
        }
Esempio n. 11
0
        // GET: /Admin/EditRoles/TestUser
        //[Authorize(Roles = "Administrator")]
        #region ActionResult EditRoles(string UserName)
        public ActionResult EditRoles(string UserName)
        {
            if (UserName == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            UserName = UserName.ToLower();

            // Check that we have an actual user
            ExpandedUser objExpandedUser = GetUser(UserName);

            if (objExpandedUser == null)
            {
                return(HttpNotFound());
            }

            UserAndRoles objUserAndRoles =
                GetUserAndRoles(UserName);

            return(View(objUserAndRoles));
        }
Esempio n. 12
0
        private ExpandedUser UpdateUser(ExpandedUser expUser)
        {
            ApplicationUser result = UserManager.FindByName(expUser.UserName);

            if (result == null)
            {
                throw new Exception("Could not find the User");
            }

            result.Email = expUser.Email;

            if (UserManager.IsLockedOut(result.Id))
            {
                UserManager.ResetAccessFailedCountAsync(result.Id);
            }

            UserManager.Update(result);

            if (!string.IsNullOrEmpty(expUser.Password))
            {
                var removePassword = UserManager.RemovePassword(result.Id);
                if (removePassword.Succeeded)
                {
                    var AddPassword =
                        UserManager.AddPassword(
                            result.Id,
                            expUser.Password
                            );

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

            return(expUser);
        }
Esempio n. 13
0
        public ActionResult EditUser(ExpandedUser expUser)
        {
            try
            {
                if (expUser == null)
                {
                    return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
                }

                ExpandedUser objExpandedUser = UpdateUser(expUser);

                if (objExpandedUser == null)
                {
                    return(HttpNotFound());
                }

                return(Redirect("~/Admin"));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(string.Empty, "Error: " + ex);
                return(View("EditUser", GetUser(expUser.UserName)));
            }
        }
Esempio n. 14
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 <ExpandedUser> col_UserDTO = new List <ExpandedUser>();
                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)
                {
                    ExpandedUser objUserDTO = new ExpandedUser();

                    objUserDTO.UserName          = item.UserName;
                    objUserDTO.Email             = item.Email;
                    objUserDTO.LockoutEndDateUtc = item.LockoutEndDateUtc;

                    col_UserDTO.Add(objUserDTO);
                }

                // Set the number of pages
                var _UserDTOAsIPagedList =
                    new StaticPagedList <ExpandedUser>
                    (
                        col_UserDTO, intPage, intPageSize, intTotalPageCount
                    );

                return(View(_UserDTOAsIPagedList));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(string.Empty, "Error: " + ex);
                List <ExpandedUser> col_UserDTO = new List <ExpandedUser>();

                return(View(col_UserDTO.ToPagedList(1, 25)));
            }
        }