// 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, Upload} from 'antd'; import {LinkOutlined, UploadOutlined} 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 * as ResourceBackend from "./backend/ResourceBackend"; import SignupPage from "./auth/SignupPage"; import LoginPage from "./auth/LoginPage"; import i18next from "i18next"; import UrlTable from "./UrlTable"; import ProviderTable from "./ProviderTable"; import SignupTable from "./SignupTable"; import PromptPage from "./auth/PromptPage"; 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: [], uploading: false, }; } UNSAFE_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.msg === undefined) ? 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, }); } handleUpload(info) { if (info.file.type !== "text/html") { Setting.showMessage("error", i18next.t("application:Please select a HTML file")); return; } this.setState({uploading: true}); const fullFilePath = `termsOfUse/${this.state.application.owner}/${this.state.application.name}.html`; ResourceBackend.uploadResource(this.props.account.owner, this.props.account.name, "termsOfUse", this.state.application.name, fullFilePath, info.file) .then(res => { if (res.status === "ok") { Setting.showMessage("success", i18next.t("application:File uploaded successfully")); this.updateApplicationField("termsOfUse", res.data); } else { Setting.showMessage("error", res.msg); } }).finally(() => { this.setState({uploading: false}); }) } renderApplication() { return ( {i18next.t("application:Edit Application")}     } style={(Setting.isMobile())? {margin: '5px'}:{}} type="inner"> {Setting.getLabel(i18next.t("general:Name"), i18next.t("general:Name - Tooltip"))} : { this.updateApplicationField('name', e.target.value); }} /> {Setting.getLabel(i18next.t("general:Display name"), i18next.t("general:Display name - Tooltip"))} : { this.updateApplicationField('displayName', e.target.value); }} /> {Setting.getLabel("Logo", i18next.t("general:Logo - Tooltip"))} : URL: } value={this.state.application.logo} onChange={e => { this.updateApplicationField('logo', e.target.value); }} /> {i18next.t("general:Preview")}: {this.state.application.logo} {Setting.getLabel(i18next.t("general:Home"), i18next.t("general:Home - Tooltip"))} : } value={this.state.application.homepageUrl} onChange={e => { this.updateApplicationField('homepageUrl', e.target.value); }} /> {Setting.getLabel(i18next.t("general:Description"), i18next.t("general:Description - Tooltip"))} : { this.updateApplicationField('description', e.target.value); }} /> {Setting.getLabel(i18next.t("general:Organization"), i18next.t("general:Organization - Tooltip"))} : {Setting.getLabel(i18next.t("provider:Client ID"), i18next.t("provider:Client ID - Tooltip"))} : { this.updateApplicationField('clientId', e.target.value); }} /> {Setting.getLabel(i18next.t("provider:Client secret"), i18next.t("provider:Client secret - Tooltip"))} : { this.updateApplicationField('clientSecret', e.target.value); }} /> {Setting.getLabel(i18next.t("application:Redirect URLs"), i18next.t("application:Redirect URLs - Tooltip"))} : { this.updateApplicationField('redirectUris', value)}} /> {Setting.getLabel(i18next.t("general:Token expire"), i18next.t("general:Token expire - Tooltip"))} : { this.updateApplicationField('expireInHours', e.target.value); }} /> {Setting.getLabel(i18next.t("application:Password ON"), i18next.t("application:Password ON - Tooltip"))} : { this.updateApplicationField('enablePassword', checked); }} /> {Setting.getLabel(i18next.t("application:Enable signup"), i18next.t("application:Enable signup - Tooltip"))} : { this.updateApplicationField('enableSignUp', checked); }} /> {Setting.getLabel(i18next.t("general:Signup URL"), i18next.t("general:Signup URL - Tooltip"))} : } value={this.state.application.signupUrl} onChange={e => { this.updateApplicationField('signupUrl', e.target.value); }} /> {Setting.getLabel(i18next.t("general:Signin URL"), i18next.t("general:Signin URL - Tooltip"))} : } value={this.state.application.signinUrl} onChange={e => { this.updateApplicationField('signinUrl', e.target.value); }} /> {Setting.getLabel(i18next.t("general:Forget URL"), i18next.t("general:Forget URL - Tooltip"))} : } value={this.state.application.forgetUrl} onChange={e => { this.updateApplicationField('forgetUrl', e.target.value); }} /> {Setting.getLabel(i18next.t("general:Affiliation URL"), i18next.t("general:Affiliation URL - Tooltip"))} : } value={this.state.application.affiliationUrl} onChange={e => { this.updateApplicationField('affiliationUrl', e.target.value); }} /> {Setting.getLabel(i18next.t("provider:Terms of Use"), i18next.t("provider:Terms of Use - Tooltip"))} : { this.updateApplicationField("termsOfUse", e.target.value) }}/> {return false}} onChange={info => {this.handleUpload(info)}}> {Setting.getLabel(i18next.t("general:Providers"), i18next.t("general:Providers - Tooltip"))} : { this.updateApplicationField('providers', value)}} /> {Setting.getLabel(i18next.t("general:Preview"), i18next.t("general:Preview - Tooltip"))} : { this.renderPreview() } { !this.state.application.enableSignUp ? null : ( {Setting.getLabel(i18next.t("application:Signup items"), i18next.t("application:Signup items - Tooltip"))} : { this.updateApplicationField('signupItems', value)}} /> ) } {Setting.getLabel(i18next.t("general:Preview"), i18next.t("general:Preview - Tooltip"))} : { this.renderPreview2() } ) } renderPreview() { let signUpUrl = `/signup/${this.state.application.name}`; let signInUrl = `/login/oauth/authorize?client_id=${this.state.application.clientId}&response_type=code&redirect_uri=${this.state.application.redirectUris[0]}&scope=read&state=casdoor`; if (!this.state.application.enablePassword) { signUpUrl = signInUrl.replace("/login/oauth/authorize", "/signup/oauth/authorize"); } if (!Setting.isMobile()) { return (

{ this.state.application.enablePassword ? ( ) : ( ) }


) } else{ return(
{ this.state.application.enablePassword ? ( ) : ( ) }
) } } renderPreview2() { let promptUrl = `/prompt/${this.state.application.name}`; return (

) } submitApplicationEdit() { let application = Setting.deepCopy(this.state.application); ApplicationBackend.updateApplication(this.state.application.owner, this.state.applicationName, application) .then((res) => { if (res.msg === "") { 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", res.msg); this.updateApplicationField('name', this.state.applicationName); } }) .catch(error => { Setting.showMessage("error", `Failed to connect to server: ${error}`); }); } render() { return (
{ this.state.application !== null ? this.renderApplication() : null }
); } } export default ApplicationEditPage;