UNPKG

10.3 kBJavaScriptView Raw
1var _ = require('lodash');
2
3// checks for the password in the /config/app.json file if it's set
4exports.checkLogin = function(req, res, next){
5 var passwordConf = req.nconf.app.get('app');
6
7 // only check for login if a password is specified in the /config/app.json file
8 if(passwordConf && passwordConf.hasOwnProperty('password')){
9 // dont require login session for login route
10 if(req.path === '/app/login' || req.path === '/app/logout' || req.path === '/app/login_action'){
11 next();
12 }else{
13 // if the session exists we continue, else renter login page
14 if(req.session.loggedIn){
15 next(); // allow the next route to run
16 }else{
17 res.redirect(req.app_context + '/app/login');
18 }
19 }
20 }else{
21 // no password is set so we continue
22 next();
23 }
24};
25
26// gets the db stats
27exports.get_db_stats = function (mongo_db, db_name, cb){
28 var async = require('async');
29 var db_obj = {};
30
31 // if at connection level we loop db's and collections
32 if(db_name == null){
33 var adminDb = mongo_db.admin();
34 adminDb.listDatabases(function (err, db_list){
35 if(err){
36 cb('User is not authorised', null);
37 return;
38 }
39 if(db_list !== undefined){
40 async.forEachOf(exports.order_object(db_list.databases), function (value, key, callback){
41 exports.order_object(db_list.databases);
42 var skipped_dbs = ['null', 'admin', 'local'];
43 if(skipped_dbs.indexOf(value.name) === -1){
44 var tempDBName = value.name;
45 mongo_db.db(tempDBName).listCollections().toArray(function (err, coll_list){
46 var coll_obj = {};
47 async.forEachOf(exports.cleanCollections(coll_list), function (value, key, callback){
48 mongo_db.db(tempDBName).collection(value).stats(function (err, coll_stat){
49 coll_obj[value] = {Storage: coll_stat.size, Documents: coll_stat.count};
50 callback();
51 });
52 }, function (err){
53 if(err) console.error(err.message);
54 // add the collection object to the DB object with the DB as key
55 db_obj[value.name] = exports.order_object(coll_obj);
56 callback();
57 });
58 });
59 }else{
60 callback();
61 }
62 }, function (err){
63 if(err) console.error(err.message);
64 // wrap this whole thing up
65 cb(null, exports.order_object(db_obj));
66 });
67 }else{
68 // if doesnt have the access to get all DB's
69 cb(null, null);
70 }
71 });
72 // if at DB level, we just grab the collections below
73 }else{
74 mongo_db.db(db_name).listCollections().toArray(function (err, coll_list){
75 var coll_obj = {};
76 async.forEachOf(exports.cleanCollections(coll_list), function (value, key, callback){
77 mongo_db.db(db_name).collection(value).stats(function (err, coll_stat){
78 coll_obj[value] = {
79 Storage: coll_stat ? coll_stat.size : 0,
80 Documents: coll_stat ? coll_stat.count : 0
81 };
82
83 callback();
84 });
85 }, function (err){
86 if(err) console.error(err.message);
87 db_obj[db_name] = exports.order_object(coll_obj);
88 cb(null, db_obj);
89 });
90 });
91 }
92};
93
94// gets the Databases
95exports.get_db_list = function (uri, mongo_db, cb){
96 var async = require('async');
97 var adminDb = mongo_db.admin();
98 var db_arr = [];
99
100 // if a DB is not specified in the Conn string we try get a list
101 if(uri.database === undefined || uri.database === null){
102 // try go all admin and get the list of DB's
103 adminDb.listDatabases(function (err, db_list){
104 if(db_list !== undefined){
105 async.forEachOf(db_list.databases, function (value, key, callback){
106 var skipped_dbs = ['null', 'admin', 'local'];
107 if(skipped_dbs.indexOf(value.name) === -1){
108 db_arr.push(value.name);
109 }
110 callback();
111 }, function (err){
112 if(err) console.error(err.message);
113 exports.order_array(db_arr);
114 cb(null, db_arr);
115 });
116 }else{
117 cb(null, null);
118 }
119 });
120 }else{
121 cb(null, null);
122 }
123};
124
125// Normally you would know how your ID's are stored in your DB. As the _id value which is used to handle
126// all document viewing in adminMongo is a parameter we dont know if it is an ObjectId, string or integer. We can check if
127// the _id string is a valid MongoDb ObjectId but this does not guarantee it is stored as an ObjectId in the DB. It's most likely
128// the value will be an ObjectId (hopefully) so we try that first then go from there
129exports.get_id_type = function (mongo, collection, doc_id, cb){
130 if(doc_id){
131 var ObjectID = require('mongodb').ObjectID;
132 // if a valid ObjectId we try that, then then try as a string
133 if(ObjectID.isValid(doc_id)){
134 mongo.collection(collection).findOne({_id: ObjectID(doc_id)}, function (err, doc){
135 if(doc){
136 // doc_id is an ObjectId
137 cb(null, {'doc_id_type': ObjectID(doc_id), 'doc': doc});
138 }else{
139 mongo.collection(collection).findOne({_id: doc_id}, function (err, doc){
140 if(doc){
141 // doc_id is string
142 cb(null, {'doc_id_type': doc_id, 'doc': doc});
143 }else{
144 cb('Document not found', {'doc_id_type': null, 'doc': null});
145 }
146 });
147 }
148 });
149 }else{
150 // if the value is not a valid ObjectId value we try as an integer then as a last resort, a string.
151 mongo.collection(collection).findOne({_id: parseInt(doc_id)}, function (err, doc){
152 if(doc){
153 // doc_id is integer
154 cb(null, {'doc_id_type': parseInt(doc_id), 'doc': doc});
155 return;
156 }else{
157 mongo.collection(collection).findOne({_id: doc_id}, function (err, doc){
158 if(doc){
159 // doc_id is string
160 cb(null, {'doc_id_type': doc_id, 'doc': doc});
161 }else{
162 cb('Document not found', {'doc_id_type': null, 'doc': null});
163 }
164 });
165 }
166 });
167 }
168 }else{
169 cb(null, {'doc_id_type': null, 'doc': null});
170 }
171};
172
173// gets the Databases and collections
174exports.get_sidebar_list = function (mongo_db, db_name, cb){
175 var async = require('async');
176 var db_obj = {};
177
178 // if no DB is specified, we get all DBs and collections
179 if(db_name == null){
180 var adminDb = mongo_db.admin();
181 adminDb.listDatabases(function (err, db_list){
182 if(db_list){
183 async.forEachOf(db_list.databases, function (value, key, callback){
184 var skipped_dbs = ['null', 'admin', 'local'];
185 if(skipped_dbs.indexOf(value.name) === -1){
186 mongo_db.db(value.name).listCollections().toArray(function (err, collections){
187 collections = exports.cleanCollections(collections);
188 exports.order_array(collections);
189 db_obj[value.name] = collections;
190 callback();
191 });
192 }else{
193 callback();
194 }
195 }, function (err){
196 if(err) console.error(err.message);
197 cb(null, exports.order_object(db_obj));
198 });
199 }else{
200 cb(null, exports.order_object(db_obj));
201 }
202 });
203 }else{
204 mongo_db.db(db_name).listCollections().toArray(function (err, collections){
205 collections = exports.cleanCollections(collections);
206 exports.order_array(collections);
207 db_obj[db_name] = collections;
208 cb(null, db_obj);
209 });
210 }
211};
212
213// order the object by alpha key
214exports.order_object = function(unordered){
215 if(unordered !== undefined){
216 var ordered = {};
217 var keys = Object.keys(unordered);
218 exports.order_array(keys);
219 keys.forEach(function (key){
220 ordered[key] = unordered[key];
221 });
222 }
223 return ordered;
224};
225
226exports.order_array = function(array){
227 if(array){
228 array.sort(function (a, b){
229 a = a.toLowerCase();
230 b = b.toLowerCase();
231 if(a === b)return 0;
232 if(a > b)return 1;
233 return-1;
234 });
235 }
236 return array;
237};
238
239// render the error page
240exports.render_error = function(res, req, err, conn){
241 var connection_list = req.nconf.connections.get('connections');
242
243 var conn_string = '';
244 if(connection_list[conn] !== undefined){
245 conn_string = connection_list[conn].connection_string;
246 }
247
248 res.render('error', {
249 message: err,
250 conn: conn,
251 conn_string: conn_string,
252 connection_list: exports.order_object(connection_list),
253 helpers: req.handlebars.helpers
254 });
255};
256
257exports.cleanCollections = function(collection_list){
258 var list = [];
259 _.each(collection_list, function (item){
260 list.push(item.name);
261 });
262 return list;
263};