Skip to content

Commit

Permalink
implemented feature troygoode#14 - can now specify initial roles for …
Browse files Browse the repository at this point in the history
…user when creating them from admin panel
  • Loading branch information
troygoode committed Aug 17, 2011
1 parent 70c1e01 commit 1637768
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,7 @@ public void CreateUser_creates_a_new_user()
{
Username = username,
Password = password,
ConfirmPassword = password,
Email = email,
PasswordQuestion = passwordQuestion,
PasswordAnswer = passwordAnswer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -26,5 +27,8 @@ public class CreateUserViewModel
[StringLength(100)]
[Display(Name = "Secret Answer")]
public string PasswordAnswer { get; set; }

[Display(Name = "Initial Roles")]
public IDictionary<string, bool> InitialRoles { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,22 @@
<div class="mvcMembership-account">
@using(Html.BeginForm("CreateUser", "UserAdministration")){
@Html.ValidationSummary(true)
@Html.EditorForModel()

<fieldset>
@Html.EditorForModel()
</fieldset>

<fieldset>
<h3 class="mvcMembership">Initial Roles</h3>
@for(var i = 0; i < Model.InitialRoles.Count; i++){
var role = Model.InitialRoles.ElementAt(i);
<div>
<input name="InitialRoles[@i].Key" type="hidden" value="@role.Key" />
<label>@Html.CheckBox("InitialRoles[" + i + "].Value", role.Value) @role.Key</label>
</div>
}
</fieldset>

<input type="submit" value="Create" />
}
</div>

0 comments on commit 1637768

Please sign in to comment.