Credential option for config entries

Implement a credential option to config entries which is a list of
locations the config entry can be read from.  When specified and the
option is a string field this gets rendered as a password field in the
config UI.  If the credential is not readable from control the value of
the fields is cleared when the UI is refreshed.
This commit is contained in:
Hornwitser committed 2024-09-16 23:23:21 +02:00
1 parent 77fea7e295
commit eedf5b8f67
4 files changed
+168 -49

No files matched your search

@@ -16,13 +16,13 @@ const { Title } = Typography;
function getInitialValues(config: lib.Config<any>, props: BaseConfigTreeProps) {
let initialValues: any = {};
for (let [name, def] of Object.entries(config.constructor.fieldDefinitions)) {
if (!config.canAccess(name)) {
if (!config.canAccess(name, lib.ConfigAccess.read)) {
continue;
}
let value = config.get(name) as any;
if (def.type === "object") {
for (let prop of Object.keys(value)) {
for (let prop of Object.keys(value ?? {})) {
initialValues[`${name}.${prop}`] = JSON.stringify(value[prop]);
}
} else {
@@ -43,6 +43,9 @@ function renderInput(inputComponents: Record<string, InputComponent>, def: lib.F
return <Checkbox/>;
}
if (def.type === "string") {
if (def.credential) {
return <Input.Password/>;
}
return <Input/>;
}
if (def.type === "number") {
@@ -110,7 +113,8 @@ export default function BaseConfigTree(props: BaseConfigTreeProps) {
if (key.endsWith(":add.name") || key.endsWith(":add.value")) {
continue;
}
if (initialValues[key] === value) {
const def = config?.constructor.fieldDefinitions[key];
if (!def?.credential && initialValues[key] === value) {
changed = newChangedFields.delete(key) || changed;
} else {
changed = !newChangedFields.has(key) || changed;
@@ -213,6 +217,7 @@ export default function BaseConfigTree(props: BaseConfigTreeProps) {
</>;
}
// eslint-disable-next-line complexity
function computeTreeData(
control: Control,
form: FormInstance<any>,
@@ -259,7 +264,9 @@ function computeTreeData(
};
for (let [field, def] of Object.entries(groupDefs)) {
if (!config.canAccess(`${groupName}.${field}`)) {
const canWrite = config.canAccess(`${groupName}.${field}`, lib.ConfigAccess.write);
const canRead = config.canAccess(`${groupName}.${field}`, lib.ConfigAccess.read);
if (!canWrite && !canRead) {
continue;
}
@@ -279,7 +286,7 @@ function computeTreeData(
</>}
tooltip={def.description}
/>;
for (let prop of Object.keys(value)) {
for (let prop of Object.keys(value ?? {})) {
let propPath = `${groupName}.${field}.${prop}`;
let restart = Boolean(
def.restartRequiredProps && Number(def.restartRequired) ^ Number(restartRequiredProps.has(prop))
@@ -333,7 +340,7 @@ function computeTreeData(
onClick={() => {
let propName = form.getFieldValue(`${newPropPath}.name`);
let propValue = form.getFieldValue(`${newPropPath}.value`);
if (!Object.prototype.hasOwnProperty.call(value, propName)) {
if (!value || !Object.prototype.hasOwnProperty.call(value, propName)) {
let newConfig = ConfigClass.fromJSON(config!.toJSON(), "control");
(newConfig as lib.Config<any>).setProp(fieldName, propName, null);
setConfig(newConfig);