1 | var _ = require('lodash');
|
2 |
|
3 |
|
4 | exports.checkLogin = function(req, res, next){
|
5 | var passwordConf = req.nconf.app.get('app');
|
6 |
|
7 |
|
8 | if(passwordConf && passwordConf.hasOwnProperty('password')){
|
9 |
|
10 | if(req.path === '/app/login' || req.path === '/app/logout' || req.path === '/app/login_action'){
|
11 | next();
|
12 | }else{
|
13 |
|
14 | if(req.session.loggedIn){
|
15 | next();
|
16 | }else{
|
17 | res.redirect(req.app_context + '/app/login');
|
18 | }
|
19 | }
|
20 | }else{
|
21 |
|
22 | next();
|
23 | }
|
24 | };
|
25 |
|
26 |
|
27 | exports.get_db_stats = function (mongo_db, db_name, cb){
|
28 | var async = require('async');
|
29 | var db_obj = {};
|
30 |
|
31 |
|
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 |
|
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 |
|
65 | cb(null, exports.order_object(db_obj));
|
66 | });
|
67 | }else{
|
68 |
|
69 | cb(null, null);
|
70 | }
|
71 | });
|
72 |
|
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 |
|
95 | exports.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 |
|
101 | if(uri.database === undefined || uri.database === null){
|
102 |
|
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 |
|
126 |
|
127 |
|
128 |
|
129 | exports.get_id_type = function (mongo, collection, doc_id, cb){
|
130 | if(doc_id){
|
131 | var ObjectID = require('mongodb').ObjectID;
|
132 |
|
133 | if(ObjectID.isValid(doc_id)){
|
134 | mongo.collection(collection).findOne({_id: ObjectID(doc_id)}, function (err, doc){
|
135 | if(doc){
|
136 |
|
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 |
|
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 |
|
151 | mongo.collection(collection).findOne({_id: parseInt(doc_id)}, function (err, doc){
|
152 | if(doc){
|
153 |
|
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 |
|
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 |
|
174 | exports.get_sidebar_list = function (mongo_db, db_name, cb){
|
175 | var async = require('async');
|
176 | var db_obj = {};
|
177 |
|
178 |
|
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 |
|
214 | exports.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 |
|
226 | exports.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 |
|
240 | exports.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 |
|
257 | exports.cleanCollections = function(collection_list){
|
258 | var list = [];
|
259 | _.each(collection_list, function (item){
|
260 | list.push(item.name);
|
261 | });
|
262 | return list;
|
263 | };
|