casdoor/web/src/ProviderEditPage.js

298 lines
10 KiB
JavaScript
Raw Normal View History

2021-02-13 13:30:51 +08:00
// Copyright 2021 The casbin Authors. All Rights Reserved.
//
// 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.
2021-02-13 12:15:19 +08:00
import React from "react";
2021-05-14 23:45:20 +08:00
import {Button, Card, Col, Input, InputNumber, Row, Select} from 'antd';
import {LinkOutlined} from "@ant-design/icons";
2021-02-13 12:15:19 +08:00
import * as ProviderBackend from "./backend/ProviderBackend";
import * as Setting from "./Setting";
import i18next from "i18next";
2021-02-13 12:15:19 +08:00
2021-02-14 10:20:42 +08:00
const { Option } = Select;
2021-02-13 12:15:19 +08:00
class ProviderEditPage extends React.Component {
constructor(props) {
super(props);
this.state = {
classes: props,
providerName: props.match.params.providerName,
provider: null,
};
}
2021-03-27 11:38:15 +08:00
UNSAFE_componentWillMount() {
2021-02-13 12:15:19 +08:00
this.getProvider();
}
getProvider() {
ProviderBackend.getProvider("admin", this.state.providerName)
.then((provider) => {
this.setState({
provider: provider,
});
});
}
parseProviderField(key, value) {
2021-05-14 23:45:20 +08:00
if (["port"].includes(key)) {
value = Setting.myParseInt(value);
}
2021-02-13 12:15:19 +08:00
return value;
}
updateProviderField(key, value) {
value = this.parseProviderField(key, value);
let provider = this.state.provider;
provider[key] = value;
this.setState({
provider: provider,
});
}
2021-05-14 23:45:20 +08:00
getProviderTypeOptions(provider) {
if (provider.category === "OAuth") {
return (
[
{id: 'Google', name: 'Google'},
{id: 'GitHub', name: 'GitHub'},
{id: 'QQ', name: 'QQ'},
{id: 'WeChat', name: 'WeChat'},
]
);
} else if (provider.category === "Email") {
return (
[
{id: 'Default', name: 'Default'},
]
);
} else {
return (
2021-05-15 13:43:21 +08:00
[
{id: 'aliyun', name: 'Aliyun'},
{id: 'tencent', name: 'Tencent Cloud'},
]
2021-05-14 23:45:20 +08:00
);
}
}
2021-02-13 12:15:19 +08:00
renderProvider() {
return (
<Card size="small" title={
<div>
{i18next.t("provider:Edit Provider")}&nbsp;&nbsp;&nbsp;&nbsp;
<Button type="primary" onClick={this.submitProviderEdit.bind(this)}>{i18next.t("general:Save")}</Button>
2021-02-13 12:15:19 +08:00
</div>
} style={{marginLeft: '5px'}} type="inner">
<Row style={{marginTop: '10px'}} >
<Col style={{marginTop: '5px'}} span={2}>
{i18next.t("general:Name")}:
2021-02-13 12:15:19 +08:00
</Col>
<Col span={22} >
<Input value={this.state.provider.name} onChange={e => {
this.updateProviderField('name', e.target.value);
}} />
</Col>
</Row>
<Row style={{marginTop: '20px'}} >
<Col style={{marginTop: '5px'}} span={2}>
2021-04-28 00:13:50 +08:00
{i18next.t("general:Display name")}:
2021-02-13 12:15:19 +08:00
</Col>
<Col span={22} >
<Input value={this.state.provider.displayName} onChange={e => {
this.updateProviderField('displayName', e.target.value);
}} />
</Col>
</Row>
2021-05-14 23:45:20 +08:00
<Row style={{marginTop: '20px'}} >
<Col style={{marginTop: '5px'}} span={2}>
{i18next.t("provider:Category")}:
</Col>
<Col span={22} >
<Select virtual={false} style={{width: '100%'}} value={this.state.provider.category} onChange={(value => {this.updateProviderField('category', value);})}>
{
[
{id: 'OAuth', name: 'OAuth'},
{id: 'Email', name: 'Email'},
{id: 'Phone', name: 'Phone'},
].map((providerCategory, index) => <Option key={index} value={providerCategory.id}>{providerCategory.name}</Option>)
}
</Select>
</Col>
</Row>
2021-02-13 12:15:19 +08:00
<Row style={{marginTop: '20px'}} >
<Col style={{marginTop: '5px'}} span={2}>
{i18next.t("provider:Type")}:
2021-02-13 12:15:19 +08:00
</Col>
<Col span={22} >
2021-02-14 10:20:42 +08:00
<Select virtual={false} style={{width: '100%'}} value={this.state.provider.type} onChange={(value => {this.updateProviderField('type', value);})}>
{
2021-05-14 23:45:20 +08:00
this.getProviderTypeOptions(this.state.provider).map((providerType, index) => <Option key={index} value={providerType.id}>{providerType.name}</Option>)
2021-02-14 10:20:42 +08:00
}
</Select>
2021-02-13 12:15:19 +08:00
</Col>
</Row>
<Row style={{marginTop: '20px'}} >
<Col style={{marginTop: '5px'}} span={2}>
2021-05-14 23:45:20 +08:00
{this.state.provider.category === "Email" ? i18next.t("signup:Username") : i18next.t("provider:Client ID")}:
2021-02-13 12:15:19 +08:00
</Col>
<Col span={22} >
<Input value={this.state.provider.clientId} onChange={e => {
this.updateProviderField('clientId', e.target.value);
}} />
</Col>
</Row>
<Row style={{marginTop: '20px'}} >
<Col style={{marginTop: '5px'}} span={2}>
2021-05-14 23:45:20 +08:00
{this.state.provider.category === "Email" ? i18next.t("login:Password") : i18next.t("provider:Client secret")}:
2021-02-13 12:15:19 +08:00
</Col>
<Col span={22} >
<Input value={this.state.provider.clientSecret} onChange={e => {
this.updateProviderField('clientSecret', e.target.value);
}} />
</Col>
</Row>
2021-05-15 13:43:21 +08:00
{
this.state.provider.category === "Email" ? (
<React.Fragment>
<Row style={{marginTop: '20px'}} >
<Col style={{marginTop: '5px'}} span={2}>
{i18next.t("provider:Host")}:
</Col>
<Col span={22} >
<Input prefix={<LinkOutlined/>} value={this.state.provider.host} onChange={e => {
this.updateProviderField('host', e.target.value);
}} />
</Col>
</Row>
<Row style={{marginTop: '20px'}} >
<Col style={{marginTop: '5px'}} span={2}>
{i18next.t("provider:Port")}:
</Col>
<Col span={22} >
<InputNumber value={this.state.provider.port} onChange={value => {
this.updateProviderField('port', value);
}} />
</Col>
</Row>
</React.Fragment>
2021-05-23 23:38:38 +08:00
) : this.state.provider.category === "SMS" ? (
2021-05-15 13:43:21 +08:00
<React.Fragment>
<Row style={{marginTop: '20px'}} >
<Col style={{marginTop: '5px'}} span={2}>
{i18next.t("provider:Region ID")}:
</Col>
<Col span={22} >
<Input value={this.state.provider.regionId} onChange={e => {
this.updateProviderField('regionId', e.target.value);
}} />
</Col>
</Row>
<Row style={{marginTop: '20px'}} >
<Col style={{marginTop: '5px'}} span={2}>
{i18next.t("provider:Sign Name")}:
</Col>
<Col span={22} >
<Input value={this.state.provider.signName} onChange={e => {
this.updateProviderField('signName', e.target.value);
}} />
</Col>
</Row>
<Row style={{marginTop: '20px'}} >
<Col style={{marginTop: '5px'}} span={2}>
{i18next.t("provider:Template Code")}:
</Col>
<Col span={22} >
<Input value={this.state.provider.templateCode} onChange={e => {
this.updateProviderField('templateCode', e.target.value);
}} />
</Col>
</Row>
</React.Fragment>
) : null
}
2021-05-23 23:38:38 +08:00
{this.state.provider.category === "SMS" && this.state.provider.type === "tencent" ? (
<Row style={{marginTop: '20px'}} >
<Col style={{marginTop: '5px'}} span={2}>
{i18next.t("provider:App ID")}:
</Col>
<Col span={22} >
<Input value={this.state.provider.appId} onChange={e => {
this.updateProviderField('appId', e.target.value);
}} />
</Col>
</Row>
) : null}
2021-02-13 12:15:19 +08:00
<Row style={{marginTop: '20px'}} >
<Col style={{marginTop: '5px'}} span={2}>
{i18next.t("provider:Provider URL")}:
2021-02-13 12:15:19 +08:00
</Col>
<Col span={22} >
2021-05-14 23:45:20 +08:00
<Input prefix={<LinkOutlined/>} value={this.state.provider.providerUrl} onChange={e => {
2021-02-13 12:15:19 +08:00
this.updateProviderField('providerUrl', e.target.value);
}} />
</Col>
</Row>
</Card>
)
}
submitProviderEdit() {
let provider = Setting.deepCopy(this.state.provider);
ProviderBackend.updateProvider(this.state.provider.owner, this.state.providerName, provider)
.then((res) => {
2021-03-28 00:48:34 +08:00
if (res.msg === "") {
2021-02-13 12:15:19 +08:00
Setting.showMessage("success", `Successfully saved`);
this.setState({
providerName: this.state.provider.name,
});
this.props.history.push(`/providers/${this.state.provider.name}`);
} else {
2021-03-28 00:48:34 +08:00
Setting.showMessage("error", res.msg);
2021-02-13 12:15:19 +08:00
this.updateProviderField('name', this.state.providerName);
}
})
.catch(error => {
2021-03-28 08:59:12 +08:00
Setting.showMessage("error", `Failed to connect to server: ${error}`);
2021-02-13 12:15:19 +08:00
});
}
render() {
return (
<div>
<Row style={{width: "100%"}}>
<Col span={1}>
</Col>
<Col span={22}>
{
this.state.provider !== null ? this.renderProvider() : null
}
</Col>
<Col span={1}>
</Col>
</Row>
<Row style={{margin: 10}}>
<Col span={2}>
</Col>
<Col span={18}>
<Button type="primary" size="large" onClick={this.submitProviderEdit.bind(this)}>{i18next.t("general:Save")}</Button>
2021-02-13 12:15:19 +08:00
</Col>
</Row>
</div>
);
}
}
export default ProviderEditPage;