forked from formbuddy/d2d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
d2d.js
82 lines (67 loc) · 2.11 KB
/
d2d.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var dbConfig = require('./lib/db.js');
var mongoose = require('mongoose');
// Connect to DB
mongoose.connect(dbConfig.url);
//setup handlebars and view engine
var handlebars = require('express-handlebars')
.create({defaultLayout: 'main'});
app.engine('handlebars', handlebars.engine);
app.set('view engine', 'handlebars');
app.set('port', process.env.PORT || 3000);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(cookieParser());
// Configuring Passport
var passport = require('passport');
var session = require('express-session');
app.use(session({
secret: 'randomstring',
resave: false,
saveUninitialized: true
}));
app.use(passport.initialize());
app.use(passport.session());
// Using the flash middleware provided by connect-flash
// to store messages in session and displaying in templates
var flash = require('connect-flash');
app.use(flash());
// Initialize Passport
var initPassport = require('./lib/initPassport.js');
initPassport(passport);
var ConnectRoles = require('connect-roles');
var user = new ConnectRoles({
failureHandler: function (req, res, action) {
// optional function to customise code that runs when
// user fails authorisation
var accept = req.headers.accept || '';
res.status(403);
res.send('Access Denied - You don\'t have permission to: ' + action);
}
});
app.use(user.middleware());
//admin users can access all pages
user.use(function (req) {
if (req.user.role === 'admin') {
return true;
}
});
user.use('acvolunteer', function (req) {
if(req.user.ac.indexOf(req.params.acno) >= 0){
return true;
}
else{
return false;
}
});
var routes = require('./lib/routes.js')(passport, user);
app.use('/', routes);
app.listen(app.get('port'), '0.0.0.0', function(){
console.log('Express started on http://0.0.0.0:' +
app.get('port') + '; press Ctrl+C to terminate');
});