Commit dafc9906a16e4ebcd535777be17bc164750a2932

Authored by Adhidarma Hadiwinoto
1 parent a636bde97c
Exists in master

uppercase and remove space on edit products

Showing 1 changed file with 10 additions and 7 deletions Inline Diff

1 var express = require('express'); 1 var express = require('express');
2 var app = express(); 2 var app = express();
3 var nunjucks = require('nunjucks'); 3 var nunjucks = require('nunjucks');
4 var passport = require('passport'); 4 var passport = require('passport');
5 var LocalStrategy = require('passport-local').Strategy; 5 var LocalStrategy = require('passport-local').Strategy;
6 var crypto = require('crypto'); 6 var crypto = require('crypto');
7 //var sha256sum = crypto.createHash('sha256'); 7 //var sha256sum = crypto.createHash('sha256');
8 var fsextra = require('fs-extra'); 8 var fsextra = require('fs-extra');
9 var strftime = require('strftime'); 9 var strftime = require('strftime');
10 var fs = require('fs'); 10 var fs = require('fs');
11 var ini = require('ini'); 11 var ini = require('ini');
12 var os = require('os'); 12 var os = require('os');
13 13
14 var config; 14 var config;
15 var aaa; 15 var aaa;
16 var matrix; 16 var matrix;
17 var view_path = __dirname + '/views'; 17 var view_path = __dirname + '/views';
18 18
19 function loggedIn(req, res, next) { 19 function loggedIn(req, res, next) {
20 if (req.user) { 20 if (req.user) {
21 next(); 21 next();
22 } else { 22 } else {
23 res.redirect('/login'); 23 res.redirect('/login');
24 } 24 }
25 } 25 }
26 26
27 function matchedPasswordAndHash(password, hash) { 27 function matchedPasswordAndHash(password, hash) {
28 var sha256sum = crypto.createHash('sha256'); 28 var sha256sum = crypto.createHash('sha256');
29 sha256sum.update(password); 29 sha256sum.update(password);
30 return (hash == sha256sum.digest('hex')) 30 return (hash == sha256sum.digest('hex'))
31 } 31 }
32 32
33 function renderConfigIndex(req, res) { 33 function renderConfigIndex(req, res) {
34 34
35 masked = ['config.expresso.password']; 35 masked = ['config.expresso.password'];
36 36
37 res.render('config.index.html', { 37 res.render('config.index.html', {
38 gateway_name: config.globals.gateway_name, 38 gateway_name: config.globals.gateway_name,
39 title: 'CONFIG', configs: config, 39 title: 'CONFIG', configs: config,
40 config_dirty: matrix.config_dirty, 40 config_dirty: matrix.config_dirty,
41 isMask: function(scope, keyword) { 41 isMask: function(scope, keyword) {
42 return masked.indexOf('config.' + scope + '.' + keyword) >= 0; 42 return masked.indexOf('config.' + scope + '.' + keyword) >= 0;
43 }, 43 },
44 }); 44 });
45 } 45 }
46 46
47 function renderConfigEdit(req, res) { 47 function renderConfigEdit(req, res) {
48 var template = "config.edit.html"; 48 var template = "config.edit.html";
49 49
50 if (req.scope == 'expresso' && req.keyword == 'password') { 50 if (req.scope == 'expresso' && req.keyword == 'password') {
51 template = "config.edit.expresso.password.html"; 51 template = "config.edit.expresso.password.html";
52 } 52 }
53 53
54 res.render(template, { 54 res.render(template, {
55 gateway_name: config.globals.gateway_name, 55 gateway_name: config.globals.gateway_name,
56 title: 'Edit Konfigurasi', 56 title: 'Edit Konfigurasi',
57 scope: req.scope, 57 scope: req.scope,
58 keyword: req.keyword, 58 keyword: req.keyword,
59 old_value: config[req.scope][req.keyword] 59 old_value: config[req.scope][req.keyword]
60 }); 60 });
61 } 61 }
62 62
63 function renderConfigAdd(req, res){ 63 function renderConfigAdd(req, res){
64 64
65 var template = 'config.add.html'; 65 var template = 'config.add.html';
66 if (req.scope == 'products') { 66 if (req.scope == 'products') {
67 template = 'config.add.products.html'; 67 template = 'config.add.products.html';
68 } 68 }
69 69
70 res.render(template, { 70 res.render(template, {
71 gateway_name: config.globals.gateway_name, 71 gateway_name: config.globals.gateway_name,
72 title: 'Tambah Item Konfigurasi', 72 title: 'Tambah Item Konfigurasi',
73 scope: req.scope 73 scope: req.scope
74 }); 74 });
75 } 75 }
76 76
77 function submitConfigEdit(req, res) { 77 function submitConfigEdit(req, res) {
78 78
79 if (req.scope == 'expresso' && req.keyword == 'password') { 79 if (req.scope == 'expresso' && req.keyword == 'password') {
80 if (req.body.newValue != req.body.newValue2) { 80 if (req.body.newValue != req.body.newValue2) {
81 res.redirect('/config/edit/' + req.scope + '/' + req.keyword); 81 res.redirect('/config/edit/' + req.scope + '/' + req.keyword);
82 return; 82 return;
83 } 83 }
84 84
85 var sha256sum = crypto.createHash('sha256'); 85 var sha256sum = crypto.createHash('sha256');
86 sha256sum.update(req.body.newValue); 86 sha256sum.update(req.body.newValue);
87 req.body.newValue = sha256sum.digest('hex'); 87 req.body.newValue = sha256sum.digest('hex');
88 88
89 } else if (req.scope == 'globals' && req.keyword == 'products' && req.body.newValue.trim()) { 89 } else if (req.scope == 'globals' && req.keyword == 'products' && req.body.newValue.trim()) {
90 var products = req.body.newValue.split(','); 90
91 var sortedProducts = products = aaa.sortProductsArray(products); 91 try {
92 req.body.newValue = sortedProducts.join(','); 92 var unsortedString = req.body.newValue.toUpperCase().replace(/ /g, '').trim();
93 console.log('Unsorted products:'); 93 var unsortedProducts = unsortedString.split(',');
94 console.log(products); 94 var sortedProducts = aaa.sortProductsArray(unsortedProducts);
95 console.log('Sorted products:'); 95 req.body.newValue = sortedProducts.join(',');
96 console.log(sortedProducts); 96 }
97 catch(e) {
98 console.log('Error sorting products');
99 }
97 } 100 }
98 101
99 if (config[req.body.scope][req.body.keyword] != req.body.newValue.trim()) { 102 if (config[req.body.scope][req.body.keyword] != req.body.newValue.trim()) {
100 config[req.body.scope][req.body.keyword] = req.body.newValue.trim(); 103 config[req.body.scope][req.body.keyword] = req.body.newValue.trim();
101 matrix.config_dirty = 1; 104 matrix.config_dirty = 1;
102 } 105 }
103 106
104 res.redirect('/config'); 107 res.redirect('/config');
105 } 108 }
106 109
107 function submitConfigAdd(req, res) { 110 function submitConfigAdd(req, res) {
108 111
109 if (!req.body.newKeyword.trim()) { 112 if (!req.body.newKeyword.trim()) {
110 res.redirect('/config'); 113 res.redirect('/config');
111 return; 114 return;
112 } 115 }
113 116
114 if (config[req.body.scope] === undefined) { 117 if (config[req.body.scope] === undefined) {
115 config[req.body.scope] = {}; 118 config[req.body.scope] = {};
116 } 119 }
117 120
118 config[req.body.scope][req.body.newKeyword.trim()] = req.body.newValue.trim(); 121 config[req.body.scope][req.body.newKeyword.trim()] = req.body.newValue.trim();
119 matrix.config_dirty = 1; 122 matrix.config_dirty = 1;
120 123
121 res.redirect('/config#config.' + req.body.scope + '.' + req.body.newKeyword); 124 res.redirect('/config#config.' + req.body.scope + '.' + req.body.newKeyword);
122 } 125 }
123 126
124 function submitConfigDelete(req, res) { 127 function submitConfigDelete(req, res) {
125 128
126 matrix.config_dirty = 1; 129 matrix.config_dirty = 1;
127 delete config[req.scope][req.keyword]; 130 delete config[req.scope][req.keyword];
128 131
129 res.redirect('/config'); 132 res.redirect('/config');
130 } 133 }
131 134
132 function submitConfigSave(req, res) { 135 function submitConfigSave(req, res) {
133 fsextra.copy('config.ini', 'config.ini.backup_' + strftime('%F_%H%M%S', new Date()), function(err) { 136 fsextra.copy('config.ini', 'config.ini.backup_' + strftime('%F_%H%M%S', new Date()), function(err) {
134 fs.writeFileSync('./config.ini', ini.stringify(config)); 137 fs.writeFileSync('./config.ini', ini.stringify(config));
135 matrix.config_dirty = 0; 138 matrix.config_dirty = 0;
136 res.redirect('/config'); 139 res.redirect('/config');
137 }); 140 });
138 } 141 }
139 142
140 function renderLoginPage(req, res) { 143 function renderLoginPage(req, res) {
141 res.render('signin.html', {title: 'Signin'}); 144 res.render('signin.html', {title: 'Signin'});
142 } 145 }
143 146
144 function renderConfigAskDelete(req, res) { 147 function renderConfigAskDelete(req, res) {
145 res.render('config.ask.delete.html', { 148 res.render('config.ask.delete.html', {
146 gateway_name: config.globals.gateway_name, 149 gateway_name: config.globals.gateway_name,
147 title: 'Konfirmasi Penghapusan', 150 title: 'Konfirmasi Penghapusan',
148 scope: req.scope, 151 scope: req.scope,
149 keyword: req.keyword, 152 keyword: req.keyword,
150 value: config[req.scope][req.keyword], 153 value: config[req.scope][req.keyword],
151 }); 154 });
152 } 155 }
153 156
154 function renderDashboardIndex(req, res) { 157 function renderDashboardIndex(req, res) {
155 var template = "dashboard.index.html"; 158 var template = "dashboard.index.html";
156 159
157 var os_info = { 160 var os_info = {
158 uptime: os.uptime(), 161 uptime: os.uptime(),
159 loadavg: os.loadavg(), 162 loadavg: os.loadavg(),
160 hostname: os.hostname(), 163 hostname: os.hostname(),
161 type: os.type(), 164 type: os.type(),
162 platform: os.platform(), 165 platform: os.platform(),
163 arch: os.arch(), 166 arch: os.arch(),
164 release: os.release(), 167 release: os.release(),
165 totalmem: os.totalmem(), 168 totalmem: os.totalmem(),
166 } 169 }
167 170
168 res.render(template, { 171 res.render(template, {
169 gateway_name: config.globals.gateway_name, 172 gateway_name: config.globals.gateway_name,
170 title: 'Dashboard', 173 title: 'Dashboard',
171 matrix: JSON.stringify(matrix, null, 2), 174 matrix: JSON.stringify(matrix, null, 2),
172 config: JSON.stringify(config, null, 2), 175 config: JSON.stringify(config, null, 2),
173 nodejs_versions: JSON.stringify(process.versions, null, 2), 176 nodejs_versions: JSON.stringify(process.versions, null, 2),
174 pendings: JSON.stringify(aaa.getPendingList(), null, 2), 177 pendings: JSON.stringify(aaa.getPendingList(), null, 2),
175 memory_usage: JSON.stringify(process.memoryUsage(), null, 2), 178 memory_usage: JSON.stringify(process.memoryUsage(), null, 2),
176 uptime: process.uptime(), 179 uptime: process.uptime(),
177 os_info: JSON.stringify(os_info, null, 2), 180 os_info: JSON.stringify(os_info, null, 2),
178 net_ifaces: JSON.stringify(os.networkInterfaces(), null, 2), 181 net_ifaces: JSON.stringify(os.networkInterfaces(), null, 2),
179 }); 182 });
180 } 183 }
181 184
182 function isNoTemplateCache() { 185 function isNoTemplateCache() {
183 var retval = false; 186 var retval = false;
184 try { 187 try {
185 retval = config.expresso.no_template_cache.toUpperCase() == 'YES'; 188 retval = config.expresso.no_template_cache.toUpperCase() == 'YES';
186 } 189 }
187 catch(err) { 190 catch(err) {
188 return retval; 191 return retval;
189 } 192 }
190 193
191 return retval; 194 return retval;
192 } 195 }
193 196
194 function createServer() { 197 function createServer() {
195 if (!config.expresso || !config.expresso.listen_port) { 198 if (!config.expresso || !config.expresso.listen_port) {
196 console.log('Not starting expresso admin UI'); 199 console.log('Not starting expresso admin UI');
197 return; 200 return;
198 } 201 }
199 202
200 nunjucks.configure(view_path, { 203 nunjucks.configure(view_path, {
201 autoescape: true, 204 autoescape: true,
202 noCache: isNoTemplateCache(), 205 noCache: isNoTemplateCache(),
203 express: app 206 express: app
204 }); 207 });
205 208
206 app.use(express.static(__dirname + '/public')); 209 app.use(express.static(__dirname + '/public'));
207 app.use(require('cookie-parser')()); 210 app.use(require('cookie-parser')());
208 app.use(require('body-parser').urlencoded({ extended: true })); 211 app.use(require('body-parser').urlencoded({ extended: true }));
209 app.use(require('express-session')({ secret: 'keyboard cat', resave: false, saveUninitialized: false })); 212 app.use(require('express-session')({ secret: 'keyboard cat', resave: false, saveUninitialized: false }));
210 app.use(passport.initialize()); 213 app.use(passport.initialize());
211 app.use(passport.session()); 214 app.use(passport.session());
212 215
213 passport.use(new LocalStrategy( 216 passport.use(new LocalStrategy(
214 function(username, password, done) { 217 function(username, password, done) {
215 218
216 if (username == 'admin' && matchedPasswordAndHash(password, config.expresso.password)) { 219 if (username == 'admin' && matchedPasswordAndHash(password, config.expresso.password)) {
217 var user = { 220 var user = {
218 username: username, 221 username: username,
219 last_login: Date.now() / 1000 | 0 222 last_login: Date.now() / 1000 | 0
220 } 223 }
221 224
222 return done(null, user); 225 return done(null, user);
223 } 226 }
224 227
225 return done(null, false, { message: 'Incorrect password.' }); 228 return done(null, false, { message: 'Incorrect password.' });
226 } 229 }
227 )); 230 ));
228 231
229 passport.serializeUser(function(user, done) { 232 passport.serializeUser(function(user, done) {
230 done(null, user.username); 233 done(null, user.username);
231 }); 234 });
232 235
233 passport.deserializeUser(function(username, done) { 236 passport.deserializeUser(function(username, done) {
234 var user = { 237 var user = {
235 username: username 238 username: username
236 }; 239 };
237 done(null, user); 240 done(null, user);
238 }); 241 });
239 242
240 app.get('/', loggedIn, function(req, res) { res.redirect('/dashboard'); }); 243 app.get('/', loggedIn, function(req, res) { res.redirect('/dashboard'); });
241 app.get('/config', loggedIn, renderConfigIndex); 244 app.get('/config', loggedIn, renderConfigIndex);
242 app.get('/login', renderLoginPage); 245 app.get('/login', renderLoginPage);
243 app.post('/login', passport.authenticate('local', { successRedirect: '/dashboard', failureRedirect: '/login' })); 246 app.post('/login', passport.authenticate('local', { successRedirect: '/dashboard', failureRedirect: '/login' }));
244 app.get('/logout', function(req, res){ 247 app.get('/logout', function(req, res){
245 req.logout(); 248 req.logout();
246 res.redirect('/login'); 249 res.redirect('/login');
247 }); 250 });
248 251
249 app.param('scope', function(req, res, next, value) { 252 app.param('scope', function(req, res, next, value) {
250 req.scope = value; 253 req.scope = value;
251 next(); 254 next();
252 }); 255 });
253 256
254 app.param('keyword', function(req, res, next, value) { 257 app.param('keyword', function(req, res, next, value) {
255 req.keyword = value; 258 req.keyword = value;
256 next(); 259 next();
257 }); 260 });
258 261
259 app.get('/config/edit/:scope/:keyword', loggedIn, renderConfigEdit); 262 app.get('/config/edit/:scope/:keyword', loggedIn, renderConfigEdit);
260 app.post('/config/edit/:scope/:keyword', loggedIn, submitConfigEdit); 263 app.post('/config/edit/:scope/:keyword', loggedIn, submitConfigEdit);
261 264
262 app.get('/config/ask-delete/:scope/:keyword', loggedIn, renderConfigAskDelete); 265 app.get('/config/ask-delete/:scope/:keyword', loggedIn, renderConfigAskDelete);
263 app.get('/config/delete/:scope/:keyword', loggedIn, submitConfigDelete); 266 app.get('/config/delete/:scope/:keyword', loggedIn, submitConfigDelete);
264 267
265 app.get('/config/add/:scope', loggedIn, renderConfigAdd); 268 app.get('/config/add/:scope', loggedIn, renderConfigAdd);
266 app.post('/config/add/:scope', loggedIn, submitConfigAdd); 269 app.post('/config/add/:scope', loggedIn, submitConfigAdd);
267 270
268 app.get('/config/save', loggedIn, submitConfigSave); 271 app.get('/config/save', loggedIn, submitConfigSave);
269 272
270 app.get('/dashboard', loggedIn, renderDashboardIndex); 273 app.get('/dashboard', loggedIn, renderDashboardIndex);
271 274
272 var server = app.listen(config.expresso.listen_port, function () { 275 var server = app.listen(config.expresso.listen_port, function () {
273 var host = server.address().address; 276 var host = server.address().address;
274 var port = server.address().port; 277 var port = server.address().port;
275 278
276 console.log('Expresso admin UI listening at http://%s:%s', host, port); 279 console.log('Expresso admin UI listening at http://%s:%s', host, port);
277 }); 280 });
278 } 281 }
279 282
280 function sanitizeMatrix(matrix) { 283 function sanitizeMatrix(matrix) {
281 if (matrix === undefined) { 284 if (matrix === undefined) {
282 matrix = {}; 285 matrix = {};
283 } 286 }
284 if (matrix.config_dirty === undefined) { 287 if (matrix.config_dirty === undefined) {
285 matrix.config_dirty = 0; 288 matrix.config_dirty = 0;
286 } 289 }
287 } 290 }
288 291
289 function start(options) { 292 function start(options) {
290 if (options['config']) { 293 if (options['config']) {
291 config = options['config']; 294 config = options['config'];
292 } 295 }
293 296
294 if (options['matrix']) { 297 if (options['matrix']) {
295 matrix = options['matrix']; 298 matrix = options['matrix'];
296 } 299 }
297 sanitizeMatrix(matrix); 300 sanitizeMatrix(matrix);
298 301
299 if (options['aaa']) { 302 if (options['aaa']) {
300 aaa = options['aaa']; 303 aaa = options['aaa'];
301 } 304 }
302 305
303 if (options['expresso_views']) { 306 if (options['expresso_views']) {
304 view_path = options['expresso_views']; 307 view_path = options['expresso_views'];
305 } 308 }
306 309
307 createServer(); 310 createServer();
308 } 311 }
309 312
310 exports.start = start; 313 exports.start = start;
311 314