-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlinz.js
815 lines (671 loc) · 22.2 KB
/
linz.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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
'use strict';
const { deprecate } = require('util');
/**
* Load required modules
*/
var express = require('express'),
bodyParser = require('body-parser'),
cookieParser = require('cookie-parser'),
expressSession = require('express-session'),
path = require('path'),
events = require('events'),
bunyan = require('bunyan'),
async = require('async'),
lessMiddleware = require('less-middleware'),
flash = require('connect-flash'),
xssFilter = require('x-xss-protection');
/**
* Define error handling loggers
*/
var debugModels = require('debug')('linz:models'),
debugConfigs = require('debug')('linz:configs'),
debugGeneral = require('debug')('linz:general'),
debugSet = require('debug')('linz:set');
/**
* Linz constructor
*/
function Linz() {
// internal properties
this.settings = {};
this.loggers = {};
// apply default linz options
this.options(require('./lib/defaults'));
}
/**
* Linz inherits from EventEmitter for Linz.prototype.on('event') handling
*
*/
Linz.prototype.__proto__ = events.EventEmitter.prototype;
/**
* Internal method used to setup options
*
* @return Linz for chaining
*/
Linz.prototype.options = function(opts) {
var _this = this,
opts = opts || {};
for (var opt in opts) {
_this.set(opt, opts[opt]);
}
return this;
};
/**
* Set a value or setting on Linz
*
* @return Linz for chaining
*/
Linz.prototype.set = function(setting, val, override) {
if (arguments.length === 1) {
return this.settings[setting];
}
// can only set the following values once
var onceOnly = ['models path'];
if (
onceOnly.indexOf(setting) >= 0 &&
this.settings[setting] !== undefined
) {
return debugSet(
"You can only set '" + setting + "' once. Ignoring set this time."
);
}
// defaults to true
override = !(override === false);
const shouldOverride =
override ||
(override === false && this.settings[setting] === undefined);
if (
shouldOverride &&
['requiredScripts', 'requiredStyles'].includes(setting)
) {
debugSet(`Merging into ${setting}`);
this.settings[setting] = { ...this.settings[setting], ...val };
this.emit(setting, val);
return this;
}
// override unless previously set
if (shouldOverride) {
debugSet('Setting ' + setting + ' to ' + val);
this.settings[setting] = val;
this.emit(setting, val);
}
return this;
};
/**
* Get a value or setting on Linz
*
* @return setting value
*/
Linz.prototype.get = function(setting) {
return this.settings[setting];
};
/*
* Set a settings value to true
* synonym for admin.set('setting', true);
* @return Linz for chaining
*/
Linz.prototype.enable = function(setting) {
this.set(setting, true);
return this;
};
/*
* Set a settings value to false
* synonym for admin.set('setting', false);
* @return Linz for chaining
*/
Linz.prototype.disable = function(setting) {
this.set(setting, false);
return this;
};
/**
* Expose Linz
*/
var linz = (module.exports = exports = new Linz());
/**
* Expose linz modules
*/
linz.hbs = require('./lib/hbs-helpers');
linz.formtools = require('./lib/formtools');
linz.versions = require('./lib/versions');
linz.concurrencyControl = require('./lib/concurrency-control');
linz.middleware = require('./lib/api/middleware');
linz.api = require('./lib/api');
linz.utils = require('./lib/utils');
/**
* Define local variables used by the Linz class
*/
var middlewareManager = require('./lib/middleware'),
routesManager = require('./lib/router'),
helpersModels = require('./lib/helpers-models'),
helpersConfigs = require('./lib/helpers-configs'),
libPassport = require('./lib/passport'),
error = require('./lib/errors');
/**
* Initialize linz with the express app
* signature: app, mongoose, options
*
*/
Linz.prototype.init = function(opts = {}) {
// Use anything that has been passed through, or default it as required.
this.app = opts.express || express();
this.mongoose = opts.mongoose || require('mongoose');
this.passport = opts.passport || require('passport');
// overlay runtime options, these will override linz defaults
this.options(opts.options || {});
// configure everything
this.configure();
return this;
};
/**
* Initialize the application
*
* @api private
*/
Linz.prototype.configure = function() {
var _this = this;
// Start connecting to the database, prior to actually needing it.
// This will speed up Initialisation of Linz a little.
// Check for either a connect(ed|ing) mongoose object, or mongoose URI.
if ([1, 2].indexOf(_this.mongoose.connection.readyState) < 0) {
if (!_this.get('mongo')) {
throw new Error(
'You must either supply a connected mongoose object, or a mongo URI'
);
}
debugGeneral('Connecting to the database');
_this.mongoose.connect(_this.get('mongo'), _this.get('mongoOptions'));
_this.mongoose.connection.on('connected', function() {
debugGeneral('Database connected');
});
}
async.series(
[
function(cb) {
return _this.defaultConfiguration(cb);
},
function(cb) {
return _this.bootstrapExpress(cb);
},
function(cb) {
return _this.mountAdmin(cb);
},
function(cb) {
return _this.setupORM(cb);
},
function(cb) {
return _this.loadConfigs(cb);
},
function(cb) {
_this.set('app', _this.app);
// Wait until we have a connection to the database to proceed.
var intervalId = setInterval(function() {
if (_this.mongoose.connection.readyState === 1) {
clearInterval(intervalId);
_this.initConfigs(cb);
}
}, 100);
},
function(cb) {
return _this.loadModels(cb);
},
function(cb) {
return _this.buildNavigation(cb);
},
function(cb) {
return _this.bootstrapExpressLocals(cb);
},
function(cb) {
return _this.checkSetup(cb);
},
],
function(err) {
if (err) {
throw err;
}
return _this.emit('initialised');
}
);
};
/**
* Load ORM
*
* @return void
*/
Linz.prototype.setupORM = function(cb) {
// extend the mongoose types
helpersModels.extendTypes();
return cb(null);
};
/**
* Search and look for the models folder, then go ahead and load them
*
* @return void
*/
Linz.prototype.loadModels = function(cb) {
if (!this.get('load models')) {
this.set('models', {});
return cb();
}
var _this = this;
// set the default models path
this.set('models path', path.resolve(this.get('cwd'), 'models'), false);
var modelsPath = this.get('models path');
helpersModels
.loadModels(modelsPath)
.then((models) => {
_this.set('models', models || {});
return helpersModels.initModels();
})
.then(() => cb())
.catch(cb);
};
/**
* Search and look for the configs folder, then go ahead and load them
*
* @return void
*/
Linz.prototype.loadConfigs = function(cb) {
if (!this.get('load configs')) {
this.set('configs', {});
return cb(null);
}
// set the default configs path
this.set('configs path', path.resolve(this.get('cwd'), 'configs'), false);
var configsPath = this.get('configs path');
this.set('configs', helpersConfigs.loadConfigs(configsPath));
return cb(null);
};
/**
* Load document from configs collection and create new ones if none are found.
*
* @return void
*/
Linz.prototype.initConfigs = function(cb) {
const db = this.mongoose.connection.db;
const configs = this.get('configs');
const collection = db.collection(this.get('configs collection name'));
async.each(
Object.keys(configs),
async function(configName) {
const doc = await collection.findOne({ _id: configName });
if (doc) {
const updatedDoc = {};
// doc exists, check if there are any new properties
configs[configName].schema.eachPath(function(fieldName, field) {
if (doc[fieldName] === undefined) {
updatedDoc[
fieldName
] = linz.formtools.utils.getDefaultValue(field);
doc[fieldName] = updatedDoc[fieldName];
}
});
if (!Object.keys(updatedDoc).length) {
configs[configName].config = doc;
debugConfigs('Initialised config %s', configName);
// since there are no change, return early
return;
}
try {
await collection.updateOne(
{ _id: configName },
{ $set: updatedDoc },
{ w: 1 }
);
// add config to linz
configs[configName].config = doc;
debugConfigs('Initialised config %s', configName);
return;
} catch (err) {
throw new Error(
'Unable to write config file %s to database. ' +
err.message,
configName
);
}
}
const newConfig = {};
// contruct doc from config schema
configs[configName].schema.eachPath(function(fieldName, field) {
if (fieldName !== '_id') {
newConfig[fieldName] = linz.formtools.utils.getDefaultValue(
field
);
}
});
// Use update instead of insert to prevent duplicate key errors.
try {
await collection.updateOne(
{ _id: configName },
{ $set: newConfig },
{
upsert: true,
w: 1,
}
);
debugConfigs('Initialised config %s', configName);
// add new config to linz
configs[configName].config = newConfig;
return;
} catch (err) {
throw new Error(
'Unable to write config file %s to database. ' +
err.message,
configName
);
}
},
cb
);
};
/**
* Setup the default configuration for Linz
*
* @return void
*/
Linz.prototype.defaultConfiguration = function(cb) {
var _this = this;
debugGeneral('Setting up the default configuration');
// setup the router
this.router = routesManager.getRouter();
// assign the default middleware (across all routes)
middlewareManager.mountDefaults(this.app);
// assign admin specific middleware
middlewareManager.mountAdminMiddleware(this.router);
// assign the route params
routesManager.setupParams();
// assign the admin routes
routesManager.setupAdminRoutes();
// assign the model CRUD routes
routesManager.setupModelRoutes();
// assign the config CRUD routes
routesManager.setupConfigsRoutes();
// assign the logging routes
if (this.get('request logging') === true)
routesManager.setupLoggingRoutes();
// store the cwd
this.set('cwd', process.cwd());
// place holder for the models, which get loaded in next
this.set('models', []);
return cb(null);
};
Linz.prototype.mountAdmin = function(cb) {
// We need access to the linz defaults so the require has to go here.
const csrf = require('./lib/csrf');
this.app.use(csrf());
debugGeneral('Mounting Linz on ' + this.get('admin path'));
this.app.use(this.get('admin path'), this.router);
return cb(null);
};
/**
* We need to setup the host express app with a few bits and pieces
*
* @api private
*/
Linz.prototype.bootstrapExpress = function(cb) {
// only run once
if (this.bBootstrapExpress) {
return cb(null);
}
if (!this.bBootstrapExpress) {
this.bBootstrapExpress = true;
}
debugGeneral('Bootstrapping express');
this.app.disable('x-powered-by');
this.app.use(xssFilter());
// Setup `/public` middleware first as we don't need session handle to resolve this routes
if ((process.env.NODE_ENV || 'development') === 'development') {
this.app.use(
this.get('admin path') + '/public',
lessMiddleware(__dirname + '/public', {
force: true,
preprocess: {
path: function(pathname, reqs) {
return pathname.replace(/\/css/, '/src/css');
},
},
})
);
}
// setup admin static routes
this.app.use(
this.get('admin path') + '/public/',
express.static(path.resolve(__dirname, 'public'))
);
this.app.set('view engine', 'pug');
// setup body-parser, and cookie-parser
this.app.use(bodyParser());
// need to hook in custom cookie parser if provided
if (typeof this.get('cookie parser') === 'function') {
this.app.use(this.get('cookie parser'));
} else {
this.app.use(
cookieParser(this.get('cookie secret'), this.get('cookie options'))
);
}
// need to hook session in here as it must be before passport.initialize
if (typeof this.get('session middleware') === 'function') {
this.app.use(this.get('session middleware'));
} else {
this.app.use(expressSession(this.get('session options')));
}
// setup connect-flash
this.app.use(flash());
// configure passport
libPassport(this.passport);
// initialize passport (on all routes)
this.app.use(this.passport.initialize());
this.app.use(this.passport.session());
return cb(null);
};
/**
* Setup express locals for admin template generation
*
* @api private
*/
Linz.prototype.bootstrapExpressLocals = function(cb) {
// expose our settings to the rendering engine
this.app.locals['linz'] = this;
this.app.locals['adminPath'] = this.get('admin path');
this.app.locals['adminTitle'] = this.get('admin title');
this.app.locals['env'] = process.env.NODE_ENV || 'development';
if (this.get('css file')) {
deprecate(() => {
this.app.locals.adminCSSFile = this.get('css file');
}, 'adminCSSFile: Use the styles default instead')();
}
if (this.get('js file')) {
deprecate(() => {
this.app.locals.adminJSFile = this.get('js file');
}, 'adminJSFile: Use the scripts default instead')();
}
return cb(null);
};
/*
* Set a settings value to true
* synonym for admin.set('setting', true);
* @return Linz for chaining
*/
Linz.prototype.enable = function(setting) {
this.set(setting, true);
return this;
};
/*
* Set a settings value to false
* synonym for admin.set('setting', false);
* @return Linz for chaining
*/
Linz.prototype.disable = function(setting) {
this.set(setting, false);
return this;
};
/*
* Return the linz navigation structure. This is the complete navigation structure.
* It will be further manipulated by middleware (using the API) to customise per user.
*
* @api private
*/
Linz.prototype.buildNavigation = function(cb) {
const collator = new Intl.Collator();
var nav = [],
_this = this,
linzModels = this.get('models'),
linzConfigs = this.get('configs');
// models, configs and logs
async.parallel(
[
function(done) {
// add a reference for all of the models
var models = {
name: 'Models',
href: _this.get('admin path') + '/models/list',
children: [],
permission: function(user, callback) {
return linz.api.permissions.hasPermission(
user,
'models',
'canList',
callback
);
},
};
async.each(
Object.keys(linzModels),
function(model, callback) {
// get the model options
linzModels[model].getModelOptions(function(
err,
options
) {
if (err) {
return callback(err);
}
if (options.hide === true) {
return callback(null);
}
models.children.push({
name:
linzModels[model].linz.formtools.model
.plural,
href:
_this.get('admin path') +
'/model/' +
model +
'/list',
permission: function(user, permCallback) {
return linz.api.permissions.hasPermission(
user,
{ type: 'model', model: model },
'canList',
permCallback
);
},
});
return callback(null);
});
},
function(err) {
if (err) {
return done(err);
}
models.children.sort((a, b) =>
collator.compare(a.name, b.name)
);
nav.push(models);
return done();
}
);
},
function(done) {
// add a reference for configs, if we have some
if (Object.keys(linzConfigs).length) {
var configs = {
name: 'Configs',
href: _this.get('admin path') + '/configs/list',
permission: function(user, callback) {
return linz.api.permissions.hasPermission(
user,
'configs',
'canList',
callback
);
},
};
nav.push(configs);
}
return done(null);
},
// this function will run and return either a no-op function,
// or a function that will execute the 'navigation configuration' function and override
// nav with the customisation as returned to the callback
(function() {
// if we don't have a function, return a no-op that will simply execute the callback with null
if (
typeof _this.get('navigation configuration') !== 'function'
) {
return linz.utils.noOp();
}
return function(done) {
return _this.get('navigation configuration')(nav, function(
err,
_nav
) {
if (err) {
errors.log(
'The navigation configuration function errored.'
);
return done(null);
}
// override with the newly updated navigation
nav = _nav;
return done(null);
});
};
})(),
],
function(err) {
// set this data on linz
_this.set(
'navigation',
nav.sort((a, b) => collator.compare(a.name, b.name))
);
// return the callback
return cb(err);
}
);
};
/**
* Check the setup of linz and make sure we've got everything we require
* @return {[type]} [description]
*/
Linz.prototype.checkSetup = function(cb) {
if (this.get('user model') === undefined) {
error.log('You must define a user model');
}
return cb(null);
};
/**
* Create a logger which you can use in your applications.
* Linz uses bunyan internally and exposes this method to easily create logs within your application.
*
* @param {String|Object} options
* @return an instance of the logger
* @api public
*/
Linz.prototype.logger = function(options) {
// user can pass either a String or an Object
if (typeof options === 'string') options = { name: options };
// The name parameter must exist
if (!options.name)
throw new Error(
'You must pass the name parameter when creating a logger.'
);
// If we haven't already created a logger, create one now, transparently passing the options
if (!this.loggers[options.name]) {
this.loggers[options.name] = bunyan.createLogger(options);
}
return this.loggers[options.name];
};
/**
* Initialise the models.
* @returns {Void} Intialises the models, setting up some linz properties.
*/
Linz.prototype.initModels = () => helpersModels.initModels();