Feature: edit device remarks from the web console

The Go web console now stores a per-device remark and renders it as
"hostname (remark)" on the card, the label displayName already
supported. The card's "..." menu gets an Edit remark entry directly
below Terminal; it opens a small dialog, and an empty value clears the
label. Remarks live in device_notes.json next to users.json, independent
of the device history and ACL, so offline records carry them too.

The WebSocket command set_device_remark is admin-only and honors the
device ACL (a private device of another owner is refused). Updates are
audited as admin.device.remark and broadcast as devices_changed so every
open console refreshes. Deleting a device drops its remark. The
device_list reply reports remarkEdit only when a notes table is wired,
so the shared page hides the entry on the MFC server, which does not
implement it.

A malformed or newer-version device_notes.json disables writes instead
of letting the first save overwrite labels it could not parse; loaded
values are re-sanitized and capped. Verified with go test ./..., vet,
gofmt, and a node --check pass on the inline script.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-11-27 13:22:21 +01:00
1 parent 1bd7fa5b2d
commit 95d2bbc1b6
9 files changed
+741 -7

No files matched your search

+89 -7
View File
@@ -657,8 +657,8 @@
max-width: 240px;
flex-shrink: 1;
}
/* 卡片操作:右上角只留一个「…」按钮,点开才弹出菜单(终端 / 绑定 /
转移 / 删除),菜单项带简短功能说明。独立于卡片本体点击事件
/* 卡片操作:右上角只留一个「…」按钮,点开才弹出菜单(终端 / 备注 /
绑定 / 转移 / 删除),菜单项带简短功能说明。独立于卡片本体点击事件
(onclick stopPropagation)。List 视图卡片是一行,按钮垂直居中。 */
.device-card .card-actions {
position: absolute;
@@ -2289,6 +2289,31 @@
</div>
</div>
<!-- 修改备注(Go 后端):备注是卡片标题旁的显示标签 "hostname (备注)"。 -->
<div class="modal-overlay" id="remark-device-modal" onclick="if(event.target===this)closeRemarkModal()">
<div class="modal-content" style="max-width:440px;">
<div class="modal-header">
<h3>Edit Remark</h3>
<button class="modal-close" onclick="closeRemarkModal()">&times;</button>
</div>
<div class="user-form">
<div id="remark-device-name" style="font-size:13px;color:#aaa;margin-bottom:10px;"></div>
<label style="font-size:13px;color:#aaa;display:block;margin:8px 0 4px;">
Remark
</label>
<input type="text" id="remark-input" maxlength="64"
placeholder="e.g. Reception PC" autocomplete="off">
<div style="font-size:12px;color:#888;margin-top:8px;">
Shown on the card as "hostname (remark)". Leave it empty to clear the remark.
</div>
<div style="display:flex;gap:8px;margin-top:14px;">
<button onclick="confirmSetRemark()">Save</button>
<button class="cancel-btn" onclick="closeRemarkModal()">Cancel</button>
</div>
</div>
</div>
</div>
<!-- Generic alert / confirmation dialog. Replaces native alert()/confirm():
their button labels come from the browser locale (e.g. German on a
German system) and do not match the app. -->
@@ -3020,6 +3045,8 @@
// 只有实现了「转移主机」的后端(Go)才会带这个字段;
// MFC 复用同一份页面但不下发它,按钮因此不会出现。
transferCfg = msg.transfer || null;
// 备注编辑同理由后端能力位决定(Go 才有;MFC 页面不显示)。
remarkEditAvailable = msg.remarkEdit === true;
// Keep current page (renderDeviceList adjusts if out of range)
renderDeviceList();
renderWallSidebar();
@@ -3339,6 +3366,14 @@
appAlert(msg.msg || 'Failed to unbind device', 'Device Binding');
}
break;
case 'set_device_remark_result':
if (msg.ok) {
closeRemarkModal();
getDevices();
} else {
appAlert(msg.msg || 'Failed to save remark', 'Edit Remark');
}
break;
case 'device_revoked': {
// The superadmin bound this device to someone else while we
// were watching it. Stop streaming immediately.
@@ -5109,14 +5144,16 @@
}
// ---- 卡片「…」操作菜单 ----
// 无论有几个操作,卡片上只显示一个「…」;点开弹出的菜单里才是终端/绑定/
// 转移/删除,每项带一句功能说明。菜单项在点击时按设备实时生成,权限和
// 无论有几个操作,卡片上只显示一个「…」;点开弹出的菜单里才是终端/备注/
// 绑定/转移/删除,每项带一句功能说明。菜单项在点击时按设备实时生成,权限和
// 在线状态不会因为列表渲染后变化而过期。
let cardMenuEl = null;
let cardMenuBtn = null;
const cardMenuSvg = {
term: '<svg viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">' +
'<polyline points="4 17 10 11 4 5"/><line x1="12" y1="19" x2="20" y2="19"/></svg>',
edit: '<svg viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">' +
'<path d="M12 20h9"/><path d="M16.5 3.5a2.12 2.12 0 0 1 3 3L7 19l-4 1 1-4Z"/></svg>',
bind: '<svg viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">' +
'<rect x="3" y="11" width="18" height="11" rx="2"/><path d="M7 11V7a5 5 0 0 1 10 0v4"/></svg>',
transfer: '<svg viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">' +
@@ -5126,9 +5163,17 @@
'<path d="M10 11v6"/><path d="M14 11v6"/><path d="M9 6V4a1 1 0 0 1 1-1h4a1 1 0 0 1 1 1v2"/></svg>'
};
// 备注编辑:只有实现了该功能的后端(Go)在 device_list 里报 remarkEdit
// 才出现;MFC 复用同一份页面但不下发这个字段,菜单项因此不会出现。
let remarkEditAvailable = false;
function canEditRemark() {
return remarkEditAvailable && currentUserRole === 'admin';
}
function hasCardActions(d) {
if (d.online !== false) return true; // 在线设备至少有终端
return canBindDevices() || canDeleteDevices();
return canEditRemark() || canBindDevices() || canDeleteDevices();
}
function cardMenuEntries(d) {
@@ -5138,6 +5183,10 @@
out.push({ kind: 'term', label: 'Terminal', desc: 'Open a remote shell',
run: () => openTerminal(d.id) });
}
if (canEditRemark()) {
out.push({ kind: 'edit', label: 'Edit remark', desc: 'Set the label shown on the card',
run: () => openRemarkModal(d.id) });
}
if (canBindDevices()) {
out.push({ kind: 'bind', label: 'Bind owner', desc: 'Assign this host to a user',
run: () => openBindModal(d.id) });
@@ -5260,7 +5309,7 @@
const rtt = d.rtt || '-';
const ver = d.version || '-';
const activeWin = d.activeWindow || '';
// 操作全部收进右上角的「…」菜单(终端 / 绑定 / 转移 / 删除),
// 操作全部收进右上角的「…」菜单(终端 / 备注 / 绑定 / 转移 / 删除),
// 具体条目见 cardMenuEntries。这里只决定按钮是否出现。
const actionsHtml = hasCardActions(d)
? '<div class="card-actions">' +
@@ -5587,6 +5636,38 @@
}));
}
// ===== 修改备注(Go 后端) =====
// 备注显示为卡片标题 "hostname (备注)";留空 = 清除。服务端持久化到
// device_notes.json,与设备历史/绑定独立,离线记录也能改。
let pendingRemarkDeviceId = null;
function openRemarkModal(id) {
if (!canEditRemark()) return;
const d = devices.find(x => String(x.id) === String(id));
if (!d) { appAlert('Device not found.', 'Edit Remark'); return; }
pendingRemarkDeviceId = String(id);
document.getElementById('remark-device-name').textContent =
(d.name || 'Unknown') + ' (' + String(id) + ')';
document.getElementById('remark-input').value = d.remark || '';
document.getElementById('remark-device-modal').classList.add('active');
const input = document.getElementById('remark-input');
input.focus();
input.select();
}
function closeRemarkModal() {
pendingRemarkDeviceId = null;
document.getElementById('remark-device-modal').classList.remove('active');
}
function confirmSetRemark() {
if (!pendingRemarkDeviceId || !ws || ws.readyState !== WebSocket.OPEN) return;
const remark = document.getElementById('remark-input').value.trim();
ws.send(JSON.stringify({
cmd: 'set_device_remark', token, id: pendingRemarkDeviceId, remark,
}));
}
// ===== 删除设备 =====
// 两种情况都删除服务端 devices.json 中的历史记录;在线设备额外按 MFC
// 的做法发送单字节 TOKEN_UNINSTALL(卸载主机)。都先弹二次确认。
@@ -5794,7 +5875,8 @@
'remote.screen.start', 'remote.screen.stop', 'remote.screen.denied',
'remote.terminal.start', 'remote.terminal.stop', 'remote.terminal.denied',
'remote.clipboard.set', 'remote.clipboard.get', 'remote.rdp_reset',
'admin.user.create', 'admin.user.delete', 'admin.device.delete', 'admin.audit.query',
'admin.user.create', 'admin.user.delete', 'admin.device.delete', 'admin.device.bind',
'admin.device.unbind', 'admin.device.transfer', 'admin.device.remark', 'admin.audit.query',
'device.online', 'device.offline',
'device.auth.success', 'device.auth.failure', 'device.auth.throttled',
'device.auth.heartbeat.success',