// 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, {Component} from 'react'; import './App.less'; import {Helmet} from "react-helmet"; import * as Setting from "./Setting"; import {DownOutlined, LogoutOutlined, SettingOutlined} from '@ant-design/icons'; import {Avatar, BackTop, Dropdown, Layout, Menu, Card, Result, Button} from 'antd'; import {Link, Redirect, Route, Switch, withRouter} from 'react-router-dom' import OrganizationListPage from "./OrganizationListPage"; import OrganizationEditPage from "./OrganizationEditPage"; import UserListPage from "./UserListPage"; import UserEditPage from "./UserEditPage"; import RoleListPage from "./RoleListPage"; import RoleEditPage from "./RoleEditPage"; import PermissionListPage from "./PermissionListPage"; import PermissionEditPage from "./PermissionEditPage"; import ProviderListPage from "./ProviderListPage"; import ProviderEditPage from "./ProviderEditPage"; import ApplicationListPage from "./ApplicationListPage"; import ApplicationEditPage from "./ApplicationEditPage"; import ResourceListPage from "./ResourceListPage"; import LdapEditPage from "./LdapEditPage"; import LdapSyncPage from "./LdapSyncPage"; import TokenListPage from "./TokenListPage"; import TokenEditPage from "./TokenEditPage"; import RecordListPage from "./RecordListPage"; import WebhookListPage from "./WebhookListPage"; import WebhookEditPage from "./WebhookEditPage"; import SyncerListPage from "./SyncerListPage"; import SyncerEditPage from "./SyncerEditPage"; import CertListPage from "./CertListPage"; import CertEditPage from "./CertEditPage"; import PaymentListPage from "./PaymentListPage"; import PaymentEditPage from "./PaymentEditPage"; import AccountPage from "./account/AccountPage"; import HomePage from "./basic/HomePage"; import CustomGithubCorner from "./CustomGithubCorner"; import * as Auth from "./auth/Auth"; import SignupPage from "./auth/SignupPage"; import ResultPage from "./auth/ResultPage"; import LoginPage from "./auth/LoginPage"; import SelfLoginPage from "./auth/SelfLoginPage"; import SelfForgetPage from "./auth/SelfForgetPage"; import ForgetPage from "./auth/ForgetPage"; import * as AuthBackend from "./auth/AuthBackend"; import AuthCallback from "./auth/AuthCallback"; import SelectLanguageBox from './SelectLanguageBox'; import i18next from 'i18next'; import PromptPage from "./auth/PromptPage"; import OdicDiscoveryPage from "./auth/OidcDiscoveryPage"; import SamlCallback from './auth/SamlCallback'; const { Header, Footer } = Layout; class App extends Component { constructor(props) { super(props); this.state = { classes: props, selectedMenuKey: 0, account: undefined, uri: null, }; Setting.initServerUrl(); Auth.initAuthWithConfig({ serverUrl: Setting.ServerUrl, appName: "app-built-in", // the application name of Casdoor itself, do not change it }); } UNSAFE_componentWillMount() { this.updateMenuKey(); this.getAccount(); } componentDidUpdate() { // eslint-disable-next-line no-restricted-globals const uri = location.pathname; if (this.state.uri !== uri) { this.updateMenuKey(); } } updateMenuKey() { // eslint-disable-next-line no-restricted-globals const uri = location.pathname; this.setState({ uri: uri, }); if (uri === '/') { this.setState({ selectedMenuKey: '/' }); } else if (uri.includes('/organizations')) { this.setState({ selectedMenuKey: '/organizations' }); } else if (uri.includes('/users')) { this.setState({ selectedMenuKey: '/users' }); } else if (uri.includes('/roles')) { this.setState({ selectedMenuKey: '/roles' }); } else if (uri.includes('/permissions')) { this.setState({ selectedMenuKey: '/permissions' }); } else if (uri.includes('/providers')) { this.setState({ selectedMenuKey: '/providers' }); } else if (uri.includes('/applications')) { this.setState({ selectedMenuKey: '/applications' }); } else if (uri.includes('/resources')) { this.setState({ selectedMenuKey: '/resources' }); } else if (uri.includes('/tokens')) { this.setState({ selectedMenuKey: '/tokens' }); } else if (uri.includes('/records')) { this.setState({ selectedMenuKey: '/records' }); } else if (uri.includes('/webhooks')) { this.setState({ selectedMenuKey: '/webhooks' }); } else if (uri.includes('/syncers')) { this.setState({ selectedMenuKey: '/syncers' }); } else if (uri.includes('/certs')) { this.setState({ selectedMenuKey: '/certs' }); } else if (uri.includes('/payments')) { this.setState({ selectedMenuKey: '/payments' }); } else if (uri.includes('/signup')) { this.setState({ selectedMenuKey: '/signup' }); } else if (uri.includes('/login')) { this.setState({ selectedMenuKey: '/login' }); } else if (uri.includes('/result')) { this.setState({ selectedMenuKey: '/result' }); } else { this.setState({ selectedMenuKey: -1 }); } } getAccessTokenParam() { // "/page?access_token=123" const params = new URLSearchParams(this.props.location.search); const accessToken = params.get("access_token"); return accessToken === null ? "" : `?accessToken=${accessToken}`; } getCredentialParams() { // "/page?username=abc&password=123" const params = new URLSearchParams(this.props.location.search); if (params.get("username") === null || params.get("password") === null) { return ""; } return `?username=${params.get("username")}&password=${params.get("password")}`; } getUrlWithoutQuery() { // eslint-disable-next-line no-restricted-globals return location.toString().replace(location.search, ""); } setLanguage(account) { let language = account?.language; if (language !== "" && language !== i18next.language) { Setting.setLanguage(language); } } getAccount() { let query = this.getAccessTokenParam(); if (query === "") { query = this.getCredentialParams(); } if (query !== "") { window.history.replaceState({}, document.title, this.getUrlWithoutQuery()); } AuthBackend.getAccount(query) .then((res) => { let account = null; if (res.status === "ok") { account = res.data; account.organization = res.data2; this.setLanguage(account); } else { if (res.msg !== "Please sign in first") { Setting.showMessage("error", `Failed to sign in: ${res.msg}`); } } this.setState({ account: account, }); }); } logout() { this.setState({ expired: false, submitted: false, }); AuthBackend.logout() .then((res) => { if (res.status === 'ok') { this.setState({ account: null }); Setting.showMessage("success", `Logged out successfully`); Setting.goToLinkSoft(this, "/"); } else { Setting.showMessage("error", `Failed to log out: ${res.msg}`); } }); } onUpdateAccount(account) { this.setState({ account: account }); } handleRightDropdownClick(e) { if (e.key === '/account') { this.props.history.push(`/account`); } else if (e.key === '/logout') { this.logout(); } } renderAvatar() { if (this.state.account.avatar === "") { return ( {Setting.getShortName(this.state.account.name)} ) } else { return ( {Setting.getShortName(this.state.account.name)} ) } } renderRightDropdown() { const menu = ( {i18next.t("account:My Account")} {i18next.t("account:Logout")} ); return (
    { this.renderAvatar() }     {Setting.isMobile() ? null : Setting.getShortName(this.state.account.displayName)}        
) } renderAccount() { let res = []; if (this.state.account === undefined) { return null; } else if (this.state.account === null) { // res.push( // // // {i18next.t("account:Sign Up")} // // // ); // res.push( // // // {i18next.t("account:Login")} // // // ); } else { res.push(this.renderRightDropdown()); } return res; } renderMenu() { let res = []; if (this.state.account === null || this.state.account === undefined) { return []; } res.push( {i18next.t("general:Home")} ); if (Setting.isAdminUser(this.state.account)) { res.push( {i18next.t("general:Organizations")} ); res.push( {i18next.t("general:Users")} ); res.push( {i18next.t("general:Roles")} ); res.push( {i18next.t("general:Permissions")} ); res.push( {i18next.t("general:Providers")} ); res.push( {i18next.t("general:Applications")} ); } if (Setting.isAdminUser(this.state.account)) { res.push( {i18next.t("general:Resources")} ); res.push( {i18next.t("general:Tokens")} ); res.push( {i18next.t("general:Records")} ); res.push( {i18next.t("general:Webhooks")} ); res.push( {i18next.t("general:Syncers")} ); res.push( {i18next.t("general:Certs")} ); res.push( {i18next.t("general:Payments")} ); res.push( {i18next.t("general:Swagger")} ); } return res; } renderHomeIfLoggedIn(component) { if (this.state.account !== null && this.state.account !== undefined) { return } else { return component; } } renderLoginIfNotLoggedIn(component) { if (this.state.account === null) { sessionStorage.setItem("from", window.location.pathname); return } else if (this.state.account === undefined) { return null; } else { return component; } } isStartPages() { return window.location.pathname.startsWith('/login') || window.location.pathname.startsWith('/signup') || window.location.pathname === '/'; } renderRouter(){ return(
this.renderHomeIfLoggedIn()}/> this.renderHomeIfLoggedIn()}/> this.renderLoginIfNotLoggedIn()}/> this.renderLoginIfNotLoggedIn()}/> this.renderLoginIfNotLoggedIn()}/> this.renderLoginIfNotLoggedIn()}/> this.renderLoginIfNotLoggedIn()}/> this.renderLoginIfNotLoggedIn()}/> }/> this.renderLoginIfNotLoggedIn()}/> this.renderLoginIfNotLoggedIn()}/> this.renderLoginIfNotLoggedIn()}/> this.renderLoginIfNotLoggedIn()}/> this.renderLoginIfNotLoggedIn()}/> this.renderLoginIfNotLoggedIn()}/> this.renderLoginIfNotLoggedIn()}/> this.renderLoginIfNotLoggedIn()}/> this.renderLoginIfNotLoggedIn()}/> {/* this.renderLoginIfNotLoggedIn()}/>*/} this.renderLoginIfNotLoggedIn()}/> this.renderLoginIfNotLoggedIn()}/> this.renderLoginIfNotLoggedIn()}/> this.renderLoginIfNotLoggedIn()}/> this.renderLoginIfNotLoggedIn()}/> this.renderLoginIfNotLoggedIn()}/> this.renderLoginIfNotLoggedIn()}/> this.renderLoginIfNotLoggedIn()}/> this.renderLoginIfNotLoggedIn()}/> this.renderLoginIfNotLoggedIn()}/> this.renderLoginIfNotLoggedIn()}/> this.renderLoginIfNotLoggedIn()}/> this.renderLoginIfNotLoggedIn()}/> }/> } />} />
) } renderContent() { if (!Setting.isMobile()) { return (
{ Setting.isMobile() ? null : (
) } { this.renderMenu() }
{ this.renderAccount() }
{ this.renderRouter() }
) } else { return(
{ Setting.isMobile() ? null : (
) } { this.renderMenu() }
{ this.renderAccount() }
{ this.renderRouter() }
) } } renderFooter() { // How to keep your footer where it belongs ? // https://www.freecodecamp.org/neyarnws/how-to-keep-your-footer-where-it-belongs-59c6aa05c59c/ return ( ) } isDoorPages() { return window.location.pathname.startsWith("/signup") || window.location.pathname.startsWith("/login") || window.location.pathname.startsWith("/callback") || window.location.pathname.startsWith("/prompt") || window.location.pathname.startsWith("/forget"); } renderPage() { if (this.isDoorPages()) { return ( this.renderHomeIfLoggedIn()}/> this.renderHomeIfLoggedIn( {this.onUpdateAccount(account)}} />)}/> this.renderHomeIfLoggedIn()}/> {this.onUpdateAccount(account)}} />}/> {this.onUpdateAccount(account)}} />}/> this.renderHomeIfLoggedIn()}/> this.renderHomeIfLoggedIn()}/> this.renderLoginIfNotLoggedIn()}/> this.renderLoginIfNotLoggedIn( {this.onUpdateAccount(account)}} {...props} />)}/> }/>} /> ) } return (
{ this.renderContent() }
{ this.renderFooter() }
); } render() { if (this.state.account === undefined || this.state.account === null) { return ( { this.renderPage() } ) } const organization = this.state.account.organization; return ( {organization.displayName} { this.renderPage() } ) } } export default withRouter(App);