diff --git a/lib/units/api/controllers/devices.js b/lib/units/api/controllers/devices.js index 39a68fbf..ba236b4f 100644 --- a/lib/units/api/controllers/devices.js +++ b/lib/units/api/controllers/devices.js @@ -23,10 +23,12 @@ function getDevices(req, res) { list.forEach(function(device) { datautil.normalize(device, req.user) + var responseDevice = device + if (fields) { - device = _.pick(device, fields.split(',')) + responseDevice = _.pick(device, fields.split(',')) } - deviceList.push(device) + deviceList.push(responseDevice) }) res.json({ @@ -57,13 +59,15 @@ function getDeviceBySerial(req, res) { } datautil.normalize(device, req.user) - if(fields) { - device = _.pick(device, fields.split(',')) + var responseDevice = device + + if (fields) { + responseDevice = _.pick(device, fields.split(',')) } res.json({ success: true - , device: device + , device: responseDevice }) }) .catch(function(err) { diff --git a/lib/units/api/controllers/user.js b/lib/units/api/controllers/user.js index 581b9c64..6cfb5fa2 100644 --- a/lib/units/api/controllers/user.js +++ b/lib/units/api/controllers/user.js @@ -43,10 +43,11 @@ function getUserDevices(req, res) { list.forEach(function(device) { datautil.normalize(device, req.user) + var responseDevice = device if (fields) { - device = _.pick(device, fields.split(',')) + responseDevice = _.pick(device, fields.split(',')) } - deviceList.push(device) + deviceList.push(responseDevice) }) res.json({ @@ -84,13 +85,14 @@ function getUserDeviceBySerial(req, res) { }) } - if(fields) { - device = _.pick(device, fields.split(',')) + var responseDevice = device + if (fields) { + responseDevice = _.pick(device, fields.split(',')) } res.json({ success: true - , device: device + , device: responseDevice }) }) .catch(function(err) { @@ -158,9 +160,9 @@ function addUserDevice(req, res) { ) , timeout , wireutil.toDeviceRequirements({ - 'serial': { - 'value': serial - , 'match': 'exact' + serial: { + value: serial + , match: 'exact' } }) ) @@ -193,7 +195,6 @@ function deleteUserDeviceBySerial(req, res) { success: false , description: 'You cannot release this device. Not owned by you' }) - } // Timer will be called if no JoinGroupMessage is received till 5 seconds @@ -226,9 +227,9 @@ function deleteUserDeviceBySerial(req, res) { , wireutil.envelope( new wire.UngroupMessage( wireutil.toDeviceRequirements({ - 'serial': { - 'value': serial - , 'match': 'exact' + serial: { + value: serial + , match: 'exact' } }) ) @@ -314,7 +315,6 @@ function remoteDisconnectUserDeviceBySerial(req, res) { dbapi.loadDevice(serial) .then(function(device) { - if (!device) { return res.status(404).json({ success: false diff --git a/lib/units/api/helpers/securityHandlers.js b/lib/units/api/helpers/securityHandlers.js index a856eb0f..99eedd1c 100644 --- a/lib/units/api/helpers/securityHandlers.js +++ b/lib/units/api/helpers/securityHandlers.js @@ -14,8 +14,8 @@ module.exports = { function accessTokenAuth(req, res, next) { if (req.headers.authorization) { var authHeader = req.headers.authorization.split(' ') - , format = authHeader[0] - , tokenId = authHeader[1] + var format = authHeader[0] + var tokenId = authHeader[1] if (format !== 'Bearer') { return res.status(401).json({ @@ -42,7 +42,7 @@ function accessTokenAuth(req, res, next) { } var jwt = token.jwt - , data = jwtutil.decode(jwt, req.options.secret) + var data = jwtutil.decode(jwt, req.options.secret) if (!data) { return res.status(500).json({ @@ -54,7 +54,8 @@ function accessTokenAuth(req, res, next) { if (user) { req.user = user next() - } else { + } + else { return res.status(500).json({ success: false }) diff --git a/lib/units/api/index.js b/lib/units/api/index.js index 8bc3dd46..f89e4620 100644 --- a/lib/units/api/index.js +++ b/lib/units/api/index.js @@ -16,9 +16,9 @@ var wireutil = require('../../wire/util') module.exports = function(options) { var log = logger.createLogger('api') - , app = express() - , server = http.createServer(app) - , channelRouter = new events.EventEmitter() + var app = express() + var server = http.createServer(app) + var channelRouter = new events.EventEmitter() var push = zmqutil.socket('push') Promise.map(options.endpoints.push, function(endpoint) { @@ -30,7 +30,7 @@ module.exports = function(options) { }) }) }) - .catch(function(err) {w + .catch(function(err) { log.fatal('Unable to connect to push endpoint', err) lifecycle.fatal() }) @@ -65,22 +65,22 @@ module.exports = function(options) { var config = { appRoot: __dirname , swaggerFile: path.resolve(__dirname, 'swagger', 'api_v1.yaml') - }; + } SwaggerExpress.create(config, function(err, swaggerExpress) { if (err) { throw err } - swaggerExpress.register(app); + swaggerExpress.register(app) }) // Adding options in request, so that swagger controller // can use it. app.use(function(req, res, next) { var reqOptions = _.merge(options, { - 'push': push - , 'sub': sub - , 'channelRouter': channelRouter + push: push + , sub: sub + , channelRouter: channelRouter }) req.options = reqOptions diff --git a/lib/util/datautil.js b/lib/util/datautil.js index 12dd9e42..1220a0b5 100644 --- a/lib/util/datautil.js +++ b/lib/util/datautil.js @@ -57,7 +57,9 @@ datautil.applyOwner = function(device, user) { // Only owner can see this information datautil.applyOwnerOnlyInfo = function(device, user) { if (device.owner && device.owner.email === user.email) { - } else { + // No-op + } + else { device.remoteConnect = false device.remoteConnectUrl = null } diff --git a/lib/util/deviceutil.js b/lib/util/deviceutil.js index d5264830..7b125d9a 100644 --- a/lib/util/deviceutil.js +++ b/lib/util/deviceutil.js @@ -5,9 +5,16 @@ var log = logger.createLogger('util:deviceutil') var deviceutil = module.exports = Object.create(null) deviceutil.isOwnedByUser = function(device, user) { - return device.present && device.ready && device.owner && device.owner.email === user.email && device.using + return device.present && + device.ready && + device.owner && + device.owner.email === user.email && + device.using } deviceutil.isAddable = function(device, user) { - return device.present && device.ready && !device.using && !device.owner + return device.present && + device.ready && + !device.using && + !device.owner } diff --git a/res/app/components/stf/device/device-service.js b/res/app/components/stf/device/device-service.js index 833962ed..a5672a46 100644 --- a/res/app/components/stf/device/device-service.js +++ b/res/app/components/stf/device/device-service.js @@ -182,7 +182,7 @@ module.exports = function DeviceServiceFactory($http, socket, EnhanceDeviceServi }) oboe('/api/v1/user/devices') - .node('devices[*]', function (device) { + .node('devices[*]', function(device) { tracker.add(device) })