Example #1
0
        public static void PrintNodeTree(this IEnterpriseContext context, EnterpriseNode eNode, string indent, bool last)
        {
            var isRoot = string.IsNullOrEmpty(indent);

            Console.WriteLine(indent + (isRoot ? "" : "+-- ") + eNode.DisplayName);
            indent += isRoot ? " " : (last ? "    " : "|   ");
            var subNodes = eNode.Subnodes
                           .Select(x => context.Enterprise.TryGetNode(x, out var node) ? node : null)
                           .Where(x => x != null)
                           .OrderBy(x => x.DisplayName ?? "")
                           .ToArray();

            for (var i = 0; i < subNodes.Length; i++)
            {
                context.PrintNodeTree(subNodes[i], indent, i == subNodes.Length - 1);
            }
        }
Example #2
0
        }// GetNodeIndexNumber

        internal CSinglePermissionSet AddPermissionSet(PolicyLevel pl, NamedPermissionSet ps)
        {
            // Find the policy level we're after
            if (UserNode.MyPolicyLevel == pl)
            {
                return(UserNode.AddPermissionSet(ps));
            }

            else if (MachineNode.MyPolicyLevel == pl)
            {
                return(MachineNode.AddPermissionSet(ps));
            }

            else if (EnterpriseNode.MyPolicyLevel == pl)
            {
                return(EnterpriseNode.AddPermissionSet(ps));
            }

            else
            {
                throw new Exception("I don't know about this policy level");
            }
        }// AddPermissionSet
