From 1b15a3490644120816053d25089c705ae43d4aac Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 22 May 2017 21:19:17 -0400 Subject: [PATCH] addusertorole --- Project-Unite/Controllers/AdminController.cs | 36 ++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/Project-Unite/Controllers/AdminController.cs b/Project-Unite/Controllers/AdminController.cs index 8289072..701110f 100644 --- a/Project-Unite/Controllers/AdminController.cs +++ b/Project-Unite/Controllers/AdminController.cs @@ -27,6 +27,42 @@ public ActionResult Index(string id = "home") return View(); } + public ActionResult AddUserToRole(string id) + { + var model = new AddUserToRoleViewModel(); + model.Roles = new List(); + var db = new ApplicationDbContext(); + foreach(var r in db.Roles.ToArray()) + { + var converted = r as Role; + model.Roles.Add(new SelectListItem + { + Text = converted.Name, + Value = converted.Id + }); + } + model.Users = new List(); + foreach(var u in db.Users.OrderBy(x => x.DisplayName).ToArray()) + { + model.Users.Add(new SelectListItem + { + Text = u.DisplayName, + Value = u.Id + }); + } + model.RoleId = id; + return View(model); + } + + [HttpPost] + [ValidateAntiForgeryToken] + public ActionResult AddUserToRole(AddUserToRoleViewModel model) + { + var usermanager = HttpContext.GetOwinContext().Get(); + usermanager.AddToRole(model.Username, model.RoleId); + return Index("roles"); + } + public ActionResult RoleDetails(string id) { var db = new ApplicationDbContext();