Add support for adding more grants through an admin page

This commit is contained in:
R. Tyler Croy 2018-09-21 10:48:11 -07:00
parent 7a9b29dd62
commit 03ea364b19
No known key found for this signature in database
GPG Key ID: 1426C7DC3F51E16F
6 changed files with 120 additions and 6 deletions

View File

@ -22,6 +22,7 @@ import services from './services';
import { appHooks } from './app.hooks';
import channels from './channels';
import Admin from './controllers/admin';
import Dashboard from './controllers/dashboard';
import Export from './controllers/export';
@ -50,6 +51,18 @@ app.configure(socketio());
* authentication wherever we damn well please
*/
app.get('*', cookieParser());
app.post('*', cookieParser());
/*
* Ensure that POST calls which have _method set to DELETE get passed through
* properly
*/
app.post('*', (req, res, next) => {
if (req.body._method == 'DELETE') {
req.method = req.body._method;
}
next();
});
/*
* Allow overriding the JWT secret in the environment, a la Kubernetes
@ -75,6 +88,7 @@ app.set('view engine', 'pug');
Dashboard(app);
Export(app);
Admin(app);
app.get('/logout',
cookieParser(),

32
src/controllers/admin.ts Normal file
View File

@ -0,0 +1,32 @@
import authentication from '@feathersjs/authentication';
import { NotAuthenticated } from '@feathersjs/errors';
import cookieParser from 'cookie-parser';
export default (app) => {
app.get('/admin',
cookieParser(),
authentication.express.authenticate('jwt'),
async (req, res, next) => {
const user = (req as any).user;
const name : string = user.github.profile.username;
app.service('grants').find({
query: {
$sort: {
name: 1,
},
},
})
.then((records) => {
const isAdmin : boolean = (records.filter(r => (r.name == name) && (r.type == '*')).length > 0);
if (!isAdmin) {
throw new NotAuthenticated();
}
res.render('admin', {
user: user,
grants: records,
});
})
.catch(next);
});
};

View File

@ -10,24 +10,30 @@ export default (app) => {
app.get('/dashboard',
cookieParser(),
authentication.express.authenticate('jwt'),
(req, res, next) => {
let query = Object.assign({
async (req, res, next) => {
const query = Object.assign({
$sort: {
createdAt: -1,
}
}, req.query);
const user = (req as any).user;
const name : string = user.github.profile.username;
const grants : Array<string> = await app.service('grants').find({ query: { name: name }});
const isAdmin : boolean = (grants.filter(g => g != '*').length > 0);
app.service('events')
.find({
query: query,
// propogate our user object down
user: (req as any).user,
user: user,
})
.then(result =>
res.render('dashboard', {
events: result,
user: (req as any).user,
user: user,
query: req.query,
isAdmin: isAdmin,
grants: grants,
}))
.catch(next);
});

View File

@ -3,6 +3,7 @@ import { HooksObject } from '@feathersjs/feathers';
import service from 'feathers-sequelize';
import { DataTypes } from 'sequelize';
import logger from '../logger';
import db from '../models';
import Grant from '../models/grant';

53
views/admin.pug Normal file
View File

@ -0,0 +1,53 @@
doctype html
html(lang="en")
link(rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous")
head
title Uplink Admin
body.bg-dark
.container
div.float-right
ul.list-inline
li.list-inline-item
a(href='/logout').text-warning Logout
li.list-inline-item
a(href='/Dashboard').text-success.bg-light
strong Dashboard
h1.text-light Uplink Admin
.container.text-light
div Hey there &nbsp;
strong #{user.github.profile.displayName}
.container.text-light
table.table-dark.table.table-striped.table-bordered.table-hover
tr
th(scope='col').text-center Name
th(scope='col').text-center Type
th(scope='col').text-center Actions
tr.bg-secondary
form(method='POST', action='/grants')
input(type='hidden', name='_return', value='true')
input(name='_method', type='hidden', value='PUT')
td.text-center
input(name='name', placeholder='Enter GitHub username').form-control
td.text-center
input(name='type', placeholder='Enter event type').form-control
td.text-center
button(type='submit').btn.btn-success Grant
each g in grants
tr
td.text-center.
#{g.name}
td.text-center.
#{g.type}
td.text-center
form(method='POST', action='/grants/' + g.id)
input(type='hidden', name='_return', value='true')
input(type='hidden', name='_method', value='DELETE')
button(type=submit, title='Delete grants record ' + g.id).btn.btn-danger Delete
// vim: ft=haml

View File

@ -6,7 +6,15 @@ html(lang="en")
body.bg-dark
.container
div.float-right
a(href='/logout').text-success Logout
ul.list-inline
li.list-inline-item
a(href='/logout').text-warning Logout
if isAdmin
li.list-inline-item
a(href='/admin').text-danger.bg-light
strong Admin
h1.text-light Uplink Dashboard
.container.text-light
div Hey there &nbsp;
@ -23,7 +31,7 @@ html(lang="en")
li.list-inline-item
a(href='?id[$lt]=' + events.data[events.data.length - 1].id, title='View the next page of results').
Next
table.table-dark.table
table.table-dark.table.table-striped.table-borderd.table-hover
tr
th(scope='col').text-center ID &nbsp;
strong