Example #3
0
        public static async Task EnterpriseTeamCommand(this IEnterpriseContext context, EnterpriseTeamOptions arguments)
        {
            if (arguments.Force)
            {
                await context.Enterprise.PopulateEnterprise();
            }

            if (string.IsNullOrEmpty(arguments.Command))
            {
                arguments.Command = "list";
            }
            if (string.CompareOrdinal(arguments.Command, "list") == 0)
            {
                var teams = context.Enterprise.Teams
                            .Where(x =>
                {
                    if (string.IsNullOrEmpty(arguments.Name))
                    {
                        return(true);
                    }
                    if (arguments.Name == x.Uid)
                    {
                        return(true);
                    }
                    var m = Regex.Match(x.Name, arguments.Name, RegexOptions.IgnoreCase);
                    return(m.Success);
                })
                            .ToArray();
                var tab = new Tabulate(7)
                {
                    DumpRowNo = true
                };
                tab.AddHeader("Team Name", "Team UID", "Node Name", "Restrict Edit", "Restrict Share", "Restrict View", "Users");
                foreach (var team in teams)
                {
                    EnterpriseNode node = null;
                    if (team.ParentNodeId > 0)
                    {
                        context.Enterprise.TryGetNode(team.ParentNodeId, out node);
                    }
                    else
                    {
                        node = context.Enterprise.RootNode;
                    }

                    tab.AddRow(team.Name,
                               team.Uid,
                               node != null ? node.DisplayName : "",
                               team.RestrictEdit ? "X" : "-",
                               team.RestrictSharing ? "X" : "-",
                               team.RestrictView ? "X" : "-",
                               team.Users.Count.ToString());
                }

                tab.Sort(1);
                tab.Dump();
            }
            else
            {
                var team = context.Enterprise.Teams
                           .FirstOrDefault(x =>
                {
                    if (string.IsNullOrEmpty(arguments.Name))
                    {
                        return(true);
                    }
                    if (arguments.Name == x.Uid)
                    {
                        return(true);
                    }
                    return(string.Compare(x.Name, arguments.Name, StringComparison.CurrentCultureIgnoreCase) == 0);
                });
                if (string.CompareOrdinal(arguments.Command, "delete") == 0)
                {
                    if (team == null)
                    {
                        Console.WriteLine($"Team \"{arguments.Name}\" not found");
                        return;
                    }

                    await context.Enterprise.DeleteTeam(team.Uid);
                }
                else if (string.CompareOrdinal(arguments.Command, "view") == 0)
                {
                    if (team == null)
                    {
                        Console.WriteLine($"Team \"{arguments.Name}\" not found");
                        return;
                    }

                    var tab = new Tabulate(2)
                    {
                        DumpRowNo = false
                    };
                    tab.SetColumnRightAlign(0, true);
                    tab.AddRow(" Team Name:", team.Name);
                    tab.AddRow(" Team UID:", team.Uid);
                    tab.AddRow(" Restrict Edit:", team.RestrictEdit ? "Yes" : "No");
                    tab.AddRow(" Restrict Share:", team.RestrictSharing ? "Yes" : "No");
                    tab.AddRow(" Restrict View:", team.RestrictView ? "Yes" : "No");
                    var users = team.Users
                                .Select(x => context.Enterprise.TryGetUserById(x, out var user) ? user.Email : null)
                                .Where(x => !string.IsNullOrEmpty(x))
                                .ToArray();
                    Array.Sort(users);
                    tab.AddRow(" Users:", users.Length > 0 ? users[0] : "");
                    for (var i = 1; i < users.Length; i++)
                    {
                        tab.AddRow("", users[i]);
                    }

                    if (context.Enterprise.TryGetNode(team.ParentNodeId, out var node))
                    {
                        var nodes = context.GetNodePath(node).ToArray();
                        Array.Reverse(nodes);
                        tab.AddRow(" Node:", string.Join(" -> ", nodes));
                    }

                    tab.Dump();
                }
                else if (string.CompareOrdinal(arguments.Command, "update") == 0 || string.CompareOrdinal(arguments.Command, "add") == 0)
                {
                    if (team == null)
                    {
                        if (string.CompareOrdinal(arguments.Command, "update") == 0 ||
                            string.CompareOrdinal(arguments.Command, "view") == 0)
                        {
                            Console.WriteLine($"Team \"{arguments.Name}\" not found");
                            return;
                        }

                        team = new EnterpriseTeam
                        {
                            ParentNodeId = context.Enterprise.RootNode.Id
                        };
                    }
                    else
                    {
                        if (string.CompareOrdinal(arguments.Command, "add") == 0)
                        {
                            Console.WriteLine($"Team with name \"{arguments.Name}\" already exists.\nDo you want to create a new one? Yes/No");
                            var answer = await Program.GetInputManager().ReadLine();

                            if (string.Compare("y", answer, StringComparison.InvariantCultureIgnoreCase) == 0)
                            {
                                answer = "yes";
                            }

                            if (string.Compare(answer, "yes", StringComparison.InvariantCultureIgnoreCase) != 0)
                            {
                                return;
                            }
                        }
                    }

                    team.Name = arguments.Name;
                    if (CliCommands.ParseBoolOption(arguments.RestrictEdit, out var b))
                    {
                        team.RestrictEdit = b;
                    }

                    if (CliCommands.ParseBoolOption(arguments.RestrictShare, out b))
                    {
                        team.RestrictSharing = b;
                    }

                    if (CliCommands.ParseBoolOption(arguments.RestrictView, out b))
                    {
                        team.RestrictView = b;
                    }

                    if (!string.IsNullOrEmpty(arguments.Node))
                    {
                        long?asId = null;
                        if (arguments.Node.All(char.IsDigit))
                        {
                            if (long.TryParse(arguments.Node, out var l))
                            {
                                asId = l;
                            }
                        }

                        var node = context.Enterprise.Nodes
                                   .FirstOrDefault(x =>
                        {
                            if (asId.HasValue && asId.Value == x.Id)
                            {
                                return(true);
                            }
                            return(string.Compare(x.DisplayName, arguments.Node, StringComparison.CurrentCultureIgnoreCase) == 0);
                        });
                        if (node != null)
                        {
                            team.ParentNodeId = node.Id;
                        }
                    }

                    await context.Enterprise.UpdateTeam(team);
                }
                else
                {
                    Console.WriteLine($"Unsupported command \"{arguments.Command}\". Valid commands are  \"list\", \"view\", \"add\", \"delete\", \"update\"");
                }
            }
        }
Example #4
0
        public static IEnumerable <string> GetNodePath(this IEnterpriseContext context, EnterpriseNode node)
        {
            while (true)
            {
                yield return(node.DisplayName);

                if (node.Id <= 0)
                {
                    yield break;
                }
                if (!context.Enterprise.TryGetNode(node.ParentNodeId, out var parent))
                {
                    yield break;
                }
                node = parent;
            }
        }
