forked from brigadehub/brigadehub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
455 lines (430 loc) · 15.2 KB
/
app.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
/**
* Check Node Version FIRST
*/
require('node-version-checker')
/**
* Module dependencies.
*/
var express = require('express')
var cookieParser = require('cookie-parser')
var compress = require('compression')
var favicon = require('serve-favicon')
var session = require('express-session')
var bodyParser = require('body-parser')
var logger = require('morgan')
var errorHandler = require('errorhandler')
var lusca = require('lusca')
var methodOverride = require('method-override')
var MongoStore = require('connect-mongo/es5')(session)
var flash = require('express-flash')
var mongoose = require('mongoose')
var passport = require('passport')
var expressValidator = require('express-validator')
var sass = require('node-sass-middleware')
var path = require('path')
var requireDir = require('require-dir')
/* var _ = require('lodash') */
var DB_INSTANTIATED
/**
* Load environment variables from .env file, where API keys and passwords are configured.
*
* Default path: .env
*/
require('./dotenv.js')()
/**
* Controllers (route handlers).
*/
var homeCtrl = require('./controllers/home')
var eventsCtrl = require('./controllers/events')
var blogCtrl = require('./controllers/blog')
var projectsCtrl = require('./controllers/projects')
var contactCtrl = require('./controllers/contact')
var usersCtrl = require('./controllers/user')
var brigadeCtrl = require('./controllers/brigade')
/*
* Helpers
*/
var helpers = requireDir('./helpers')
var brigadeDetails
/**
* API keys and Passport configuration.
*/
var passportConf = require('./config/passport')
/**
* Create Express server.
*/
var app = express()
/**
* Connect to MongoDB.
*/
mongoose.connect(process.env.MONGODB || process.env.MONGOLAB_URI)
mongoose.connection.on('error', function () {
console.log('new auth callback!')
process.exit(1)
})
/**
* Check Model Settings in db
*/
var Brigade = require('./models/Brigade')
var User = require('./models/Users')
/**
* Express configuration.
*/
app.set('port', process.env.PORT || 5465)
app.set('views', path.join(__dirname, 'themes'))
app.set('view engine', 'jade')
app.use(compress())
app.use(logger('dev'))
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
app.use(expressValidator())
app.use(methodOverride())
app.use(cookieParser())
app.use(session({
resave: true,
saveUninitialized: true,
secret: process.env.SESSION_SECRET,
store: new MongoStore({
url: process.env.MONGODB || process.env.MONGOLAB_URI,
autoReconnect: true
})
}))
/* Check if db is connected */
app.use(checkDB)
function checkDB (req, res, next) {
if (!DB_INSTANTIATED) {
return setTimeout(function () {
checkDB(req, res, next)
}, 500)
}
next()
}
/* Attach brigade info to req */
app.use(function (req, res, next) {
Brigade.find({}, function (err, results) {
if (err) throw err
if (!results.length) throw new Error('BRIGADE NOT IN DATABASE')
res.locals = res.locals || {}
res.locals.brigade = results[0]
helpers.tokenLoader(passport, res.locals.brigade.auth)
next()
})
})
app.use(passport.initialize())
app.use(passport.session())
app.use(flash())
app.use(lusca({
csrf: true,
xframe: 'SAMEORIGIN',
xssProtection: true
}))
app.use(function (req, res, next) {
// check postAuthLink and see if going to auth callback
if (req.user && req.user.postAuthLink && req.user.postAuthLink.length) {
console.log('req.user.postAuthLink present!', req.user.postAuthLink)
if (
!(/auth\/github\/callback/i.test(req.path)) &&
!(/auth\/github\/elevate/i.test(req.path))
) {
console.log('path not callback or elevate!', req.path)
return User.findById(req.user.id, function (err, user) {
if (err) console.error(err)
user.postAuthLink = req.user.postAuthLink = ''
user.save(function (err) {
if (err) console.error(err)
res.locals.user = req.user
next()
})
})
}
}
res.locals.user = req.user
next()
})
app.use(function (req, res, next) {
req.previousURL = req.header('Referer') || '/'
next()
})
app.use(function (req, res, next) {
if (/api/i.test(req.path)) {
req.session.returnTo = req.path
}
next()
})
/**
* Primary app routes.
*/
app.get('/', homeCtrl.index)
app.get('/login', usersCtrl.getLogin)
app.post('/login', usersCtrl.postLogin)
app.get('/login/edit',
passportConf.isAuthenticated,
passportConf.checkRoles(['core', 'superAdmin']),
usersCtrl.getLoginEdit)
app.post('/login/edit',
passportConf.isAuthenticated,
passportConf.checkRoles(['core', 'superAdmin']),
usersCtrl.postLoginEdit)
app.get('/logout', usersCtrl.getLogout)
app.get('/contact', contactCtrl.getContact)
app.post('/contact', contactCtrl.postContact)
app.get('/contact/edit',
passportConf.isAuthenticated,
passportConf.checkRoles(['core', 'superAdmin']),
contactCtrl.getContactEdit)
app.post('/contact/message/new',
passportConf.isAuthenticated,
passportConf.checkRoles(['core', 'superAdmin']),
contactCtrl.postContactMessage)
/**
* Meta Routes
*/
app.get('/account', passportConf.isAuthenticated, usersCtrl.getAccount)
app.post('/account/profile', passportConf.isAuthenticated, usersCtrl.postUpdateProfile)
app.post('/account/delete', passportConf.isAuthenticated, usersCtrl.postDeleteAccount)
app.get('/brigade',
passportConf.isAuthenticated,
passportConf.checkRoles(['superAdmin']),
passportConf.checkScopes(['user', 'repo', 'admin:org', 'admin:repo_hook', 'admin:org_hook']),
brigadeCtrl.getBrigade)
app.post('/brigade',
passportConf.isAuthenticated,
passportConf.checkRoles(['superAdmin']),
passportConf.checkScopes(['user', 'repo', 'admin:org', 'admin:repo_hook', 'admin:org_hook']),
brigadeCtrl.postBrigade)
/**
* Project routes.
*/
app.get('/projects', projectsCtrl.getProjects)
app.get('/projects/manage',
passportConf.isAuthenticated,
passportConf.checkRoles(['lead', 'core', 'superAdmin']),
passportConf.checkScopes(['user', 'repo']),
projectsCtrl.getProjectsManage)
app.post('/projects/manage',
passportConf.isAuthenticated,
passportConf.checkRoles(['lead', 'core', 'superAdmin']),
passportConf.checkScopes(['user', 'repo']),
projectsCtrl.postProjectsManage)
app.post('/projects/sync',
passportConf.isAuthenticated,
passportConf.checkRoles(['core', 'superAdmin']),
passportConf.checkScopes(['user', 'repo', 'admin:org', 'admin:repo_hook', 'admin:org_hook']),
projectsCtrl.postProjectsSync)
app.get('/projects/new', passportConf.isAuthenticated, projectsCtrl.getProjectsNew)
app.post('/projects/new', passportConf.isAuthenticated, projectsCtrl.postProjectsNew)
app.get('/projects/:projectId', projectsCtrl.getProjectsID)
app.post('/projects/:projectId',
passportConf.isAuthenticated,
passportConf.checkRoles(['lead', 'core', 'superAdmin']),
passportConf.checkScopes(['user', 'repo']),
projectsCtrl.postProjectsIDSettings)
app.get('/projects/:projectId/settings',
passportConf.isAuthenticated,
passportConf.checkRoles(['lead', 'core', 'superAdmin']),
passportConf.checkScopes(['user', 'repo']),
projectsCtrl.getProjectsIDSettings)
app.post('/projects/:projectId/sync',
passportConf.isAuthenticated,
passportConf.checkRoles(['lead', 'core', 'superAdmin']),
passportConf.checkScopes(['user', 'repo']),
projectsCtrl.postProjectsIDSync)
/**
* Events routes.
*/
app.get('/events', eventsCtrl.getEvents)
app.get('/events/manage',
passportConf.isAuthenticated,
passportConf.checkRoles(['core', 'superAdmin']),
passportConf.checkScopes(['user', 'repo', 'admin:org', 'admin:repo_hook', 'admin:org_hook']),
eventsCtrl.getEventsManage)
app.post('/events/manage',
passportConf.isAuthenticated,
passportConf.checkRoles(['core', 'superAdmin']),
passportConf.checkScopes(['user', 'repo', 'admin:org', 'admin:repo_hook', 'admin:org_hook']),
eventsCtrl.postEventsManage)
app.post('/events/sync',
passportConf.isAuthenticated,
passportConf.checkRoles(['core', 'superAdmin']),
passportConf.checkScopes(['user', 'repo', 'admin:org', 'admin:repo_hook', 'admin:org_hook']),
eventsCtrl.postEventsSync)
app.get('/events/new',
passportConf.isAuthenticated,
passportConf.checkRoles(['core', 'superAdmin']),
passportConf.checkScopes(['user', 'repo', 'admin:org', 'admin:repo_hook', 'admin:org_hook']),
eventsCtrl.getEventsNew)
app.post('/events/new',
passportConf.isAuthenticated,
passportConf.checkRoles(['core', 'superAdmin']),
passportConf.checkScopes(['user', 'repo', 'admin:org', 'admin:repo_hook', 'admin:org_hook']),
eventsCtrl.postEventsNew)
app.post('/events/delete',
passportConf.isAuthenticated,
passportConf.checkRoles(['core', 'superAdmin']),
passportConf.checkScopes(['user', 'repo', 'admin:org', 'admin:repo_hook', 'admin:org_hook']),
eventsCtrl.postDeleteAllEvents)
app.get('/events/:eventId', eventsCtrl.getEventsID)
app.post('/events/:eventId',
passportConf.isAuthenticated,
passportConf.checkRoles(['core', 'superAdmin']),
passportConf.checkScopes(['user', 'repo', 'admin:org', 'admin:repo_hook', 'admin:org_hook']),
eventsCtrl.postEventsIDSettings)
app.get('/events/:eventId/settings',
passportConf.isAuthenticated,
passportConf.checkRoles(['core', 'superAdmin']),
passportConf.checkScopes(['user', 'repo', 'admin:org', 'admin:repo_hook', 'admin:org_hook']),
eventsCtrl.getEventsIDSettings)
app.post('/events/:eventId/sync',
passportConf.isAuthenticated,
passportConf.checkRoles(['core', 'superAdmin']),
passportConf.checkScopes(['user', 'repo', 'admin:org', 'admin:repo_hook', 'admin:org_hook']),
eventsCtrl.postEventsIDSync)
app.post('/events/:eventId/delete',
passportConf.isAuthenticated,
passportConf.checkRoles(['core', 'superAdmin']),
passportConf.checkScopes(['user', 'repo', 'admin:org', 'admin:repo_hook', 'admin:org_hook']),
eventsCtrl.postDeleteEvent)
/**
* Blog routes.
*/
app.get('/blog', blogCtrl.getBlog)
app.get('/blog/manage',
passportConf.isAuthenticated,
passportConf.checkRoles(['core', 'superAdmin', 'coreLead', 'blog', 'lead']),
blogCtrl.getBlogManage)
app.post('/blog/manage',
passportConf.isAuthenticated,
passportConf.checkRoles(['core', 'superAdmin', 'coreLead', 'blog', 'lead']),
blogCtrl.postBlogManage)
app.post('/blog/sync',
passportConf.isAuthenticated,
passportConf.checkRoles(['core', 'superAdmin']),
blogCtrl.postBlogSync)
app.get('/blog/new',
passportConf.isAuthenticated,
passportConf.checkRoles(['core', 'superAdmin', 'coreLead', 'blog', 'lead']),
blogCtrl.getBlogNew)
app.post('/blog/new',
passportConf.isAuthenticated,
passportConf.checkRoles(['core', 'superAdmin', 'coreLead', 'blog', 'lead']),
blogCtrl.postBlogNew)
app.get('/blog/post/:blogId', blogCtrl.getBlogID)
app.post('/blog/post/:blogId',
passportConf.isAuthenticated,
passportConf.checkRoles(['core', 'superAdmin', 'coreLead', 'blog', 'lead']),
blogCtrl.postBlogIDEdit)
app.get('/blog/post/:blogId/edit',
passportConf.isAuthenticated,
passportConf.checkRoles(['core', 'superAdmin', 'coreLead', 'blog', 'lead']),
blogCtrl.getBlogIDEdit)
app.post('/blog/:blogId/sync',
passportConf.isAuthenticated,
passportConf.checkRoles(['core', 'superAdmin', 'coreLead', 'blog', 'lead']),
blogCtrl.postBlogIDSync)
app.post('/blog/post/:blogId/delete', passportConf.isAuthenticated, blogCtrl.postBlogIDDelete)
app.get('/author/:authorId', passportConf.isAuthenticated, blogCtrl.getAuthorId)
/**
* Users routes.
*/
app.get('/users', usersCtrl.getUsers)
app.get('/users/manage',
passportConf.isAuthenticated,
passportConf.checkRoles(['core', 'superAdmin']),
passportConf.checkScopes(['user', 'repo', 'admin:org', 'admin:repo_hook', 'admin:org_hook']),
usersCtrl.getUsersManage)
app.post('/users/manage',
passportConf.isAuthenticated,
passportConf.checkRoles(['core', 'superAdmin']),
passportConf.checkScopes(['user', 'repo', 'admin:org', 'admin:repo_hook', 'admin:org_hook']),
usersCtrl.postUsersManage)
app.post('/users/sync',
passportConf.isAuthenticated,
passportConf.checkRoles(['core', 'superAdmin']),
passportConf.checkScopes(['user', 'repo', 'admin:org', 'admin:repo_hook', 'admin:org_hook']),
usersCtrl.postUsersSync)
app.get('/users/new',
passportConf.isAuthenticated,
passportConf.checkRoles(['core', 'superAdmin']),
passportConf.checkScopes(['user', 'repo', 'admin:org', 'admin:repo_hook', 'admin:org_hook']),
usersCtrl.getUsersNew)
app.post('/users/new',
passportConf.isAuthenticated,
passportConf.checkRoles(['core', 'superAdmin']),
passportConf.checkScopes(['user', 'repo', 'admin:org', 'admin:repo_hook', 'admin:org_hook']),
usersCtrl.postUsersNew)
app.get('/users/:userId', usersCtrl.getUsersID)
app.post('/users/:userId',
passportConf.isAuthenticated,
passportConf.checkRoles(['core', 'superAdmin']),
passportConf.checkScopes(['user', 'repo', 'admin:org', 'admin:repo_hook', 'admin:org_hook']),
usersCtrl.postUsersIDSettings)
app.get('/users/:userId/settings',
passportConf.isAuthenticated,
passportConf.checkRoles(['core', 'superAdmin']),
passportConf.checkScopes(['user', 'repo', 'admin:org', 'admin:repo_hook', 'admin:org_hook']),
usersCtrl.getUsersIDSettings)
app.post('/users/:userId/sync',
passportConf.isAuthenticated,
passportConf.checkRoles(['core', 'superAdmin']),
passportConf.checkScopes(['user', 'repo', 'admin:org', 'admin:repo_hook', 'admin:org_hook']),
usersCtrl.postUsersIDSync)
/**
* OAuth authentication routes. (Sign in)
*/
app.get('/auth/github', passport.authenticate('github', {
scope: [
'user',
'public_repo'
]
}))
app.get('/auth/github/elevate', passportConf.elevateScope)
app.get('/auth/github/callback', passport.authenticate('github', { failureRedirect: '/login' }), function (req, res) {
console.log('new github callback!', req.user.postAuthLink)
req.user.postAuthLink = req.user.postAuthLink || ''
res.redirect(req.user.postAuthLink.length ? req.user.postAuthLink : '/')
})
app.get('/auth/meetup', passport.authenticate('meetup', { scope: ['basic', 'rsvp'] }))
app.get('/auth/meetup/callback', passport.authenticate('meetup', { failureRedirect: '/account' }), function (req, res) {
res.redirect(req.session.returnTo || '/account')
})
app.get('/auth/disconnect/:service', passportConf.isAuthenticated, usersCtrl.disconnectService)
/**
* Error Handler.
*/
app.use(errorHandler())
/**
* Check if brigade exists before starting Express server.
*/
Brigade.find({slug: process.env.BRIGADE}, function (err, results) {
if (err) throw err
if (!results.length) {
var defaultBrigadeData = require('./seeds/development/Brigade')()[0]
defaultBrigadeData.slug = process.env.BRIGADE
brigadeDetails = defaultBrigadeData
var defaultBrigade = new Brigade(defaultBrigadeData)
defaultBrigade.save(function (err) {
if (err) throw err
DB_INSTANTIATED = true
startServer()
})
} else {
DB_INSTANTIATED = true
brigadeDetails = results[0]
startServer()
}
})
function startServer () {
app.use(sass({
src: path.join(__dirname, 'themes/' + brigadeDetails.theme.slug + '/public'),
dest: path.join(__dirname, 'themes/' + brigadeDetails.theme.slug + '/public'),
debug: true,
sourceMap: true,
outputStyle: 'expanded'
}))
app.use(favicon(path.join(__dirname, 'themes/' + brigadeDetails.theme.slug + '/public', 'favicon.png')))
app.use(express.static(path.join(__dirname, 'themes/' + brigadeDetails.theme.slug + '/public'), { maxAge: 31557600000 }))
app.listen(app.get('port'), function () {
console.log('Brigadehub listening on port', app.get('port'))
})
}
module.exports = app