Files
SCM-Manager/scm-ui/ui-webapp/src/admin/components/form/ConfigForm.tsx

218 lines
6.7 KiB
TypeScript
Raw Normal View History

/*
* MIT License
*
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import React from "react";
import { WithTranslation, withTranslation } from "react-i18next";
import { Config, NamespaceStrategies } from "@scm-manager/ui-types";
import { Level, Notification, SubmitButton } from "@scm-manager/ui-components";
import ProxySettings from "./ProxySettings";
import GeneralSettings from "./GeneralSettings";
import BaseUrlSettings from "./BaseUrlSettings";
import LoginAttempt from "./LoginAttempt";
2018-08-13 16:33:56 +02:00
type Props = WithTranslation & {
submitForm: (p: Config) => void;
config?: Config;
loading?: boolean;
configReadPermission: boolean;
configUpdatePermission: boolean;
namespaceStrategies?: NamespaceStrategies;
2018-08-13 16:33:56 +02:00
};
type State = {
config: Config;
showNotification: boolean;
2018-08-21 08:40:49 +02:00
error: {
loginAttemptLimitTimeout: boolean;
loginAttemptLimit: boolean;
};
changed: boolean;
2018-08-13 16:33:56 +02:00
};
class ConfigForm extends React.Component<Props, State> {
constructor(props: Props) {
super(props);
this.state = {
config: {
proxyPassword: null,
proxyPort: 0,
proxyServer: "",
2018-08-13 16:33:56 +02:00
proxyUser: null,
enableProxy: false,
realmDescription: "",
2018-08-13 16:33:56 +02:00
disableGroupingGrid: false,
dateFormat: "",
anonymousMode: "OFF",
baseUrl: "",
2018-08-13 16:33:56 +02:00
forceBaseUrl: false,
loginAttemptLimit: 0,
proxyExcludes: [],
skipFailedAuthenticators: false,
pluginUrl: "",
2018-08-13 16:33:56 +02:00
loginAttemptLimitTimeout: 0,
enabledXsrfProtection: true,
namespaceStrategy: "",
loginInfoUrl: "",
_links: {}
},
2018-08-16 16:41:17 +02:00
showNotification: false,
2018-08-21 08:40:49 +02:00
error: {
loginAttemptLimitTimeout: false,
loginAttemptLimit: false
},
changed: false
2018-08-13 16:33:56 +02:00
};
}
componentDidMount() {
const { config, configUpdatePermission } = this.props;
2018-08-13 16:33:56 +02:00
if (config) {
this.setState({
...this.state,
config: {
...config
}
});
}
if (!configUpdatePermission) {
this.setState({
...this.state,
showNotification: true
});
2018-08-13 16:33:56 +02:00
}
}
submit = (event: Event) => {
event.preventDefault();
this.setState({
changed: false
});
2018-08-13 16:33:56 +02:00
this.props.submitForm(this.state.config);
};
render() {
2019-10-21 10:57:56 +02:00
const { loading, t, namespaceStrategies, configReadPermission, configUpdatePermission } = this.props;
const config = this.state.config;
let noPermissionNotification = null;
2019-03-04 16:47:44 +01:00
if (!configReadPermission) {
2019-10-21 10:57:56 +02:00
return <Notification type={"danger"} children={t("config.form.no-read-permission-notification")} />;
2019-03-04 16:47:44 +01:00
}
if (this.state.showNotification) {
noPermissionNotification = (
<Notification
type={"info"}
children={t("config.form.no-write-permission-notification")}
onClose={() => this.onClose()}
/>
);
}
2018-08-13 16:33:56 +02:00
return (
<form onSubmit={this.submit}>
{noPermissionNotification}
2018-08-13 16:33:56 +02:00
<GeneralSettings
namespaceStrategies={namespaceStrategies}
loginInfoUrl={config.loginInfoUrl}
2018-08-13 16:33:56 +02:00
realmDescription={config.realmDescription}
disableGroupingGrid={config.disableGroupingGrid}
dateFormat={config.dateFormat}
anonymousMode={config.anonymousMode}
2018-08-13 16:33:56 +02:00
skipFailedAuthenticators={config.skipFailedAuthenticators}
pluginUrl={config.pluginUrl}
enabledXsrfProtection={config.enabledXsrfProtection}
namespaceStrategy={config.namespaceStrategy}
2019-10-21 10:57:56 +02:00
onChange={(isValid, changedValue, name) => this.onChange(isValid, changedValue, name)}
hasUpdatePermission={configUpdatePermission}
2018-08-13 16:33:56 +02:00
/>
<hr />
2018-08-16 16:41:17 +02:00
<LoginAttempt
loginAttemptLimit={config.loginAttemptLimit}
loginAttemptLimitTimeout={config.loginAttemptLimitTimeout}
2019-10-21 10:57:56 +02:00
onChange={(isValid, changedValue, name) => this.onChange(isValid, changedValue, name)}
2018-08-16 16:41:17 +02:00
hasUpdatePermission={configUpdatePermission}
/>
<hr />
2018-08-13 16:33:56 +02:00
<BaseUrlSettings
baseUrl={config.baseUrl}
forceBaseUrl={config.forceBaseUrl}
2019-10-21 10:57:56 +02:00
onChange={(isValid, changedValue, name) => this.onChange(isValid, changedValue, name)}
hasUpdatePermission={configUpdatePermission}
2018-08-13 16:33:56 +02:00
/>
<hr />
2018-08-13 16:33:56 +02:00
<ProxySettings
proxyPassword={config.proxyPassword ? config.proxyPassword : ""}
2018-08-13 16:33:56 +02:00
proxyPort={config.proxyPort}
proxyServer={config.proxyServer ? config.proxyServer : ""}
proxyUser={config.proxyUser ? config.proxyUser : ""}
2018-08-13 16:33:56 +02:00
enableProxy={config.enableProxy}
2018-08-16 11:18:28 +02:00
proxyExcludes={config.proxyExcludes}
2019-10-21 10:57:56 +02:00
onChange={(isValid, changedValue, name) => this.onChange(isValid, changedValue, name)}
hasUpdatePermission={configUpdatePermission}
2018-08-13 16:33:56 +02:00
/>
<hr />
2019-11-26 15:22:22 +01:00
<Level
right={
<SubmitButton
loading={loading}
label={t("config.form.submit")}
disabled={!configUpdatePermission || this.hasError() || !this.state.changed}
/>
}
2018-08-13 16:33:56 +02:00
/>
</form>
);
}
onChange = (isValid: boolean, changedValue: any, name: string) => {
2018-08-16 16:41:17 +02:00
this.setState({
...this.state,
config: {
...this.state.config,
[name]: changedValue
2018-08-21 08:40:49 +02:00
},
error: {
...this.state.error,
[name]: !isValid
},
changed: true
2018-08-16 16:41:17 +02:00
});
};
2018-08-21 08:40:49 +02:00
hasError = () => {
2019-10-21 10:57:56 +02:00
return this.state.error.loginAttemptLimit || this.state.error.loginAttemptLimitTimeout;
2018-08-13 16:33:56 +02:00
};
onClose = () => {
this.setState({
...this.state,
showNotification: false
});
};
2018-08-13 16:33:56 +02:00
}
export default withTranslation("config")(ConfigForm);