예제 #1
0
        /// <summary>
        /// returns UserAndRolesDTO objUserAndRolesDTO that contains user and role
        /// </summary>
        /// <param name="UserName"></param>
        /// <returns>objUserAndRolesDTO</returns>
        /// <includesource>yes</includesource>
        #region private UserAndRolesDTO GetUserAndRoles(string UserName)
        private UserAndRolesDTO GetUserAndRoles(string UserName)
        {
            // Go get the User
            ApplicationUser user = UserManager.FindByName(UserName);

            List <UserRoleDTO> colUserRoleDTO =
                (from objRole in UserManager.GetRoles(user.Id)
                 select new UserRoleDTO
            {
                RoleName = objRole,
                UserName = UserName
            }).ToList();

            if (colUserRoleDTO.Count() == 0)
            {
                colUserRoleDTO.Add(new UserRoleDTO {
                    RoleName = "No Roles Found"
                });
            }

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

            // Create UserRolesAndPermissionsDTO
            UserAndRolesDTO objUserAndRolesDTO =
                new UserAndRolesDTO();

            objUserAndRolesDTO.UserName       = UserName;
            objUserAndRolesDTO.colUserRoleDTO = colUserRoleDTO;
            return(objUserAndRolesDTO);
        }
예제 #2
0
 // DELETE: /Admin/User/DeleteRole?userName="******"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));
         UserAndRolesDTO objUserAndRolesDTO = GetUserAndRoles(userName);
         return(View("EditRoles", objUserAndRolesDTO));
     }
 }
예제 #3
0
        public ActionResult EditRoles(UserAndRolesDTO paramUserAndRolesDTO)
        {
            try
            {
                if (paramUserAndRolesDTO == null)
                {
                    return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
                }

                string UserName   = paramUserAndRolesDTO.UserName;
                string strNewRole = Convert.ToString(Request.Form["AddRole"]);

                if (strNewRole != "No Roles Found")
                {
                    // Go get the User
                    ApplicationUser user = UserManager.FindByName(UserName);

                    // Put user in role
                    UserManager.AddToRole(user.Id, strNewRole);
                }

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

                UserAndRolesDTO objUserAndRolesDTO =
                    GetUserAndRoles(UserName);

                return(View(objUserAndRolesDTO));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(string.Empty, "Error: " + ex);
                return(View("EditRoles"));
            }
        }
예제 #4
0
        public ActionResult Details(string UserName)
        {
            if (UserName == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            ExpandedUserDTO objExpandedUserDTO = GetUser(UserName);

            if (objExpandedUserDTO == null)
            {
                return(HttpNotFound());
            }
            UserAndRolesDTO objUserAndRolesDTO =
                GetUserAndRoles(UserName);

            //foreach (var item in objUserAndRolesDTO.UserName)
            //{
            //    objExpandedUserDTO.RoleName;
            //}
            // objExpandedUserDTO.Team = (IEnumerable<UserRolesDTO>)objUserAndRolesDTO;
            //var context = new ApplicationDbContext();
            //string UserEmail = context.Users.Find();
            //IdentityDbContext instructor = db..Find(id);
            //if (instructor == null)
            //{
            //    return HttpNotFound();
            //}
            return(View(objExpandedUserDTO));
        }
예제 #5
0
        public void Test2()
        {
            // Arrange
            AdminController    controller     = new AdminController();
            string             UserName       = "******";
            ApplicationUser    user           = _userManager.FindByName(UserName);
            List <UserRoleDTO> colUserRoleDTO =
                (from objRole in _userManager.GetRoles(user.Id)
                 select new UserRoleDTO
            {
                RoleName = objRole,
                UserName = UserName
            }).ToList();
            UserAndRolesDTO objUserAndRolesDTO =
                new UserAndRolesDTO();

            objUserAndRolesDTO.UserName = UserName;
            //objUserAndRolesDTO.colUserRoleDTO = colUserRoleDTO;
            Assert.IsNotNull(objUserAndRolesDTO.UserName);

            //var objExpandedUserDTO = controller.GetUserAndRoles(UserName); OLD
            //var result = objExpandedUserDTO.colUserRoleDTO;



            //// Act
            ////ContentResult operation = controller.UserManager.RemoveFromRoles(UserName, "Sales");

            ////var objExpandedUserDTO = controller.GetUserAndRoles(UserName);
            //var result = objExpandedUserDTO.colUserRoleDTO.Find(r => r.RoleName == "Sales");
            // Assert
            //Assert.AreEqual("Your application description page.", result.ViewBag.Message);
        }
예제 #6
0
        // GET: /Admin/EditRoles/TestUser
        public PartialViewResult EditRoles(string UserName)
        {
            UserName = UserName.ToLower();

            // Check that we have an actual user
            ExpandedUserDTO objExpandedUserDTO = GetUser(UserName);

            UserAndRolesDTO objUserAndRolesDTO =
                GetUserAndRoles(UserName);

            return(PartialView("PartialAdmin/_EditRoles", objUserAndRolesDTO));
        }
예제 #7
0
        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
                ExpandedUserDTO objExpandedUserDTO = GetUser(UserName);

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

                if (UserName.ToLower() ==
                    this.User.Identity.Name.ToLower() && RoleName == "Administrator")
                {
                    ModelState.AddModelError(string.Empty,
                                             "Помилка: Неможливо видалити роль адміністратора для даного користувача.");
                }

                // 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, "Помилка: " + ex);

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

                UserAndRolesDTO objUserAndRolesDTO =
                    GetUserAndRoles(UserName);

                return(View("EditRoles", objUserAndRolesDTO));
            }
        }
