private static void SaveToDB(User user, List <string> groups) { DBEntities context = COREobject.i.Context; User dbUser = context.Users.SingleOrDefault(u => u.UserName == user.UserName); // update user if (dbUser != null) { dbUser.Update(user); context.ADgroup_Users.RemoveRange(context.ADgroup_Users.Where(adu => adu.UserId == dbUser.Id)); } // new user else { dbUser = user; context.Users.Add(dbUser); } // groups foreach (string groupName in groups) { ADgroup group = context.ADgroups.SingleOrDefault(g => g.Name == groupName); if (group == null) { group = new ADgroup { Name = groupName, Application = context.Applications.SingleOrDefault(a => a.Name == groupName) }; } context.ADgroup_Users.Add(new ADgroup_User { User = dbUser, ADgroup = group }); } // save context.SaveChanges(); }
// GET: Roles public ActionResult App(int Id) { ViewBag.Saved = false; DBEntities masterContext = COREobject.i.Context; Application masterApp = masterContext.Applications.Find(Id); DBEntities context = COREobject.i.AppContext; Application app = masterApp.similarApp; if (app == null) { app = new Application(); app.CopyPropertiesFrom(masterApp, skip: new string[] { "Id" }); context.Applications.Add(app); context.SaveChanges(); } #region Basic variables declaration List <ColumnHeaderAppRolesForTable> colHeaders = new List <ColumnHeaderAppRolesForTable>(); List <RowHeaderAppRolesForTable> rowHeaders = new List <RowHeaderAppRolesForTable>(); List <bool[]> data = new List <bool[]>(); //bool[] jsou sloupce, List jsou radky #endregion #region Rows headers if (masterApp.IsAllowedForAll) { foreach (User u in masterContext.Users) { rowHeaders.Add(new RowHeaderAppRolesForTable(u.Id, u.DisplayName)); } } else { //find ad_group => ADgroup adg = masterContext.ADgroups.SingleOrDefault(i => i.ApplicationId == Id); if (adg != null) { foreach (ADgroup_User adgu in adg.ADgroup_Users) { rowHeaders.Add(new RowHeaderAppRolesForTable(adgu.User.Id, adgu.User.DisplayName)); } } } #endregion #region Column headers + data IQueryable <PersonaAppRole> roles = context.AppRoles.Where(c => c.ApplicationId == app.Id); int x = 0; foreach (PersonaAppRole role in roles) { #region Data column prepare //Creating a column length of rowHeaders.Count bool[] boolColumn = new bool[rowHeaders.Count]; data.Add(boolColumn); #endregion colHeaders.Add(new ColumnHeaderAppRolesForTable(role.Id, role.Name, role.Priority)); #region Data List <int> MemberList = role.getUsers_roles(masterContext).Select(u => u.UserId).ToList(); for (int y = 0; y < MemberList.Count; y++) { int currID = MemberList[y]; int index = 0; for (; index < rowHeaders.Count; index++) { if (currID == rowHeaders[index].Id) { break; } if (index == rowHeaders.Count + 1) { throw new IndexOutOfRangeException("There is no user with this ID"); } } if (index == rowHeaders.Count) { data[x][index - 1] = true; } else { data[x][index] = true; } } #endregion x++; } #endregion AjaxPersonaAppRolesForTable model = new AjaxPersonaAppRolesForTable(colHeaders, rowHeaders, data); model.AppName = app.DisplayName; model.AppID = app.Id; return(View("App", model)); }