// 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} from 'react-router-dom' import * as AccountBackend from "./backend/AccountBackend"; const { Header, Footer } = Layout; class App extends Component { constructor(props) { super(props); this.state = { classes: props, selectedMenuKey: 0, account: undefined, }; } componentWillMount() { this.updateMenuKey(); this.getAccount(); } updateMenuKey() { // eslint-disable-next-line no-restricted-globals const uri = location.pathname; if (uri === '/') { this.setState({ selectedMenuKey: 0 }); } else { this.setState({ selectedMenuKey: -1 }); } } onLogined() { this.getAccount(); } onUpdateAccount(account) { this.setState({ account: account }); } getAccount() { AccountBackend.getAccount() .then((res) => { const account = Setting.parseJson(res.data); if (window.location.pathname === '/' && account === null) { Setting.goToLink("/"); } this.setState({ account: account, }); if (account !== undefined && account !== null) { window.mouselogUserId = account.username; } }); } 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 = (
); return (