diff --git a/frontend/public/locales/en-US/application.json b/frontend/public/locales/en-US/application.json index ee335367c1..2df3ac0249 100644 --- a/frontend/public/locales/en-US/application.json +++ b/frontend/public/locales/en-US/application.json @@ -1112,11 +1112,18 @@ "agentTokenLifetimeNever": "Never", "agentTokenExpiresCap": "This token was issued before editable lifetimes. It cannot outlive its original expiry date.", "agentTokenAccess": "Access", - "agentTokenAccessRead": "Read files", - "agentTokenAccessWrite": "Read & write files", - "agentTokenAccessFull": "Files and shares", + "agentScopeFilesRead": "Read files", + "agentScopeFilesReadHint": "list, search, download, read text", + "agentScopeFilesWrite": "Write files", + "agentScopeFilesWriteHint": "create, upload, rename, move, delete — includes read", + "agentScopeSharesRead": "Read shares", + "agentScopeSharesReadHint": "list share links", + "agentScopeSharesWrite": "Share links", + "agentScopeSharesWriteHint": "create and delete share links — includes read", + "agentScopeFull": "Full access", "agentTokenFolderLimit": "Limit to a folder", "agentTokenFolderScope": "Folder scope", + "agentTokenAddFolder": "Add folder", "webdavHint": "WebDAV server: {{url}}; Username: {{name}} ; The password is the password of the created account below.", "annotation": "Annotation", "rootFolder": "Relative root folder", diff --git a/frontend/public/locales/zh-CN/application.json b/frontend/public/locales/zh-CN/application.json index 17824b1aae..aa5333c5dd 100644 --- a/frontend/public/locales/zh-CN/application.json +++ b/frontend/public/locales/zh-CN/application.json @@ -1112,11 +1112,18 @@ "agentTokenLifetimeNever": "永久", "agentTokenExpiresCap": "该令牌在可编辑有效期功能之前签发,无法超过原始到期时间。", "agentTokenAccess": "访问权限", - "agentTokenAccessRead": "读取文件", - "agentTokenAccessWrite": "读写文件", - "agentTokenAccessFull": "文件和分享", + "agentScopeFilesRead": "读取文件", + "agentScopeFilesReadHint": "列目录、搜索、下载、读取文本", + "agentScopeFilesWrite": "写入文件", + "agentScopeFilesWriteHint": "新建、上传、重命名、移动、删除 —— 包含读取", + "agentScopeSharesRead": "读取分享", + "agentScopeSharesReadHint": "列出分享链接", + "agentScopeSharesWrite": "分享链接", + "agentScopeSharesWriteHint": "创建和删除分享链接 —— 包含读取", + "agentScopeFull": "全部权限", "agentTokenFolderLimit": "限定目录", "agentTokenFolderScope": "目录范围", + "agentTokenAddFolder": "添加目录", "webdavHint": "WebDAV 地址为:{{url}};登录用户名统一为:{{name}};密码为所创建账号的密码。", "annotation": "备注名", "rootFolder": "相对根目录", diff --git a/frontend/src/component/Pages/Devices/McpConnect.tsx b/frontend/src/component/Pages/Devices/McpConnect.tsx index 88b90818a0..90ed9172e9 100644 --- a/frontend/src/component/Pages/Devices/McpConnect.tsx +++ b/frontend/src/component/Pages/Devices/McpConnect.tsx @@ -1,9 +1,11 @@ import { Alert, + Box, Button, Checkbox, DialogContent, FormControl, + FormGroup, InputLabel, MenuItem, Select, @@ -50,28 +52,31 @@ export function agentConfig(token: string) { ); } -const scopeRead = "Files.Read"; -const scopeFilesWrite = "Files.Write"; -const scopeSharesWrite = "Shares.Write"; +const scopeKeys = ["Files.Read", "Files.Write", "Shares.Read", "Shares.Write"]; -const accessScopes: Record = { - read: [scopeRead], - write: [scopeFilesWrite], - full: [scopeFilesWrite, scopeSharesWrite], +const scopeI18n: Record = { + "Files.Read": "agentScopeFilesRead", + "Files.Write": "agentScopeFilesWrite", + "Shares.Read": "agentScopeSharesRead", + "Shares.Write": "agentScopeSharesWrite", }; -const accessFromScopes = (scopes?: string[]) => { - if (!scopes || scopes.length === 0 || scopes.includes(scopeSharesWrite)) { - return "full"; +const describeScopes = (scopes: string[] | undefined, t: (key: string) => string) => { + if (!scopes || scopes.length === 0) { + return t("setting.agentScopeFull"); } - if (scopes.includes(scopeFilesWrite)) { - return "write"; + const parts = scopeKeys + .filter((key) => scopes.includes(key)) + .filter((key) => !(key.endsWith(".Read") && scopes.includes(`${key.slice(0, -5)}.Write`))); + if (parts.length === scopeKeys.length) { + return t("setting.agentScopeFull"); } - return "read"; + return parts.map((key) => t(`setting.${scopeI18n[key]}`)).join(" + "); }; const lifetimeOptions = [604800, 2592000, 7776000, 31536000, 0]; const keepLifetime = -1; +const maxFolders = 5; const rootDisplay = (root: string) => root.replace(/^cloudreve:\/\/my/, "") || "/"; @@ -89,28 +94,34 @@ const AgentTokenDialog = ({ open, editTarget, onClose, onCreated, onUpdated }: A const { enqueueSnackbar } = useSnackbar(); const [loading, setLoading] = useState(false); const [lifetime, setLifetime] = useState(2592000); - const [access, setAccess] = useState("full"); + const [scopes, setScopes] = useState([...scopeKeys]); const [limitFolder, setLimitFolder] = useState(false); - const [folder, setFolder] = useState("cloudreve://my"); + const [folders, setFolders] = useState([]); useEffect(() => { if (open && editTarget) { setLifetime(keepLifetime); - setAccess(accessFromScopes(editTarget.scopes)); + setScopes(editTarget.scopes?.length ? [...editTarget.scopes] : [...scopeKeys]); setLimitFolder((editTarget.roots?.length ?? 0) > 0); - setFolder(editTarget.roots?.[0] ?? "cloudreve://my"); + setFolders(editTarget.roots?.length ? [...editTarget.roots] : []); } else if (open) { setLifetime(2592000); - setAccess("full"); + setScopes([...scopeKeys]); setLimitFolder(false); - setFolder("cloudreve://my"); + setFolders([]); } }, [open]); + const toggleScope = (key: string) => + setScopes((current) => (current.includes(key) ? current.filter((x) => x !== key) : [...current, key])); + + const updateFolder = (index: number, path: string) => + setFolders((current) => current.map((item, i) => (i === index ? path : item))); + const onAccept = () => { const req: AgentTokenRequest = { - scopes: accessScopes[access], - roots: limitFolder ? [folder] : [], + scopes, + roots: limitFolder ? [...new Set(folders.filter(Boolean))] : [], }; if (!editTarget || lifetime !== keepLifetime) { req.ttl_seconds = lifetime; @@ -130,6 +141,9 @@ const AgentTokenDialog = ({ open, editTarget, onClose, onCreated, onUpdated }: A .finally(() => setLoading(false)); }; + const invalid = + scopes.length === 0 || (limitFolder && folders.filter(Boolean).length === 0); + return ( )} - - {t("setting.agentTokenAccess")} - - + + {t("setting.agentTokenAccess")} + + {scopeKeys.map((key) => ( + + toggleScope(key)} /> + } + label={t(`setting.${scopeI18n[key]}`)} + /> + + {t(`setting.${scopeI18n[key]}Hint`)} + + + ))} + + setLimitFolder(e.target.checked)} /> + { + setLimitFolder(e.target.checked); + if (e.target.checked && folders.length === 0) { + setFolders(["cloudreve://my"]); + } + }} + /> } label={t("setting.agentTokenFolderLimit")} /> {limitFolder && ( - + + {folders.map((folder, i) => ( + + + updateFolder(i, path)} + path={folder} + variant="agentTokenRoot" + label={t("setting.agentTokenFolderScope")} + /> + + {folders.length > 1 && ( + setFolders(folders.filter((_, j) => j !== i))}> + + + )} + + ))} + {folders.length < maxFolders && ( + } + onClick={() => setFolders([...folders, "cloudreve://my"])} + > + {t("setting.agentTokenAddFolder")} + + )} + )} @@ -233,10 +283,7 @@ const McpConnect = () => { .catch(() => undefined); }; - const accessLabel = (token: AgentToken) => { - const key = accessFromScopes(token.scopes); - return t(`setting.agentTokenAccess${key[0].toUpperCase()}${key.slice(1)}`); - }; + return ( @@ -280,7 +327,7 @@ const McpConnect = () => { {t("setting.agentTokenLifetimeNever")} )} - {accessLabel(token)} + {describeScopes(token.scopes, t)} {(token.roots ?? []).map((root) => (