# all files / lib/routes/ config.js

14.81% Statements 4/27
0% Branches 0/16
0% Functions 0/4
14.81% Lines 4/27

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    1×

    1×

    1×

    1×
     // jscs:disable jsDoc
    var _ = require('lodash'),
    	util = require('../util'),
    	AppC = require('appc-platform-sdk');

    function isAuthenticated(config, authCallback, user, org, callback) {
    	if (authCallback) {
    		// if in production and not enabled, return false
    		if (config.env !== 'development' && !config.enableAdminInProduction) {
    			return callback(true);
    		}
    		authCallback(user, org, callback);
    	} else {
    		callback(true);
    	}
    }

    function configure(arrow, app, config, prefix, authCallback) {
    	var CONFIG = {};
    	CONFIG.env = config && config.env;
    	CONFIG.devmode = config && config.env === 'development';
    	CONFIG.prefix = prefix;
    	CONFIG.dashboard = AppC.baseurl;
    	CONFIG.enableDataEditor = config && config.enableDataEditor;
    	app.get(prefix + '/config.js', function (req, resp, next) {

    		var user = req.query.username,
    			org = +req.query.org_id;

    		if (!user || !org) {
    			// should always be passed, return a 500
    			return next(new Error('invalid parameters passed'));
    		}
    		isAuthenticated(config, authCallback, user, org, function (err) {
    			try {
    				CONFIG.authenticated = !err;
    				var content = JSON.stringify(CONFIG, null, 2),
    					result = 'var CONFIG = ' + content + ';\n' +
    						'window.ConfigLoaded && window.ConfigLoaded(CONFIG);';
    				util.javascript(resp, result);
    			}
    			catch (E) {
    				arrow.logger.error('Error handling admin configure', E);
    				arrow.logger.trace(E.stack);
    				next(E);
    			}
    		});
    	});
    }

    exports.configure = configure;