public ActionResult Group_EditLocal(int id)
        {
            U_FunctionGroup _functionGroup;
            if (id == -1)
            {
                _functionGroup = new U_FunctionGroup { Id = -1 };
            }
            else
            {
                _functionGroup = _inFlowDb.U_FunctionGroups.Find(id);
            }

            EditGroupViewModel model = new EditGroupViewModel() { GroupId = _functionGroup.Id, GroupName = _functionGroup.Name };

            _functionGroup.Roles.ToList().ForEach(r => model.Roles.Add(new ListViewItemModel() { Id = r.Id, Name = r.Name, Selected = true }));
            _inFlowDb.U_Roles.ToList().Except(_functionGroup.Roles.ToList()).ToList().ForEach(r => model.Roles.Add(new ListViewItemModel() { Id = r.Id, Name = r.Name, Selected = false }));


            List<string> assignedUsers = new List<string>();
            _functionGroup.Users.ToList().ForEach(u => assignedUsers.Add(u.Username));
            assignedUsers.ForEach(u => model.Users.Add(new ListViewItemModel { Name = u, Selected = true }));

            List<string> allUsers = new List<string>();
            _identityDb.Users.ToList().ForEach(u => allUsers.Add(u.UserName));
            // _inFlowDb.U_User_FunctionGroups.ToList().ForEach(u => allUsers.Add(u.Username));

            allUsers.Except(assignedUsers).ToList().ForEach(u => model.Users.Add(new ListViewItemModel { Name = u, Selected = false }));

            return View("Group_Edit",model);
        }
          public async Task<ActionResult> Group_EditAD(int id)
          {
              U_FunctionGroup _functionGroup;
              if (id == -1)
              {
                  _functionGroup = new U_FunctionGroup { Id = -1 };
              }
              else
              {
                  _functionGroup = _inFlowDb.U_FunctionGroups.Find(id);
              }

              EditGroupViewModel model = new EditGroupViewModel() { GroupId = _functionGroup.Id, GroupName = _functionGroup.Name };

              _functionGroup.Roles.ToList().ForEach(r => model.Roles.Add(new ListViewItemModel() { Id = r.Id, Name = r.Name, Selected = true }));
              _inFlowDb.U_Roles.ToList().Except(_functionGroup.Roles.ToList()).ToList().ForEach(r => model.Roles.Add(new ListViewItemModel() { Id = r.Id, Name = r.Name, Selected = false }));


              List<string> assignedUsers = new List<string>();
              _functionGroup.Users.ToList().ForEach(u => assignedUsers.Add(u.Username));
              assignedUsers.ForEach(u => model.Users.Add(new ListViewItemModel { Name = u, Selected = true }));

              List<string> allUsers = new List<string>();
              List<strICT.InFlow.Web.Models.User> users = (await _graph.getUsers()).ToList<strICT.InFlow.Web.Models.User>();
              users.ForEach(u => allUsers.Add(u.userPrincipalName));
              // _inFlowDb.U_User_FunctionGroups.ToList().ForEach(u => allUsers.Add(u.Username));

              allUsers.Except(assignedUsers).ToList().ForEach(u => model.Users.Add(new ListViewItemModel { Name = u, Selected = false }));

              return View("Group_EditAD", model);
          }
        public ActionResult Group_Edit(EditGroupViewModel model)
        {
            U_FunctionGroup _group;

            if (model.GroupId == -1)
            {
                _group = new U_FunctionGroup() { Name = model.GroupName };
                _inFlowDb.U_FunctionGroups.Add(_group);
                _inFlowDb.SaveChanges();
            }
            else
            {
                _group = _inFlowDb.U_FunctionGroups.Find(model.GroupId);
            }

            if(!_group.Name.Equals(model.GroupName))
            {
                _group.Name = model.GroupName;
            }


            //users
            List<string> oldAssignedUsers = new List<string>();
            List<string> newAssignedUsers = new List<string>();

            if (model.SelectedUsers != null)
            {
                newAssignedUsers = new List<string>(model.SelectedUsers);
            }

            _group.Users.ToList().ForEach(u => oldAssignedUsers.Add(u.Username));

            List<string> usersToAssign = new List<string>(newAssignedUsers.Except(oldAssignedUsers));

            List<string> usersToRemove = new List<string>(oldAssignedUsers.Except(newAssignedUsers));

            usersToAssign.ForEach(username => _inFlowDb.U_User_FunctionGroups.Add(new strICT.InFlow.Db.Contracts.InFlow.U_User_FunctionGroup { FunctionGroup = _group, Username = username }));

            foreach(string username in usersToRemove)
            {
                var item = _inFlowDb.U_User_FunctionGroups.First(fg => fg.Username == username && fg.FunctionGroup_Id == _group.Id);
                _inFlowDb.U_User_FunctionGroups.Remove(item);
            }

            //roles
            List<int> oldAssignedRoles = new List<int>();
            List<int> newAssignedRoles = new List<int>();

            if (model.SelectedRoles != null)
            {
                (new List<string>(model.SelectedRoles)).ForEach(id => newAssignedRoles.Add(Int16.Parse(id)));
            }

            _group.Roles.ToList().ForEach(r => oldAssignedRoles.Add(r.Id));

            List<int> rolesToAssign = new List<int>(newAssignedRoles.Except(oldAssignedRoles));

            List<int> rolesToRemove = new List<int>(oldAssignedRoles.Except(newAssignedRoles));

            rolesToAssign.ForEach(role => _group.Roles.Add(_inFlowDb.U_Roles.Find(role)));

            rolesToRemove.ForEach(role => _group.Roles.Remove(_inFlowDb.U_Roles.Find(role)));

            //save and return
            _inFlowDb.SaveChanges();

            return RedirectToAction("GroupsRoles");
        }