From ec2d4dc56bf81b26df945006744175423d1e9fea Mon Sep 17 00:00:00 2001 From: Ylian Saint-Hilaire Date: Wed, 29 Apr 2020 12:30:29 -0700 Subject: [PATCH] Added proper user group handling. --- DeviceUserControl.cs | 7 ++-- MainForm.Designer.cs | 24 +++++++------- MainForm.cs | 66 ++++++++++++++++++++++--------------- MainForm.resx | 6 ++-- MeshCentralServer.cs | 78 ++++++++++++++++++++++++++++++++++++++++++-- NodeClass.cs | 1 + 6 files changed, 135 insertions(+), 47 deletions(-) diff --git a/DeviceUserControl.cs b/DeviceUserControl.cs index 3cd8234..c9b7b82 100644 --- a/DeviceUserControl.cs +++ b/DeviceUserControl.cs @@ -58,9 +58,10 @@ namespace MeshCentralRouter } // Compute rights on this device - ulong rights = node.rights; - if (mesh != null) { rights |= mesh.rights; } - + ulong rights = node.rights; // Direct device rights + if (mesh != null) { rights |= mesh.rights; } // Device group rights + foreach (string i in node.links.Keys) { if (parent.meshcentral.userGroups.ContainsKey(i)) { rights |= node.links[i]; } } // Take a look at group rights + // Must have remote control rights if ((rights & 8) != 0) { diff --git a/MainForm.Designer.cs b/MainForm.Designer.cs index 5541565..e83a126 100644 --- a/MainForm.Designer.cs +++ b/MainForm.Designer.cs @@ -50,6 +50,7 @@ this.pictureBox2 = new System.Windows.Forms.PictureBox(); this.tabPage2 = new System.Windows.Forms.TabPage(); this.panel2 = new System.Windows.Forms.Panel(); + this.smsTokenButton = new System.Windows.Forms.Button(); this.tokenEmailSentLabel = new System.Windows.Forms.Label(); this.emailTokenButton = new System.Windows.Forms.Button(); this.label3 = new System.Windows.Forms.Label(); @@ -102,7 +103,6 @@ this.openToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.toolStripMenuItem1 = new System.Windows.Forms.ToolStripSeparator(); this.exitToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); - this.smsTokenButton = new System.Windows.Forms.Button(); this.panel5.SuspendLayout(); this.mainPanel.SuspendLayout(); this.mainTabControl.SuspendLayout(); @@ -379,6 +379,17 @@ this.panel2.Size = new System.Drawing.Size(478, 316); this.panel2.TabIndex = 7; // + // smsTokenButton + // + this.smsTokenButton.Location = new System.Drawing.Point(322, 177); + this.smsTokenButton.Name = "smsTokenButton"; + this.smsTokenButton.Size = new System.Drawing.Size(75, 23); + this.smsTokenButton.TabIndex = 22; + this.smsTokenButton.Text = "SMS"; + this.smsTokenButton.UseVisualStyleBackColor = true; + this.smsTokenButton.Visible = false; + this.smsTokenButton.Click += new System.EventHandler(this.smsTokenButton_Click); + // // tokenEmailSentLabel // this.tokenEmailSentLabel.AutoSize = true; @@ -939,17 +950,6 @@ this.exitToolStripMenuItem.Text = "E&xit"; this.exitToolStripMenuItem.Click += new System.EventHandler(this.exitToolStripMenuItem_Click_1); // - // smsTokenButton - // - this.smsTokenButton.Location = new System.Drawing.Point(322, 177); - this.smsTokenButton.Name = "smsTokenButton"; - this.smsTokenButton.Size = new System.Drawing.Size(75, 23); - this.smsTokenButton.TabIndex = 22; - this.smsTokenButton.Text = "SMS"; - this.smsTokenButton.UseVisualStyleBackColor = true; - this.smsTokenButton.Visible = false; - this.smsTokenButton.Click += new System.EventHandler(this.smsTokenButton_Click); - // // MainForm // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); diff --git a/MainForm.cs b/MainForm.cs index 2d7fad2..6169491 100644 --- a/MainForm.cs +++ b/MainForm.cs @@ -308,30 +308,33 @@ namespace MeshCentralRouter */ ArrayList controlsToAdd = new ArrayList(); - foreach (NodeClass node in meshcentral.nodes.Values) + if (meshcentral.nodes != null) { - if (node.agentid == -1) { continue; } - if (node.control == null) + foreach (NodeClass node in meshcentral.nodes.Values) { - // Add a new device - DeviceUserControl device = new DeviceUserControl(); - if ((node.meshid != null) && meshcentral.meshes.ContainsKey(node.meshid)) { device.mesh = (MeshClass)meshcentral.meshes[node.meshid]; } - device.node = node; - device.parent = this; - device.Dock = DockStyle.Top; - device.present = true; - node.control = device; - device.UpdateInfo(); - device.Visible = (search == "") || (node.name.ToLower().IndexOf(search) >= 0); - controlsToAdd.Add(device); - } - else - { - // Tag the device as present - if (node.control != null) + if (node.agentid == -1) { continue; } + if (node.control == null) { - node.control.present = true; - node.control.UpdateInfo(); + // Add a new device + DeviceUserControl device = new DeviceUserControl(); + if ((node.meshid != null) && meshcentral.meshes.ContainsKey(node.meshid)) { device.mesh = (MeshClass)meshcentral.meshes[node.meshid]; } + device.node = node; + device.parent = this; + device.Dock = DockStyle.Top; + device.present = true; + node.control = device; + device.UpdateInfo(); + device.Visible = (search == "") || (node.name.ToLower().IndexOf(search) >= 0); + controlsToAdd.Add(device); + } + else + { + // Tag the device as present + if (node.control != null) + { + node.control.present = true; + node.control.UpdateInfo(); + } } } } @@ -374,12 +377,19 @@ namespace MeshCentralRouter DeviceGroupComparer comp = new DeviceGroupComparer(); sortlist.Sort(comp); } - devicesPanel.Controls.Clear(); + remoteAllDeviceControls(); devicesPanel.Controls.AddRange((DeviceUserControl[])sortlist.ToArray(typeof(DeviceUserControl))); devicesPanel.ResumeLayout(); - noDevicesLabel.Visible = (devicesPanel.Controls.Count == 0); - noSearchResultsLabel.Visible = ((devicesPanel.Controls.Count > 0) && (visibleDevices == 0)); + noDevicesLabel.Visible = (sortlist.Count == 0); + noSearchResultsLabel.Visible = ((sortlist.Count > 0) && (visibleDevices == 0)); + } + + private void remoteAllDeviceControls() + { + ArrayList removelist = new ArrayList(); + foreach (Control c in devicesPanel.Controls) { if (c.GetType() == typeof(DeviceUserControl)) { removelist.Add(c); } } + foreach (Control c in removelist) { devicesPanel.Controls.Remove(c); } } public bool getShowGroupNames() { return showGroupNamesToolStripMenuItem.Checked; } @@ -445,7 +455,7 @@ namespace MeshCentralRouter foreach (Control c in devicesPanel.Controls) { if (c.GetType() == typeof(DeviceUserControl)) { ((DeviceUserControl)c).Dispose(); } } - devicesPanel.Controls.Clear(); + remoteAllDeviceControls(); noSearchResultsLabel.Visible = false; noDevicesLabel.Visible = true; @@ -821,11 +831,13 @@ namespace MeshCentralRouter { // Filter devices int visibleDevices = 0; + int deviceCount = 0; string search = searchTextBox.Text.ToLower(); foreach (Control c in devicesPanel.Controls) { if (c.GetType() == typeof(DeviceUserControl)) { + deviceCount++; NodeClass n = ((DeviceUserControl)c).node; bool connVisible = ((showOfflineDevicesToolStripMenuItem.Checked) || ((n.conn & 1) != 0)); if ((search == "") || (n.name.ToLower().IndexOf(search) >= 0) || (showGroupNamesToolStripMenuItem.Checked && (((DeviceUserControl)c).mesh.name.ToLower().IndexOf(search) >= 0))) @@ -839,8 +851,8 @@ namespace MeshCentralRouter } } - noDevicesLabel.Visible = (devicesPanel.Controls.Count == 0); - noSearchResultsLabel.Visible = ((devicesPanel.Controls.Count > 0) && (visibleDevices == 0)); + noDevicesLabel.Visible = (deviceCount == 0); + noSearchResultsLabel.Visible = ((deviceCount > 0) && (visibleDevices == 0)); } private void devicesTabControl_SelectedIndexChanged(object sender, EventArgs e) diff --git a/MainForm.resx b/MainForm.resx index cf1281e..ef449c8 100644 --- a/MainForm.resx +++ b/MainForm.resx @@ -117,12 +117,12 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - This server presented a un-trusted certificate. This may indicate that this is not the correct server or that the server does not have a valid certificate. It is not recommanded, but you can press the ignore button to continue connection to this server. - 613, 17 + + This server presented a un-trusted certificate. This may indicate that this is not the correct server or that the server does not have a valid certificate. It is not recommanded, but you can press the ignore button to continue connection to this server. + diff --git a/MeshCentralServer.cs b/MeshCentralServer.cs index 1efad0a..05e19a4 100644 --- a/MeshCentralServer.cs +++ b/MeshCentralServer.cs @@ -57,6 +57,8 @@ namespace MeshCentralRouter public bool debug = false; public bool ignoreCert = false; public string userid = null; + public Dictionary userRights = null; + public Dictionary userGroups = null; // Mesh Rights /* @@ -193,6 +195,7 @@ namespace MeshCentralRouter } case "serverinfo": { + wc.WriteStringWebSocket("{\"action\":\"usergroups\"}"); wc.WriteStringWebSocket("{\"action\":\"meshes\"}"); wc.WriteStringWebSocket("{\"action\":\"nodes\"}"); wc.WriteStringWebSocket("{\"action\":\"authcookie\"}"); @@ -215,6 +218,37 @@ namespace MeshCentralRouter { Dictionary userinfo = (Dictionary)jsonAction["userinfo"]; userid = (string)userinfo["_id"]; + userRights = new Dictionary(); + if (userinfo.ContainsKey("links")) + { + Dictionary userLinks = (Dictionary)userinfo["links"]; + foreach (string i in userLinks.Keys) + { + Dictionary userLinksEx = (Dictionary)userLinks[i]; + if (userLinksEx.ContainsKey("rights")) + { + userRights[i] = int.Parse(userLinksEx["rights"].ToString()); + } + } + } + break; + } + case "usergroups": + { + userGroups = new Dictionary(); + if (jsonAction.ContainsKey("ugroups")) + { + Dictionary usergroups = (Dictionary)jsonAction["ugroups"]; + foreach (string i in usergroups.Keys) + { + Dictionary usergroupsEx = (Dictionary)usergroups[i]; + if (usergroupsEx.ContainsKey("name")) + { + userGroups.Add(i, usergroupsEx["name"].ToString()); + } + } + if (onNodesChanged != null) onNodesChanged(); + } break; } case "event": @@ -286,10 +320,25 @@ namespace MeshCentralRouter Dictionary linksEx = ((Dictionary)links[userid]); if (linksEx.ContainsKey("rights")) { nodeRights = (ulong)(int)linksEx["rights"]; } } + + n.links = new Dictionary(); + foreach (string j in links.Keys) + { + Dictionary linksEx = ((Dictionary)links[j]); + if (linksEx.ContainsKey("rights")) + { + n.links.Add(j, ulong.Parse(linksEx["rights"].ToString())); + } + } } n.rights = nodeRights; - if ((n.rights == 0) && (meshes.ContainsKey(n.meshid) == false)) { + // Compute rights on this device + ulong rights = n.rights; // Direct device rights + if (meshes.ContainsKey(n.meshid)) { rights |= meshes[n.meshid].rights; } // Device group rights + foreach (string i in n.links.Keys) { if (userGroups.ContainsKey(i)) { rights |= n.links[i]; } } // Take a look at group rights + + if (rights == 0) { // We have no rights to this device, remove it nodes.Remove(n.nodeid); } else { @@ -320,6 +369,12 @@ namespace MeshCentralRouter } break; } + case "usergroupchange": + { + wc.WriteStringWebSocket("{\"action\":\"usergroups\"}"); + wc.WriteStringWebSocket("{\"action\":\"nodes\"}"); + break; + } } break; } @@ -379,6 +434,7 @@ namespace MeshCentralRouter if (node.ContainsKey("conn")) { n.conn = (int)node["conn"]; } else { n.conn = 0; } if (node.ContainsKey("icon")) { n.icon = (int)node["icon"]; } n.rights = 0; + n.links = new Dictionary(); if (node.ContainsKey("links")) { Dictionary links = ((Dictionary)node["links"]); @@ -387,6 +443,14 @@ namespace MeshCentralRouter Dictionary linksEx = ((Dictionary)links[userid]); if (linksEx.ContainsKey("rights")) { n.rights = (ulong)(int)linksEx["rights"]; } } + foreach (string j in links.Keys) + { + Dictionary linksEx = ((Dictionary)links[j]); + if (linksEx.ContainsKey("rights")) + { + n.links.Add(j, ulong.Parse(linksEx["rights"].ToString())); + } + } } nodes2[n.nodeid] = n; } @@ -401,13 +465,23 @@ namespace MeshCentralRouter if (node.ContainsKey("conn")) { n.conn = (int)node["conn"]; } else { n.conn = 0; } if (node.ContainsKey("icon")) { n.icon = (int)node["icon"]; } n.rights = 0; + n.links = new Dictionary(); if (node.ContainsKey("links")) { Dictionary links = ((Dictionary)node["links"]); - if (links.ContainsKey(userid)) { + if (links.ContainsKey(userid)) + { Dictionary linksEx = ((Dictionary)links[userid]); if (linksEx.ContainsKey("rights")) { n.rights = (ulong)(int)linksEx["rights"]; } } + foreach (string j in links.Keys) + { + Dictionary linksEx = ((Dictionary)links[j]); + if (linksEx.ContainsKey("rights")) + { + n.links.Add(j, ulong.Parse(linksEx["rights"].ToString())); + } + } } nodes2[n.nodeid] = n; } diff --git a/NodeClass.cs b/NodeClass.cs index 1f79c51..0f57fd1 100644 --- a/NodeClass.cs +++ b/NodeClass.cs @@ -17,6 +17,7 @@ namespace MeshCentralRouter public int rdpport; public ulong rights; public DeviceUserControl control; + public Dictionary links; public override string ToString() { return name; } }