private void listUserTabUserGroups_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (_isFillingList || SelectedUser == null)
            {
                return;
            }
            if (listUserTabUserGroups.SelectedItems.OfType <ODBoxItem <UserGroup> >().Select(x => x.Tag.UserGroupNum).Count() == 0)
            {
                MsgBox.Show(this, "A user must have at least one User Group attached.");
                RefreshUserTabGroups();                 //set the groups back to what they were before.
                return;
            }
            List <long> listSelectedUserUserGroupsOld = SelectedUser.GetGroups(IsForCEMT).Select(x => x.UserGroupNum).ToList();
            List <long> listSelectedUserUserGroups    = listUserTabUserGroups.SelectedTags <UserGroup>().Select(x => x.UserGroupNum).ToList();

            if (           //Current selected groups do not contain SecurityAdmin permission
                GroupPermissions.GetForUserGroups(listSelectedUserUserGroups, Permissions.SecurityAdmin).Count == 0
                //Selected user had SecurityAdmin permission before new selections
                && GroupPermissions.GetForUserGroups(listSelectedUserUserGroupsOld, Permissions.SecurityAdmin).Count > 0)
            {
                //The SelectedUser is no longer part of SecurityAdmin group. Check that at least one other user is part of a SecurityAdmin Group.
                if (!Userods.IsSomeoneElseSecurityAdmin(SelectedUser))
                {
                    MsgBox.Show(this, Lan.g(this, "At least one user must have Security Admin permission."));
                    RefreshUserTabGroups();                     //set the groups back to what they were before.
                    return;
                }
            }
            if (UserGroupAttaches.SyncForUser(SelectedUser, listSelectedUserUserGroups) != 0)
            {
                UserGroupAttaches.RefreshCache();                //only refreshes local cache.
            }
            RefreshUserTree();
        }
 private void InitializeOnStartup()
 {
     _listUserGroups = UserGroups.GetList();
     comboUserGroup.Items.AddList(_listUserGroups, x => x.Description);
     comboUserGroup.SetSelectedKey <UserGroup>(_userGroupNum, x => x.UserGroupNum);
     if (comboUserGroup.SelectedIndex == -1)
     {
         comboUserGroup.SelectedIndex = 0;
     }
     _listGroupPermissions    = GroupPermissions.GetForUserGroups(_listUserGroups.Select(x => x.UserGroupNum).ToList(), Permissions.DashboardWidget);
     _listGroupPermissionsOld = _listGroupPermissions.Select(x => x.Copy()).ToList();
 }
        ///<summary>A recursive function that sets the checkbox for a node.  Also sets the text for the node.</summary>
        private void FillNodes(TreeNode node, List <long> listUserGroupNums)
        {
            //first, any child nodes
            for (int i = 0; i < node.Nodes.Count; i++)
            {
                FillNodes(node.Nodes[i], listUserGroupNums);
            }
            //then this node
            if (node.ImageIndex == 0)
            {
                return;
            }
            node.ImageIndex = 1;
            node.Text       = GroupPermissions.GetDesc((Permissions)node.Tag);
            //get all grouppermissions for the passed-in usergroups
            List <GroupPermission> listGroupPerms            = GroupPermissions.GetForUserGroups(listUserGroupNums);
            List <GroupPermission> listBaseReportingPerms    = listGroupPerms.Where(x => x.PermType == Permissions.Reports && x.FKey == 0).ToList();
            List <GroupPermission> listDisplayReportingPerms = listGroupPerms.Where(x => x.PermType == Permissions.Reports && x.FKey != 0).ToList();

            //group by permtype, preferring newerdays/newerdate that are further back in the past.
            listGroupPerms = listGroupPerms.GroupBy(x => x.PermType)
                             .Select(x => x
                                     .OrderBy((GroupPermission y) => {
                if (y.NewerDays == 0 && y.NewerDate == DateTime.MinValue)
                {
                    return(DateTime.MinValue);
                }
                if (y.NewerDays == 0)
                {
                    return(y.NewerDate);
                }
                return(DateTimeOD.Today.AddDays(-y.NewerDays));
            }).FirstOrDefault())
                             .ToList();
            //display the correct newerdays/newerdate that was found for each permission.
            for (int i = 0; i < listGroupPerms.Count; i++)
            {
                if (listUserGroupNums.Contains(listGroupPerms[i].UserGroupNum) &&
                    listGroupPerms[i].PermType == (Permissions)node.Tag)
                {
                    node.ImageIndex = 2;
                    if (listGroupPerms[i].NewerDate.Year > 1880)
                    {
                        node.Text += " (" + Lan.g(this, "if date newer than") + " " + listGroupPerms[i].NewerDate.ToShortDateString() + ")";
                    }
                    else if (listGroupPerms[i].NewerDays > 0)
                    {
                        node.Text += " (" + Lan.g(this, "if days newer than") + " " + listGroupPerms[i].NewerDays.ToString() + ")";
                    }
                }
            }
            //Special case for Reports permission.
            //Get a list of all report permissions from usergroups that this user is associated to IF the usergroup has the "base" (FKey = 0) report permission.
            if ((Permissions)node.Tag == Permissions.Reports)
            {
                List <GroupPermission> listReportPermsForUser = listDisplayReportingPerms.FindAll(x => listUserGroupNums.Contains(x.UserGroupNum)).ToList();
                listReportPermsForUser.RemoveAll(x => !listBaseReportingPerms.Select(y => y.UserGroupNum).Contains(x.UserGroupNum));
                int state = DisplayReports.GetReportState(listReportPermsForUser);
                if (state == 1)
                {
                    node.ImageIndex = 2;                  //Checked
                }
                else if (state == 0)
                {
                    node.ImageIndex = 3;                  //Partially Checked
                }
                else
                {
                    node.ImageIndex = 1;                  //Unchecked
                }
            }
        }