mirror of
https://github.com/casdoor/casdoor.git
synced 2025-05-23 02:35:49 +08:00
334 lines
9.8 KiB
JavaScript
334 lines
9.8 KiB
JavaScript
// Copyright 2020 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.css';
|
||
import * as Setting from "./Setting";
|
||
import {DownOutlined, LogoutOutlined, SettingOutlined} from '@ant-design/icons';
|
||
import {Avatar, BackTop, Dropdown, Layout, Menu} from 'antd';
|
||
import {Switch, Route, withRouter, Redirect, Link} from 'react-router-dom'
|
||
import * as AccountBackend from "./backend/AccountBackend";
|
||
import OrganizationListPage from "./OrganizationListPage";
|
||
import OrganizationEditPage from "./OrganizationEditPage";
|
||
import UserListPage from "./UserListPage";
|
||
import UserEditPage from "./UserEditPage";
|
||
import ProviderListPage from "./ProviderListPage";
|
||
import ProviderEditPage from "./ProviderEditPage";
|
||
import ApplicationListPage from "./ApplicationListPage";
|
||
import ApplicationEditPage from "./ApplicationEditPage";
|
||
import Face from "./Face";
|
||
import AccountPage from "./account/AccountPage";
|
||
import LoginPage from "./account/LoginPage";
|
||
import HomePage from "./HomePage";
|
||
|
||
const { Header, Footer } = Layout;
|
||
|
||
class App extends Component {
|
||
constructor(props) {
|
||
super(props);
|
||
this.state = {
|
||
classes: props,
|
||
selectedMenuKey: 0,
|
||
account: undefined,
|
||
};
|
||
|
||
Setting.initServerUrl();
|
||
}
|
||
|
||
componentWillMount() {
|
||
this.updateMenuKey();
|
||
this.getAccount();
|
||
}
|
||
|
||
updateMenuKey() {
|
||
// eslint-disable-next-line no-restricted-globals
|
||
const uri = location.pathname;
|
||
if (uri === '/') {
|
||
this.setState({ selectedMenuKey: 0 });
|
||
} else if (uri.includes('organizations')) {
|
||
this.setState({ selectedMenuKey: 1 });
|
||
} else if (uri.includes('users')) {
|
||
this.setState({ selectedMenuKey: 2 });
|
||
} else if (uri.includes('providers')) {
|
||
this.setState({ selectedMenuKey: 3 });
|
||
} else if (uri.includes('applications')) {
|
||
this.setState({ selectedMenuKey: 4 });
|
||
} else {
|
||
this.setState({ selectedMenuKey: -1 });
|
||
}
|
||
}
|
||
|
||
onLoggedIn() {
|
||
this.getAccount();
|
||
}
|
||
|
||
onUpdateAccount(account) {
|
||
this.setState({
|
||
account: account
|
||
});
|
||
}
|
||
|
||
getAccount() {
|
||
AccountBackend.getAccount()
|
||
.then((res) => {
|
||
const account = Setting.parseJson(res.data);
|
||
this.setState({
|
||
account: account,
|
||
});
|
||
});
|
||
}
|
||
|
||
logout() {
|
||
this.setState({
|
||
expired: false,
|
||
submitted: false,
|
||
});
|
||
|
||
AccountBackend.logout()
|
||
.then((res) => {
|
||
if (res.status === 'ok') {
|
||
this.setState({
|
||
account: null
|
||
});
|
||
|
||
Setting.showMessage("success", `Successfully logged out, redirected to homepage`);
|
||
|
||
Setting.goToLink("/");
|
||
} else {
|
||
Setting.showMessage("error", `Logout failed: ${res.msg}`);
|
||
}
|
||
});
|
||
}
|
||
|
||
handleRightDropdownClick(e) {
|
||
if (e.key === '0') {
|
||
this.props.history.push(`/account`);
|
||
} else if (e.key === '1') {
|
||
this.logout();
|
||
}
|
||
}
|
||
|
||
renderRightDropdown() {
|
||
const menu = (
|
||
<Menu onClick={this.handleRightDropdownClick.bind(this)}>
|
||
<Menu.Item key='0'>
|
||
<SettingOutlined />
|
||
My Account
|
||
</Menu.Item>
|
||
<Menu.Item key='1'>
|
||
<LogoutOutlined />
|
||
Logout
|
||
</Menu.Item>
|
||
</Menu>
|
||
);
|
||
|
||
return (
|
||
<Dropdown key="4" overlay={menu} >
|
||
<a className="ant-dropdown-link" href="#" style={{float: 'right'}}>
|
||
<Avatar style={{ backgroundColor: Setting.getAvatarColor(this.state.account.name), verticalAlign: 'middle' }} size="large">
|
||
{Setting.getShortName(this.state.account.name)}
|
||
</Avatar>
|
||
|
||
|
||
{Setting.isMobile() ? null : Setting.getShortName(this.state.account.name)} <DownOutlined />
|
||
|
||
|
||
|
||
</a>
|
||
</Dropdown>
|
||
)
|
||
}
|
||
|
||
renderAccount() {
|
||
let res = [];
|
||
|
||
if (this.state.account !== null && this.state.account !== undefined) {
|
||
res.push(this.renderRightDropdown());
|
||
} else {
|
||
res.push(
|
||
<Menu.Item key="100" style={{float: 'right', marginRight: '20px'}}>
|
||
<Link to="/register">
|
||
Register
|
||
</Link>
|
||
</Menu.Item>
|
||
);
|
||
res.push(
|
||
<Menu.Item key="101" style={{float: 'right'}}>
|
||
<Link to="/login">
|
||
Login
|
||
</Link>
|
||
</Menu.Item>
|
||
);
|
||
}
|
||
|
||
return res;
|
||
}
|
||
|
||
renderMenu() {
|
||
let res = [];
|
||
|
||
if (this.state.account === null || this.state.account === undefined) {
|
||
return [];
|
||
}
|
||
|
||
res.push(
|
||
<Menu.Item key="0">
|
||
<Link to="/">
|
||
Home
|
||
</Link>
|
||
</Menu.Item>
|
||
);
|
||
res.push(
|
||
<Menu.Item key="1">
|
||
<Link to="/organizations">
|
||
Organizations
|
||
</Link>
|
||
</Menu.Item>
|
||
);
|
||
res.push(
|
||
<Menu.Item key="2">
|
||
<Link to="/users">
|
||
Users
|
||
</Link>
|
||
</Menu.Item>
|
||
);
|
||
res.push(
|
||
<Menu.Item key="3">
|
||
<Link to="/providers">
|
||
Providers
|
||
</Link>
|
||
</Menu.Item>
|
||
);
|
||
res.push(
|
||
<Menu.Item key="4">
|
||
<Link to="/applications">
|
||
Applications
|
||
</Link>
|
||
</Menu.Item>
|
||
);
|
||
|
||
return res;
|
||
}
|
||
|
||
renderHomeIfLoggedIn(component) {
|
||
if (this.state.account !== null && this.state.account !== undefined) {
|
||
return <Redirect to='/' />
|
||
} else {
|
||
return component;
|
||
}
|
||
}
|
||
|
||
renderLoginIfNotLoggedIn(component) {
|
||
if (this.state.account === null) {
|
||
return <Redirect to='/login' />
|
||
} else if (this.state.account === undefined) {
|
||
return null;
|
||
}
|
||
else {
|
||
return component;
|
||
}
|
||
}
|
||
|
||
isStartPages() {
|
||
return window.location.pathname.startsWith('/login') ||
|
||
window.location.pathname.startsWith('/register') ||
|
||
window.location.pathname === '/';
|
||
}
|
||
|
||
renderContent() {
|
||
return (
|
||
<div>
|
||
<Header style={{ padding: '0', marginBottom: '3px'}}>
|
||
{
|
||
Setting.isMobile() ? null : <a className="logo" href={"/"} />
|
||
}
|
||
<Menu
|
||
// theme="dark"
|
||
mode={(Setting.isMobile() && this.isStartPages()) ? "inline" : "horizontal"}
|
||
defaultSelectedKeys={[`${this.state.selectedMenuKey}`]}
|
||
style={{ lineHeight: '64px' }}
|
||
>
|
||
{
|
||
this.renderMenu()
|
||
}
|
||
{
|
||
this.renderAccount()
|
||
}
|
||
</Menu>
|
||
</Header>
|
||
<Switch>
|
||
<Route exact path="/login" render={(props) => this.renderHomeIfLoggedIn(<LoginPage onLoggedIn={this.onLoggedIn.bind(this)} {...props} />)}/>
|
||
<Route exact path="/" render={(props) => this.renderLoginIfNotLoggedIn(<HomePage onLoggedIn={this.onLoggedIn.bind(this)} {...props} />)}/>
|
||
<Route exact path="/account" render={(props) => this.renderLoginIfNotLoggedIn(<AccountPage account={this.state.account} {...props} />)}/>
|
||
<Route exact path="/organizations" render={(props) => this.renderLoginIfNotLoggedIn(<OrganizationListPage account={this.state.account} {...props} />)}/>
|
||
<Route exact path="/organizations/:organizationName" render={(props) => this.renderLoginIfNotLoggedIn(<OrganizationEditPage account={this.state.account} {...props} />)}/>
|
||
<Route exact path="/users" render={(props) => this.renderLoginIfNotLoggedIn(<UserListPage account={this.state.account} {...props} />)}/>
|
||
<Route exact path="/users/:organizationName/:userName" render={(props) => this.renderLoginIfNotLoggedIn(<UserEditPage account={this.state.account} {...props} />)}/>
|
||
<Route exact path="/providers" render={(props) => this.renderLoginIfNotLoggedIn(<ProviderListPage account={this.state.account} {...props} />)}/>
|
||
<Route exact path="/providers/:providerName" render={(props) => this.renderLoginIfNotLoggedIn(<ProviderEditPage account={this.state.account} {...props} />)}/>
|
||
<Route exact path="/applications" render={(props) => this.renderLoginIfNotLoggedIn(<ApplicationListPage account={this.state.account} {...props} />)}/>
|
||
<Route exact path="/applications/:applicationName" render={(props) => this.renderLoginIfNotLoggedIn(<ApplicationEditPage account={this.state.account} {...props} />)}/>
|
||
</Switch>
|
||
</div>
|
||
)
|
||
}
|
||
|
||
renderFooter() {
|
||
// How to keep your footer where it belongs ?
|
||
// https://www.freecodecamp.org/neyarnws/how-to-keep-your-footer-where-it-belongs-59c6aa05c59c/
|
||
|
||
return (
|
||
<Footer id="footer" style={
|
||
{
|
||
borderTop: '1px solid #e8e8e8',
|
||
backgroundColor: 'white',
|
||
textAlign: 'center',
|
||
}
|
||
}>
|
||
Made with <span style={{color: 'rgb(255, 255, 255)'}}>❤️</span> by <a style={{fontWeight: "bold", color: "black"}} target="_blank" href="https://casbin.org">Casbin</a>
|
||
</Footer>
|
||
)
|
||
}
|
||
|
||
isDoorPages() {
|
||
return window.location.pathname.startsWith('/doors/');
|
||
}
|
||
|
||
render() {
|
||
if (this.isDoorPages()) {
|
||
return (
|
||
<Switch>
|
||
<Route exact path="/doors/:applicationName" render={(props) => this.renderLoginIfNotLoggedIn(<Face account={this.state.account} onLoggedIn={this.onLoggedIn.bind(this)} {...props} />)}/>
|
||
</Switch>
|
||
)
|
||
}
|
||
|
||
return (
|
||
<div id="parent-area">
|
||
<BackTop />
|
||
<div id="content-wrap">
|
||
{
|
||
this.renderContent()
|
||
}
|
||
</div>
|
||
{
|
||
this.renderFooter()
|
||
}
|
||
</div>
|
||
);
|
||
}
|
||
}
|
||
|
||
export default withRouter(App);
|