// 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. import React from "react"; import {Button, Card, Col, Input, Row, Select, Switch} from 'antd'; import {LinkOutlined} from "@ant-design/icons"; import * as ApplicationBackend from "./backend/ApplicationBackend"; import * as Setting from "./Setting"; import * as ProviderBackend from "./backend/ProviderBackend"; import * as OrganizationBackend from "./backend/OrganizationBackend"; import LoginPage from "./auth/LoginPage"; import i18next from "i18next"; import UrlTable from "./UrlTable"; const { Option } = Select; class ApplicationEditPage extends React.Component { constructor(props) { super(props); this.state = { classes: props, applicationName: props.match.params.applicationName, application: null, organizations: [], providers: [], }; } componentWillMount() { this.getApplication(); this.getOrganizations(); this.getProviders(); } getApplication() { ApplicationBackend.getApplication("admin", this.state.applicationName) .then((application) => { this.setState({ application: application, }); }); } getOrganizations() { OrganizationBackend.getOrganizations("admin") .then((res) => { this.setState({ organizations: res, }); }); } getProviders() { ProviderBackend.getProviders("admin") .then((res) => { this.setState({ providers: res, }); }); } parseApplicationField(key, value) { if (["expireInHours"].includes(key)) { value = Setting.myParseInt(value); } return value; } updateApplicationField(key, value) { value = this.parseApplicationField(key, value); let application = this.state.application; application[key] = value; this.setState({ application: application, }); } renderApplication() { return ( {i18next.t("application:Edit Application")}     } style={{marginLeft: '5px'}} type="inner"> {i18next.t("general:Name")}: { this.updateApplicationField('name', e.target.value); }} /> {i18next.t("general:Display Name")}: { this.updateApplicationField('displayName', e.target.value); }} /> Logo: URL: } value={this.state.application.logo} onChange={e => { this.updateApplicationField('logo', e.target.value); }} /> {i18next.t("general:Preview")}: {this.state.application.logo} {i18next.t("general:Homepage URL")}: } value={this.state.application.homepageUrl} onChange={e => { this.updateApplicationField('homepageUrl', e.target.value); }} /> {i18next.t("general:Description")}: { this.updateApplicationField('description', e.target.value); }} /> {i18next.t("general:Organization")}: {i18next.t("provider:Client ID")}: { this.updateApplicationField('clientId', e.target.value); }} /> {i18next.t("provider:Client Secret")}: { this.updateApplicationField('clientSecret', e.target.value); }} /> {i18next.t("application:Redirect URLs")}: { this.updateApplicationField('redirectUris', value)}} /> {i18next.t("general:Expire In Hours")}: { this.updateApplicationField('expireInHours', e.target.value); }} /> {i18next.t("application:Enable Password")}: { this.updateApplicationField('enablePassword', checked); }} /> {i18next.t("application:Enable Sign Up")}: { this.updateApplicationField('enableSignUp', checked); }} /> {i18next.t("general:Providers")}: {i18next.t("application:Login Page Preview")}: { `${window.location.host}/login/oauth/authorize?client_id=${this.state.application.clientId}&response_type=code&redirect_uri=${this.state.application.redirectUris[0]}&scope=read&state=casdoor` }

) } submitApplicationEdit() { let application = Setting.deepCopy(this.state.application); ApplicationBackend.updateApplication(this.state.application.owner, this.state.applicationName, application) .then((res) => { if (res) { Setting.showMessage("success", `Successfully saved`); this.setState({ applicationName: this.state.application.name, }); this.props.history.push(`/applications/${this.state.application.name}`); } else { Setting.showMessage("error", `failed to save: server side failure`); this.updateApplicationField('name', this.state.applicationName); } }) .catch(error => { Setting.showMessage("error", `failed to save: ${error}`); }); } render() { return (
{ this.state.application !== null ? this.renderApplication() : null }
); } } export default ApplicationEditPage;