diff --git a/src/SampleWebsite.Mvc3.Tests/UserAdministrationControllerFacts.cs b/src/SampleWebsite.Mvc3.Tests/UserAdministrationControllerFacts.cs index ce31157..f1646d6 100644 --- a/src/SampleWebsite.Mvc3.Tests/UserAdministrationControllerFacts.cs +++ b/src/SampleWebsite.Mvc3.Tests/UserAdministrationControllerFacts.cs @@ -678,6 +678,7 @@ public void CreateUser_creates_a_new_user() { Username = username, Password = password, + ConfirmPassword = password, Email = email, PasswordQuestion = passwordQuestion, PasswordAnswer = passwordAnswer diff --git a/src/SampleWebsite.Mvc3/Areas/MvcMembership/Controllers/UserAdministrationController.cs b/src/SampleWebsite.Mvc3/Areas/MvcMembership/Controllers/UserAdministrationController.cs index 636ece2..2279250 100644 --- a/src/SampleWebsite.Mvc3/Areas/MvcMembership/Controllers/UserAdministrationController.cs +++ b/src/SampleWebsite.Mvc3/Areas/MvcMembership/Controllers/UserAdministrationController.cs @@ -168,7 +168,11 @@ public ViewResult UsersRoles(Guid id) public ViewResult CreateUser() { - return View(); + var model = new CreateUserViewModel + { + InitialRoles = _rolesService.FindAll().ToDictionary(k => k, v => false) + }; + return View(model); } [AcceptVerbs(HttpVerbs.Post)] @@ -189,6 +193,14 @@ public ActionResult CreateUser(CreateUserViewModel createUserViewModel) createUserViewModel.PasswordQuestion, createUserViewModel.PasswordAnswer, true); + + if (createUserViewModel.InitialRoles != null) + { + var rolesToAddUserTo = createUserViewModel.InitialRoles.Where(x => x.Value).Select(x => x.Key); + foreach (var role in rolesToAddUserTo) + _rolesService.AddToRole(user, role); + } + return RedirectToAction("Details", new { id = user.ProviderUserKey }); } catch (MembershipCreateUserException e) diff --git a/src/SampleWebsite.Mvc3/Areas/MvcMembership/Models/UserAdministration/CreateUserViewModel.cs b/src/SampleWebsite.Mvc3/Areas/MvcMembership/Models/UserAdministration/CreateUserViewModel.cs index df44566..9715b99 100644 --- a/src/SampleWebsite.Mvc3/Areas/MvcMembership/Models/UserAdministration/CreateUserViewModel.cs +++ b/src/SampleWebsite.Mvc3/Areas/MvcMembership/Models/UserAdministration/CreateUserViewModel.cs @@ -1,4 +1,5 @@ -using System.ComponentModel.DataAnnotations; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; using DataAnnotationsExtensions; namespace SampleWebsite.Mvc3.Areas.MvcMembership.Models.UserAdministration @@ -26,5 +27,8 @@ public class CreateUserViewModel [StringLength(100)] [Display(Name = "Secret Answer")] public string PasswordAnswer { get; set; } + + [Display(Name = "Initial Roles")] + public IDictionary InitialRoles { get; set; } } } \ No newline at end of file diff --git a/src/SampleWebsite.Mvc3/Areas/MvcMembership/Views/UserAdministration/CreateUser.cshtml b/src/SampleWebsite.Mvc3/Areas/MvcMembership/Views/UserAdministration/CreateUser.cshtml index 49a600e..9e8d169 100644 --- a/src/SampleWebsite.Mvc3/Areas/MvcMembership/Views/UserAdministration/CreateUser.cshtml +++ b/src/SampleWebsite.Mvc3/Areas/MvcMembership/Views/UserAdministration/CreateUser.cshtml @@ -11,7 +11,22 @@
@using(Html.BeginForm("CreateUser", "UserAdministration")){ @Html.ValidationSummary(true) - @Html.EditorForModel() + +
+ @Html.EditorForModel() +
+ +
+

Initial Roles

+ @for(var i = 0; i < Model.InitialRoles.Count; i++){ + var role = Model.InitialRoles.ElementAt(i); +
+ + +
+ } +
+ }
\ No newline at end of file