casdoor/web/src/App.js

334 lines
9.8 KiB
JavaScript
Raw Normal View History

2020-10-20 22:37:38 +08:00
// 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';
2020-10-20 21:46:44 +08:00
import './App.css';
2020-10-20 22:37:38 +08:00
import * as Setting from "./Setting";
import {DownOutlined, LogoutOutlined, SettingOutlined} from '@ant-design/icons';
import {Avatar, BackTop, Dropdown, Layout, Menu} from 'antd';
2021-02-12 00:31:53 +08:00
import {Switch, Route, withRouter, Redirect, Link} from 'react-router-dom'
2020-10-20 22:37:38 +08:00
import * as AccountBackend from "./backend/AccountBackend";
2020-12-20 20:31:48 +08:00
import OrganizationListPage from "./OrganizationListPage";
import OrganizationEditPage from "./OrganizationEditPage";
2020-10-20 23:14:03 +08:00
import UserListPage from "./UserListPage";
import UserEditPage from "./UserEditPage";
2020-12-20 21:25:23 +08:00
import ProviderListPage from "./ProviderListPage";
import ProviderEditPage from "./ProviderEditPage";
2020-12-20 23:24:09 +08:00
import ApplicationListPage from "./ApplicationListPage";
import ApplicationEditPage from "./ApplicationEditPage";
2021-02-11 16:43:30 +08:00
import Face from "./Face";
2021-02-13 01:33:06 +08:00
import AccountPage from "./account/AccountPage";
import LoginPage from "./account/LoginPage";
import HomePage from "./HomePage";
2020-10-20 22:37:38 +08:00
const { Header, Footer } = Layout;
class App extends Component {
constructor(props) {
super(props);
this.state = {
classes: props,
selectedMenuKey: 0,
account: undefined,
};
2020-10-20 23:14:03 +08:00
Setting.initServerUrl();
2020-10-20 22:37:38 +08:00
}
componentWillMount() {
this.updateMenuKey();
this.getAccount();
}
updateMenuKey() {
// eslint-disable-next-line no-restricted-globals
const uri = location.pathname;
if (uri === '/') {
this.setState({ selectedMenuKey: 0 });
2020-12-20 20:31:48 +08:00
} else if (uri.includes('organizations')) {
2020-10-20 23:14:03 +08:00
this.setState({ selectedMenuKey: 1 });
2020-12-20 20:31:48 +08:00
} else if (uri.includes('users')) {
this.setState({ selectedMenuKey: 2 });
2020-12-20 21:25:23 +08:00
} else if (uri.includes('providers')) {
this.setState({ selectedMenuKey: 3 });
2020-12-20 23:24:09 +08:00
} else if (uri.includes('applications')) {
this.setState({ selectedMenuKey: 4 });
2020-10-20 22:37:38 +08:00
} else {
this.setState({ selectedMenuKey: -1 });
}
}
2021-02-13 00:11:12 +08:00
onLoggedIn() {
2020-10-20 22:37:38 +08:00
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>
&nbsp;
&nbsp;
{Setting.isMobile() ? null : Setting.getShortName(this.state.account.name)} &nbsp; <DownOutlined />
&nbsp;
&nbsp;
&nbsp;
</a>
</Dropdown>
)
}
renderAccount() {
let res = [];
if (this.state.account !== null && this.state.account !== undefined) {
res.push(this.renderRightDropdown());
} else {
res.push(
2021-02-12 00:31:53 +08:00
<Menu.Item key="100" style={{float: 'right', marginRight: '20px'}}>
<Link to="/register">
2020-10-20 22:37:38 +08:00
Register
2021-02-12 00:31:53 +08:00
</Link>
2020-10-20 22:37:38 +08:00
</Menu.Item>
);
res.push(
2021-02-12 00:31:53 +08:00
<Menu.Item key="101" style={{float: 'right'}}>
<Link to="/login">
2020-10-20 22:37:38 +08:00
Login
2021-02-12 00:31:53 +08:00
</Link>
2020-10-20 22:37:38 +08:00
</Menu.Item>
);
}
return res;
}
renderMenu() {
let res = [];
2021-02-13 01:33:06 +08:00
if (this.state.account === null || this.state.account === undefined) {
return [];
}
2020-10-20 22:37:38 +08:00
res.push(
<Menu.Item key="0">
2021-02-12 00:31:53 +08:00
<Link to="/">
2020-10-20 22:37:38 +08:00
Home
2021-02-12 00:31:53 +08:00
</Link>
2020-10-20 22:37:38 +08:00
</Menu.Item>
);
res.push(
<Menu.Item key="1">
2021-02-12 00:31:53 +08:00
<Link to="/organizations">
2020-12-20 20:31:48 +08:00
Organizations
2021-02-12 00:31:53 +08:00
</Link>
2020-12-20 20:31:48 +08:00
</Menu.Item>
);
res.push(
<Menu.Item key="2">
2021-02-12 00:31:53 +08:00
<Link to="/users">
2020-10-20 23:14:03 +08:00
Users
2021-02-12 00:31:53 +08:00
</Link>
2020-10-20 22:37:38 +08:00
</Menu.Item>
);
2020-12-20 21:25:23 +08:00
res.push(
<Menu.Item key="3">
2021-02-12 00:31:53 +08:00
<Link to="/providers">
2020-12-20 21:25:23 +08:00
Providers
2021-02-12 00:31:53 +08:00
</Link>
2020-12-20 21:25:23 +08:00
</Menu.Item>
);
2020-12-20 23:24:09 +08:00
res.push(
<Menu.Item key="4">
2021-02-12 00:31:53 +08:00
<Link to="/applications">
2020-12-20 23:24:09 +08:00
Applications
2021-02-12 00:31:53 +08:00
</Link>
2020-12-20 23:24:09 +08:00
</Menu.Item>
);
2020-10-20 22:37:38 +08:00
return res;
}
2021-02-13 01:33:06 +08:00
renderHomeIfLoggedIn(component) {
2020-10-20 22:37:38 +08:00
if (this.state.account !== null && this.state.account !== undefined) {
return <Redirect to='/' />
} else {
return component;
}
}
2021-02-13 00:11:12 +08:00
renderLoginIfNotLoggedIn(component) {
2020-10-20 22:37:38 +08:00
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>
2021-02-13 01:33:06 +08:00
<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} />)}/>
2021-02-13 00:11:12 +08:00
<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} />)}/>
2020-10-20 22:37:38 +08:00
</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>
)
}
2021-02-11 16:43:30 +08:00
isDoorPages() {
return window.location.pathname.startsWith('/doors/');
}
2020-10-20 22:37:38 +08:00
render() {
2021-02-11 16:43:30 +08:00
if (this.isDoorPages()) {
return (
<Switch>
2021-02-13 01:33:06 +08:00
<Route exact path="/doors/:applicationName" render={(props) => this.renderLoginIfNotLoggedIn(<Face account={this.state.account} onLoggedIn={this.onLoggedIn.bind(this)} {...props} />)}/>
2021-02-11 16:43:30 +08:00
</Switch>
)
}
2020-10-20 22:37:38 +08:00
return (
<div id="parent-area">
<BackTop />
<div id="content-wrap">
{
this.renderContent()
}
</div>
{
this.renderFooter()
}
</div>
);
}
2020-10-20 21:46:44 +08:00
}
2020-10-20 22:37:38 +08:00
export default withRouter(App);