# all files / lib/routes/ configurations.js

12.5% Statements 5/40
0% Branches 0/20
0% Functions 0/14
12.5% Lines 5/40

    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
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    1×

    1×

    1×

    1×

    1×
     // jscs:disable jsDoc
    var _ = require('lodash'),
    	util = require('../util'),
    	fs = require('fs'),
    	path = require('path'),
    	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); // Note: 'true' is, in this case, an error object..
    		}
    		authCallback(user, org, callback);
    	} else {
    		callback(true); // Note: 'true' is, in this case, an error object..
    	}
    }

    function configure(arrow, app, config, prefix, authCallback) {
    	var confDir = path.resolve(path.join(arrow.getComponentDir('connector', false), '..', 'conf'));

    	function checkAuth(req, resp, next, cb) {
    		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) {
    			if (err) {
    				return resp.json({
    					success: false,
    					message: 'Not authenticated.'
    				});
    			}
    			try {
    				cb();
    			}
    			catch (err) {
    				return resp.json({
    					success: false,
    					message: err.toString()
    				});
    			}
    		});
    	}

    	app.get(prefix + '/configurations', function (req, resp, next) {
    		checkAuth(req, resp, next, function () {
    			var files = fs.readdirSync(confDir)
    				.filter(function (f) {
    					return f[0] !== '.';
    				});
    			return resp.json({
    				success: true,
    				results: files
    			});
    		});
    	});

    	app.get(prefix + '/configurations/:id', function (req, resp, next) {
    		checkAuth(req, resp, next, function () {
    			var files = fs.readdirSync(confDir)
    				.filter(function (f) {
    					return f[0] !== '.' && f === req.params.id;
    				})
    				.map(function (f) {
    					return fs.readFileSync(path.join(confDir, f), 'UTF-8');
    				});
    			return resp.json({
    				success: files.length === 1,
    				results: files[0]
    			});
    		});
    	});

    	app.post(prefix + '/configurations/:id', function (req, resp, next) {
    		if (!req.body.code) {
    			return resp.json({
    				success: false,
    				message: 'code is a required body parameter.'
    			});
    		}
    		checkAuth(req, resp, next, function () {
    			var files = fs.readdirSync(confDir)
    				.filter(function (f) {
    					return f[0] !== '.' && f === req.params.id;
    				});
    			// TODO: In the future, we should let them create config files, too.
    			if (files.length === 1) {
    				fs.writeFileSync(path.join(confDir, files[0]), req.body.code, 'UTF-8');
    			}
    			return resp.json({
    				success: files.length === 1,
    				results: files[0]
    			});
    		});
    	});
    }

    exports.configure = configure;