using System; using System.Collections.Generic; using System.Data; using System.Data.Entity; using System.Linq; using System.Net; using System.Web; using System.Web.Mvc; using HealthchoiceMVC.Models; using HealthchoiceMVC.Services; using System.Web.Security; using Microsoft.AspNet.Identity; using Microsoft.AspNet.Identity.EntityFramework; using System.Collections; using System.Threading.Tasks; using HealthchoiceMVC.NonDbModels; namespace HealthchoiceMVC.Controllers { [Authorize(Roles = "SiteAdmin,Provider,MCOAdmin,ProviderAdmin")] public class SiteAdminController : Controller { private ApplicationDbContext db = new ApplicationDbContext(); private HCDBEntities entities = new HCDBEntities(); private HCLocalService service = new HCLocalService(); // GET: SiteAdmin public ActionResult Index() { return View(db.Users.ToList()); } // GET: SiteAdmin/Details/5 public ActionResult Details(string id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } ApplicationUser applicationUser = db.Users.Find(id); if (applicationUser == null) { return HttpNotFound(); } return View(applicationUser); } // GET: SiteAdmin/Create public ActionResult Create() { return View(); } // POST: SiteAdmin/Create // To protect from overposting attacks, please enable the specific properties you want to bind to, for // more details see http://go.microsoft.com/fwlink/?LinkId=317598. [HttpPost] [ValidateAntiForgeryToken] public ActionResult Create([Bind(Include = "Id,FirstName,LastName,ContactNumber,AccountType,Email,EmailConfirmed,PasswordHash,SecurityStamp,PhoneNumber,PhoneNumberConfirmed,TwoFactorEnabled,LockoutEndDateUtc,LockoutEnabled,AccessFailedCount,UserName")] ApplicationUser applicationUser) { if (ModelState.IsValid) { db.Users.Add(applicationUser); db.SaveChanges(); return RedirectToAction("Index"); } return View(applicationUser); } // GET: SiteAdmin/Edit/5 public ActionResult Edit(string id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } ApplicationUser applicationUser = db.Users.Find(id); if (applicationUser == null) { return HttpNotFound(); } List roleList = service.GetUserRolesByType(applicationUser.AccountType); //var roleList2 = roleList; var context = new ApplicationDbContext(); var userStore = new UserStore(context); var userManager = new UserManager(userStore); var userRoles = userManager.GetRoles(applicationUser.Id); List rolesToRemove = new List(); string userRoleDesc = ""; foreach (var role in roleList) { foreach (var userRole in userRoles) { userRoleDesc = service.GetUserRoleDescByValue(userRole); if (role.Text == userRoleDesc) { rolesToRemove.Add(role); } } } if (roleList.Count() == 1) //to get rid of the please select { foreach (var role in roleList) { rolesToRemove.Add(role); } } foreach (var role in rolesToRemove) { roleList.Remove(role); } ViewBag.UserRolesList = roleList; ViewBag.UserRoles = userRoles; return View(applicationUser); } // POST: SiteAdmin/Edit/5 // To protect from overposting attacks, please enable the specific properties you want to bind to, for // more details see http://go.microsoft.com/fwlink/?LinkId=317598. [HttpPost] [ValidateAntiForgeryToken] public ActionResult Edit([Bind(Include = "Id,FirstName,LastName,ContactNumber,AccountType,Email,EmailConfirmed,PasswordHash,SecurityStamp,PhoneNumber,PhoneNumberConfirmed,TwoFactorEnabled,LockoutEndDateUtc,LockoutEnabled,AccessFailedCount,UserName")] ApplicationUser applicationUser) { if (ModelState.IsValid) { db.Entry(applicationUser).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(applicationUser); } // GET: SiteAdmin/Delete/5 public ActionResult Delete(string id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } ApplicationUser applicationUser = db.Users.Find(id); if (applicationUser == null) { return HttpNotFound(); } return View(applicationUser); } // POST: SiteAdmin/Delete/5 [HttpPost, ActionName("Delete")] [ValidateAntiForgeryToken] public ActionResult DeleteConfirmed(string id) { ApplicationUser applicationUser = db.Users.Find(id); db.Users.Remove(applicationUser); db.SaveChanges(); return RedirectToAction("Index"); } public ActionResult Register() { PopulateViewBag(); return View(); } private void PopulateViewBag() { var UserTypeList = service.GetUserTypeList(); List UserTypeListFiltered = new List(); List UserRoleList = new List(); UserRoleList.Add(new SelectListItem { Value = "", Text = "-Select User Type First-" }); string UserId = User.Identity.GetUserId(); ApplicationUser applicationUser = db.Users.Find(UserId); if (applicationUser.AccountType==1) //Healthchoice User { foreach (var usertype in UserTypeList) { UserTypeListFiltered.Add(usertype); } } else { //RolePrincipal r = (RolePrincipal)User; //string[] rolesArray = r.GetRoles(); IList userRoles = null; using (var context = new ApplicationDbContext()) { var userStore = new UserStore(context); var userManager = new UserManager(userStore); userRoles = userManager.GetRoles(User.Identity.GetUserId()); } //ArrayList rolesArrayList = new ArrayList(); //foreach(string role in rolesArray) //{ // rolesArrayList.Add(new {role=role}); //} var UserTypeFiltered = (from ut in entities.user_type join ur in entities.user_role on ut.user_type_key equals ur.user_type_key join urr in entities.user_role_by_role_permission on ur.user_role_key equals urr.user_role_key where ut.user_type_key == applicationUser.AccountType select new { Text = ut.user_type_desc, Value = ut.user_type_key.ToString(), }).Distinct().ToList(); UserTypeListFiltered.Add(new SelectListItem { Value = "", Text = "-Please Select-" }); foreach (var usertype in UserTypeFiltered) { UserTypeListFiltered.Add(new SelectListItem { Value = usertype.Value, Text = usertype.Text }); } } ViewBag.UserTypeList = UserTypeListFiltered; ViewBag.UserRoleList = UserRoleList; } // POST: /SiteAdmin/Register [HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public async Task Register(RegisterViewModel model, string UserRole) { if (ModelState.IsValid) { var user = new ApplicationUser { UserName = model.Email, Email = model.Email }; user.FirstName = model.FirstName; user.LastName = model.LastName; user.ContactNumber = model.ContactNumber; user.AccountType = model.AccountType; using (var context = new ApplicationDbContext()) { var userStore = new UserStore(context); var userManager = new UserManager(userStore); var result = await userManager.CreateAsync(user, model.Password); //Add the role string userRole = service.GetUserRoleValueByKey(Convert.ToInt32(UserRole)); await userManager.AddToRoleAsync(user.Id, userRole); return RedirectToAction("Index", "Home"); } } // If we got this far, something failed, redisplay form PopulateViewBag(); return View(model); } protected override void Dispose(bool disposing) { if (disposing) { db.Dispose(); } base.Dispose(disposing); } } }