UNPKG

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