2022-02-13 23:39:27 +08:00
|
|
|
// Copyright 2021 The Casdoor Authors. All Rights Reserved.
|
2021-12-17 16:32:53 +08:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
import React from "react";
|
2021-12-17 20:33:03 +08:00
|
|
|
import {Button, Card, Col, Input, InputNumber, Row, Select, Switch} from 'antd';
|
|
|
|
import {LinkOutlined} from "@ant-design/icons";
|
2021-12-17 16:32:53 +08:00
|
|
|
import * as SyncerBackend from "./backend/SyncerBackend";
|
|
|
|
import * as OrganizationBackend from "./backend/OrganizationBackend";
|
|
|
|
import * as Setting from "./Setting";
|
|
|
|
import i18next from "i18next";
|
2021-12-19 10:21:05 +08:00
|
|
|
import SyncerTableColumnTable from "./SyncerTableColumnTable";
|
2021-12-17 16:32:53 +08:00
|
|
|
|
2022-01-17 21:16:32 +08:00
|
|
|
import {Controlled as CodeMirror} from 'react-codemirror2';
|
|
|
|
import "codemirror/lib/codemirror.css";
|
|
|
|
require('codemirror/theme/material-darker.css');
|
|
|
|
require("codemirror/mode/javascript/javascript");
|
|
|
|
|
2021-12-17 16:32:53 +08:00
|
|
|
const { Option } = Select;
|
|
|
|
|
|
|
|
class SyncerEditPage extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
classes: props,
|
|
|
|
syncerName: props.match.params.syncerName,
|
|
|
|
syncer: null,
|
|
|
|
organizations: [],
|
2022-02-25 18:16:02 +08:00
|
|
|
mode: props.location.mode !== undefined ? props.location.mode : "edit",
|
2021-12-17 16:32:53 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
UNSAFE_componentWillMount() {
|
|
|
|
this.getSyncer();
|
|
|
|
this.getOrganizations();
|
|
|
|
}
|
|
|
|
|
|
|
|
getSyncer() {
|
|
|
|
SyncerBackend.getSyncer("admin", this.state.syncerName)
|
|
|
|
.then((syncer) => {
|
|
|
|
this.setState({
|
|
|
|
syncer: syncer,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getOrganizations() {
|
|
|
|
OrganizationBackend.getOrganizations("admin")
|
|
|
|
.then((res) => {
|
|
|
|
this.setState({
|
|
|
|
organizations: (res.msg === undefined) ? res : [],
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
parseSyncerField(key, value) {
|
|
|
|
if (["port"].includes(key)) {
|
|
|
|
value = Setting.myParseInt(value);
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
updateSyncerField(key, value) {
|
|
|
|
value = this.parseSyncerField(key, value);
|
|
|
|
|
|
|
|
let syncer = this.state.syncer;
|
|
|
|
syncer[key] = value;
|
|
|
|
this.setState({
|
|
|
|
syncer: syncer,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
renderSyncer() {
|
|
|
|
return (
|
|
|
|
<Card size="small" title={
|
|
|
|
<div>
|
2022-02-25 18:16:02 +08:00
|
|
|
{this.state.mode === "add" ? i18next.t("syncer:New Syncer") : i18next.t("syncer:Edit Syncer")}
|
2021-12-17 16:32:53 +08:00
|
|
|
<Button onClick={() => this.submitSyncerEdit(false)}>{i18next.t("general:Save")}</Button>
|
|
|
|
<Button style={{marginLeft: '20px'}} type="primary" onClick={() => this.submitSyncerEdit(true)}>{i18next.t("general:Save & Exit")}</Button>
|
2022-02-25 18:16:02 +08:00
|
|
|
{this.state.mode === "add" ? <Button style={{marginLeft: '20px'}} onClick={() => this.deleteSyncer()}>{i18next.t("general:Cancel")}</Button> : null}
|
2021-12-17 16:32:53 +08:00
|
|
|
</div>
|
|
|
|
} style={(Setting.isMobile())? {margin: '5px'}:{}} type="inner">
|
|
|
|
<Row style={{marginTop: '10px'}} >
|
|
|
|
<Col style={{marginTop: '5px'}} span={(Setting.isMobile()) ? 22 : 2}>
|
|
|
|
{Setting.getLabel(i18next.t("general:Organization"), i18next.t("general:Organization - Tooltip"))} :
|
|
|
|
</Col>
|
|
|
|
<Col span={22} >
|
|
|
|
<Select virtual={false} style={{width: '100%'}} value={this.state.syncer.organization} onChange={(value => {this.updateSyncerField('organization', value);})}>
|
|
|
|
{
|
|
|
|
this.state.organizations.map((organization, index) => <Option key={index} value={organization.name}>{organization.name}</Option>)
|
|
|
|
}
|
|
|
|
</Select>
|
|
|
|
</Col>
|
|
|
|
</Row>
|
|
|
|
<Row style={{marginTop: '20px'}} >
|
|
|
|
<Col style={{marginTop: '5px'}} span={(Setting.isMobile()) ? 22 : 2}>
|
|
|
|
{Setting.getLabel(i18next.t("general:Name"), i18next.t("general:Name - Tooltip"))} :
|
|
|
|
</Col>
|
|
|
|
<Col span={22} >
|
|
|
|
<Input value={this.state.syncer.name} onChange={e => {
|
|
|
|
this.updateSyncerField('name', e.target.value);
|
|
|
|
}} />
|
|
|
|
</Col>
|
|
|
|
</Row>
|
|
|
|
<Row style={{marginTop: '20px'}} >
|
|
|
|
<Col style={{marginTop: '5px'}} span={(Setting.isMobile()) ? 22 : 2}>
|
|
|
|
{Setting.getLabel(i18next.t("provider:Type"), i18next.t("provider:Type - Tooltip"))} :
|
|
|
|
</Col>
|
|
|
|
<Col span={22} >
|
|
|
|
<Select virtual={false} style={{width: '100%'}} value={this.state.syncer.type} onChange={(value => {this.updateSyncerField('type', value);})}>
|
|
|
|
{
|
|
|
|
['Database', 'LDAP']
|
|
|
|
.map((item, index) => <Option key={index} value={item}>{item}</Option>)
|
|
|
|
}
|
|
|
|
</Select>
|
|
|
|
</Col>
|
|
|
|
</Row>
|
|
|
|
<Row style={{marginTop: '20px'}} >
|
|
|
|
<Col style={{marginTop: '5px'}} span={(Setting.isMobile()) ? 22 : 2}>
|
|
|
|
{Setting.getLabel(i18next.t("provider:Host"), i18next.t("provider:Host - Tooltip"))} :
|
|
|
|
</Col>
|
|
|
|
<Col span={22} >
|
|
|
|
<Input value={this.state.syncer.host} onChange={e => {
|
|
|
|
this.updateSyncerField('host', e.target.value);
|
|
|
|
}} />
|
|
|
|
</Col>
|
|
|
|
</Row>
|
|
|
|
<Row style={{marginTop: '20px'}} >
|
|
|
|
<Col style={{marginTop: '5px'}} span={(Setting.isMobile()) ? 22 : 2}>
|
|
|
|
{Setting.getLabel(i18next.t("provider:Port"), i18next.t("provider:Port - Tooltip"))} :
|
|
|
|
</Col>
|
|
|
|
<Col span={22} >
|
|
|
|
<InputNumber value={this.state.syncer.port} onChange={value => {
|
|
|
|
this.updateSyncerField('port', value);
|
|
|
|
}} />
|
|
|
|
</Col>
|
|
|
|
</Row>
|
|
|
|
<Row style={{marginTop: '20px'}} >
|
|
|
|
<Col style={{marginTop: '5px'}} span={(Setting.isMobile()) ? 22 : 2}>
|
|
|
|
{Setting.getLabel(i18next.t("general:User"), i18next.t("general:User - Tooltip"))} :
|
|
|
|
</Col>
|
|
|
|
<Col span={22} >
|
|
|
|
<Input value={this.state.syncer.user} onChange={e => {
|
|
|
|
this.updateSyncerField('user', e.target.value);
|
|
|
|
}} />
|
|
|
|
</Col>
|
|
|
|
</Row>
|
|
|
|
<Row style={{marginTop: '20px'}} >
|
|
|
|
<Col style={{marginTop: '5px'}} span={(Setting.isMobile()) ? 22 : 2}>
|
|
|
|
{Setting.getLabel(i18next.t("general:Password"), i18next.t("general:Password - Tooltip"))} :
|
|
|
|
</Col>
|
|
|
|
<Col span={22} >
|
|
|
|
<Input value={this.state.syncer.password} onChange={e => {
|
|
|
|
this.updateSyncerField('password', e.target.value);
|
|
|
|
}} />
|
|
|
|
</Col>
|
|
|
|
</Row>
|
2021-12-24 22:53:35 +08:00
|
|
|
<Row style={{marginTop: '20px'}} >
|
|
|
|
<Col style={{marginTop: '5px'}} span={(Setting.isMobile()) ? 22 : 2}>
|
|
|
|
{Setting.getLabel(i18next.t("syncer:Database type"), i18next.t("syncer:Database type - Tooltip"))} :
|
|
|
|
</Col>
|
|
|
|
<Col span={22} >
|
|
|
|
<Select virtual={false} style={{width: '100%'}} value={this.state.syncer.databaseType} onChange={(value => {this.updateSyncerField('databaseType', value);})}>
|
|
|
|
{
|
|
|
|
[
|
|
|
|
{id: 'mysql', name: 'MySQL'},
|
|
|
|
{id: 'postgres', name: 'PostgreSQL'},
|
|
|
|
{id: 'mssql', name: 'SQL Server'},
|
|
|
|
{id: 'oracle', name: 'Oracle'},
|
|
|
|
{id: 'sqlite3', name: 'Sqlite 3'},
|
|
|
|
].map((databaseType, index) => <Option key={index} value={databaseType.id}>{databaseType.name}</Option>)
|
|
|
|
}
|
|
|
|
</Select>
|
|
|
|
</Col>
|
|
|
|
</Row>
|
2021-12-17 16:32:53 +08:00
|
|
|
<Row style={{marginTop: '20px'}} >
|
|
|
|
<Col style={{marginTop: '5px'}} span={(Setting.isMobile()) ? 22 : 2}>
|
|
|
|
{Setting.getLabel(i18next.t("syncer:Database"), i18next.t("syncer:Database - Tooltip"))} :
|
|
|
|
</Col>
|
|
|
|
<Col span={22} >
|
|
|
|
<Input value={this.state.syncer.database} onChange={e => {
|
|
|
|
this.updateSyncerField('database', e.target.value);
|
|
|
|
}} />
|
|
|
|
</Col>
|
|
|
|
</Row>
|
|
|
|
<Row style={{marginTop: '20px'}} >
|
|
|
|
<Col style={{marginTop: '5px'}} span={(Setting.isMobile()) ? 22 : 2}>
|
|
|
|
{Setting.getLabel(i18next.t("syncer:Table"), i18next.t("syncer:Table - Tooltip"))} :
|
|
|
|
</Col>
|
|
|
|
<Col span={22} >
|
|
|
|
<Input value={this.state.syncer.table} onChange={e => {
|
|
|
|
this.updateSyncerField('table', e.target.value);
|
|
|
|
}} />
|
|
|
|
</Col>
|
|
|
|
</Row>
|
2021-12-19 23:33:22 +08:00
|
|
|
<Row style={{marginTop: '20px'}} >
|
|
|
|
<Col style={{marginTop: '5px'}} span={(Setting.isMobile()) ? 22 : 2}>
|
|
|
|
{Setting.getLabel(i18next.t("syncer:Table primary key"), i18next.t("syncer:Table primary key - Tooltip"))} :
|
|
|
|
</Col>
|
|
|
|
<Col span={22} >
|
|
|
|
<Input value={this.state.syncer.tablePrimaryKey} onChange={e => {
|
|
|
|
this.updateSyncerField('tablePrimaryKey', e.target.value);
|
|
|
|
}} />
|
|
|
|
</Col>
|
|
|
|
</Row>
|
2021-12-19 10:21:05 +08:00
|
|
|
<Row style={{marginTop: '20px'}} >
|
|
|
|
<Col style={{marginTop: '5px'}} span={(Setting.isMobile()) ? 22 : 2}>
|
|
|
|
{Setting.getLabel(i18next.t("syncer:Table columns"), i18next.t("syncer:Table columns - Tooltip"))} :
|
|
|
|
</Col>
|
|
|
|
<Col span={22} >
|
|
|
|
<SyncerTableColumnTable
|
|
|
|
title={i18next.t("syncer:Table columns")}
|
|
|
|
table={this.state.syncer.tableColumns}
|
|
|
|
onUpdateTable={(value) => { this.updateSyncerField('tableColumns', value)}}
|
|
|
|
/>
|
|
|
|
</Col>
|
|
|
|
</Row>
|
2021-12-17 20:33:03 +08:00
|
|
|
<Row style={{marginTop: '20px'}} >
|
|
|
|
<Col style={{marginTop: '5px'}} span={(Setting.isMobile()) ? 22 : 2}>
|
|
|
|
{Setting.getLabel(i18next.t("syncer:Affiliation table"), i18next.t("syncer:Affiliation table - Tooltip"))} :
|
|
|
|
</Col>
|
|
|
|
<Col span={22} >
|
|
|
|
<Input value={this.state.syncer.affiliationTable} onChange={e => {
|
|
|
|
this.updateSyncerField('affiliationTable', e.target.value);
|
|
|
|
}} />
|
|
|
|
</Col>
|
|
|
|
</Row>
|
|
|
|
<Row style={{marginTop: '20px'}} >
|
|
|
|
<Col style={{marginTop: '5px'}} span={(Setting.isMobile()) ? 22 : 2}>
|
|
|
|
{Setting.getLabel(i18next.t("syncer:Avatar base URL"), i18next.t("syncer:Avatar base URL - Tooltip"))} :
|
|
|
|
</Col>
|
|
|
|
<Col span={22} >
|
|
|
|
<Input prefix={<LinkOutlined/>} value={this.state.syncer.avatarBaseUrl} onChange={e => {
|
|
|
|
this.updateSyncerField('avatarBaseUrl', e.target.value);
|
|
|
|
}} />
|
|
|
|
</Col>
|
|
|
|
</Row>
|
2021-12-17 16:32:53 +08:00
|
|
|
<Row style={{marginTop: '20px'}} >
|
|
|
|
<Col style={{marginTop: '5px'}} span={(Setting.isMobile()) ? 22 : 2}>
|
|
|
|
{Setting.getLabel(i18next.t("syncer:Sync interval"), i18next.t("syncer:Sync interval - Tooltip"))} :
|
|
|
|
</Col>
|
|
|
|
<Col span={22} >
|
|
|
|
<InputNumber value={this.state.syncer.syncInterval} onChange={value => {
|
|
|
|
this.updateSyncerField('syncInterval', value);
|
|
|
|
}} />
|
|
|
|
</Col>
|
|
|
|
</Row>
|
2022-01-17 21:16:32 +08:00
|
|
|
<Row style={{marginTop: '20px'}} >
|
|
|
|
<Col style={{marginTop: '5px'}} span={(Setting.isMobile()) ? 22 : 2}>
|
|
|
|
{Setting.getLabel(i18next.t("syncer:Error text"), i18next.t("syncer:Error text - Tooltip"))} :
|
|
|
|
</Col>
|
|
|
|
<Col span={22} >
|
|
|
|
<div style={{width: "100%", height: "300px"}} >
|
|
|
|
<CodeMirror
|
|
|
|
value={this.state.syncer.errorText}
|
|
|
|
options={{mode: 'javascript', theme: "material-darker"}}
|
|
|
|
onBeforeChange={(editor, data, value) => {
|
|
|
|
this.updateSyncerField("errorText", value);
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</Col>
|
|
|
|
</Row>
|
2021-12-17 20:33:03 +08:00
|
|
|
<Row style={{marginTop: '20px'}} >
|
|
|
|
<Col style={{marginTop: '5px'}} span={(Setting.isMobile()) ? 19 : 2}>
|
2021-12-26 21:02:56 +08:00
|
|
|
{Setting.getLabel(i18next.t("general:Is enabled"), i18next.t("general:Is enabled - Tooltip"))} :
|
2021-12-17 20:33:03 +08:00
|
|
|
</Col>
|
|
|
|
<Col span={1} >
|
|
|
|
<Switch checked={this.state.syncer.isEnabled} onChange={checked => {
|
|
|
|
this.updateSyncerField('isEnabled', checked);
|
|
|
|
}} />
|
|
|
|
</Col>
|
|
|
|
</Row>
|
2021-12-17 16:32:53 +08:00
|
|
|
</Card>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
submitSyncerEdit(willExist) {
|
|
|
|
let syncer = Setting.deepCopy(this.state.syncer);
|
|
|
|
SyncerBackend.updateSyncer(this.state.syncer.owner, this.state.syncerName, syncer)
|
|
|
|
.then((res) => {
|
|
|
|
if (res.msg === "") {
|
|
|
|
Setting.showMessage("success", `Successfully saved`);
|
|
|
|
this.setState({
|
|
|
|
syncerName: this.state.syncer.name,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (willExist) {
|
|
|
|
this.props.history.push(`/syncers`);
|
|
|
|
} else {
|
|
|
|
this.props.history.push(`/syncers/${this.state.syncer.name}`);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Setting.showMessage("error", res.msg);
|
|
|
|
this.updateSyncerField('name', this.state.syncerName);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
Setting.showMessage("error", `Failed to connect to server: ${error}`);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-25 18:16:02 +08:00
|
|
|
deleteSyncer() {
|
|
|
|
SyncerBackend.deleteSyncer(this.state.syncer)
|
|
|
|
.then(() => {
|
|
|
|
this.props.history.push(`/syncers`);
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
Setting.showMessage("error", `Syncer failed to delete: ${error}`);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-12-17 16:32:53 +08:00
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
{
|
|
|
|
this.state.syncer !== null ? this.renderSyncer() : null
|
|
|
|
}
|
|
|
|
<div style={{marginTop: '20px', marginLeft: '40px'}}>
|
|
|
|
<Button size="large" onClick={() => this.submitSyncerEdit(false)}>{i18next.t("general:Save")}</Button>
|
|
|
|
<Button style={{marginLeft: '20px'}} type="primary" size="large" onClick={() => this.submitSyncerEdit(true)}>{i18next.t("general:Save & Exit")}</Button>
|
2022-02-25 18:16:02 +08:00
|
|
|
{this.state.mode === "add" ? <Button style={{marginLeft: '20px'}} size="large" onClick={() => this.deleteSyncer()}>{i18next.t("general:Cancel")}</Button> : null}
|
2021-12-17 16:32:53 +08:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default SyncerEditPage;
|