1 line
11 KiB
JavaScript
1 line
11 KiB
JavaScript
import{n as e}from"./rolldown-runtime-WkwZ5CU-.js";import{S as t,t as n}from"./react-QV7DAAIx.js";import{G as r,I as i,Lt as a,N as o,P as s,Pt as c,R as l,U as u,c as d,ft as f,i as p,ut as m,y as h}from"./semi-65W_4I1v.js";import{E as g,F as _,I as v,L as y,a as b,b as x,c as S,l as C,m as w,n as T,p as E,s as D,x as O,y as k}from"./index-DNMXun-U.js";var A=e(t(),1),j=e(n(),1),M=[[`project`,`项目`],[`conversation`,`会话`],[`asset`,`资产`],[`vulnerability`,`漏洞`],[`deep_assessment`,`深度测评`],[`batch_task`,`批量任务`]];function N({users:e,onCancel:t,onSaved:n}){let a=(0,A.useId)(),[o,s]=(0,A.useState)(M[0][0]),[c,d]=(0,A.useState)(``),[f,m]=(0,A.useState)(0),[g,_]=(0,A.useState)([]),[v,x]=(0,A.useState)(null),[w,E]=(0,A.useState)(!1),D=T(c,250),k=b(async()=>({type:o,q:D,offset:f,payload:await y.get(`/rbac/resources`,{type:o,q:D,limit:20,offset:f})}),[o,D,f]),N=k.data?.type===o&&k.data.q===D&&k.data.offset===f?k.data.payload:null,P=Array.isArray(N?.resources)?N.resources:[],F=new Map([...g,...P.map(e=>[e.id,e.label||e.id])]);return(0,j.jsx)(S,{visible:!0,title:`新增资源授权`,width:560,closable:!w,maskClosable:!w,closeOnEsc:!w,onCancel:t,formId:a,okText:`保存授权`,confirmLoading:w,cancelButtonProps:{disabled:w},children:(0,j.jsxs)(p,{id:a,onSubmit:async e=>{if(w)return;x(null);let t=String(e.ids||``).split(/[\s,,;;]+/).filter(Boolean),r=[...new Set([...g.map(([e])=>e),...t])];if(!r.length||r.length>100)x(Error(`请选择或输入 1 至 100 个资源 ID`));else{E(!0);try{await y.post(`/rbac/resource-assignments`,{user_id:e.user_id,resource_type:o,resource_ids:r}),O(`资源授权已保存`),n()}catch(e){x(e)}finally{E(!1)}}},initValues:{resource_type:o},children:[(0,j.jsx)(p.Select,{field:`user_id`,label:`用户`,filter:!0,optionList:e.map(e=>({value:e.id,label:e.display_name||e.username})),rules:[{required:!0,message:`请选择用户`}]}),(0,j.jsx)(p.Select,{field:`resource_type`,label:`资源类型`,optionList:M.map(([e,t])=>({value:e,label:t})),onChange:e=>{s(e),_([]),d(``),m(0)}}),(0,j.jsx)(p.Slot,{label:`按名称或 ID 搜索`,children:(0,j.jsx)(u,{"aria-label":`搜索资源`,value:c,onChange:e=>{d(e),m(0)}})}),(0,j.jsxs)(p.Slot,{label:`选择资源`,children:[(0,j.jsxs)(i,{wrap:!0,children:[(0,j.jsx)(l,{size:`small`,disabled:f===0||k.loading,onClick:()=>m(e=>Math.max(0,e-20)),children:`上一页`}),(0,j.jsxs)(r.Text,{type:`tertiary`,children:[f+P.length,` / `,N?.total??`—`]}),(0,j.jsx)(l,{size:`small`,disabled:!N?.has_more||k.loading,onClick:()=>m(e=>e+20),children:`下一页`})]}),(0,j.jsx)(h,{multiple:!0,style:{width:`100%`,marginTop:8},loading:k.loading,value:g.map(([e])=>e),"aria-label":`选择资源`,optionList:[...F].map(([e,t])=>({value:e,label:t})),onChange:e=>_((e||[]).map(e=>[e,F.get(e)||e]))})]}),(0,j.jsx)(p.TextArea,{field:`ids`,label:`手动输入资源 ID`,placeholder:`多个 ID 用空格或换行分隔`,rows:2}),(0,j.jsx)(C,{error:v||k.error})]})})}var{Text:P}=r;function F({value:e=[],options:t,onChange:n}){let r=(0,A.useMemo)(()=>{let e=new Map;return t.forEach(t=>{let[n]=t.split(`:`);e.has(n)||e.set(n,[]),e.get(n).push(t)}),[...e.entries()].sort((e,t)=>e[0].localeCompare(t[0]))},[t]);return(0,j.jsx)(`div`,{style:{maxHeight:320,overflow:`auto`,border:`1px solid var(--semi-color-border)`,borderRadius:8,padding:12},children:r.map(([t,r])=>(0,j.jsxs)(`div`,{style:{marginBottom:10},children:[(0,j.jsx)(P,{type:`tertiary`,style:{fontSize:12,textTransform:`uppercase`},children:t}),(0,j.jsx)(`div`,{style:{display:`flex`,flexWrap:`wrap`,gap:8,marginTop:4},children:r.map(t=>(0,j.jsx)(s,{checked:e.includes(t),onChange:r=>{let i=new Set(e);r.target.checked?i.add(t):i.delete(t),n([...i])},children:t.split(`:`)[1]},t))})]},t))})}function I(){let e=_(),{hasPermission:t}=v(),n=t(`rbac:write`),[r,s]=(0,A.useState)(`roles`),[u,h]=(0,A.useState)(null),[T,M]=(0,A.useState)(null),[I,L]=(0,A.useState)(!1),[R,z]=(0,A.useState)(!1),B=b(()=>y.get(`/rbac/metadata`),[]),V=b(()=>y.get(`/rbac/roles`),[]),H=b(()=>y.get(`/rbac/users`),[]),U=b(()=>y.get(`/rbac/resource-assignments`),[]),W=B.data?.permissions?Object.keys(B.data.permissions):[],G=Array.isArray(V.data?.roles)?V.data.roles:[],K=Array.isArray(H.data?.users)?H.data.users:[],q=Array.isArray(U.data?.assignments)?U.data.assignments:[],J=G.map(e=>({value:e.id,label:e.name})),Y=new Map(G.map(e=>[e.id,e.name])),X=new Map(K.map(e=>[e.id,e.display_name||e.username])),Z=async()=>{let e=await u.form.validate();z(!0);try{let t={name:e.name.trim(),description:e.description||``,scope:e.scope||``,permissions:u.permissions||[]};u.role?.id?await y.put(`/rbac/roles/${encodeURIComponent(u.role.id)}`,t):await y.post(`/rbac/roles`,t),O(`角色已保存`),h(null),V.reload(),B.reload()}catch(e){x(e,`保存角色失败`)}finally{z(!1)}},Q=async()=>{let e=await T.form.validate();z(!0);try{let t={display_name:e.display_name||``,enabled:e.enabled!==!1,roles:e.roles||[]};T.user?.id?(e.password&&(t.password=e.password),await y.put(`/rbac/users/${encodeURIComponent(T.user.id)}`,t)):await y.post(`/rbac/users`,{...t,username:e.username.trim(),password:e.password}),O(`用户已保存`),M(null),H.reload()}catch(e){x(e,`保存用户失败`)}finally{z(!1)}},$=e=>k({title:`删除角色「${e.name}」?`,onOk:async()=>{await y.del(`/rbac/roles/${encodeURIComponent(e.id)}`),O(`已删除`),V.reload()}}),ee=e=>k({title:`删除用户「${e.username}」?`,onOk:async()=>{await y.del(`/rbac/users/${encodeURIComponent(e.id)}`),O(`已删除`),H.reload()}}),te=e=>k({title:`删除该资源授权?`,content:`${e.resourceType||``} ${e.resourceId||``}`,onOk:async()=>{await y.del(`/rbac/resource-assignments/${encodeURIComponent(e.id)}`),O(`已删除`),U.reload()}});return(0,j.jsxs)(`div`,{children:[(0,j.jsx)(E,{title:e(`auth.rbacTitle`,null,`平台权限`),description:`平台账号、角色与资源授权;角色决定接口权限键,资源授权决定可见数据范围。`,extra:(0,j.jsx)(l,{icon:(0,j.jsx)(m,{}),onClick:()=>{V.reload(),H.reload(),U.reload(),B.reload()},children:`刷新`})}),(0,j.jsxs)(d,{type:`line`,activeKey:r,onChange:s,children:[(0,j.jsx)(d.TabPane,{itemKey:`roles`,tab:`角色权限(${G.length})`,children:(0,j.jsxs)(w,{extra:n&&(0,j.jsx)(l,{theme:`solid`,type:`primary`,icon:(0,j.jsx)(f,{}),onClick:()=>h({role:null,permissions:[],form:{}}),children:`新建角色`}),children:[(0,j.jsx)(C,{error:V.error,onRetry:()=>V.reload()}),(0,j.jsx)(D,{columns:[{title:`角色`,dataIndex:`name`,render:(e,t)=>(0,j.jsxs)(i,{vertical:!0,spacing:0,align:`start`,children:[(0,j.jsx)(P,{strong:!0,children:e}),(0,j.jsx)(P,{type:`tertiary`,children:t.description||`—`})]})},{title:`数据范围`,dataIndex:`scope`,width:130,render:e=>e?(0,j.jsx)(o,{size:`small`,children:e}):`—`},{title:`权限数`,dataIndex:`permissions`,width:100,render:e=>Array.isArray(e)?e.length:0},{title:`系统内置`,dataIndex:`is_system`,width:100,render:e=>e?(0,j.jsx)(o,{size:`small`,color:`blue`,children:`是`}):`否`},{title:`操作`,width:160,render:(e,t)=>(0,j.jsxs)(i,{children:[n&&!t.is_system&&(0,j.jsx)(l,{size:`small`,theme:`borderless`,icon:(0,j.jsx)(c,{}),onClick:()=>h({role:t,permissions:t.permissions||[],form:{}}),children:`编辑`}),n&&!t.is_system&&(0,j.jsx)(l,{size:`small`,theme:`borderless`,type:`danger`,icon:(0,j.jsx)(a,{}),onClick:()=>$(t)})]})}],dataSource:G,rowKey:`id`,loading:V.loading,pagination:{pageSize:20}})]})}),(0,j.jsx)(d.TabPane,{itemKey:`users`,tab:`平台用户(${K.length})`,children:(0,j.jsxs)(w,{extra:n&&(0,j.jsx)(l,{theme:`solid`,type:`primary`,icon:(0,j.jsx)(f,{}),onClick:()=>M({user:null,form:{}}),children:`新建用户`}),children:[(0,j.jsx)(C,{error:H.error,onRetry:()=>H.reload()}),(0,j.jsx)(D,{columns:[{title:`用户名`,dataIndex:`username`,render:(e,t)=>(0,j.jsxs)(i,{vertical:!0,spacing:0,align:`start`,children:[(0,j.jsx)(P,{strong:!0,children:e}),(0,j.jsx)(P,{type:`tertiary`,children:t.display_name||`—`})]})},{title:`角色`,dataIndex:`roles`,width:260,render:e=>Array.isArray(e)&&e.length?(0,j.jsx)(i,{wrap:!0,spacing:4,children:e.map(e=>(0,j.jsx)(o,{size:`small`,children:Y.get(e)||e},e))}):`—`},{title:`状态`,dataIndex:`enabled`,width:100,render:e=>e===!1?(0,j.jsx)(o,{size:`small`,color:`grey`,children:`停用`}):(0,j.jsx)(o,{size:`small`,color:`green`,children:`启用`})},{title:`操作`,width:160,render:(e,t)=>(0,j.jsxs)(i,{children:[n&&(0,j.jsx)(l,{size:`small`,theme:`borderless`,icon:(0,j.jsx)(c,{}),onClick:()=>M({user:t,form:{}}),children:`编辑`}),n&&(0,j.jsx)(l,{size:`small`,theme:`borderless`,type:`danger`,icon:(0,j.jsx)(a,{}),onClick:()=>ee(t)})]})}],dataSource:K,rowKey:`id`,loading:H.loading,pagination:{pageSize:20}})]})}),(0,j.jsx)(d.TabPane,{itemKey:`assignments`,tab:`资源授权(${q.length})`,children:(0,j.jsxs)(w,{extra:n&&(0,j.jsx)(l,{theme:`solid`,icon:(0,j.jsx)(f,{}),onClick:()=>L(!0),children:`新增资源授权`}),children:[(0,j.jsx)(C,{error:U.error,onRetry:()=>U.reload()}),(0,j.jsx)(D,{columns:[{title:`资源类型`,dataIndex:`resourceType`,width:140,render:e=>e||`—`},{title:`资源`,dataIndex:`resourceId`,render:(e,t)=>t.resourceLabel||e||`—`},{title:`用户`,dataIndex:`userId`,render:e=>X.get(e)||e||`—`},{title:`创建时间`,dataIndex:`createdAt`,width:180,render:e=>g(e)},{title:`操作`,width:100,render:(e,t)=>n&&(0,j.jsx)(l,{size:`small`,theme:`borderless`,type:`danger`,icon:(0,j.jsx)(a,{}),onClick:()=>te(t)})}],dataSource:q,rowKey:`id`,loading:U.loading,pagination:{pageSize:20}})]})})]}),n&&I&&(0,j.jsx)(N,{users:K,onCancel:()=>L(!1),onSaved:()=>{L(!1),U.reload()}}),(0,j.jsx)(S,{title:u?.role?`编辑角色`:`新建角色`,visible:!!u,onCancel:()=>h(null),onOk:Z,confirmLoading:R,width:820,children:u&&(0,j.jsxs)(p,{getFormApi:e=>h(t=>t&&{...t,form:e}),initValues:u.role?{name:u.role.name,description:u.role.description,scope:u.role.scope}:{},children:[(0,j.jsx)(p.Input,{field:`name`,label:`角色名称`,rules:[{required:!0,message:`请输入角色名称`}]}),(0,j.jsx)(p.Input,{field:`description`,label:`描述`}),(0,j.jsx)(p.Select,{field:`scope`,label:`数据范围`,optionList:(B.data?.scopes||[`all`,`assigned`,`own`]).map(e=>({value:e,label:e}))}),(0,j.jsx)(p.Slot,{label:`权限`,children:(0,j.jsx)(F,{value:u.permissions,options:W,onChange:e=>h(t=>({...t,permissions:e}))})})]})},u?.role?.id||`new-rbac-role`),(0,j.jsx)(S,{title:T?.user?`编辑用户`:`新建用户`,visible:!!T,onCancel:()=>M(null),onOk:Q,confirmLoading:R,width:620,children:T&&(0,j.jsxs)(p,{getFormApi:e=>M(t=>t&&{...t,form:e}),initValues:T.user?{username:T.user.username,display_name:T.user.display_name,enabled:T.user.enabled!==!1,roles:T.user.roles||[]}:{enabled:!0,roles:[]},children:[(0,j.jsx)(p.Input,{field:`username`,label:`用户名`,disabled:!!T.user,rules:[{required:!0,message:`请输入用户名`}]}),(0,j.jsx)(p.Input,{field:`display_name`,label:`显示名称`}),(0,j.jsx)(p.Input,{field:`password`,label:T.user?`重置密码(留空不修改)`:`密码`,mode:`password`,rules:T.user?[]:[{required:!0,min:8,message:`至少 8 位`}]}),(0,j.jsx)(p.Select,{field:`roles`,label:`角色`,multiple:!0,optionList:J}),(0,j.jsx)(p.Slot,{label:`启用`,children:(0,j.jsx)(p.Switch,{field:`enabled`})})]})},T?.user?.id||`new-rbac-user`)]})}export{I as default}; |