1 | var _ = require('lodash');
|
2 | var fs = require('fs');
|
3 | var path = require('path');
|
4 |
|
5 |
|
6 | exports.checkLogin = function(req, res, next){
|
7 | var passwordConf = req.nconf.app.get('app');
|
8 |
|
9 |
|
10 | if(passwordConf && passwordConf.hasOwnProperty('password')){
|
11 |
|
12 | if(req.path === '/app/login' || req.path === '/app/logout' || req.path === '/app/login_action'){
|
13 | next();
|
14 | }else{
|
15 |
|
16 | if(req.session.loggedIn){
|
17 | next();
|
18 | }else{
|
19 | res.redirect(req.app_context + '/app/login');
|
20 | }
|
21 | }
|
22 | }else{
|
23 |
|
24 | next();
|
25 | }
|
26 | };
|
27 |
|
28 |
|
29 | exports.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 |
|
40 | exports.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 |
|
50 | exports.get_db_stats = function (mongo_db, db_name, cb){
|
51 | var async = require('async');
|
52 | var db_obj = {};
|
53 |
|
54 |
|
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 |
|
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 |
|
88 | cb(null, exports.order_object(db_obj));
|
89 | });
|
90 | }else{
|
91 |
|
92 | cb(null, null);
|
93 | }
|
94 | });
|
95 |
|
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 |
|
118 | exports.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 |
|
124 | if(uri.database === undefined || uri.database === null){
|
125 |
|
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 |
|
149 |
|
150 |
|
151 |
|
152 | exports.get_id_type = function (mongo, collection, doc_id, cb){
|
153 | if(doc_id){
|
154 | var ObjectID = require('mongodb').ObjectID;
|
155 |
|
156 | if(ObjectID.isValid(doc_id)){
|
157 | mongo.collection(collection).findOne({_id: ObjectID(doc_id)}, function (err, doc){
|
158 | if(doc){
|
159 |
|
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 |
|
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 |
|
174 | mongo.collection(collection).findOne({_id: parseInt(doc_id)}, function (err, doc){
|
175 | if(doc){
|
176 |
|
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 |
|
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 |
|
197 | exports.get_sidebar_list = function (mongo_db, db_name, cb){
|
198 | var async = require('async');
|
199 | var db_obj = {};
|
200 |
|
201 |
|
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 |
|
237 | exports.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 |
|
249 | exports.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 |
|
263 | exports.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 |
|
280 | exports.cleanCollections = function(collection_list){
|
281 | var list = [];
|
282 | _.each(collection_list, function (item){
|
283 | list.push(item.name);
|
284 | });
|
285 | return list;
|
286 | };
|