Example #5
0
        }// AddMenuItems

        internal override void MenuCommand(int iCommandID)
        {
            // All these menu commands are going to require the policy nodes to be created and expanded.
            // Let's check to see if we've done that already....
            if (NumChildren == 0)
            {
                CNodeManager.CNamespace.Expand(HScopeItem);
            }

            if (iCommandID == COMMANDS.NEW_SECURITYPOLICY)
            {
                CNewSecurityPolicyDialog          nspd = new CNewSecurityPolicyDialog();
                System.Windows.Forms.DialogResult dr   = nspd.ShowDialog();
                if (dr == System.Windows.Forms.DialogResult.OK)
                {
                    // To get a 'New' Security policy, we just load a policy from a file that doesn't exist
                    CSecurityPolicy secpolnode = GetSecurityPolicyNode(nspd.SecPolType);

                    // Ok, this is really dumb. I need to create a security file where the user
                    // wants to store the policy before I can tell the Security Manager about it.
                    File.Copy(secpolnode.MyPolicyLevel.StoreLocation, nspd.Filename, true);
                    PolicyLevel pl = SecurityManager.LoadPolicyLevelFromFile(nspd.Filename, nspd.SecPolType);
                    pl.Reset();
                    secpolnode.SetNewSecurityPolicyLevel(pl);
                    secpolnode.SecurityPolicyChanged();
                    // Select the policy node the user just mucked with
                    CNodeManager.SelectScopeItem(secpolnode.HScopeItem);
                }
            }
            else if (iCommandID == COMMANDS.OPEN_SECURITYPOLICY)
            {
                COpenSecurityPolicyDialog ospd = new COpenSecurityPolicyDialog(new String[] { EnterpriseNode.ComputerPolicyFilename,
                                                                                              MachineNode.ComputerPolicyFilename,
                                                                                              UserNode.ComputerPolicyFilename });
                System.Windows.Forms.DialogResult dr = ospd.ShowDialog();
                if (dr == System.Windows.Forms.DialogResult.OK)
                {
                    // Try and load the given security policy
                    PolicyLevel pl;
                    try
                    {
                        pl = SecurityManager.LoadPolicyLevelFromFile(ospd.Filename, ospd.SecPolType);
                    }
                    catch
                    {
                        MessageBox(String.Format(CResourceStore.GetString("CGenSecurity:isNotASecurityFile"), ospd.Filename),
                                   CResourceStore.GetString("CGenSecurity:isNotASecurityFileTitle"),
                                   MB.ICONEXCLAMATION);
                        return;
                    }
                    CSecurityPolicy secpolnode = GetSecurityPolicyNode(ospd.SecPolType);
                    secpolnode.SetNewSecurityPolicyLevel(pl);
                    // Select the policy node the user just mucked with
                    CNodeManager.SelectScopeItem(secpolnode.HScopeItem);
                }
            }
            else if (iCommandID == COMMANDS.EVALUATE_ASSEMBLY)
            {
                CWizard wiz = new CEvalAssemWizard();
                wiz.LaunchWizard(Cookie);
            }
            else if (iCommandID == COMMANDS.TRUST_ASSEMBLY)
            {
                // Let's create a new wizard now to dump any old settings we have
                CFullTrustWizard wiz = new CFullTrustWizard(MachineNode.ReadOnly, UserNode.ReadOnly);

                wiz.LaunchWizard(Cookie);

                // See if it updated anything a codegroup
                if (wiz.MadeChanges)
                {
                    CSecurityPolicy sp = GetSecurityPolicyNode(Security.GetPolicyLevelTypeFromLabel(wiz.PolLevel.Label));
                    sp.RedoChildren();
                    sp.SecurityPolicyChanged();
                }
            }
            else if (iCommandID == COMMANDS.ADJUST_SECURITYPOLICY)
            {
                CSecurityAdjustmentWizard wiz = new CSecurityAdjustmentWizard(MachineNode.ReadOnly, UserNode.ReadOnly);
                wiz.LaunchWizard(Cookie);

                // Check to see if we need to tell any policies that we changed them
                if (wiz.didUserPolicyChange)
                {
                    UserNode.RedoChildren();
                    UserNode.SecurityPolicyChanged();
                }

                if (wiz.didMachinePolicyChange)
                {
                    MachineNode.RedoChildren();
                    MachineNode.SecurityPolicyChanged();
                }
            }
            else if (iCommandID == COMMANDS.CREATE_MSI)
            {
                CWizard wiz = new CCreateDeploymentPackageWizard(this);
                wiz.LaunchWizard(Cookie);
            }
            else if (iCommandID == COMMANDS.RESET_POLICY)
            {
                int nRes = MessageBox(CResourceStore.GetString("CGenSecurity:ConfirmResetAll"),
                                      CResourceStore.GetString("CGenSecurity:ConfirmResetAllTitle"),
                                      MB.YESNO | MB.ICONQUESTION);
                if (nRes == MB.IDYES)
                {
                    if (!EnterpriseNode.ReadOnly)
                    {
                        EnterpriseNode.ResetSecurityPolicy();
                    }
                    if (!MachineNode.ReadOnly)
                    {
                        MachineNode.ResetSecurityPolicy();
                    }
                    if (!UserNode.ReadOnly)
                    {
                        UserNode.ResetSecurityPolicy();
                    }
                    MessageBox(CResourceStore.GetString("CGenSecurity:PoliciesResetAll"),
                               CResourceStore.GetString("CGenSecurity:PoliciesResetAllTitle"),
                               MB.ICONINFORMATION);
                }
            }
        }// MenuCommand