Add hidden and readonly config properties (#665)

This commit is contained in:
Cooldude2606 authored and GitHub committed 2024-10-27 23:02:45 +00:00
commit c49bc07e15
8 files changed
+101 -57

No files matched your search

+1
View File
@@ -36,6 +36,7 @@ Many thanks to the following for contributing to this release:
- Monorepos are now supported for external plugins [#654](https://github.com/clusterio/clusterio/pull/654). - Monorepos are now supported for external plugins [#654](https://github.com/clusterio/clusterio/pull/654).
- Changed default role selection to be a dropdown menu rather than id [#664](https://github.com/clusterio/clusterio/pull/664). - Changed default role selection to be a dropdown menu rather than id [#664](https://github.com/clusterio/clusterio/pull/664).
- Added credential option to config entries to allow writing sensitive data that can't be read back remotely. - Added credential option to config entries to allow writing sensitive data that can't be read back remotely.
- Allow hidden and readonly graphical representations of config values. [#665](https://github.com/clusterio/clusterio/pull/665).
### Changes ### Changes
+19 -5
View File
@@ -118,10 +118,21 @@ export interface FieldDefinition {
*/ */
access?: ConfigLocation[]; access?: ConfigLocation[];
/** /**
* If present treat the config value as a credential and only allow reading * If present treat the config value as a credential.
* from the locations specified. * Only the locations specified will be able to read the current value.
* Access can be used to specify which locations are able to overwrite the value.
*/ */
credential?: ConfigLocation[]; credential?: ConfigLocation[];
/**
* If present treat the config value as readonly.
* Only the locations specified will be able to update the config value.
* Access can be used to specify which locations are able to read the value.
*/
readonly?: ConfigLocation[];
/**
* True if graphical interfaces should hide the config value
*/
hidden?: boolean;
} }
export type ConfigDefs<Fields> = { export type ConfigDefs<Fields> = {
@@ -231,11 +242,14 @@ export class Config<
*/ */
_checkAccess(name: string, def: FieldDefinition, location: ConfigLocation, mode: ConfigAccess, error: boolean) { _checkAccess(name: string, def: FieldDefinition, location: ConfigLocation, mode: ConfigAccess, error: boolean) {
// If credential array is present it acts as read permission and the access array as write permission, // If credential array is present it acts as read permission and the access array as write permission,
// if not present access array acts as a combined read/write permission check. // If readonly array is present it acts as write permission and the access array as the read permission.
// If neither is present access array acts as a combined read/write permission check.
if ( if (
def.credential && mode & ConfigAccess.read && !(def.credential).includes(location) def.credential && mode & ConfigAccess.read && !(def.credential).includes(location)
|| def.readonly && mode & ConfigAccess.write && !(def.readonly).includes(location)
|| ( || (
(!def.credential || mode & ConfigAccess.write) (!def.credential || mode & ConfigAccess.write)
&& (!def.readonly || mode & ConfigAccess.read)
&& !(def.access ?? this.defaultAccess).includes(location) && !(def.access ?? this.defaultAccess).includes(location)
) )
) { ) {
@@ -295,7 +309,7 @@ export class Config<
for (let [name, value] of Object.entries(this.fields)) { for (let [name, value] of Object.entries(this.fields)) {
let def = this.constructor.fieldDefinitions[name]; let def = this.constructor.fieldDefinitions[name];
if ( if (
!this._checkAccess(name, def, this.location, ConfigAccess.write, false) this.location !== "control" && !this._checkAccess(name, def, this.location, ConfigAccess.write, false)
|| this.location !== remote && !this._checkAccess(name, def, remote, ConfigAccess.read, false) || this.location !== remote && !this._checkAccess(name, def, remote, ConfigAccess.read, false)
) { ) {
continue; continue;
@@ -344,7 +358,7 @@ export class Config<
} }
if ( if (
!this._checkAccess(name, def, remote, ConfigAccess.write, false) this.location !== "control" && !this._checkAccess(name, def, remote, ConfigAccess.write, false)
) { ) {
continue; continue;
} }
+3
View File
@@ -223,6 +223,7 @@ export class HostConfig extends classes.Config<HostConfigFields> {
description: "ID of the host", description: "ID of the host",
type: "number", type: "number",
initialValue: () => Math.random() * 2**31 | 0, initialValue: () => Math.random() * 2**31 | 0,
hidden: true,
}, },
"host.factorio_directory": { "host.factorio_directory": {
description: "Path to directory to look for factorio installs", description: "Path to directory to look for factorio installs",
@@ -345,10 +346,12 @@ export class InstanceConfig extends classes.Config<InstanceConfigFields> {
description: "ID of the instance", description: "ID of the instance",
type: "number", type: "number",
initialValue: () => Math.random() * 2**31 | 0, initialValue: () => Math.random() * 2**31 | 0,
hidden: true,
}, },
"instance.assigned_host": { "instance.assigned_host": {
type: "number", type: "number",
optional: true, optional: true,
hidden: true,
}, },
"instance.auto_start": { "instance.auto_start": {
description: "Automatically start this instance when the host hosting it is started up", description: "Automatically start this instance when the host hosting it is started up",
+1
View File
@@ -24,6 +24,7 @@ export interface PluginLoginForm {
} }
export interface InputComponentProps { export interface InputComponentProps {
disabled?: boolean,
fieldDefinition: FieldDefinition, fieldDefinition: FieldDefinition,
value: null | boolean | number | string, value: null | boolean | number | string,
onChange: (value: null | boolean | number | string) => void, onChange: (value: null | boolean | number | string) => void,
@@ -32,24 +32,24 @@ function getInitialValues(config: lib.Config<any>, props: BaseConfigTreeProps) {
return initialValues; return initialValues;
} }
function renderInput(inputComponents: Record<string, InputComponent>, def: lib.FieldDefinition) { function renderInput(inputComponents: Record<string, InputComponent>, def: lib.FieldDefinition, readonly: boolean) {
if (def.inputComponent && Object.prototype.hasOwnProperty.call(inputComponents, def.inputComponent)) { if (def.inputComponent && Object.prototype.hasOwnProperty.call(inputComponents, def.inputComponent)) {
// Field.Item will provide the value and onChange props to the component. // Field.Item will provide the value and onChange props to the component.
type InputPartial = React.ComponentClass<Omit<InputComponentProps, "value" | "onChange">>; type InputPartial = React.ComponentClass<Omit<InputComponentProps, "value" | "onChange">>;
const CustomInput = inputComponents[def.inputComponent] as unknown as InputPartial; const CustomInput = inputComponents[def.inputComponent] as unknown as InputPartial;
return <CustomInput fieldDefinition={def} />; return <CustomInput fieldDefinition={def} disabled={readonly} />;
} }
if (def.type === "boolean") { if (def.type === "boolean") {
return <Checkbox/>; return <Checkbox disabled={readonly} />;
} }
if (def.type === "string") { if (def.type === "string") {
if (def.credential) { if (def.credential) {
return <Input.Password autoComplete={def.autoComplete} />; return <Input.Password autoComplete={def.autoComplete} disabled={readonly} />;
} }
return <Input autoComplete={def.autoComplete} />; return <Input autoComplete={def.autoComplete} disabled={readonly} />;
} }
if (def.type === "number") { if (def.type === "number") {
return <InputNumber autoComplete={def.autoComplete} />; return <InputNumber autoComplete={def.autoComplete} disabled={readonly} />;
} }
return `Unknown type ${def.type}`; return `Unknown type ${def.type}`;
@@ -264,13 +264,17 @@ function computeTreeData(
}; };
for (let [field, def] of Object.entries(groupDefs)) { for (let [field, def] of Object.entries(groupDefs)) {
const canWrite = config.canAccess(`${groupName}.${field}`, lib.ConfigAccess.write); if (def.hidden === true) {
const canRead = config.canAccess(`${groupName}.${field}`, lib.ConfigAccess.read); continue;
}
const fieldName = `${groupName}.${field}`;
const canWrite = config.canAccess(fieldName, lib.ConfigAccess.write);
const canRead = config.canAccess(fieldName, lib.ConfigAccess.read);
if (!canWrite && !canRead) { if (!canWrite && !canRead) {
continue; continue;
} }
let fieldName = `${groupName}.${field}`;
let value = config.get(fieldName) as any; let value = config.get(fieldName) as any;
let childNode: ChildNode = { let childNode: ChildNode = {
key: fieldName, key: fieldName,
@@ -311,7 +315,7 @@ function computeTreeData(
return Promise.resolve(); return Promise.resolve();
}}]} }}]}
> >
<Input className="json-input" /> <Input className="json-input" disabled={!canWrite} />
</Form.Item>, </Form.Item>,
}); });
@@ -319,47 +323,53 @@ function computeTreeData(
propsMap.set(propPath, [fieldName, prop]); propsMap.set(propPath, [fieldName, prop]);
} }
let newPropPath = `${groupName}.${field}:add`; if (canWrite) {
childNode.children.push({ let newPropPath = `${groupName}.${field}:add`;
key: newPropPath, childNode.children.push({
title: <Space> key: newPropPath,
<Form.Item title: <Space>
name={`${newPropPath}.name`} <Form.Item
noStyle={true} name={`${newPropPath}.name`}
> noStyle={true}
<Input /> >
</Form.Item>: <Input />
<Form.Item </Form.Item>:
name={`${newPropPath}.value`} <Form.Item
noStyle={true} name={`${newPropPath}.value`}
> noStyle={true}
<Input className="json-input" /> >
</Form.Item> <Input className="json-input" />
<Button </Form.Item>
size="small" <Button
onClick={() => { size="small"
let propName = form.getFieldValue(`${newPropPath}.name`); onClick={() => {
let propValue = form.getFieldValue(`${newPropPath}.value`); let propName = form.getFieldValue(`${newPropPath}.name`);
if (!value || !Object.prototype.hasOwnProperty.call(value, propName)) { let propValue = form.getFieldValue(`${newPropPath}.value`);
let newConfig = ConfigClass.fromJSON(config!.toJSON(), "control"); if (!value || !Object.prototype.hasOwnProperty.call(value, propName)) {
(newConfig as lib.Config<any>).setProp(fieldName, propName, null); let newConfig = ConfigClass.fromJSON(config!.toJSON(), "control");
setConfig(newConfig); (newConfig as lib.Config<any>).setProp(fieldName, propName, null);
} setConfig(newConfig);
}
let propPath = `${groupName}.${field}.${propName}`; let propPath = `${groupName}.${field}.${propName}`;
form.setFieldsValue({ form.setFieldsValue({
[`${newPropPath}.name`]: "", [`${newPropPath}.name`]: "",
[`${newPropPath}.value`]: "", [`${newPropPath}.value`]: "",
[propPath]: propValue, [propPath]: propValue,
}); });
form.validateFields([propPath]); form.validateFields([propPath]);
onValuesChange({ [propPath]: propValue }); onValuesChange({ [propPath]: propValue });
}} }}
> >
Add Add
</Button> </Button>
</Space>, </Space>,
}); });
}
if (!childNode.children.length) {
continue;
}
} else { } else {
childNode.title = <Form.Item childNode.title = <Form.Item
@@ -373,7 +383,7 @@ function computeTreeData(
tooltip={def.description} tooltip={def.description}
valuePropName={def.type === "boolean" ? "checked" : "value"} valuePropName={def.type === "boolean" ? "checked" : "value"}
> >
{renderInput(control.inputComponents, def)} {renderInput(control.inputComponents, def, !canWrite)}
</Form.Item>; </Form.Item>;
initialValues[fieldName] = value; initialValues[fieldName] = value;
} }
@@ -15,6 +15,6 @@ export default function InputModPack(props: InputComponentProps) {
value: modPack.id, value: modPack.id,
}))} }))}
allowClear={props.fieldDefinition.optional} allowClear={props.fieldDefinition.optional}
disabled={props.disabled}
/>; />;
} }
@@ -26,5 +26,6 @@ export default function InputRole(props: InputComponentProps) {
value: role.id, value: role.id,
}))} }))}
allowClear={props.fieldDefinition.optional} allowClear={props.fieldDefinition.optional}
disabled={props.disabled}
/>; />;
} }
+14
View File
@@ -27,6 +27,7 @@ describe("lib/config/classes", function() {
"test.json": { type: "object", initialValue: {}, optional: true }, "test.json": { type: "object", initialValue: {}, optional: true },
"test.priv": { access: ["local"], type: "string", optional: true }, "test.priv": { access: ["local"], type: "string", optional: true },
"test.cred": { credential: ["local"] }, "test.cred": { credential: ["local"] },
"test.rdo": { readonly: ["local"] },
}; };
constructor(location, fields) { constructor(location, fields) {
@@ -74,6 +75,7 @@ describe("lib/config/classes", function() {
"test.json": {}, "test.json": {},
"test.priv": null, "test.priv": null,
"test.cred": null, "test.cred": null,
"test.rdo": null,
}); });
}); });
}); });
@@ -89,6 +91,7 @@ describe("lib/config/classes", function() {
"test.func": 42, "test.func": 42,
"test.bool": false, "test.bool": false,
"test.json": {}, "test.json": {},
"test.rdo": null,
}); });
}); });
}); });
@@ -151,6 +154,7 @@ describe("lib/config/classes", function() {
"test.json": {}, "test.json": {},
"test.priv": null, "test.priv": null,
"test.cred": null, "test.cred": null,
"test.rdo": null,
"test.alpha": null, "test.alpha": null,
"test.beta": "decay", "test.beta": "decay",
"test.gamma": 99, "test.gamma": 99,
@@ -185,6 +189,7 @@ describe("lib/config/classes", function() {
"test.json": {}, "test.json": {},
"test.priv": null, "test.priv": null,
"test.cred": null, "test.cred": null,
"test.rdo": null,
"test.test": null, "test.test": null,
}); });
}); });
@@ -256,6 +261,9 @@ describe("lib/config/classes", function() {
assert.equal(testInstance.canAccess("test.cred", CA.read), true); assert.equal(testInstance.canAccess("test.cred", CA.read), true);
assert.equal(testInstance.canAccess("test.cred", CA.read, "local"), true); assert.equal(testInstance.canAccess("test.cred", CA.read, "local"), true);
assert.equal(testInstance.canAccess("test.cred", CA.read, "remote"), false); assert.equal(testInstance.canAccess("test.cred", CA.read, "remote"), false);
assert.equal(testInstance.canAccess("test.rdo", CA.read), true);
assert.equal(testInstance.canAccess("test.rdo", CA.read, "local"), true);
assert.equal(testInstance.canAccess("test.rdo", CA.read, "remote"), true);
}); });
it("should return true checking write for fields that are writeable", function() { it("should return true checking write for fields that are writeable", function() {
let testInstance = new TestConfig("local"); let testInstance = new TestConfig("local");
@@ -271,6 +279,9 @@ describe("lib/config/classes", function() {
assert.equal(testInstance.canAccess("test.cred", CA.write), true); assert.equal(testInstance.canAccess("test.cred", CA.write), true);
assert.equal(testInstance.canAccess("test.cred", CA.write, "local"), true); assert.equal(testInstance.canAccess("test.cred", CA.write, "local"), true);
assert.equal(testInstance.canAccess("test.cred", CA.write, "remote"), true); assert.equal(testInstance.canAccess("test.cred", CA.write, "remote"), true);
assert.equal(testInstance.canAccess("test.rdo", CA.write), true);
assert.equal(testInstance.canAccess("test.rdo", CA.write, "local"), true);
assert.equal(testInstance.canAccess("test.rdo", CA.write, "remote"), false);
}); });
it("should return true for checking readWrite for fields that are readable and writeable", function() { it("should return true for checking readWrite for fields that are readable and writeable", function() {
let testInstance = new TestConfig("local"); let testInstance = new TestConfig("local");
@@ -286,6 +297,9 @@ describe("lib/config/classes", function() {
assert.equal(testInstance.canAccess("test.cred", CA.readWrite), true); assert.equal(testInstance.canAccess("test.cred", CA.readWrite), true);
assert.equal(testInstance.canAccess("test.cred", CA.readWrite, "local"), true); assert.equal(testInstance.canAccess("test.cred", CA.readWrite, "local"), true);
assert.equal(testInstance.canAccess("test.cred", CA.readWrite, "remote"), false); assert.equal(testInstance.canAccess("test.cred", CA.readWrite, "remote"), false);
assert.equal(testInstance.canAccess("test.rdo", CA.readWrite), true);
assert.equal(testInstance.canAccess("test.rdo", CA.readWrite, "local"), true);
assert.equal(testInstance.canAccess("test.rdo", CA.readWrite, "remote"), false);
}); });
}); });