Add capability to specify different session store
Project: http://git-wip-us.apache.org/repos/asf/helix/repo Commit: http://git-wip-us.apache.org/repos/asf/helix/commit/b1540f08 Tree: http://git-wip-us.apache.org/repos/asf/helix/tree/b1540f08 Diff: http://git-wip-us.apache.org/repos/asf/helix/diff/b1540f08 Branch: refs/heads/master Commit: b1540f08d72c9c564f3e9b6e9744a2042485fa75 Parents: 093f7ab Author: Vivo Xu <[email protected]> Authored: Mon Nov 27 17:47:41 2017 -0800 Committer: Junkai Xue <[email protected]> Committed: Wed Jan 24 18:31:56 2018 -0800 ---------------------------------------------------------------------- helix-front/server/app.ts | 8 +++++++- helix-front/server/config.ts | 6 ++++-- helix-front/server/controllers/helix.ts | 9 ++------- helix-front/server/controllers/user.ts | 18 +++++++++++------- 4 files changed, 24 insertions(+), 17 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/helix/blob/b1540f08/helix-front/server/app.ts ---------------------------------------------------------------------- diff --git a/helix-front/server/app.ts b/helix-front/server/app.ts index 5d9568c..b35aef6 100644 --- a/helix-front/server/app.ts +++ b/helix-front/server/app.ts @@ -8,7 +8,7 @@ import * as http from 'http'; import * as https from 'https'; import * as session from 'express-session'; -import { SSL } from './config'; +import { SSL, SESSION_STORE } from './config'; import setRoutes from './routes'; const app = express(); @@ -21,6 +21,7 @@ app.use('/', express.static(path.join(__dirname, '../public'))); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true })); app.use(session({ + store: SESSION_STORE, secret: 'helix', resave: true, saveUninitialized: true, @@ -39,6 +40,11 @@ server.listen(app.get('port'), () => { console.log(`App is listening on port ${ app.get('port') } as HTTP`); }); +process.on('uncaughtException', function(err){ + console.error('uncaughtException: ' + err.message); + console.error(err.stack); +}); + // setup SSL if (SSL.port > 0 && fs.existsSync(SSL.keyfile) && fs.existsSync(SSL.certfile)) { let credentials: any = { http://git-wip-us.apache.org/repos/asf/helix/blob/b1540f08/helix-front/server/config.ts ---------------------------------------------------------------------- diff --git a/helix-front/server/config.ts b/helix-front/server/config.ts index c49c5f7..1f49dd2 100644 --- a/helix-front/server/config.ts +++ b/helix-front/server/config.ts @@ -4,6 +4,8 @@ export const HELIX_ENDPOINTS = { }] }; +export const SESSION_STORE = undefined; + export const SSL = { port: 0, keyfile: '', @@ -12,6 +14,6 @@ export const SSL = { cafiles: [] }; -export function IsAdmin(username: string) { - return username === 'root'; +export function CheckAdmin(username: string, callback: (boolean) => void) { + callback(username === 'root'); } http://git-wip-us.apache.org/repos/asf/helix/blob/b1540f08/helix-front/server/controllers/helix.ts ---------------------------------------------------------------------- diff --git a/helix-front/server/controllers/helix.ts b/helix-front/server/controllers/helix.ts index ee8b412..9d7fcf5 100644 --- a/helix-front/server/controllers/helix.ts +++ b/helix-front/server/controllers/helix.ts @@ -2,7 +2,7 @@ import { Request, Response, Router } from 'express'; import * as request from 'request'; -import { HELIX_ENDPOINTS, IsAdmin } from '../config'; +import { HELIX_ENDPOINTS } from '../config'; export class HelixCtrl { @@ -25,7 +25,7 @@ export class HelixCtrl { const user = req.session.username; const method = req.method.toLowerCase(); - if (method != 'get' && !IsAdmin(user)) { + if (method != 'get' && !req.session.isAdmin) { res.status(403).send('Forbidden'); return; } @@ -58,11 +58,6 @@ export class HelixCtrl { } else { res.status(404).send('Not found'); } - - process.on('uncaughtException', function(err){ - console.error('uncaughtException: ' + err.message); - console.error(err.stack); - }); } protected list(req: Request, res: Response) { http://git-wip-us.apache.org/repos/asf/helix/blob/b1540f08/helix-front/server/controllers/user.ts ---------------------------------------------------------------------- diff --git a/helix-front/server/controllers/user.ts b/helix-front/server/controllers/user.ts index e12b08f..84d2c11 100644 --- a/helix-front/server/controllers/user.ts +++ b/helix-front/server/controllers/user.ts @@ -2,7 +2,7 @@ import { Request, Response, Router } from 'express'; import * as request from 'request'; -import { IsAdmin } from '../config'; +import { CheckAdmin } from '../config'; export class UserCtrl { @@ -16,11 +16,15 @@ export class UserCtrl { protected authorize(req: Request, res: Response) { if (req.query.name) { req.session.username = req.query.name; - if (req.query.url) { - res.redirect(req.query.url); - } else { - res.redirect('/'); - } + CheckAdmin(req.session.username, (isAdmin: boolean) => { + req.session.isAdmin = isAdmin; + + if (req.query.url) { + res.redirect(req.query.url); + } else { + res.redirect('/'); + } + }); } else { res.status(401).send('Unauthorized'); } @@ -31,6 +35,6 @@ export class UserCtrl { } protected can(req: Request, res: Response) { - res.json(IsAdmin(req.session.username)); + res.json(req.session.isAdmin ? true : false); } }
