Ability to add user to role

This commit is contained in:
Michael 2017-03-26 12:21:39 -04:00
parent ec06cd1752
commit f8808ebe13
4 changed files with 42 additions and 5 deletions

View file

@ -548,21 +548,50 @@ This user has been anonymized by an administrator.";
} }
// GET: Admin/AddUserToRole // GET: Admin/AddUserToRole
public ActionResult AddUserToRole() public ActionResult AddUserToRole(string id = "")
{ {
ViewBag.Admin = true; ViewBag.Admin = true;
var model = new AddUserToRoleViewModel();
if (id == "")
id = "user";
model.RoleId = id;
model.Users = new List<SelectListItem>();
foreach (var u in db.Users.ToArray())
{
var user = u as ApplicationUser;
model.Users.Add(new SelectListItem
{
Value = user.Id,
Text = user.DisplayName
});
}
model.Roles = new List<SelectListItem>();
foreach(var role in db.Roles.ToArray())
{
var r = role as Role;
model.Roles.Add(new SelectListItem
{
Value = r.Id,
Text = r.Name
});
}
return View(new AddUserToRoleViewModel()); return View(new AddUserToRoleViewModel());
} }
// POST: Admin/AddUserToRole // POST: Admin/AddUserToRole
[HttpPost] [HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> AddUserToRole(AddUserToRoleViewModel model) public async Task<ActionResult> AddUserToRole(AddUserToRoleViewModel model)
{ {
var r = db.Roles.FirstOrDefault(role => role.Name == model.RoleId); var r = db.Roles.FirstOrDefault(role => role.Id == model.RoleId);
var user = db.Users.FirstOrDefault(u => u.DisplayName == model.Username); var user = db.Users.FirstOrDefault(u => u.DisplayName == model.Username);
var uMan = HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>(); var uMan = HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
var isInRole = await uMan.IsInRoleAsync(user.Id, r.Id); var isInRole = await uMan.IsInRoleAsync(user.Id, r.Name);
if(isInRole) if(isInRole)
{ {

View file

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Linq; using System.Linq;
using System.Web; using System.Web;
using System.Web.Mvc;
namespace Project_Unite.Models namespace Project_Unite.Models
{ {
@ -13,5 +14,8 @@ namespace Project_Unite.Models
[DisplayName("Role")] [DisplayName("Role")]
public string RoleId { get; set; } public string RoleId { get; set; }
public List<SelectListItem> Roles { get; set; }
public List<SelectListItem> Users { get; set; }
} }
} }

View file

@ -11,11 +11,11 @@
<table class="table-condensed"> <table class="table-condensed">
<tr> <tr>
<td>@Html.DisplayNameFor(model => model.Username)</td> <td>@Html.DisplayNameFor(model => model.Username)</td>
<td>@Html.TextBoxFor(model => model.Username)</td> <td>@Html.DropdownListFor(model => model.Username, Model.Users, new { @class = "form-control" })</td>
</tr> </tr>
<tr> <tr>
<td>@Html.DisplayNameFor(model => model.RoleId)</td> <td>@Html.DisplayNameFor(model => model.RoleId)</td>
<td>@Html.TextBoxFor(model => model.RoleId)</td> <td>@Html.DropDownListFor(model => model.RoleId, Model.Roles, new { @class = "form-control" })</td>
</tr> </tr>
</table> </table>

View file

@ -20,6 +20,10 @@
<p>Below is a list of users in this role.</p> <p>Below is a list of users in this role.</p>
<ul class="nav nav-pills">
<li><a href="@Url.Action("AddUserToRole", new { id = Model.Id })"><span class="glyphicon glyphicon-plus"></span> Add user to role</a></li>
</ul>
<table class="table"> <table class="table">
<tr> <tr>
<th style="width:65%">User</th> <th style="width:65%">User</th>