// Copyright 2021 The Casdoor 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, ConfigProvider, Input, InputNumber, Popover, Radio, Result, Row, Select, Space, Switch, Upload} from "antd"; import {CopyOutlined, HolderOutlined, LinkOutlined, UploadOutlined, UsergroupAddOutlined} from "@ant-design/icons"; import * as ApplicationBackend from "./backend/ApplicationBackend"; import * as CertBackend from "./backend/CertBackend"; import * as Setting from "./Setting"; import * as Conf from "./Conf"; 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 "./table/UrlTable"; import ProviderTable from "./table/ProviderTable"; import SigninMethodTable from "./table/SigninMethodTable"; import SignupTable from "./table/SignupTable"; import SamlAttributeTable from "./table/SamlAttributeTable"; import PromptPage from "./auth/PromptPage"; import copy from "copy-to-clipboard"; import ThemeEditor from "./common/theme/ThemeEditor"; import SigninTable from "./table/SigninTable"; import Editor from "./common/Editor"; import * as GroupBackend from "./backend/GroupBackend"; const {Option} = Select; const template = ``; const previewGrid = Setting.isMobile() ? 22 : 11; const previewWidth = Setting.isMobile() ? "110%" : "90%"; const sideTemplate = `
Casdoor
`; class ApplicationEditPage extends React.Component { constructor(props) { super(props); this.state = { classes: props, owner: props.organizationName !== undefined ? props.organizationName : props.match.params.organizationName, applicationName: props.match.params.applicationName, application: null, organizations: [], certs: [], providers: [], uploading: false, mode: props.location.mode !== undefined ? props.location.mode : "edit", samlAttributes: [], samlMetadata: null, isAuthorized: true, }; } UNSAFE_componentWillMount() { this.getApplication(); this.getOrganizations(); this.getGroups(); } getApplication() { ApplicationBackend.getApplication("admin", this.state.applicationName) .then((res) => { if (res.data === null) { this.props.history.push("/404"); return; } if (res.status === "error") { Setting.showMessage("error", res.msg); return; } const application = res.data; if (application.grantTypes === null || application.grantTypes === undefined || application.grantTypes.length === 0) { application.grantTypes = ["authorization_code"]; } if (application.tags === null || application.tags === undefined) { application.tags = []; } this.setState({ application: application, }); this.getProviders(application); this.getCerts(application); this.getSamlMetadata(application.enableSamlPostBinding); }); } getOrganizations() { OrganizationBackend.getOrganizations("admin") .then((res) => { if (res.status === "error") { this.setState({ isAuthorized: false, }); } else { this.setState({ organizations: res.data || [], }); } }); } getGroups() { GroupBackend.getGroups(this.state.organizationName) .then((res) => { if (res.status === "ok") { this.setState({ groups: res.data, }); } }); } getCerts(application) { let owner = application.organization; if (application.isShared) { owner = this.props.owner; } CertBackend.getCerts(owner) .then((res) => { this.setState({ certs: res.data || [], }); }); } getProviders(application) { let owner = application.organization; if (application.isShared) { owner = this.props.account.owner; } ProviderBackend.getProviders(owner) .then((res) => { if (res.status === "ok") { this.setState({ providers: res.data, }); } else { Setting.showMessage("error", res.msg); } }); } getSamlMetadata(checked) { ApplicationBackend.getSamlMetadata("admin", this.state.applicationName, checked) .then((data) => { this.setState({ samlMetadata: data, }); }); } parseApplicationField(key, value) { if (["offset"].includes(key)) { value = Setting.myParseInt(value); } return value; } updateApplicationField(key, value) { value = this.parseApplicationField(key, value); const 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", "ApplicationEditPage", 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", `${i18next.t("general:Failed to save")}: ${res.msg}`); } }).finally(() => { this.setState({uploading: false}); }); } renderApplication() { return ( {this.state.mode === "add" ? i18next.t("application:New Application") : i18next.t("application:Edit Application")}     {this.state.mode === "add" ? : null} } 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(i18next.t("general:Is shared"), i18next.t("general:Is shared - Tooltip"))} : { this.updateApplicationField("isShared", checked); }} /> {Setting.getLabel(i18next.t("general:Logo"), i18next.t("general:Logo - Tooltip"))} : {Setting.getLabel(i18next.t("general:URL"), i18next.t("general:URL - Tooltip"))} : } 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("organization:Tags"), i18next.t("application:Tags - 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("general:Cert"), i18next.t("general:Cert - Tooltip"))} : {Setting.getLabel(i18next.t("application:Redirect URLs"), i18next.t("application:Redirect URLs - Tooltip"))} : {this.updateApplicationField("redirectUris", value);}} /> {Setting.getLabel(i18next.t("application:Forced redirect origin"), i18next.t("general:Forced redirect origin - Tooltip"))} : } value={this.state.application.forcedRedirectOrigin} onChange={e => { this.updateApplicationField("forcedRedirectOrigin", e.target.value); }} /> {Setting.getLabel(i18next.t("application:Token format"), i18next.t("application:Token format - Tooltip"))} : {this.updateApplicationField("tokenSigningMethod", value);})} options={["RS256", "RS512", "ES256", "ES512", "ES384"].map((item) => Setting.getOption(item, item))} /> {Setting.getLabel(i18next.t("application:Token fields"), i18next.t("application:Token fields - Tooltip"))} : {Setting.getLabel(i18next.t("application:Token expire"), i18next.t("application:Token expire - Tooltip"))} : { this.updateApplicationField("expireInHours", value); }} /> {Setting.getLabel(i18next.t("application:Refresh token expire"), i18next.t("application:Refresh token expire - Tooltip"))} : { this.updateApplicationField("refreshExpireInHours", value); }} /> {Setting.getLabel(i18next.t("application:Failed signin limit"), i18next.t("application:Failed signin limit - Tooltip"))} : { this.updateApplicationField("failedSigninLimit", value); }} /> {Setting.getLabel(i18next.t("application:Failed signin frozen time"), i18next.t("application:Failed signin frozen time - Tooltip"))} : { this.updateApplicationField("failedSigninFrozenTime", value); }} /> {Setting.getLabel(i18next.t("ldap:Default group"), i18next.t("ldap:Default group - Tooltip"))} : {Setting.getLabel(i18next.t("application:Enable signup"), i18next.t("application:Enable signup - Tooltip"))} : { this.updateApplicationField("enableSignUp", checked); }} /> {Setting.getLabel(i18next.t("application:Signin session"), i18next.t("application:Enable signin session - Tooltip"))} : { if (!checked) { this.updateApplicationField("enableAutoSignin", false); } this.updateApplicationField("enableSigninSession", checked); }} /> {Setting.getLabel(i18next.t("application:Auto signin"), i18next.t("application:Auto signin - Tooltip"))} : { if (!this.state.application.enableSigninSession && checked) { Setting.showMessage("error", i18next.t("application:Please enable \"Signin session\" first before enabling \"Auto signin\"")); return; } this.updateApplicationField("enableAutoSignin", checked); }} /> {Setting.getLabel(i18next.t("application:Enable Email linking"), i18next.t("application:Enable Email linking - Tooltip"))} : { this.updateApplicationField("enableLinkWithEmail", checked); }} /> {Setting.getLabel(i18next.t("application:Signin methods"), i18next.t("application:Signin methods - Tooltip"))} : { this.updateApplicationField("signinMethods", value); }} /> {Setting.getLabel(i18next.t("application:Org choice mode"), i18next.t("application:Org choice mode - Tooltip"))} : {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("general:IP whitelist"), i18next.t("general:IP whitelist - Tooltip"))} : { this.updateApplicationField("ipWhitelist", e.target.value); }} /> {Setting.getLabel(i18next.t("signup:Terms of Use"), i18next.t("signup:Terms of Use - Tooltip"))} : } value={this.state.application.termsOfUse} style={{marginBottom: "10px"}} onChange={e => { this.updateApplicationField("termsOfUse", e.target.value); }} /> {return false;}} onChange={info => {this.handleUpload(info);}}> {Setting.getLabel(i18next.t("provider:Signup HTML"), i18next.t("provider:Signup HTML - Tooltip"))} : { this.updateApplicationField("signupHtml", value); }} /> } title={i18next.t("provider:Signup HTML - Edit")} trigger="click"> { this.updateApplicationField("signupHtml", e.target.value); }} /> {Setting.getLabel(i18next.t("provider:Signin HTML"), i18next.t("provider:Signin HTML - Tooltip"))} : { this.updateApplicationField("signinHtml", value); }} /> } title={i18next.t("provider:Signin HTML - Edit")} trigger="click"> { this.updateApplicationField("signinHtml", e.target.value); }} /> {Setting.getLabel(i18next.t("application:Grant types"), i18next.t("application:Grant types - Tooltip"))} : {Setting.getLabel(i18next.t("application:SAML reply URL"), i18next.t("application:Redirect URL (Assertion Consumer Service POST Binding URL) - Tooltip"))} : } value={this.state.application.samlReplyUrl} onChange={e => { this.updateApplicationField("samlReplyUrl", e.target.value); }} /> {Setting.getLabel(i18next.t("application:Enable SAML compression"), i18next.t("application:Enable SAML compression - Tooltip"))} : { this.updateApplicationField("enableSamlCompress", checked); }} /> {Setting.getLabel(i18next.t("application:Enable SAML C14N10"), i18next.t("application:Enable SAML C14N10 - Tooltip"))} : { this.updateApplicationField("enableSamlC14n10", checked); }} /> {Setting.getLabel(i18next.t("application:Use Email as NameID"), i18next.t("application:Use Email as NameID - Tooltip"))} : { this.updateApplicationField("useEmailAsSamlNameId", checked); }} /> {Setting.getLabel(i18next.t("application:Enable SAML POST binding"), i18next.t("application:Enable SAML POST binding - Tooltip"))} : { this.updateApplicationField("enableSamlPostBinding", checked); this.getSamlMetadata(checked); }} /> {Setting.getLabel(i18next.t("general:SAML attributes"), i18next.t("general:SAML attributes - Tooltip"))} : {this.updateApplicationField("samlAttributes", value);}} /> {Setting.getLabel(i18next.t("application:SAML metadata"), i18next.t("application:SAML metadata - Tooltip"))} :
{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.renderSignupSigninPreview() } {Setting.getLabel(i18next.t("application:Background URL"), i18next.t("application:Background URL - Tooltip"))} : {Setting.getLabel(i18next.t("general:URL"), i18next.t("general:URL - Tooltip"))} : } value={this.state.application.formBackgroundUrl} onChange={e => { this.updateApplicationField("formBackgroundUrl", e.target.value); }} /> {i18next.t("general:Preview")}: {this.state.application.formBackgroundUrl} {Setting.getLabel(i18next.t("application:Background URL Mobile"), i18next.t("application:Background URL Mobile - Tooltip"))} : {Setting.getLabel(i18next.t("general:URL"), i18next.t("general:URL - Tooltip"))} : } value={this.state.application.formBackgroundUrlMobile} onChange={e => { this.updateApplicationField("formBackgroundUrlMobile", e.target.value); }} /> {i18next.t("general:Preview")}: {this.state.application.formBackgroundUrlMobile} {Setting.getLabel(i18next.t("application:Custom CSS"), i18next.t("application:Custom CSS - Tooltip"))} : { this.updateApplicationField("formCss", value); }} /> } title={i18next.t("application:Custom CSS - Edit")} trigger="click"> { this.updateApplicationField("formCss", e.target.value); }} /> {Setting.getLabel(i18next.t("application:Custom CSS Mobile"), i18next.t("application:Custom CSS Mobile - Tooltip"))} : { this.updateApplicationField("formCssMobile", value); }} /> } title={i18next.t("application:Custom CSS Mobile - Edit")} trigger="click"> { this.updateApplicationField("formCssMobile", e.target.value); }} /> {Setting.getLabel(i18next.t("application:Form position"), i18next.t("application:Form position - Tooltip"))} : {this.updateApplicationField("formOffset", e.target.value);}} value={this.state.application.formOffset}> {i18next.t("application:Left")} {i18next.t("application:Center")} {i18next.t("application:Right")} {i18next.t("application:Enable side panel")} {this.state.application.formOffset === 4 ? {Setting.getLabel(i18next.t("application:Side panel HTML"), i18next.t("application:Side panel HTML - Tooltip"))} : { this.updateApplicationField("formSideHtml", value); }} /> } title={i18next.t("application:Side panel HTML - Edit")} trigger="click"> { this.updateApplicationField("formSideHtml", e.target.value); }} /> : null} {Setting.getLabel(i18next.t("theme:Theme"), i18next.t("theme:Theme - Tooltip"))} : { const {_, ...theme} = this.state.application.themeData ?? {...Conf.ThemeDefault, isEnabled: false}; this.updateApplicationField("themeData", {...theme, isEnabled: e.target.value}); }} > {i18next.t("application:Follow organization theme")} {i18next.t("theme:Customize theme")} { this.state.application.themeData?.isEnabled ? { const {isEnabled} = this.state.application.themeData ?? {...Conf.ThemeDefault, isEnabled: false}; this.updateApplicationField("themeData", {...nextThemeData, isEnabled}); }} /> : null } {Setting.getLabel(i18next.t("application:Header HTML"), i18next.t("application:Header HTML - Tooltip"))} : { this.updateApplicationField("headerHtml", value); }} /> } title={i18next.t("application:Header HTML - Edit")} trigger="click"> { this.updateApplicationField("headerHtml", e.target.value); }} /> {Setting.getLabel(i18next.t("application:Footer HTML"), i18next.t("application:Footer HTML - Tooltip"))} : { this.updateApplicationField("footerHtml", value); }} /> } title={i18next.t("application:Footer HTML - Edit")} trigger="click"> { this.updateApplicationField("footerHtml", e.target.value); }} /> { {Setting.getLabel(i18next.t("application:Signin items"), i18next.t("application:Signin items - Tooltip"))} : { this.updateApplicationField("signinItems", value); }} /> } { !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.renderPromptPreview() }
); } renderSignupSigninPreview() { const themeData = this.state.application.themeData ?? Conf.ThemeDefault; let signUpUrl = `/signup/${this.state.application.name}`; let redirectUri; if (this.state.application.redirectUris?.length > 0) { redirectUri = this.state.application.redirectUris[0]; } else { redirectUri = "\"ERROR: You must specify at least one Redirect URL in 'Redirect URLs'\""; } let clientId = this.state.application.clientId; if (this.state.application.isShared) { clientId += `-org-${this.props.account.owner}`; } const signInUrl = `/login/oauth/authorize?client_id=${clientId}&response_type=code&redirect_uri=${redirectUri}&scope=read&state=casdoor`; const maskStyle = {position: "absolute", top: "0px", left: "0px", zIndex: 10, height: "97%", width: "100%", background: "rgba(0,0,0,0.4)"}; if (!Setting.isPasswordEnabled(this.state.application)) { signUpUrl = signInUrl.replace("/login/oauth/authorize", "/signup/oauth/authorize"); } return (
{ Setting.isPasswordEnabled(this.state.application) ? (
) : (
) }

); } renderPromptPreview() { const themeData = this.state.application.themeData ?? Conf.ThemeDefault; const promptUrl = `/prompt/${this.state.application.name}`; const maskStyle = {position: "absolute", top: "0px", left: "0px", zIndex: 10, height: "100%", width: "100%", background: "rgba(0,0,0,0.4)"}; return (
); } submitApplicationEdit(exitAfterSave) { const application = Setting.deepCopy(this.state.application); application.providers = application.providers?.filter(provider => this.state.providers.map(provider => provider.name).includes(provider.name)); application.signinMethods = application.signinMethods?.filter(signinMethod => ["Password", "Verification code", "WebAuthn", "LDAP", "Face ID"].includes(signinMethod.name)); ApplicationBackend.updateApplication("admin", this.state.applicationName, application) .then((res) => { if (res.status === "ok") { Setting.showMessage("success", i18next.t("general:Successfully saved")); this.setState({ applicationName: this.state.application.name, }); if (exitAfterSave) { this.props.history.push("/applications"); } else { this.props.history.push(`/applications/${this.state.application.organization}/${this.state.application.name}`); } } else { Setting.showMessage("error", `${i18next.t("general:Failed to save")}: ${res.msg}`); this.updateApplicationField("name", this.state.applicationName); } }) .catch(error => { Setting.showMessage("error", `${i18next.t("general:Failed to connect to server")}: ${error}`); }); } deleteApplication() { ApplicationBackend.deleteApplication(this.state.application) .then((res) => { if (res.status === "ok") { this.props.history.push("/applications"); } else { Setting.showMessage("error", `${i18next.t("general:Failed to delete")}: ${res.msg}`); } }) .catch(error => { Setting.showMessage("error", `${i18next.t("general:Failed to connect to server")}: ${error}`); }); } render() { if (!this.state.isAuthorized) { return ( } /> ); } return (
{ this.state.application !== null ? this.renderApplication() : null }
{this.state.mode === "add" ? : null}
); } } export default ApplicationEditPage;