mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-21 08:03:13 +09:00
Improved RoleViewPage
Add groups, search, better description format, and default / dangerous icons.
This commit is contained in:
2 files changed
+272
-71
No files matched your search
@@ -8,6 +8,7 @@ import BaseWebPlugin from "../BaseWebPlugin";
|
|||||||
|
|
||||||
type PluginExtraProps = {
|
type PluginExtraProps = {
|
||||||
component: string;
|
component: string;
|
||||||
|
search?: string;
|
||||||
instance?: lib.InstanceDetails;
|
instance?: lib.InstanceDetails;
|
||||||
user?: lib.UserDetails;
|
user?: lib.UserDetails;
|
||||||
host?: lib.HostDetails;
|
host?: lib.HostDetails;
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import React, { useEffect, useContext, useState } from "react";
|
import React, { useEffect, useContext, useState, useMemo } from "react";
|
||||||
import { useNavigate, useParams } from "react-router-dom";
|
import { useNavigate, useParams } from "react-router-dom";
|
||||||
import { Button, Checkbox, Form, Input, Popconfirm, Space, Spin } from "antd";
|
import { Button, Checkbox, Input, Popconfirm, Space, Spin, Tooltip } from "antd";
|
||||||
import DeleteOutlined from "@ant-design/icons/DeleteOutlined";
|
import { ExclamationCircleOutlined, StarOutlined, DeleteOutlined} from "@ant-design/icons";
|
||||||
|
|
||||||
import * as lib from "@clusterio/lib";
|
import * as lib from "@clusterio/lib";
|
||||||
|
|
||||||
@@ -13,20 +13,165 @@ import PageLayout from "./PageLayout";
|
|||||||
import PluginExtra from "./PluginExtra";
|
import PluginExtra from "./PluginExtra";
|
||||||
import { notifyErrorHandler } from "../util/notify";
|
import { notifyErrorHandler } from "../util/notify";
|
||||||
|
|
||||||
|
|
||||||
|
/** Splits a permission name into parts, removing the last part */
|
||||||
|
function getGroupName(name: string) {
|
||||||
|
const parts = name.split(".");
|
||||||
|
return parts.length <= 2
|
||||||
|
? parts.slice(0, 2).join(".")
|
||||||
|
: parts.slice(0, parts.length - 1).join(".");
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Converts a group name into Title Case with _ and . removed */
|
||||||
|
function formatGroupTitle(name: string) {
|
||||||
|
return name
|
||||||
|
.replace(/\./g, " / ")
|
||||||
|
.replace(/_/g, " ")
|
||||||
|
.split(" ")
|
||||||
|
.filter(Boolean)
|
||||||
|
.map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
|
||||||
|
.join(" ");
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Check if a permission is dangerous, in the future we might have metadata for this */
|
||||||
|
function isDangerous(permission: lib.Permission) {
|
||||||
|
return ["delete", "kill", "stop", "revoke", "update", "core.admin"]
|
||||||
|
.some(word => permission.name.includes(word));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** A single permission item inside a group; includes a checkbox, name, and description */
|
||||||
|
function PermissionItem({
|
||||||
|
permission, baseline, checked, disabled, onChange,
|
||||||
|
}: {
|
||||||
|
permission: lib.Permission;
|
||||||
|
checked: boolean;
|
||||||
|
baseline: boolean;
|
||||||
|
disabled: boolean;
|
||||||
|
onChange: (value: boolean) => void;
|
||||||
|
}) {
|
||||||
|
const modified = checked !== baseline;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div style={{
|
||||||
|
marginBottom: 8,
|
||||||
|
padding: modified ? "4px 6px" : undefined,
|
||||||
|
background: modified ? "#2a1912" : undefined,
|
||||||
|
borderRadius: 4,
|
||||||
|
}}>
|
||||||
|
<div style={{ display: "flex", alignItems: "center" }}>
|
||||||
|
<Checkbox checked={checked} disabled={disabled} onChange={e => onChange(e.target.checked)}>
|
||||||
|
<Space size={6}>
|
||||||
|
<span>{permission.title}</span>
|
||||||
|
|
||||||
|
{permission.grantByDefault && (
|
||||||
|
<Tooltip title="Granted by default">
|
||||||
|
<StarOutlined style={{ color: "#faad14" }} />
|
||||||
|
</Tooltip>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{isDangerous(permission) && (
|
||||||
|
<Tooltip title="Dangerous permission">
|
||||||
|
<ExclamationCircleOutlined style={{ color: "#ff4d4f" }} />
|
||||||
|
</Tooltip>
|
||||||
|
)}
|
||||||
|
</Space>
|
||||||
|
</Checkbox>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div style={{ marginLeft: 24, color: "#888", fontSize: 12 }}>
|
||||||
|
{permission.description}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** A group of permissions; includes a title, clear and select all buttons, and the permission items */
|
||||||
|
function PermissionGroup({
|
||||||
|
groupName, permissions, canUpdate, baselineState, permissionState, setPermissionState,
|
||||||
|
}: {
|
||||||
|
groupName: string;
|
||||||
|
permissions: lib.Permission[];
|
||||||
|
canUpdate: boolean;
|
||||||
|
|
||||||
|
baselineState: Record<string, boolean>;
|
||||||
|
permissionState: Record<string, boolean>;
|
||||||
|
setPermissionState: React.Dispatch<React.SetStateAction<Record<string, boolean>>>;
|
||||||
|
setEdited?: (value: boolean) => void;
|
||||||
|
}) {
|
||||||
|
function setGroup(value: boolean) {
|
||||||
|
setPermissionState((prev: any) => {
|
||||||
|
const next = { ...prev };
|
||||||
|
for (let p of permissions) {
|
||||||
|
next[p.name] = value;
|
||||||
|
}
|
||||||
|
return next;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div style={{ border: "1px solid #f0f0f0", padding: 12, borderRadius: 6 }}>
|
||||||
|
<Space style={{ width: "100%", justifyContent: "space-between" }}>
|
||||||
|
<strong>{formatGroupTitle(groupName)}</strong>
|
||||||
|
<Space>
|
||||||
|
<Button size="small" onClick={() => setGroup(true)}>Select all</Button>
|
||||||
|
<Button size="small" onClick={() => setGroup(false)}>Clear</Button>
|
||||||
|
</Space>
|
||||||
|
</Space>
|
||||||
|
|
||||||
|
<div style={{ marginTop: 8 }}>
|
||||||
|
{permissions.map((p: any) => (
|
||||||
|
<PermissionItem
|
||||||
|
key={p.name}
|
||||||
|
permission={p}
|
||||||
|
checked={permissionState[p.name]}
|
||||||
|
baseline={baselineState[p.name]}
|
||||||
|
disabled={!canUpdate}
|
||||||
|
onChange={v => setPermissionState((prev: any) => ({ ...prev, [p.name]: v }))}
|
||||||
|
/>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** The role page view */
|
||||||
export default function RoleViewPage() {
|
export default function RoleViewPage() {
|
||||||
let params = useParams();
|
const params = useParams();
|
||||||
let roleId = Number(params.id);
|
const roleId = Number(params.id);
|
||||||
|
|
||||||
let navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
|
const account = useAccount();
|
||||||
let account = useAccount();
|
const control = useContext(ControlContext);
|
||||||
let control = useContext(ControlContext);
|
|
||||||
const [role, synced] = useRole(roleId);
|
const [role, synced] = useRole(roleId);
|
||||||
let [edited, setEdited] = useState(false);
|
|
||||||
|
|
||||||
|
const [name, setName] = useState("");
|
||||||
|
const [description, setDescription] = useState("");
|
||||||
|
const [permissionState, setPermissionState] = useState<Record<string, boolean>>({});
|
||||||
|
const [baselineState, setBaselineState] = useState<Record<string, boolean>>({});
|
||||||
|
const [search, setSearch] = useState("");
|
||||||
|
|
||||||
let nav = [{ name: "Roles", path: "/roles" }, { name: role?.name ?? String(roleId) }];
|
const allPermissions = useMemo(() => [...lib.permissions.values()], []);
|
||||||
|
const canUpdate = Boolean(account.hasPermission("core.role.update"));
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!role) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const base = Object.fromEntries(
|
||||||
|
allPermissions.map(p => [p.name, role.permissions.has(p.name)])
|
||||||
|
);
|
||||||
|
|
||||||
|
setName(role.name);
|
||||||
|
setDescription(role.description);
|
||||||
|
setPermissionState(base);
|
||||||
|
setBaselineState(base);
|
||||||
|
}, [role, allPermissions]);
|
||||||
|
|
||||||
|
// If no role then display either the loading screen or the not found screen
|
||||||
if (!role) {
|
if (!role) {
|
||||||
|
const nav = [{ name: "Roles", path: "/roles" }, { name: String(roleId) }];
|
||||||
|
|
||||||
if (!synced) {
|
if (!synced) {
|
||||||
return <PageLayout nav={nav}>
|
return <PageLayout nav={nav}>
|
||||||
<PageHeader title={String(roleId)} />
|
<PageHeader title={String(roleId)} />
|
||||||
@@ -40,81 +185,136 @@ export default function RoleViewPage() {
|
|||||||
</PageLayout>;
|
</PageLayout>;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Update displayed perms when role perms change and have not been edited locally
|
// Apply the filter by searching all permission fields
|
||||||
let initialValues = {
|
const filtered = allPermissions.filter(p => {
|
||||||
name: role["name"],
|
const q = search.toLowerCase();
|
||||||
description: role["description"],
|
return p.name.toLowerCase().includes(q)
|
||||||
permissions: {
|
|| p.title.toLowerCase().includes(q)
|
||||||
...Object.fromEntries([...lib.permissions.values()].map(perm => [
|
|| p.description.toLowerCase().includes(q);
|
||||||
perm.name, [...role.permissions].includes(perm.name),
|
});
|
||||||
])),
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
let canUpdate = account.hasPermission("core.role.update");
|
// Organise the permissions into groups
|
||||||
return <PageLayout nav={nav}>
|
const groups = new Map<string, typeof filtered>();
|
||||||
<Form
|
for (let p of filtered) {
|
||||||
initialValues={initialValues}
|
const g = getGroupName(p.name);
|
||||||
onValuesChange={() => { setEdited(true); }}
|
if (!groups.has(g)) {
|
||||||
onFinish={values => {
|
groups.set(g, []);
|
||||||
let newPermissions = [];
|
}
|
||||||
for (let [name, value] of Object.entries(values.permissions)) {
|
groups.get(g)!.push(p);
|
||||||
if (value) { newPermissions.push(name); }
|
}
|
||||||
}
|
|
||||||
|
|
||||||
control.send(
|
// Sort permissions alphabetically within their groups
|
||||||
new lib.RoleUpdateRequest(roleId, values.name || "", values.description || "", newPermissions)
|
const sortedGroups = [...groups.entries()].sort(([a], [b]) => a.localeCompare(b));
|
||||||
).then(() => {
|
for (let [, permissions] of sortedGroups) {
|
||||||
setEdited(false);
|
permissions.sort((a, b) => a.title.localeCompare(b.title));
|
||||||
}).catch(notifyErrorHandler("Error applying changes"));
|
}
|
||||||
}}
|
|
||||||
>
|
// Check if any changes have been made to the permissions or role metadata
|
||||||
|
const permissionChanged = Object.keys(permissionState).some(k => permissionState[k] !== baselineState[k]);
|
||||||
|
const metaChanged = name !== role.name || description !== role.description;
|
||||||
|
const edited = permissionChanged || metaChanged;
|
||||||
|
|
||||||
|
/** Applies all pending changes */
|
||||||
|
function applyChanges() {
|
||||||
|
const newPermissions = Object.entries(permissionState).filter(([, v]) => v).map(([k]) => k);
|
||||||
|
control.send(new lib.RoleUpdateRequest(roleId, name || "", description || "", newPermissions))
|
||||||
|
.then(() => {
|
||||||
|
setBaselineState(permissionState);
|
||||||
|
})
|
||||||
|
.catch(notifyErrorHandler("Error applying changes"));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Revert any pending changes */
|
||||||
|
function revertChanges() {
|
||||||
|
setPermissionState(baselineState);
|
||||||
|
if (role) {
|
||||||
|
setName(role.name);
|
||||||
|
setDescription(role.description);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<PageLayout nav={[{ name: "Roles", path: "/roles" }, { name: role.name }]}>
|
||||||
<PageHeader
|
<PageHeader
|
||||||
title={role.name}
|
title={role.name}
|
||||||
extra={<Space wrap>
|
extra={<Space wrap>
|
||||||
{canUpdate && <Button type={edited ? "primary" : "default"} htmlType="submit">Apply</Button>}
|
|
||||||
{account.hasPermission("core.role.delete") && <Popconfirm
|
{account.hasPermission("core.role.delete") && <Popconfirm
|
||||||
title="Delete role?"
|
title="Delete role?"
|
||||||
placement="bottomRight"
|
placement="bottomRight"
|
||||||
okText="Delete"
|
okText="Delete"
|
||||||
okButtonProps={{ danger: true }}
|
okButtonProps={{ danger: true }}
|
||||||
onConfirm={() => {
|
onConfirm={() => {
|
||||||
control.send(
|
control.send(new lib.RoleDeleteRequest(roleId))
|
||||||
new lib.RoleDeleteRequest(roleId)
|
.then(() => navigate("/roles"))
|
||||||
).then(() => {
|
.catch(notifyErrorHandler("Error deleting role"));
|
||||||
navigate("/roles");
|
|
||||||
}).catch(notifyErrorHandler("Error deleting role"));
|
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<Button danger >
|
<Button danger><DeleteOutlined /></Button>
|
||||||
<DeleteOutlined />
|
|
||||||
</Button>
|
|
||||||
</Popconfirm>}
|
</Popconfirm>}
|
||||||
</Space>}
|
</Space>}
|
||||||
/>
|
/>
|
||||||
<Form.Item name="name" label="Name">
|
|
||||||
<Input disabled={!canUpdate}/>
|
{edited && (
|
||||||
</Form.Item>
|
<div style={{
|
||||||
<Form.Item name="description" label="Description">
|
position: "fixed",
|
||||||
<Input disabled={!canUpdate}/>
|
bottom: 24,
|
||||||
</Form.Item>
|
left: "50%",
|
||||||
|
transform: "translateX(-50%)",
|
||||||
|
background: "#2a1912",
|
||||||
|
borderRadius: 8,
|
||||||
|
boxShadow: "0 8px 24px rgba(0,0,0,0.12)",
|
||||||
|
padding: "10px 14px",
|
||||||
|
display: "flex",
|
||||||
|
alignItems: "center",
|
||||||
|
gap: 12,
|
||||||
|
zIndex: 1000,
|
||||||
|
}}>
|
||||||
|
<span style={{ color: "#FFF" }}>
|
||||||
|
You have unsaved changes
|
||||||
|
</span>
|
||||||
|
|
||||||
|
<Space>
|
||||||
|
<Button onClick={revertChanges}>
|
||||||
|
Revert
|
||||||
|
</Button>
|
||||||
|
|
||||||
|
<Button type="primary" onClick={applyChanges}>
|
||||||
|
Apply
|
||||||
|
</Button>
|
||||||
|
</Space>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
<div style={{ marginBottom: 16, display: "flex", alignItems: "center", gap: 12 }}>
|
||||||
|
<label style={{ width: 110, flexShrink: 0 }}>Name</label>
|
||||||
|
<Input value={name} disabled={!canUpdate} onChange={e => setName(e.target.value)} />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div style={{ marginBottom: 16, display: "flex", alignItems: "center", gap: 12 }}>
|
||||||
|
<label style={{ width: 110, flexShrink: 0 }}>Description</label>
|
||||||
|
<Input value={description} disabled={!canUpdate} onChange={e => setDescription(e.target.value)} />
|
||||||
|
</div>
|
||||||
|
|
||||||
<h3>Permissions</h3>
|
<h3>Permissions</h3>
|
||||||
{[...lib.permissions.values()].map(({name, title, description}) => (
|
|
||||||
<Form.Item
|
<Input.Search placeholder="Search permissions" allowClear
|
||||||
name={["permissions", name]}
|
onChange={e => setSearch(e.target.value)} style={{ marginBottom: 16 }} />
|
||||||
key={name}
|
|
||||||
label={title}
|
<Space direction="vertical" style={{ width: "100%" }}>
|
||||||
tooltip={description}
|
{sortedGroups.map(([groupName, permissions]) => (
|
||||||
valuePropName="checked"
|
<PermissionGroup
|
||||||
labelCol={{ sm: { span: 22, push: 2 }}}
|
key={groupName}
|
||||||
wrapperCol={{ sm: { span: 2, pull: 22 }}}
|
groupName={groupName}
|
||||||
labelAlign="left"
|
permissions={permissions}
|
||||||
colon={false}
|
permissionState={permissionState}
|
||||||
>
|
baselineState={baselineState}
|
||||||
<Checkbox disabled={!canUpdate} />
|
setPermissionState={setPermissionState}
|
||||||
</Form.Item>
|
canUpdate={canUpdate}
|
||||||
))}
|
/>
|
||||||
</Form>
|
))}
|
||||||
<PluginExtra component="RoleViewPage" role={role} />
|
</Space>
|
||||||
</PageLayout>;
|
|
||||||
|
<PluginExtra component="RoleViewPage" role={role} search={search}/>
|
||||||
|
</PageLayout>
|
||||||
|
);
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user