예제 #8
0
        // GET: /Admin/User/EditRoles/TestUser
        #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
            ExpandedUserDTO objExpandedUserDTO = GetUser(userName);

            if (objExpandedUserDTO == null)
            {
                return(HttpNotFound());
            }
            UserAndRolesDTO objUserAndRolesDTO =
                GetUserAndRoles(userName);

            return(View(objUserAndRolesDTO));
        }
예제 #9
0
        public ActionResult EditRoles(string UserName)
        {
            if (UserName == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            UserName = UserName.ToLower();

            // Check that we have an actual user of the system
            ExpandedUserDTO objExpandedUserDTO = GetUser(UserName);

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

            // Retrieving a user and all the roles relating to that user
            UserAndRolesDTO objUserAndRolesDTO = GetUserAndRoles(UserName);

            return(View(objUserAndRolesDTO));
        }
예제 #10
0
        //public ActionResult Index(string searchStringUserNameOrEmail)
        public ActionResult Index(string searchStringUserNameOrEmail, string currentFilter, int?page)
        {
            try
            {
                int intPage           = 1;
                int intPageSize       = 5;
                int intTotalPageCount = 0;



                if (searchStringUserNameOrEmail != null) //if no search paramater
                {
                    intPage = 1;
                }
                else
                {
                    if (currentFilter != null)
                    {
                        searchStringUserNameOrEmail = currentFilter; //sets the search
                        intPage = page ?? 1;
                    }
                    else
                    {
                        searchStringUserNameOrEmail = "";
                        intPage = page ?? 1;
                    }
                }

                ViewBag.CurrentFilter = searchStringUserNameOrEmail;

                //Sets up a list of expanded users
                List <ExpandedUserDTO> col_UserDTO = new List <ExpandedUserDTO>();
                int intSkip = (intPage - 1) * intPageSize;

                //gets how many pages their will be
                intTotalPageCount = UserManager.Users
                                    .Where(x => x.UserName.Contains(searchStringUserNameOrEmail))
                                    .Count();

                //sets result var to the results of users found with the search paramas
                var result = UserManager.Users
                             .Where(x => x.UserName.Contains(searchStringUserNameOrEmail))
                             .OrderBy(x => x.UserName)
                             .Skip(intSkip)
                             .Take(intPageSize)
                             .ToList();



                foreach (var item in result)
                {
                    ExpandedUserDTO objUserDTO         = new ExpandedUserDTO();
                    UserAndRolesDTO objUserAndRolesDTO =
                        GetUserAndRoles(item.UserName);
                    objUserDTO.UserName       = item.UserName;
                    objUserDTO.Email          = item.Email;
                    objUserDTO.LockoutEndDate = item.LockoutEndDateUtc;
                    objUserDTO.Roles          = objUserAndRolesDTO.colUserRoleDTO;

                    col_UserDTO.Add(objUserDTO);
                }

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

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

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