diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/device-api.jag b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/device-api.jag index 2d19fd02566..1f7d62b2aa2 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/device-api.jag +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/device-api.jag @@ -25,6 +25,8 @@ var constants = require("/app/modules/constants.js"); var deviceModule = require("/app/modules/device.js").deviceModule; var utility = require("/app/modules/utility.js").utility; var devicemgtProps = require('/app/conf/devicemgt-props.js').config(); +var userModule = require("/app/modules/user.js").userModule; +var serviceInvokers = require("/app/modules/backend-service-invoker.js").backendServiceInvoker; var CarbonUtils = Packages.org.wso2.carbon.utils.CarbonUtils; var user = session.get(constants.USER_SESSION_KEY); @@ -159,6 +161,88 @@ if (!user) { var deviceType = elements.deviceType; var deviceName = request.getParameter("name"); result = deviceModule.updateDevice(deviceType, deviceId, deviceName); + } else if (uriMatcher.match("/{context}/api/devices")) { + var url = request.getParameter("url"); + var draw = request.getParameter("draw"); + var length = request.getParameter("length"); + var start = request.getParameter("start"); + var search = request.getParameter("search[value]"); + var deviceName = request.getParameter("columns[1][search][value]"); + var owner = request.getParameter("columns[2][search][value]"); + var status = request.getParameter("columns[3][search][value]"); + var platform = request.getParameter("columns[4][search][value]"); + var ownership = request.getParameter("columns[5][search][value]"); + var targetURL; + + function appendQueryParam (url, queryParam , value) { + if (url.indexOf("?") > 0) { + return url + "&" + queryParam + "=" + value; + } + return url + "?" + queryParam + "=" + value; + } + targetURL = devicemgtProps.httpsURL + request.getParameter("url"); + targetURL = appendQueryParam(targetURL, "draw", draw); + targetURL = appendQueryParam(targetURL, "start", start); + targetURL = appendQueryParam(targetURL, "length", length); + + if (search && search !== "") { + targetURL = appendQueryParam(targetURL, "search", search); + } + + if (deviceName && deviceName !== "") { + targetURL = appendQueryParam(targetURL, "device-name", deviceName); + } + + if (owner && owner !== "") { + targetURL = appendQueryParam(targetURL, "user", owner); + } + + if (status && status !== "") { + targetURL = appendQueryParam(targetURL, "status", status); + } + + if (platform && platform !== "") { + targetURL = appendQueryParam(targetURL, "type", platform); + } + + if (ownership && ownership !== "") { + targetURL = appendQueryParam(targetURL, "ownership", ownership); + } + + serviceInvokers.XMLHttp.get( + targetURL, function (responsePayload) { + response.status = 200; + result = responsePayload; + }, + function (responsePayload) { + response.status = responsePayload.status; + result = responsePayload.responseText; + }); + } else if (uriMatcher.match("/{context}/api/devices/")) { + if (userModule.isAuthorized("/permission/admin/device-mgt/admin/devices/list")) { + result = deviceModule.listDevices(); + } else { + response.sendError(403); + } + } else if (uriMatcher.match("/{context}/api/devices/{type}/{deviceId}")) { + elements = uriMatcher.elements(); + deviceId = elements.deviceId; + type = elements.type; + if (userModule.isAuthorized("/permission/admin/device-mgt/admin/devices/list")) { + result = deviceModule.viewDevice(type, deviceId); + }else { + response.sendError(403); + } + } else if (uriMatcher.match("{context}/api/devices/{type}/{deviceId}/{operation}")) { + elements = uriMatcher.elements(); + deviceId = elements.deviceId; + type = elements.type; + operation = elements.operation; + if (userModule.isAuthorized("/permission/admin/device-mgt/admin/devices/operation")) { + result = deviceModule.performOperation(deviceId, operation, [], type); + } else { + response.sendError(403); + } } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/device.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/device.js index b06a2e436af..c2865c13b4e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/device.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/device.js @@ -278,9 +278,10 @@ deviceModule = function () { if (device) { var propertiesList = device["properties"]; var properties = {}; - for (var i = 0; i < propertiesList.length; i++) { - properties[propertiesList[i]["name"]] = - propertiesList[i]["value"]; + if (propertiesList){ + for (var i = 0; i < propertiesList.length; i++) { + properties[propertiesList[i]["name"]] = propertiesList[i]["value"]; + } } var deviceObject = {}; deviceObject[constants["DEVICE_IDENTIFIER"]] = device["deviceIdentifier"]; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/operation.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/operation.js index aded5dac5c8..75c9abe9e7e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/operation.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/operation.js @@ -21,6 +21,7 @@ var operationModule = function () { var utility = require('/app/modules/utility.js').utility; var constants = require('/app/modules/constants.js'); var devicemgtProps = require('/app/conf/devicemgt-props.js').config(); + var serviceInvokers = require("/app/modules/backend-service-invoker.js").backendServiceInvoker; var publicMethods = {}; var privateMethods = {}; @@ -39,51 +40,45 @@ var operationModule = function () { privateMethods.getOperationsFromFeatures = function (deviceType, operationType) { var url = devicemgtProps["httpsURL"] + constants.ADMIN_SERVICE_CONTEXT + "/features/" + deviceType; var featuresList = serviceInvokers.XMLHttp.get(url, function (responsePayload) { - var features = responsePayload.responseContent; + var features = responsePayload; var featureList = []; var feature; - for (var i = 0; i < features.size(); i++) { + for (var i = 0; i < features.length; i++) { feature = {}; - if (features.get(i).getType() != operationType) { + if (features[i].type != operationType) { continue; - } else if (features.get(i).getType() == 'monitor') { + } else if (features[i].type == 'monitor') { var analyticStreams = utility.getDeviceTypeConfig(deviceType)["analyticStreams"]; if (analyticStreams) { for (var stream in analyticStreams) { - if (analyticStreams[stream].name == features.get(i).getName()) { + if (analyticStreams[stream].name == features[i].name) { feature.ui_unit = analyticStreams[stream].ui_unit; break; } } } } - feature["operation"] = new String(features.get(i).getCode()); - feature["name"] = new String(features.get(i).getName()); - feature["description"] = new String(features.get(i).getDescription()); - feature["deviceType"] = new String(features.get(i).getDeviceType()); + feature["operation"] = features[i].code; + feature["name"] = features[i].name; + feature["description"] = features[i].description; + feature["deviceType"] = deviceType; feature["params"] = []; - var metaData = features.get(i).getMetadataEntries(); - if (metaData && metaData != null) { - for (var j = 0; j < metaData.size(); j++) { - feature["params"].push(new String(metaData.get(j).getValue())); + var metaData = features[i].metadataEntries; + if (metaData) { + for (var j = 0; j < metaData.length; j++) { + feature["params"].push(metaData[j].value); } featureList.push(feature); } } return featureList; - } - , - function (responsePayload) { + }, function (responsePayload) { var response = {}; response["status"] = "error"; return response; } ); return featuresList; - return featureList; - } catch (e) { - throw e; - } }; publicMethods.getControlOperations = function (deviceType) { @@ -108,7 +103,6 @@ var operationModule = function () { '","protocol":"mqtt", "sessionId":"' + session.getId() + '", "' + constants.AUTHORIZATION_HEADER + '":"' + constants.BEARER_PREFIX + getAccessToken(deviceType, user.username, deviceId) + '"}'; - log.warn("header: " + header); return post(endPoint, params, JSON.parse(header), "json"); }; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.devices/devices.hbs b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.devices/devices.hbs index a15a5a84b12..e4663a57ba2 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.devices/devices.hbs +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.devices/devices.hbs @@ -61,7 +61,7 @@
{{unit "cdmf.unit.device.operation-mod"}} {{#if deviceCount}} - +
    @@ -324,7 +324,7 @@ {{/zone}} {{#zone "bottomJs"}} - diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.devices/devices.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.devices/devices.js index 88cb32d704e..6a376bb0282 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.devices/devices.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.devices/devices.js @@ -31,25 +31,13 @@ function onRequest(context) { page.groupName = groupName; } page.title = title; - page.permissions = {}; var currentUser = session.get(constants.USER_SESSION_KEY); - var permissions = []; if (currentUser) { - if (userModule.isAuthorized("/permission/admin/device-mgt/admin/devices/list")) { - permissions.push("LIST_DEVICES"); - } else if (userModule.isAuthorized("/permission/admin/device-mgt/user/devices/list")) { - permissions.push("LIST_OWN_DEVICES"); - } else if (userModule.isAuthorized("/permission/admin/device-mgt/emm-admin/policies/list")) { - permissions.push("LIST_POLICIES"); - } + page.permissions = {}; + page.permissions.list = stringify(userModule.getUIPermissions()); if (userModule.isAuthorized("/permission/admin/device-mgt/admin/devices/add")) { permissions.enroll = true; } - if (userModule.isAuthorized("/permission/admin/device-mgt/admin/devices/remove")) { - permissions.push("REMOVE_DEVICE"); - } - - page.permissions.list = permissions; page.currentUser = currentUser; var deviceCount = 0; if (groupName && groupOwner) { @@ -64,15 +52,17 @@ function onRequest(context) { var utility = require("/app/modules/utility.js").utility; var data = deviceModule.getDeviceTypes(); var deviceTypes = []; - if (data.data) { - for (var i = 0; i < data.data.length; i++) { + if (data) { + for (var i = 0; i < data.length; i++) { + var deviceType = utility.getDeviceTypeConfig(data[i].name).deviceType; deviceTypes.push({ - "type": data.data[i].name, - "category": utility.getDeviceTypeConfig(data.data[i].name).deviceType.category - }); + "type": data[i].name, + "category": deviceType.category, + "label": deviceType.label + }); } } - page.deviceTypes = deviceTypes; + page.deviceTypes = stringify(deviceTypes); } } return page; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.devices/public/js/listing.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.devices/public/js/listing.js index 2711b2b29c5..767c37f6848 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.devices/public/js/listing.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.devices/public/js/listing.js @@ -72,8 +72,10 @@ $(document).ready(function () { var i; var permissionList = $("#permission").data("permission"); - for (i = 0; i < permissionList.length; i++) { - $.setPermission(permissionList[i]); + for (var key in permissionList) { + if (permissionList.hasOwnProperty(key)) { + $.setPermission(key); + } } /* for device list sorting drop down */ @@ -171,7 +173,7 @@ function loadDevices(searchType, searchParam){ serviceURL = "/devicemgt_admin/devices"; } else if ($.hasPermission("LIST_OWN_DEVICES")) { //Get authenticated users devices - serviceURL = "/devicemgt_admin/users/devices?username="+currentUser; + serviceURL = "/devicemgt_admin/users/devices?username=" + currentUser; } else { $("#loading-content").remove(); $('#device-table').addClass('hidden'); @@ -181,8 +183,11 @@ function loadDevices(searchType, searchParam){ } function getPropertyValue(deviceProperties, propertyName) { + if (!deviceProperties) { + return; + } var property; - for (var i =0; i < deviceProperties.length; i++) { + for (var i = 0; i < deviceProperties.length; i++) { property = deviceProperties[i]; if (property.name == propertyName) { return property.value; @@ -191,6 +196,16 @@ function loadDevices(searchType, searchParam){ return {}; } + function getDeviceTypeLabel(type){ + var deviceTypes = deviceListing.data("deviceTypes"); + for (var i = 0; i < deviceTypes.length; i++){ + if (deviceTypes[i].type == type){ + return deviceTypes[i].label; + } + } + return type; + } + $('#device-grid').datatables_extended ({ serverSide: true, processing: false, @@ -242,7 +257,10 @@ function loadDevices(searchType, searchParam){ } return html; }}, - { targets: 4, data: 'type' , className: 'fade-edge remove-padding-top' }, + { targets: 4, data: 'type' , className: 'fade-edge remove-padding-top' , + render: function ( status, type, row, meta ) { + return getDeviceTypeLabel(row.type); + }}, { targets: 5, data: 'enrolmentInfo.ownership' , className: 'fade-edge remove-padding-top' }, { targets: 6, data: 'enrolmentInfo.status' , className: 'text-right content-fill text-left-on-grid-view no-wrap' , render: function ( status, type, row, meta ) {