public ActionResult ManageUserRoles(int userId) { using (CGWebEntities entities = new CGWebEntities()) { UserProfile selectedProfile = entities.UserProfiles.Where(up => up.UserId.Equals(userId)).Single(); RoleManagementModel returnValue = new RoleManagementModel(); returnValue.UserId = selectedProfile.UserId; foreach (webpages_Roles role in selectedProfile.webpages_Roles) { switch (role.RoleName) { case "Root": returnValue.Root = true; break; case "Administrator": returnValue.Administrator = true; break; case "Moderator": returnValue.Moderator = true; break; case "User": returnValue.User = true; break; case "Announcer": returnValue.Announcer = true; break; } } return View(returnValue); } }
public ActionResult ManageUserRoles(RoleManagementModel model) { using (CGWebEntities entities = new CGWebEntities()) { UserProfile selectedProfile = entities.UserProfiles.Where(up => up.UserId.Equals(model.UserId)).Single(); if (model.Root) { if (!selectedProfile.webpages_Roles.Any(r => r.RoleName.Equals("Root"))) { selectedProfile.webpages_Roles.Add(entities.webpages_Roles.Where(r => r.RoleName.Equals("Root")).Single()); } } else { webpages_Roles roleTest = selectedProfile.webpages_Roles.Where(r => r.RoleName.Equals("Root")).SingleOrDefault(); if (roleTest != null) { selectedProfile.webpages_Roles.Remove(roleTest); } } if (model.Administrator) { if (!selectedProfile.webpages_Roles.Any(r => r.RoleName.Equals("Administrator"))) { selectedProfile.webpages_Roles.Add(entities.webpages_Roles.Where(r => r.RoleName.Equals("Administrator")).Single()); } } else { webpages_Roles roleTest = selectedProfile.webpages_Roles.Where(r => r.RoleName.Equals("Administrator")).SingleOrDefault(); if (roleTest != null) { selectedProfile.webpages_Roles.Remove(roleTest); } } if (model.Moderator) { if (!selectedProfile.webpages_Roles.Any(r => r.RoleName.Equals("Moderator"))) { selectedProfile.webpages_Roles.Add(entities.webpages_Roles.Where(r => r.RoleName.Equals("Moderator")).Single()); } } else { webpages_Roles roleTest = selectedProfile.webpages_Roles.Where(r => r.RoleName.Equals("Moderator")).SingleOrDefault(); if (roleTest != null) { selectedProfile.webpages_Roles.Remove(roleTest); } } if (model.User) { if (!selectedProfile.webpages_Roles.Any(r => r.RoleName.Equals("User"))) { selectedProfile.webpages_Roles.Add(entities.webpages_Roles.Where(r => r.RoleName.Equals("User")).Single()); } } else { webpages_Roles roleTest = selectedProfile.webpages_Roles.Where(r => r.RoleName.Equals("User")).SingleOrDefault(); if (roleTest != null) { selectedProfile.webpages_Roles.Remove(roleTest); } } if (model.Announcer) { if (!selectedProfile.webpages_Roles.Any(r => r.RoleName.Equals("Announcer"))) { selectedProfile.webpages_Roles.Add(entities.webpages_Roles.Where(r => r.RoleName.Equals("Announcer")).Single()); } } else { webpages_Roles roleTest = selectedProfile.webpages_Roles.Where(r => r.RoleName.Equals("Announcer")).SingleOrDefault(); if (roleTest != null) { selectedProfile.webpages_Roles.Remove(roleTest); } } try { entities.SaveChanges(); ViewBag.SaveMessage = "Save Completed"; } catch (Exception ex) { ViewBag.SaveMessage = String.Format("There was a problem saving : {0}", ex.Message); } return View("ManageUserRoles", model); } }