Feature: let the web console move a host to another group
The Go server now keeps a per-device group override (device_groups.json) edited from the card's "..." menu below Edit remark. The override wins over the group the host reported (or the offline history record) in the device list, the group filter, get_groups and the MCP host tools, so offline records can be regrouped and the change survives restarts. An empty value clears the override; names are capped at 23 characters, matching the Win32 regroup dialog. The command is admin-only and honours the device ACL; it refuses IDs the server has never seen (online or in history) so a stale card cannot create an orphan entry, and the remark handler gets the same guard. Each change is audited as admin.device.group, deleting a device drops its override, and values loaded from disk are trimmed and capped like remarks. The menu entry is gated on the groupEdit capability, so the shared page hides it on the MFC server. Like remarks, the override is a Go-side label: no CMD_SET_GROUP is sent, so the host keeps reporting its own group to other masters. Verified with go test ./..., go vet, gofmt and a node --check pass on the page. Co-Authored-By: deepseek-flash
This commit is contained in:
1 parent
5f764b2425
commit
034b11fa6a
16 files changed
+895
-54
No files matched your search
@@ -106,6 +106,8 @@ func (h *wsHub) dispatch(c *wsClient, cmd string, raw []byte) {
|
||||
h.handleUnbindDevice(c, raw)
|
||||
case "set_device_remark":
|
||||
h.handleSetDeviceRemark(c, raw)
|
||||
case "set_device_group":
|
||||
h.handleSetDeviceGroup(c, raw)
|
||||
case "get_groups":
|
||||
h.handleGetGroups(c, raw)
|
||||
case "query_audit":
|
||||
@@ -124,6 +126,16 @@ func (h *wsHub) dispatch(c *wsClient, cmd string, raw []byte) {
|
||||
}
|
||||
}
|
||||
|
||||
// deviceExists reports whether id is online or has a history record. Handlers
|
||||
// that write per-device side tables use it so a stale card (the device was
|
||||
// deleted between render and save) cannot create an orphan entry.
|
||||
func (h *wsHub) deviceExists(id string) bool {
|
||||
if h.devices.IsOnline(id) {
|
||||
return true
|
||||
}
|
||||
return h.history != nil && h.history.Has(id)
|
||||
}
|
||||
|
||||
// requireAdmin combines token validation with a role=="admin" check. The
|
||||
// reply on failure has the standard `{cmd, ok:false, msg}` shape so the
|
||||
// front-end's generic toast handler can surface the reason.
|
||||
@@ -807,6 +819,8 @@ func (h *wsHub) handleGetDevices(c *wsClient, raw []byte) {
|
||||
// remark table reports it, so the shared page hides the menu entry
|
||||
// on the MFC server.
|
||||
"remarkEdit": h.notes != nil,
|
||||
// ... and for "edit group", which only the Go server implements.
|
||||
"groupEdit": h.groups != nil,
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -819,6 +833,10 @@ type deviceListItem struct {
|
||||
// Remark is the operator's label for this host, rendered as
|
||||
// "hostname (remark)" on the card. Set by the Go web console only.
|
||||
Remark string `json:"remark,omitempty"`
|
||||
// GroupOverride marks Group as a web-console override rather than the
|
||||
// group the host reported, so the edit dialog can prefill without turning
|
||||
// the reported value into a permanent override.
|
||||
GroupOverride bool `json:"groupOverride,omitempty"`
|
||||
// TransferTo / TransferUntil carry the "handed over to another master"
|
||||
// marker so the card can say where the host went and how long it stays
|
||||
// there. Absent for a host that was never transferred.
|
||||
@@ -838,6 +856,10 @@ func (h *wsHub) listVisibleDevices(c *wsClient) []deviceListItem {
|
||||
if h.notes != nil {
|
||||
remarks = h.notes.Snapshot()
|
||||
}
|
||||
var groups map[string]string
|
||||
if h.groups != nil {
|
||||
groups = h.groups.Groups()
|
||||
}
|
||||
out := make([]deviceListItem, 0, len(all))
|
||||
for _, d := range all {
|
||||
if !h.canAccessDevice(c, d.ID) {
|
||||
@@ -851,6 +873,12 @@ func (h *wsHub) listVisibleDevices(c *wsClient) []deviceListItem {
|
||||
if remarks != nil {
|
||||
item.Remark = remarks[d.ID].Note
|
||||
}
|
||||
// A web-console group override wins over the group the host reported
|
||||
// at login (and over the group stored in the offline history record).
|
||||
if g := groups[d.ID]; g != "" {
|
||||
item.DeviceInfo.Group = g
|
||||
item.GroupOverride = true
|
||||
}
|
||||
if h.history != nil {
|
||||
if target, until, ok := h.history.Transfer(d.ID); ok {
|
||||
item.TransferTo, item.TransferUntil = target, until
|
||||
@@ -1302,6 +1330,7 @@ func (h *wsHub) handleDeleteDevice(c *wsClient, raw []byte) {
|
||||
// client that happens to reuse the same ID.
|
||||
h.dropDeviceBinding(in.ID)
|
||||
h.dropDeviceNote(in.ID)
|
||||
h.dropDeviceGroup(in.ID)
|
||||
h.audit(c, false, auditDeviceAdmin, map[string]any{
|
||||
"device_id": in.ID,
|
||||
"mode": "uninstall",
|
||||
@@ -1344,6 +1373,7 @@ func (h *wsHub) handleDeleteDevice(c *wsClient, raw []byte) {
|
||||
}
|
||||
h.dropDeviceBinding(in.ID)
|
||||
h.dropDeviceNote(in.ID)
|
||||
h.dropDeviceGroup(in.ID)
|
||||
h.audit(c, false, auditDeviceAdmin, map[string]any{
|
||||
"device_id": in.ID,
|
||||
"mode": "removed",
|
||||
@@ -1595,16 +1625,21 @@ func (h *wsHub) handleListUsers(c *wsClient, raw []byte) {
|
||||
}))
|
||||
}
|
||||
|
||||
// handleGetGroups returns the deduplicated set of group labels currently
|
||||
// observed on online devices, plus a synthetic "default" entry that the
|
||||
// admin UI uses for ungrouped devices. Admin-only — non-admins don't get
|
||||
// to enumerate the device fleet's groups. Matches the contract of
|
||||
// CWebService::HandleGetGroups (WebService.cpp:1130).
|
||||
// handleGetGroups returns the deduplicated set of group labels the admin UI
|
||||
// can offer: Web-console overrides (including groups that exist only on
|
||||
// offline records) plus the groups online devices report, and a synthetic
|
||||
// "default" entry for ungrouped devices. A device whose group is overridden
|
||||
// contributes only the override, never its stale reported group. Admin-only —
|
||||
// non-admins don't get to enumerate the device fleet's groups.
|
||||
func (h *wsHub) handleGetGroups(c *wsClient, raw []byte) {
|
||||
const replyCmd = "groups"
|
||||
if !h.requireAdmin(c, raw, replyCmd) {
|
||||
return
|
||||
}
|
||||
var overrides map[string]string
|
||||
if h.groups != nil {
|
||||
overrides = h.groups.Groups()
|
||||
}
|
||||
seen := map[string]struct{}{"default": {}}
|
||||
for _, d := range h.devices.ListDevices() {
|
||||
// Group names are device metadata: do not reveal groups that only
|
||||
@@ -1612,12 +1647,23 @@ func (h *wsHub) handleGetGroups(c *wsClient, raw []byte) {
|
||||
if !h.canAccessDevice(c, d.ID) {
|
||||
continue
|
||||
}
|
||||
if overrides[d.ID] != "" {
|
||||
continue // the override loop below adds the effective group
|
||||
}
|
||||
g := d.Group
|
||||
if g == "" {
|
||||
g = "default"
|
||||
}
|
||||
seen[g] = struct{}{}
|
||||
}
|
||||
// Web-console overrides define groups too, including groups that only
|
||||
// exist on offline records; the per-device access check still applies.
|
||||
for id, g := range overrides {
|
||||
if g == "" || !h.canAccessDevice(c, id) {
|
||||
continue
|
||||
}
|
||||
seen[g] = struct{}{}
|
||||
}
|
||||
groups := make([]string, 0, len(seen))
|
||||
for g := range seen {
|
||||
groups = append(groups, g)
|
||||
@@ -1780,9 +1826,13 @@ func (h *wsHub) handleSetDeviceRemark(c *wsClient, raw []byte) {
|
||||
if h.denyIfNoDeviceAccess(c, id, replyCmd) {
|
||||
return
|
||||
}
|
||||
if !h.deviceExists(id) {
|
||||
fail("Device not found", "not_found")
|
||||
return
|
||||
}
|
||||
// Sanitize before the length check and the reply so the echoed value is
|
||||
// exactly what the store keeps (control characters folded to spaces).
|
||||
remark := sanitizeRemark(in.Remark)
|
||||
remark := sanitizeLabel(in.Remark)
|
||||
if utf8.RuneCountInString(remark) > MaxRemarkRunes {
|
||||
fail("Remark is too long (max "+strconv.Itoa(MaxRemarkRunes)+" characters)", "too_long")
|
||||
return
|
||||
@@ -1803,6 +1853,73 @@ func (h *wsHub) handleSetDeviceRemark(c *wsClient, raw []byte) {
|
||||
}))
|
||||
}
|
||||
|
||||
// handleSetDeviceGroup sets the web-console group override for one device.
|
||||
// Admin role only, and only for a device the caller can access. An empty group
|
||||
// clears the override, so the device falls back to the group it reports.
|
||||
//
|
||||
// The override changes every Go-side view (device list, group filter, MCP) and
|
||||
// works for offline records, but it does not send CMD_SET_GROUP: the host keeps
|
||||
// reporting its own group to other masters.
|
||||
func (h *wsHub) handleSetDeviceGroup(c *wsClient, raw []byte) {
|
||||
const replyCmd = "set_device_group_result"
|
||||
if !h.requireAdmin(c, raw, replyCmd) {
|
||||
return
|
||||
}
|
||||
var in struct {
|
||||
ID string `json:"id"`
|
||||
Group string `json:"group"`
|
||||
}
|
||||
id := ""
|
||||
fail := func(msg, reason string) {
|
||||
h.audit(c, true, auditDeviceGroup, map[string]any{
|
||||
"device_id": id,
|
||||
"ok": false,
|
||||
"reason": reason,
|
||||
"error": msg,
|
||||
})
|
||||
c.queue(mustJSON(map[string]any{"cmd": replyCmd, "ok": false, "msg": msg}))
|
||||
}
|
||||
if err := json.Unmarshal(raw, &in); err != nil {
|
||||
fail("Invalid request", "invalid_request")
|
||||
return
|
||||
}
|
||||
id = strings.TrimSpace(in.ID)
|
||||
if id == "" {
|
||||
fail("Invalid device id", "invalid_id")
|
||||
return
|
||||
}
|
||||
if h.groups == nil {
|
||||
fail("Device groups are not enabled on this server", "disabled")
|
||||
return
|
||||
}
|
||||
if h.denyIfNoDeviceAccess(c, id, replyCmd) {
|
||||
return
|
||||
}
|
||||
if !h.deviceExists(id) {
|
||||
fail("Device not found", "not_found")
|
||||
return
|
||||
}
|
||||
group := sanitizeLabel(in.Group)
|
||||
if utf8.RuneCountInString(group) > MaxGroupRunes {
|
||||
fail("Group is too long (max "+strconv.Itoa(MaxGroupRunes)+" characters)", "too_long")
|
||||
return
|
||||
}
|
||||
if err := h.groups.Set(id, group, c.user); err != nil {
|
||||
fail("Failed to save group: "+err.Error(), "save_failed")
|
||||
return
|
||||
}
|
||||
h.audit(c, false, auditDeviceGroup, map[string]any{
|
||||
"device_id": id,
|
||||
"ok": true,
|
||||
"cleared": group == "",
|
||||
})
|
||||
// Every open console re-reads the list so the card and group filter move.
|
||||
h.broadcastAuthenticated(`{"cmd":"devices_changed"}`)
|
||||
c.queue(mustJSON(map[string]any{
|
||||
"cmd": replyCmd, "ok": true, "id": id, "group": group,
|
||||
}))
|
||||
}
|
||||
|
||||
// normalizeDeviceIDs merges the batch and single-device forms, trims blanks
|
||||
// and dedupes while preserving order.
|
||||
func normalizeDeviceIDs(ids []string, single string) []string {
|
||||
@@ -1912,6 +2029,16 @@ func (h *wsHub) dropDeviceNote(deviceID string) {
|
||||
}
|
||||
}
|
||||
|
||||
// dropDeviceGroup removes the group override together with the device record.
|
||||
func (h *wsHub) dropDeviceGroup(deviceID string) {
|
||||
if h.groups == nil {
|
||||
return
|
||||
}
|
||||
if err := h.groups.Remove(deviceID); err != nil {
|
||||
h.log.Warn("device group remove failed for %s: %v", deviceID, err)
|
||||
}
|
||||
}
|
||||
|
||||
// handleTermOpen kicks off a web terminal session. On success the wsClient
|
||||
// records `termWatching = deviceID` so subsequent term_input / term_resize
|
||||
// have a target, and the hub sends COMMAND_SHELL to the device. The
|
||||
|
||||
Reference in new issue
Block a user