public ActionResult MenuRoleDatabase(FormCollection form) { using (Trainee15Entities db = new Trainee15Entities()) { var roleID = int.Parse(form["selectRole"]); ExtendedRole viewModel = new ExtendedRole(); try { // Checkbox is the attribute name. // Returns values of the boxes that have been checked as csv. string[] checkboxes = form["checkbox"].Split(','); var menus = db.Menus.ToList(); var role = db.Roles.SingleOrDefault(x => x.roleID == roleID); foreach (var menu in menus) { // Add to menu to role. if (checkboxes.Contains(menu.menuID.ToString())) { if (!menu.Roles.Contains(role)) { db.Menus.SingleOrDefault(x => x.menuID == menu.menuID).Roles.Add(role); db.SaveChanges(); } } // Remove menu from role. if (!checkboxes.Contains(menu.menuID.ToString())) { if (menu.Roles.Contains(role)) { db.Roles.SingleOrDefault(x => x.roleID == role.roleID).Menus.Remove(menu); db.SaveChanges(); } } } viewModel.rolesList = db.Roles.ToList(); List <Role> list = new List <Role>(); viewModel.rolesTableList = list; TempData["Message"] = " Role - Menu updated."; return(View("RoleMaintenance", viewModel)); } catch (Exception e) // Catch exception when no item from table was selected. { viewModel.role = db.Roles.SingleOrDefault(x => x.roleID == roleID); viewModel.menu = db.Menus.ToList(); viewModel.menuRole = viewModel.role.Menus.ToList(); TempData["Message"] = "Error: No item was selected from table."; return(View("MenuRole", viewModel)); } } }
public ActionResult DeleteMenu(FormCollection form) { using (Trainee15Entities db = new Trainee15Entities()) { ExtendedMenu menu = new ExtendedMenu(); try { // Checkbox is the attribute name. // Returns values of the boxes that have been checked as csv. string[] checkboxes = form["checkbox"].Split(','); foreach (var menuID in checkboxes) { int iMenuID = Int32.Parse(menuID); Menu deleteMenu = db.Menus.SingleOrDefault(x => x.menuID == iMenuID); db.Menus.Remove(deleteMenu); db.SaveChanges(); } menu.menuList = db.Menus.ToList(); menu.menuTableList = new List <Menu>(); TempData["Message"] = "One or more user has been successfully deleted!"; return(View("MenuMaintenance", menu)); } catch (Exception e) // Catch exception when no item from table was selected. { menu.menuList = db.Menus.ToList(); menu.menuTableList = new List <Menu>(); TempData["Message"] = "Error: No item was selected from table."; return(View("MenuMaintenance", menu)); } } }
public ActionResult DeleteUser(FormCollection form) { using (Trainee15Entities db = new Trainee15Entities()) { ExtendedUser users = new ExtendedUser(); try { // Checkbox is the attribute name. // Returns values of the boxes that have been checked as csv. string[] checkboxes = form["checkbox"].Split(','); foreach (var user in checkboxes) { int iUserID = Int32.Parse(user); User deletUser = db.Users.SingleOrDefault(x => x.userID == iUserID); db.Users.Remove(deletUser); db.SaveChanges(); } users.userList = db.Users.ToList(); users.roleList = db.Roles.Where(x => x.status == true).ToList(); users.userTableList = new List <User>(); TempData["Message"] = "One or more user has been successfully deleted!"; return(View("UserMaintenance", users)); } catch (Exception e) // Catch exception when no item from table was selected. { users.userList = db.Users.ToList(); users.roleList = db.Roles.Where(x => x.status == true).ToList(); users.userTableList = new List <User>(); TempData["Message"] = "Error: No item was selected from table."; return(View("UserMaintenance", users)); } } }
public ActionResult ModifyUserDatabase(FormCollection form) { using (Trainee15Entities db = new Trainee15Entities()) { int sessionUserID = (int)Session["userID"]; var sessionUser = db.Accounts.SingleOrDefault(x => x.userID == sessionUserID); var userID = form["selectUser"]; var roleID = form["selectRole"]; var selectStatus = form["selectStatus"]; int iUserID = int.Parse(userID); int iRoleID = int.Parse(roleID); User user = db.Users.SingleOrDefault(x => x.userID == iUserID); user.roleID = iRoleID; user.status = false; if (selectStatus == "1") { user.status = true; } DateTime curretDateTime = DateTime.Now; user.modifyDate = curretDateTime; user.modifyUser = sessionUser.username; db.SaveChanges(); ExtendedUser users = new ExtendedUser(); users.userList = db.Users.ToList(); users.roleList = db.Roles.Where(x => x.status == true).ToList(); users.userTableList = new List <User>(); TempData["Message"] = "Role has been successfully modified!"; return(View("UserMaintenance", users)); } }
public ActionResult Save(string encodedHtml) { using (Trainee15Entities db = new Trainee15Entities()) { System.Diagnostics.Debug.WriteLine("--> " + encodedHtml); string myDecodedString = Uri.UnescapeDataString(encodedHtml); System.Diagnostics.Debug.WriteLine("--> " + myDecodedString); var userID = (int)Session["userID"]; Home editor = new Home(); if (db.Homes.SingleOrDefault(x => x.userID == userID) != null) { editor = db.Homes.SingleOrDefault(x => x.userID == userID); editor.home1 = myDecodedString; } else { editor.userID = userID; editor.home1 = myDecodedString; db.Homes.Add(editor); } db.SaveChanges(); return(Json(new { newUrl = Url.Action("Index", "Home") }, JsonRequestBehavior.AllowGet)); } }
public ActionResult ChangePassword(ExtendedAccount acc) { using (Trainee15Entities db = new Trainee15Entities()) { using (MD5 md5Hash = MD5.Create()) { string hash = Encryption.GetMd5Hash(md5Hash, acc.password); var userDetails = db.Accounts.SingleOrDefault(x => x.username == acc.username && x.password == hash); if (userDetails == null) { HttpCookie cookie = Request.Cookies["Profile"]; if (cookie != null) { ViewBag.username = cookie["username"].ToString(); } TempData["Message"] = "Wrong Username or Password."; return(View("ChangePassword")); } else { userDetails.password = hash; db.SaveChanges(); TempData["Message"] = "Password changed successfully!"; return(View("ChangePassword")); } } } }
// Database function to add and modify records accordingly. public void Database(FormCollection form, bool action) { using (Trainee15Entities db = new Trainee15Entities()) { int sessionUserID = (int)Session["userID"]; var sessionUser = db.Accounts.SingleOrDefault(x => x.userID == sessionUserID); Role role = new Role(); if (action) // Add role. { role.roleName = form["roleName"]; } if (!action) // Modify role. { var selectRole = form["selectRole"]; int iSelectRole = int.Parse(selectRole); role = db.Roles.SingleOrDefault(x => x.roleID == iSelectRole); } role.description = form["description"]; var selectStatus = form["selectStatus"]; role.status = false; if (selectStatus == "1") { role.status = true; } DateTime curretDateTime = DateTime.Now; if (action) // Add role { role.createDate = curretDateTime; role.createUser = sessionUser.username; } role.modifyDate = curretDateTime; role.modifyUser = sessionUser.username; if (action) // Add role { db.Roles.Add(role); } db.SaveChanges(); } }
public ActionResult AddUser(FormCollection form) { using (Trainee15Entities db = new Trainee15Entities()) { int sessionUserID = (int)Session["userID"]; var sessionUser = db.Accounts.SingleOrDefault(x => x.userID == sessionUserID); var username = form["username"]; var roleId = form["selectRole"]; int iRoleID = int.Parse(roleId); var selectStatus = form["selectStatus"]; DateTime curretDateTime = DateTime.Now; Account newAccount = new Account(); newAccount.username = username; string newPassword = username + "12345"; using (MD5 md5Hash = MD5.Create()) { string hash = Encryption.GetMd5Hash(md5Hash, newPassword); newAccount.password = hash; db.Accounts.Add(newAccount); } User addUser = new User(); addUser.username = username; addUser.roleID = iRoleID; addUser.status = false; if (selectStatus == "1") { addUser.status = true; } addUser.createDate = curretDateTime; addUser.createUser = sessionUser.username; addUser.modifyDate = curretDateTime; addUser.modifyUser = sessionUser.username; db.Users.Add(addUser); db.SaveChanges(); ExtendedUser users = new ExtendedUser(); users.userList = db.Users.ToList(); users.roleList = db.Roles.Where(x => x.status == true).ToList(); users.userTableList = new List <User>(); TempData["Message"] = "A new user has been successfully added!"; return(View("UserMaintenance", users)); } }
public ActionResult DeleteRole(FormCollection form) { using (Trainee15Entities db = new Trainee15Entities()) { ExtendedRole viewModel = new ExtendedRole(); try { // Checkbox is the attribute name. // Returns values of the boxes that have been checked as csv. string[] checkboxes = form["checkbox"].Split(','); foreach (var role in checkboxes) { int iRoleID = Int32.Parse(role); // Delete role-menu relationship first before deleting roles. List <Menu> dropMenus = db.Roles.SingleOrDefault(r => r.roleID == iRoleID).Menus.ToList(); foreach (var drop in dropMenus) { db.Roles.SingleOrDefault(r => r.roleID == iRoleID).Menus.Remove(drop); } Role deleteRole = db.Roles.SingleOrDefault(r => r.roleID == iRoleID); db.Roles.Remove(deleteRole); db.SaveChanges(); } viewModel.rolesList = db.Roles.ToList(); List <Role> list = new List <Role>(); viewModel.rolesTableList = list; TempData["Message"] = "One or more role has been successfully deleted!"; return(View("RoleMaintenance", viewModel)); } catch (Exception e) // Catch exception when no item from table was selected. { viewModel.rolesList = db.Roles.ToList(); List <Role> list = new List <Role>(); viewModel.rolesTableList = list; TempData["Message"] = "Error: No item was selected from table."; return(View("RoleMaintenance", viewModel)); } } }
public ActionResult AddMenu(FormCollection form) { using (Trainee15Entities db = new Trainee15Entities()) { var level = form["selectLinkType"]; var title = form["title"]; var linkType = form["selectLinkType"]; var subMenu = form["selectSubMenu"]; var linkUrl = form["linkUrl"]; var status = form["selectStatus"]; Menu menu = new Menu(); // Determine the new menu number. if (linkType == "1") { menu.linkType = "Menu"; // Get new main menu number. menu.menuNo = MenuNo(form["selectLinkType"], form["selectSubMenu"]); } if (linkType == "2") { if (subMenu != null) { menu.linkType = "Program"; // Get new sub menu number. menu.menuNo = MenuNo(form["selectLinkType"], form["selectSubMenu"]); } else { TempData["Message"] = "Error: Sub-menu category was not selected."; return(View("MenuMaintenance", "SystemSetup")); } } menu.level = int.Parse(level); menu.title = title; if (linkUrl != null) // Optional { menu.linkUrl = linkUrl; } else { menu.linkUrl = ""; } menu.status = false; if (status == "1") { menu.status = true; } db.Menus.Add(menu); db.SaveChanges(); ExtendedMenu menus = new ExtendedMenu(); menus.menuList = db.Menus.ToList(); menus.menuTableList = new List <Menu>(); return(View("MenuMaintenance", menus)); } }
// To modify and update the menu table. public ActionResult ModifyMenuDatabase(FormCollection form) { using (Trainee15Entities db = new Trainee15Entities()) { // Menu object to store and update database. Menu updateMenu = new Menu(); ExtendedMenu menus = new ExtendedMenu(); int menuID = int.Parse(form["menuID"]); // Get data from database and assign it to object. updateMenu = db.Menus.SingleOrDefault(x => x.menuID == menuID); if (form["selectLinkType"] == "1") //Main Menu { if (updateMenu.linkType != "Menu") // Checks if menu isn't already a menu. { updateMenu.linkType = "Menu"; // Update menu number if changed from sub menu to main menu. updateMenu.menuNo = MenuNo(form["selectLinkType"], form["selectSubMenu"]); } } if (form["selectLinkType"] == "2") // Sub Menu { if (form["selectSubMenu"] != null) { if (updateMenu.linkType != "Program") // Checks if menu isn't already sub menu. { updateMenu.linkType = "Program"; // Update menu number if changed from main menu to sub menu. updateMenu.menuNo = MenuNo(form["selectLinkType"], form["selectSubMenu"]); } else // If the menu is already a program. { // Compare the menu category from database to the selected menu category. if (string.CompareOrdinal(updateMenu.menuNo, 0, form["selectSubMenu"], 0, 3) != 0) { // Update menu number if menu changed to a different menu category. updateMenu.menuNo = MenuNo(form["selectLinkType"], form["selectSubMenu"]); } } } else { menus.menuList = db.Menus.ToList(); menus.menuTableList = new List <Menu>(); TempData["Message"] = "Error: Sub-menu category was not selected."; return(View("MenuMaintenance", menus)); } } updateMenu.level = int.Parse(form["selectLinkType"]); updateMenu.title = form["title"]; if (form["linkUrl"] != null) // Optional { updateMenu.linkUrl = form["linkUrl"]; } else { updateMenu.linkUrl = ""; } updateMenu.status = false; if (form["selectStatus"] == "1") { updateMenu.status = true; } db.SaveChanges(); menus.menuList = db.Menus.ToList(); menus.menuTableList = new List <Menu>(); TempData["Message"] = "Menu updated."; return(View("MenuMaintenance", menus)); } }