public ActionResult Create([Bind(Prefix = "authoritygroup")]authoritygroup authoritygroup, string[] managers, FormCollection collection) { using (var transaction = this.vodpointsdb.Database.BeginTransaction()) { //// create new authoritygroup this.vodpointsdb.authoritygroups.Add(authoritygroup); this.vodpointsdb.SaveChanges(); this.vodpointsdb.Entry(authoritygroup).GetDatabaseValues(); var authorityGroupId = authoritygroup.Id; //// create data in managerauthoritygroup table foreach (var manager in managers) { var model = new managerauthoritygroup(); model.AuthorityGroupId = authorityGroupId; model.ManagerId = this.vodpointsdb.managers.Where(a => a.Name == manager).First().Id; bool isExisted = this.vodpointsdb.managerauthoritygroups.Where(a => a.AuthorityGroupId == model.AuthorityGroupId && a.ManagerId == model.ManagerId).Any(); if (!isExisted) { this.vodpointsdb.managerauthoritygroups.Add(model); } } //// create data in manageraccesscontrolchildren table foreach (var key in collection.AllKeys) { var accesscontrolchildrenId = 0; if (int.TryParse(key, out accesscontrolchildrenId) && collection[key] == "True") { foreach (var manager in managers) { var managerId = this.vodpointsdb.managers.Where(a => a.Name == manager).First().Id; bool isExisted = this.vodpointsdb.manageraccesscontrolchildrens.Where(a => a.AccessControlChildrenId == accesscontrolchildrenId && a.ManagerId == managerId).Any(); if (!isExisted) { this.vodpointsdb.manageraccesscontrolchildrens.Add(new manageraccesscontrolchildren { AccessControlChildrenId = accesscontrolchildrenId, Function = 1, ManagerId = managerId, AuthorityGroupId = authorityGroupId }); } } } } this.vodpointsdb.SaveChanges(); transaction.Commit(); } return RedirectToAction("Index"); }
public ActionResult Edit([Bind(Prefix = "authoritygroup")]authoritygroup authoritygroup, string[] managers, FormCollection collection) { using (var transaction = this.vodpointsdb.Database.BeginTransaction()) { //// update authoritygroup information if (ModelState.IsValid) { this.vodpointsdb.Entry(authoritygroup).State = EntityState.Modified; } //// remove data from managerauthoritygroup table var oldManagerIds = this.vodpointsdb.managerauthoritygroups.Where(a => a.AuthorityGroupId == authoritygroup.Id).Select(b => b.ManagerId).ToList(); var newManagerIds = new List<long>(); foreach (var newManager in managers) { newManagerIds.Add(this.vodpointsdb.managers.Where(a => a.Name == newManager).First().Id); } foreach (var oldManagerId in oldManagerIds) { if (newManagerIds.Contains(oldManagerId) == false) { var oldManager = this.vodpointsdb.managerauthoritygroups.Where(a => a.AuthorityGroupId == authoritygroup.Id && a.ManagerId == oldManagerId).FirstOrDefault(); this.vodpointsdb.managerauthoritygroups.Remove(oldManager); var oldManagerAccessControlChildren = this.vodpointsdb.manageraccesscontrolchildrens.Where(a => a.AuthorityGroupId == authoritygroup.Id && a.ManagerId == oldManagerId).FirstOrDefault(); this.vodpointsdb.manageraccesscontrolchildrens.Remove(oldManagerAccessControlChildren); } } //// create data in managerauthoritygroup table foreach (var manager in managers) { var model = new managerauthoritygroup(); model.AuthorityGroupId = authoritygroup.Id; model.ManagerId = this.vodpointsdb.managers.Where(a => a.Name == manager).First().Id; bool isExisted = this.vodpointsdb.managerauthoritygroups.Where(a => a.AuthorityGroupId == model.AuthorityGroupId && a.ManagerId == model.ManagerId).Any(); if (!isExisted) { this.vodpointsdb.managerauthoritygroups.Add(model); } } //// create data in manageraccesscontrolchildren table var accesscontrolchildrenIdToRemove = new HashSet<long>(); foreach (var key in collection.AllKeys) { var accesscontrolchildrenId = 0; if (!int.TryParse(key, out accesscontrolchildrenId)) { continue; } if (collection[key] == "True") { foreach (var manager in managers) { var managerId = this.vodpointsdb.managers.Where(a => a.Name == manager).First().Id; bool isExisted = this.vodpointsdb.manageraccesscontrolchildrens.Where(a => a.AccessControlChildrenId == accesscontrolchildrenId && a.ManagerId == managerId && a.AuthorityGroupId == authoritygroup.Id).Any(); if (!isExisted) { this.vodpointsdb.manageraccesscontrolchildrens.Add(new manageraccesscontrolchildren { AccessControlChildrenId = accesscontrolchildrenId, Function = 1, ManagerId = managerId, AuthorityGroupId = authoritygroup.Id }); } } } else { //// 這些是禁止使用的子功能Id,需要從manageraccesscontrolchildrens資料表中被移除 accesscontrolchildrenIdToRemove.Add(accesscontrolchildrenId); } } //// remove data from manageraccesscontrolchildrens table var managerAccessControlChildrensToRemove = this.vodpointsdb.manageraccesscontrolchildrens.Where(a => a.AuthorityGroupId == authoritygroup.Id && a.Function == 1 && accesscontrolchildrenIdToRemove.Contains(a.AccessControlChildrenId)); this.vodpointsdb.manageraccesscontrolchildrens.RemoveRange(managerAccessControlChildrensToRemove); this.vodpointsdb.SaveChanges(); transaction.Commit(); } return RedirectToAction("Index"); }