add admin section - basic skeleton and helper methods

This commit is contained in:
Markus Schubert 2020-03-29 18:21:36 +02:00
parent 6c0daa1e3c
commit f399b14ffa
9 changed files with 115 additions and 7 deletions

View file

@ -0,0 +1,19 @@
class AdminController {
constructor(database) {
this.database = database;
};
getAll(onSuccess, onError) {
let collection = this.database.collection('publicStates');
let cursor = collection.find({}, {});
cursor.toArray((item) => {
console.log('item: ' + item);
});
onSuccess({});
};
};
module.exports = AdminController;

View file

@ -45,6 +45,7 @@ class PublicStateController {
} }
}); });
}; };
}; };
module.exports = PublicStateController; module.exports = PublicStateController;

View file

@ -1,6 +1,7 @@
const express = require('express'); const express = require('express');
const router = express.Router(); const router = express.Router();
const Authenticator = require('../Authenticator'); const Authenticator = require('../Authenticator');
const AdminController = require('../controllers/AdminController');
const UserController = require('../controllers/UserController'); const UserController = require('../controllers/UserController');
const PublicStateController = require('../controllers/PublicStateController'); const PublicStateController = require('../controllers/PublicStateController');
@ -27,6 +28,24 @@ router.post('/state', (req, res, next) => {
authenticator.withUser(req, res, (user) => ctrl.create(req.body, user, scb, ecb)); authenticator.withUser(req, res, (user) => ctrl.create(req.body, user, scb, ecb));
}); });
router.get('/state', (req, res, next) => {
app = req.app;
db = app.locals.database;
let authenticator = new Authenticator(db);
let ctrl = new AdminController(db);
let scb = (data) => {
res.json(data);
};
let ecb = (error) => {
res.status(400).send(error);
};
authenticator.withUser(req, res, (user) => ctrl.getAll(scb, ecb));
});
router.get('/user', function (req, res, next) { router.get('/user', function (req, res, next) {
app = req.app; app = req.app;
db = app.locals.database; db = app.locals.database;

View file

@ -0,0 +1,15 @@
import React, { Component } from 'react';
class PublicStateList extends Component {
render() {
return (
<div>
<h1>PublicStateList</h1>
</div>
)
}
};
export default PublicStateList;

View file

@ -1,9 +1,11 @@
import AppHeader from './AppHeader/AppHeader'; import AppHeader from './AppHeader/AppHeader';
import Login from './Login/Login'; import Login from './Login/Login';
import PublicStateList from './PublicStateList/PublicStateList';
import Register from './Register/Register'; import Register from './Register/Register';
export { export {
AppHeader, AppHeader,
PublicStateList,
Login, Login,
Register Register
}; };

View file

@ -3,7 +3,7 @@ import PropTypes from 'prop-types';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import { Redirect } from 'react-router-dom'; import { Redirect } from 'react-router-dom';
import { createPublicState } from '../../../actions/gameActions'; import { createPublicState } from '../../../actions/gameActions';
import { Form, Input, Button, Typography } from 'antd'; import { Form, Input, Button, Typography, InputNumber } from 'antd';
const layout = { const layout = {
labelCol: { labelCol: {
@ -107,6 +107,19 @@ class CreatePublicState extends Component {
<Input /> <Input />
</Form.Item> </Form.Item>
<Form.Item
label="Minimum number of Citizen per private state"
name="privateStateMinCitizen"
rules={[
{
required: false,
message: 'Please input the minimum number of citizen that can constitute a private state',
},
]}
>
<InputNumber defaultValue={3} min={2} />
</Form.Item>
<Form.Item <Form.Item
style={errorMessageStyle} style={errorMessageStyle}
label=" " label=" "

View file

@ -0,0 +1,17 @@
import React, { Component } from 'react';
import { PublicStateList } from '../../components';
class AdminDashboardPage extends Component {
render() {
return (
<div>
<h1>Admin Dashboard</h1>
<PublicStateList />
</div>
)
}
};
export default AdminDashboardPage;

View file

@ -1,3 +1,4 @@
import AdminDashboardPage from './AdminDashboardPage/AdminDashboardPage';
import DashboardPage from './DashboardPage/DashboardPage'; import DashboardPage from './DashboardPage/DashboardPage';
import LoginPage from './LoginPage/LoginPage'; import LoginPage from './LoginPage/LoginPage';
import PublicStateOverviewPage from './PublicStateOverviewPage/PublicStateOverviewPage'; import PublicStateOverviewPage from './PublicStateOverviewPage/PublicStateOverviewPage';
@ -6,6 +7,7 @@ import SplashScreenPage from './SplashScreenPage/SplashScreenPage';
import WelcomePage from './WelcomePage/WelcomePage'; import WelcomePage from './WelcomePage/WelcomePage';
export { export {
AdminDashboardPage,
DashboardPage, DashboardPage,
LoginPage, LoginPage,
PublicStateOverviewPage, PublicStateOverviewPage,

View file

@ -10,6 +10,7 @@ import {
} from 'react-router-dom'; } from 'react-router-dom';
import { Layout } from 'antd'; import { Layout } from 'antd';
import { import {
AdminDashboardPage,
DashboardPage, DashboardPage,
LoginPage, LoginPage,
PublicStateOverviewPage, PublicStateOverviewPage,
@ -22,9 +23,7 @@ import 'antd/dist/antd.css';
const { Content } = Layout; const { Content } = Layout;
// A wrapper for <Route> that redirects to the login export const AuthenticatedRoute = ({ children, ...rest }) => {
// screen if user is not yet authenticated.
export const PrivateRoute = ({ children, ...rest }) => {
return ( return (
<Route <Route
{...rest} {...rest}
@ -44,6 +43,26 @@ export const PrivateRoute = ({ children, ...rest }) => {
); );
}; };
export const AdminRoute = ({ children, ...rest }) => {
return (
<Route
{...rest}
render={({ location }) =>
(rest.user && rest.user.role === 'admin') ? (
children
) : (
<Redirect
to={{
pathname: "/admin/autherror",
state: { from: location }
}}
/>
)
}
/>
);
};
class AppRouter extends Component { class AppRouter extends Component {
@ -66,7 +85,6 @@ class AppRouter extends Component {
render() { render() {
if (this.state.identityChecked) { if (this.state.identityChecked) {
console.log('user: ' + this.props.user);
return ( return (
<Router> <Router>
<Layout style={{ height: "100%" }}> <Layout style={{ height: "100%" }}>
@ -77,8 +95,10 @@ class AppRouter extends Component {
<Route exact path='/'><WelcomePage /></Route> <Route exact path='/'><WelcomePage /></Route>
<Route exact path='/login'><LoginPage /></Route> <Route exact path='/login'><LoginPage /></Route>
<Route exact path='/register'><RegisterPage /></Route> <Route exact path='/register'><RegisterPage /></Route>
<PrivateRoute exact path='/start' user={this.props.user}><DashboardPage /></PrivateRoute> <Route exact path='/admin/autherror'><div>not an admin</div></Route>
<PrivateRoute exact path='/state/:name' user={this.props.user}><PublicStateOverviewPage /></PrivateRoute> <AuthenticatedRoute exact path='/start' user={this.props.user}><DashboardPage /></AuthenticatedRoute>
<AuthenticatedRoute exact path='/state/:name' user={this.props.user}><PublicStateOverviewPage /></AuthenticatedRoute>
<AdminRoute exact path='/admin' user={this.props.user}><AdminDashboardPage /></AdminRoute>
</Switch> </Switch>
</div> </div>
</Content> </Content>