예제 #1
0
파일: FakeData.cs 프로젝트: feanz/Example
        public User CreateUser()
        {
            User item = new User();
            item.UserName = "******";
            item.FirstName = "firstname";
            item.LastName = "LastName";
            item.Email = "*****@*****.**";

            return item;
        }
예제 #2
0
        public void Create_Action_Post_Should_Return_View_When_UnSuccessful()
        {
            // Arrange
            var controller = CreateAccountController();
            controller.ModelState.AddModelError("An error", "Message");

            // Act
            var user = new User();
            var result = controller.Create(user) as ViewResult;

            // Assert
            Assert.IsInstanceOfType(result.ViewData.Model, typeof(User));
        }
예제 #3
0
        public virtual ActionResult Create(User user)
        {
            if (ModelState.IsValid)
            {
                if (Request.Form["RoleIds"].IsNotNull())
                {
                    UpdateUserRoles(Request.Form["RoleIds"].ToArray(), user);
                }

                _repository.InsertOrUpdate(user);
                this.FlashInfo("User Created");
                return RedirectToAction("Index");
            }
            this.FlashWarning("Error creating account");
            ViewBag.PossibleRoles = _repository.GetRoles();
            return View(user);
        }
예제 #4
0
        private void UpdateUserRoles(IEnumerable<string> selectedRoles, User userToUpdate)
        {
            if (selectedRoles.IsNull())
            {
                userToUpdate.Roles.Clear();
                return;
            }

            var selectedRolesHS = new HashSet<string>(selectedRoles);
            var userRoles = new HashSet<int>(userToUpdate.Roles.Select(c => c.Id));
            var allRoles = _repository.GetRoles();
            foreach (var role in allRoles)
            {
                if (selectedRolesHS.Contains(role.Id.ToStringInvariantCulture()))
                {
                    if (!userRoles.Contains(role.Id))
                    {
                        userToUpdate.AddRole(role);
                    }
                }
                else
                {
                    if (userRoles.Contains(role.Id))
                    {
                        userToUpdate.RemoveRole(role);
                    }
                }
            }
        }
예제 #5
0
 private void PopulateAssignedRolesData(User user)
 {
     var allRoles = _repository.GetRoles();
     var userRoles = new HashSet<int>(user.Roles.Select(c => c.Id));
     var viewModel = allRoles.Select(role => new AssignedRole
                                                 {
                                                     RoleID = role.Id,
                                                     RoleName = role.RoleName,
                                                     Assigned = userRoles.Contains(role.Id)
                                                 }).ToList();
     ViewBag.PossibleRoles = viewModel;
 }