1
0
mirror of https://github.com/Ylianst/MeshCentralRouter synced 2025-12-06 00:13:33 +00:00

Added proper user group handling.

This commit is contained in:
Ylian Saint-Hilaire
2020-04-29 12:30:29 -07:00
parent 579c93c6c9
commit ec2d4dc56b
6 changed files with 135 additions and 47 deletions

View File

@@ -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)
{

24
MainForm.Designer.cs generated
View File

@@ -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);

View File

@@ -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)

View File

@@ -117,12 +117,12 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="label5.Text" xml:space="preserve">
<value>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.</value>
</data>
<metadata name="mainContextMenuStrip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>613, 17</value>
</metadata>
<data name="label5.Text" xml:space="preserve">
<value>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.</value>
</data>
<assembly alias="System.Drawing" name="System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<data name="settingsPictureBox.Image" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>

View File

@@ -57,6 +57,8 @@ namespace MeshCentralRouter
public bool debug = false;
public bool ignoreCert = false;
public string userid = null;
public Dictionary<string, int> userRights = null;
public Dictionary<string, string> 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<string, object> userinfo = (Dictionary<string, object>)jsonAction["userinfo"];
userid = (string)userinfo["_id"];
userRights = new Dictionary<string, int>();
if (userinfo.ContainsKey("links"))
{
Dictionary<string, object> userLinks = (Dictionary<string, object>)userinfo["links"];
foreach (string i in userLinks.Keys)
{
Dictionary<string, object> userLinksEx = (Dictionary<string, object>)userLinks[i];
if (userLinksEx.ContainsKey("rights"))
{
userRights[i] = int.Parse(userLinksEx["rights"].ToString());
}
}
}
break;
}
case "usergroups":
{
userGroups = new Dictionary<string, string>();
if (jsonAction.ContainsKey("ugroups"))
{
Dictionary<string, object> usergroups = (Dictionary<string, object>)jsonAction["ugroups"];
foreach (string i in usergroups.Keys)
{
Dictionary<string, object> usergroupsEx = (Dictionary<string, object>)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<string, object> linksEx = ((Dictionary<string, object>)links[userid]);
if (linksEx.ContainsKey("rights")) { nodeRights = (ulong)(int)linksEx["rights"]; }
}
n.links = new Dictionary<string, ulong>();
foreach (string j in links.Keys)
{
Dictionary<string, object> linksEx = ((Dictionary<string, object>)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<string, ulong>();
if (node.ContainsKey("links"))
{
Dictionary<string, object> links = ((Dictionary<string, object>)node["links"]);
@@ -387,6 +443,14 @@ namespace MeshCentralRouter
Dictionary<string, object> linksEx = ((Dictionary<string, object>)links[userid]);
if (linksEx.ContainsKey("rights")) { n.rights = (ulong)(int)linksEx["rights"]; }
}
foreach (string j in links.Keys)
{
Dictionary<string, object> linksEx = ((Dictionary<string, object>)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<string, ulong>();
if (node.ContainsKey("links"))
{
Dictionary<string, object> links = ((Dictionary<string, object>)node["links"]);
if (links.ContainsKey(userid)) {
if (links.ContainsKey(userid))
{
Dictionary<string, object> linksEx = ((Dictionary<string, object>)links[userid]);
if (linksEx.ContainsKey("rights")) { n.rights = (ulong)(int)linksEx["rights"]; }
}
foreach (string j in links.Keys)
{
Dictionary<string, object> linksEx = ((Dictionary<string, object>)links[j]);
if (linksEx.ContainsKey("rights"))
{
n.links.Add(j, ulong.Parse(linksEx["rights"].ToString()));
}
}
}
nodes2[n.nodeid] = n;
}

View File

@@ -17,6 +17,7 @@ namespace MeshCentralRouter
public int rdpport;
public ulong rights;
public DeviceUserControl control;
public Dictionary<string, ulong> links;
public override string ToString() { return name; }
}