diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/backend-service-invoker.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/backend-service-invoker.js index 1e3f0bd69a..6caa5bccd1 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/backend-service-invoker.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/backend-service-invoker.js @@ -46,25 +46,25 @@ var backendServiceInvoker = function () { }; /** - * This method add Oauth authentication header to outgoing XMLHTTP Requests if Oauth authentication is enabled. - * @param method HTTP request type. - * @param url target url. - * @param payload payload/data which need to be send. - * @param successCallback a function to be called if the respond if successful. - * @param errorCallback a function to be called if en error is reserved. + * --------------------------------------------------------------------------- + * Start of XML-HTTP-REQUEST based Interceptor implementations + * --------------------------------------------------------------------------- + */ + + /** + * This method add Oauth authentication header to outgoing XML-HTTP Requests if Oauth authentication is enabled. + * @param httpMethod HTTP request type. + * @param requestPayload payload/data if exists which is needed to be send. + * @param endpoint Backend REST API url. + * @param responseCallback a function to be called with response retrieved. * @param count a counter which hold the number of recursive execution */ - privateMethods.execute = function (method, url, successCallback, errorCallback, payload, count, contentType, acceptType) { + privateMethods.execute = function (httpMethod, requestPayload, endpoint, responseCallback, count) { var xmlHttpRequest = new XMLHttpRequest(); - xmlHttpRequest.open(method, url); - if(!contentType){ - contentType = constants.APPLICATION_JSON; - } - if(!acceptType){ - acceptType = constants.APPLICATION_JSON; - } - xmlHttpRequest.setRequestHeader(constants.CONTENT_TYPE_IDENTIFIER, contentType); - xmlHttpRequest.setRequestHeader(constants.ACCEPT_IDENTIFIER, acceptType); + + xmlHttpRequest.open(httpMethod, endpoint); + xmlHttpRequest.setRequestHeader(constants["CONTENT_TYPE_IDENTIFIER"], constants["APPLICATION_JSON"]); + xmlHttpRequest.setRequestHeader(constants["ACCEPT_IDENTIFIER"], constants["APPLICATION_JSON"]); if (IS_OAUTH_ENABLED) { var accessToken = privateMethods.getAccessToken(); if (!accessToken) { @@ -73,43 +73,145 @@ var backendServiceInvoker = function () { xmlHttpRequest.setRequestHeader(constants.AUTHORIZATION_HEADER, constants.BEARER_PREFIX + accessToken); } } - if (payload) { - xmlHttpRequest.send(payload); + + if (requestPayload) { + xmlHttpRequest.send(requestPayload); } else { xmlHttpRequest.send(); } - if ((xmlHttpRequest.status >= 200 && xmlHttpRequest.status < 300) || xmlHttpRequest.status == 302) { - if (xmlHttpRequest.responseText != null) { - return successCallback(parse(xmlHttpRequest.responseText)); - } else { - return successCallback({"status": xmlHttpRequest.status, "messageFromServer": "Operation Completed"}); - } - } else if (xmlHttpRequest.status == 401 && (xmlHttpRequest.responseText == TOKEN_EXPIRED || - xmlHttpRequest.responseText == TOKEN_INVALID ) && count < 5) { + + if (xmlHttpRequest.status == 401 && (xmlHttpRequest.responseText == TOKEN_EXPIRED || + xmlHttpRequest.responseText == TOKEN_INVALID ) && count < 5) { tokenUtil.refreshToken(); - return privateMethods.execute(method, url, successCallback, errorCallback, payload, (count + 1)); - } else if (xmlHttpRequest.status == 500) { - return errorCallback(xmlHttpRequest); + return privateMethods.execute(httpMethod, requestPayload, endpoint, responseCallback, ++count); } else { - return errorCallback(xmlHttpRequest); + return responseCallback(xmlHttpRequest); } }; /** - * This method add Oauth authentication header to outgoing XMLHTTP Requests if Oauth authentication is enabled. - * @param method HTTP request type. - * @param url target url. - * @param payload payload/data which need to be send. + * This method add Oauth authentication header to outgoing XML-HTTP Requests if Oauth authentication is enabled. + * @param httpMethod HTTP request type. + * @param requestPayload payload/data if exists which is needed to be send. + * @param endpoint Backend REST API url. + * @param responseCallback a function to be called with response retrieved. + */ + privateMethods.initiateXMLHTTPRequest = function (httpMethod, requestPayload, endpoint, responseCallback) { + return privateMethods.execute(httpMethod, requestPayload, endpoint, responseCallback, 0); + }; + + /** + * This method invokes return initiateXMLHttpRequest for get calls + * @param endpoint Backend REST API url. + * @param responseCallback a function to be called with response retrieved. + */ + publicXMLHTTPInvokers.get = function (endpoint, responseCallback) { + var requestPayload = null; + return privateMethods.initiateXMLHTTPRequest(constants["HTTP_GET"], requestPayload, endpoint, responseCallback); + }; + + /** + * This method invokes return initiateXMLHttpRequest for post calls + * @param endpoint Backend REST API url. + * @param requestPayload payload/data if exists which is needed to be send. + * @param responseCallback a function to be called with response retrieved. + */ + publicXMLHTTPInvokers.post = function (endpoint, requestPayload, responseCallback) { + return privateMethods.initiateXMLHTTPRequest(constants["HTTP_POST"], requestPayload, endpoint, responseCallback); + }; + + /** + * This method invokes return initiateXMLHttpRequest for put calls + * @param endpoint Backend REST API url. + * @param requestPayload payload/data if exists which is needed to be send. + * @param responseCallback a function to be called with response retrieved. + */ + publicXMLHTTPInvokers.put = function (endpoint, requestPayload, responseCallback) { + return privateMethods.initiateXMLHTTPRequest(constants["HTTP_PUT"], requestPayload, endpoint, responseCallback); + }; + + /** + * This method invokes return initiateXMLHttpRequest for delete calls + * @param endpoint Backend REST API url. + * @param responseCallback a function to be called with response retrieved. + */ + publicXMLHTTPInvokers.delete = function (endpoint, responseCallback) { + var requestPayload = null; + return privateMethods.initiateXMLHTTPRequest(constants["HTTP_DELETE"], requestPayload, endpoint, responseCallback); + }; + + /** + * --------------------------------------------------------------------------- + * Start of WS-REQUEST based Interceptor implementations + * --------------------------------------------------------------------------- + */ + + /** + * This method add Oauth authentication header to outgoing WS Requests if Oauth authentication is enabled. + * @param action + * @param endpoint service end point to be triggered. + * @param payload soap payload which need to be send. * @param successCallback a function to be called if the respond if successful. * @param errorCallback a function to be called if en error is reserved. + * @param soapVersion soapVersion which need to used. */ - privateMethods.initiateXMLHTTPRequest = function (method, url, successCallback, errorCallback, payload, contentType, acceptType) { - if (privateMethods.getAccessToken()) { - return privateMethods.execute(method, url, successCallback, errorCallback, payload, 0, contentType, acceptType); + privateMethods.initiateWSRequest = function (action, endpoint, successCallback, errorCallback, soapVersion, payload) { + var ws = require('ws'); + var wsRequest = new ws.WSRequest(); + var options = []; + if (IS_OAUTH_ENABLED) { + var accessToken = privateMethods.getAccessToken(); + if (accessToken) { + var authenticationHeaderName = String(constants.AUTHORIZATION_HEADER); + var authenticationHeaderValue = String(constants.BEARER_PREFIX + accessToken); + var headers = []; + var oAuthAuthenticationData = {}; + oAuthAuthenticationData.name = authenticationHeaderName; + oAuthAuthenticationData.value = authenticationHeaderValue; + headers.push(oAuthAuthenticationData); + options.HTTPHeaders = headers; + } else { + response.sendRedirect(devicemgtProps["httpsURL"] + "/devicemgt/login"); + } } + options.useSOAP = soapVersion; + options.useWSA = constants.WEB_SERVICE_ADDRESSING_VERSION; + options.action = action; + var wsResponse; + try { + wsRequest.open(options, endpoint, false); + if (payload) { + wsRequest.send(payload); + } else { + wsRequest.send(); + } + wsResponse = wsRequest.responseE4X; + } catch (e) { + return errorCallback(e); + } + return successCallback(wsResponse); + }; + + /** + * This method invokes return initiateWSRequest for soap calls + * @param action describes particular soap action. + * @param requestPayload SOAP request payload which is needed to be send. + * @param endpoint service end point to be triggered. + * @param successCallback a function to be called if the respond if successful. + * @param errorCallback a function to be called if en error is reserved. + * @param soapVersion soapVersion which need to used. + */ + publicWSInvokers.soapRequest = function (action, endpoint, payload, successCallback, errorCallback, soapVersion) { + return privateMethods.initiateWSRequest(action, endpoint, successCallback, errorCallback, soapVersion, payload); }; + /** + * --------------------------------------------------------------------------- + * Start of HTTP-CLIENT-REQUEST based Interceptor implementations + * --------------------------------------------------------------------------- + */ + /** * This method add Oauth authentication header to outgoing HTTPClient Requests if Oauth authentication is enabled. * @param method HTTP request type. @@ -118,7 +220,7 @@ var backendServiceInvoker = function () { * @param successCallback a function to be called if the respond if successful. * @param errorCallback a function to be called if en error is reserved. */ - privateMethods.initiateHTTPClientRequest = function (method, url, successCallback, errorCallback, payload, contentType, acceptType) { + privateMethods.initiateHTTPClientRequest = function (method, url, successCallback, errorCallback, payload) { var HttpClient = Packages.org.apache.commons.httpclient.HttpClient; var httpMethodObject; switch (method) { @@ -144,11 +246,9 @@ var backendServiceInvoker = function () { var Header = Packages.org.apache.commons.httpclient.Header; var header = new Header(); header.setName(constants.CONTENT_TYPE_IDENTIFIER); - header.setValue(contentType); httpMethodObject.addRequestHeader(header); header = new Header(); header.setName(constants.ACCEPT_IDENTIFIER); - header.setValue(acceptType); httpMethodObject.addRequestHeader(header); if (IS_OAUTH_ENABLED) { var accessToken = privateMethods.getAccessToken(); @@ -160,7 +260,6 @@ var backendServiceInvoker = function () { } else { response.sendRedirect(devicemgtProps["httpsURL"] + "/devicemgt/login"); } - } if (payload) { var stringRequestEntity = new StringRequestEntity(stringify(payload)); @@ -187,115 +286,16 @@ var backendServiceInvoker = function () { } }; - /** - * This method add Oauth authentication header to outgoing WS Requests if Oauth authentication is enabled. - * @param action - * @param endpoint service end point to be triggered. - * @param payload soap payload which need to be send. - * @param successCallback a function to be called if the respond if successful. - * @param errorCallback a function to be called if en error is reserved. - * @param soapVersion soapVersion which need to used. - */ - privateMethods.initiateWSRequest = function (action, endpoint, successCallback, errorCallback, soapVersion, payload) { - var ws = require('ws'); - var wsRequest = new ws.WSRequest(); - var options = []; - if (IS_OAUTH_ENABLED) { - var accessToken = privateMethods.getAccessToken(); - if (accessToken) { - var authenticationHeaderName = String(constants.AUTHORIZATION_HEADER); - var authenticationHeaderValue = String(constants.BEARER_PREFIX + accessToken); - var headers = []; - var oAuthAuthenticationData = {}; - oAuthAuthenticationData.name = authenticationHeaderName; - oAuthAuthenticationData.value = authenticationHeaderValue; - headers.push(oAuthAuthenticationData); - options.HTTPHeaders = headers; - } else { - response.sendRedirect(devicemgtProps["httpsURL"] + "/devicemgt/login"); - } - } - options.useSOAP = soapVersion; - options.useWSA = constants.WEB_SERVICE_ADDRESSING_VERSION; - options.action = action; - var wsResponse; - try { - wsRequest.open(options, endpoint, false); - if (payload) { - wsRequest.send(payload); - } else { - wsRequest.send(); - } - wsResponse = wsRequest.responseE4X; - } catch (e) { - return errorCallback(e); - } - return successCallback(wsResponse); - }; - - /** - * This method invokes return initiateXMLHttpRequest for get calls - * @param url target url. - * @param successCallback a function to be called if the respond if successful. - * @param errorCallback a function to be called if en error is reserved. - */ - publicXMLHTTPInvokers.get = function (url, successCallback, errorCallback, contentType, acceptType) { - return privateMethods.initiateXMLHTTPRequest(constants.HTTP_GET, url, successCallback, errorCallback, contentType, acceptType); - }; - - /** - * This method invokes return initiateXMLHttpRequest for post calls - * @param url target url. - * @param payload payload/data which need to be send. - * @param successCallback a function to be called if the respond if successful. - * @param errorCallback a function to be called if en error is reserved. - */ - publicXMLHTTPInvokers.post = function (url, payload, successCallback, errorCallback, contentType, acceptType) { - return privateMethods.initiateXMLHTTPRequest(constants.HTTP_POST, url, successCallback, errorCallback, payload, contentType, acceptType); - }; - - /** - * This method invokes return initiateXMLHttpRequest for put calls - * @param url target url. - * @param payload payload/data which need to be send. - * @param successCallback a function to be called if the respond if successful. - * @param errorCallback a function to be called if en error is reserved. - */ - publicXMLHTTPInvokers.put = function (url, payload, successCallback, errorCallback, contentType, acceptType) { - return privateMethods.initiateXMLHTTPRequest(constants.HTTP_PUT, url, successCallback, errorCallback, payload, contentType, acceptType); - }; - - /** - * This method invokes return initiateXMLHttpRequest for delete calls - * @param url target url. - * @param successCallback a function to be called if the respond if successful. - * @param errorCallback a function to be called if en error is reserved. - */ - publicXMLHTTPInvokers.delete = function (url, successCallback, errorCallback, contentType, acceptType) { - return privateMethods.initiateXMLHTTPRequest(constants.HTTP_DELETE, url, successCallback, errorCallback, contentType, acceptType); - }; - - /** - * This method invokes return initiateWSRequest for soap calls - * @param endpoint service end point to be triggered. - * @param payload soap payload which need to be send. - * @param successCallback a function to be called if the respond if successful. - * @param errorCallback a function to be called if en error is reserved. - * @param soapVersion soapVersion which need to used. - */ - publicWSInvokers.soapRequest = function (action, endpoint, payload, successCallback, errorCallback, soapVersion) { - return privateMethods.initiateWSRequest(action, endpoint, successCallback, errorCallback, soapVersion, payload); - }; - - /** * This method invokes return initiateHTTPClientRequest for get calls * @param url target url. * @param successCallback a function to be called if the respond if successful. * @param errorCallback a function to be called if en error is reserved. */ - publicHTTPClientInvokers.get = function (url, successCallback, errorCallback, contentType, acceptType) { - return privateMethods.initiateHTTPClientRequest(constants.HTTP_GET, url, successCallback, errorCallback, contentType, acceptType); + publicHTTPClientInvokers.get = function (url, successCallback, errorCallback) { + var requestPayload = null; + return privateMethods. + initiateHTTPClientRequest(constants["HTTP_GET"], url, successCallback, errorCallback, requestPayload); }; /** @@ -305,9 +305,9 @@ var backendServiceInvoker = function () { * @param successCallback a function to be called if the respond if successful. * @param errorCallback a function to be called if en error is reserved. */ - publicHTTPClientInvokers.post = function (url, payload, successCallback, errorCallback, contentType, acceptType) { + publicHTTPClientInvokers.post = function (url, payload, successCallback, errorCallback) { return privateMethods. - initiateHTTPClientRequest(constants.HTTP_POST, url, successCallback, errorCallback, payload, contentType, acceptType); + initiateHTTPClientRequest(constants["HTTP_POST"], url, successCallback, errorCallback, payload); }; /** @@ -317,8 +317,9 @@ var backendServiceInvoker = function () { * @param successCallback a function to be called if the respond if successful. * @param errorCallback a function to be called if en error is reserved. */ - publicHTTPClientInvokers.put = function (url, payload, successCallback, errorCallback, contentType, acceptType) { - return privateMethods.initiateHTTPClientRequest(constants.HTTP_PUT, url, successCallback, errorCallback, payload, contentType, acceptType); + publicHTTPClientInvokers.put = function (url, payload, successCallback, errorCallback) { + return privateMethods. + initiateHTTPClientRequest(constants["HTTP_PUT"], url, successCallback, errorCallback, payload); }; /** @@ -327,13 +328,16 @@ var backendServiceInvoker = function () { * @param successCallback a function to be called if the respond if successful. * @param errorCallback a function to be called if en error is reserved. */ - publicHTTPClientInvokers.delete = function (url, successCallback, errorCallback, contentType, acceptType) { - return privateMethods.initiateHTTPClientRequest(constants.HTTP_DELETE, url, successCallback, errorCallback, contentType, acceptType); + publicHTTPClientInvokers.delete = function (url, successCallback, errorCallback) { + var requestPayload = null; + return privateMethods. + initiateHTTPClientRequest(constants["HTTP_DELETE"], url, successCallback, errorCallback, requestPayload); }; - var publicInvokers = {}; - publicInvokers.XMLHttp = publicXMLHTTPInvokers; - publicInvokers.WS = publicWSInvokers; - publicInvokers.HttpClient = publicHTTPClientInvokers; - return publicInvokers; -}(); \ No newline at end of file + var publicMethods = {}; + publicMethods.XMLHttp = publicXMLHTTPInvokers; + publicMethods.WS = publicWSInvokers; + publicMethods.HttpClient = publicHTTPClientInvokers; + + return publicMethods; +}(); 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 367db085bc..e1eb9f6859 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 @@ -36,7 +36,8 @@ deviceModule = function () { var publicMethods = {}; var privateMethods = {}; - var deviceCloudService = devicemgtProps["httpsURL"] + "/common/device_manager"; + //var deviceCloudService = devicemgtProps["httpsURL"] + "/common/device_manager"; + var deviceManagementService = utility.getDeviceManagementService(); privateMethods.validateAndReturn = function (value) { return (value == undefined || value == null) ? constants.UNSPECIFIED : value; @@ -97,6 +98,61 @@ deviceModule = function () { } }; + /* + @Deprecated + */ + publicMethods.listDevicesForUser = function (username) { + var carbonUser = session.get(constants.USER_SESSION_KEY); + var utility = require('/modules/utility.js').utility; + if (!carbonUser) { + log.error("User object was not found in the session"); + throw constants.ERRORS.USER_NOT_FOUND; + } + try { + utility.startTenantFlow(carbonUser); + var deviceManagementService = utility.getDeviceManagementService(); + var devices = deviceManagementService.getDeviceListOfUser(username); + var deviceList = []; + var i, device, propertiesList, deviceObject; + for (i = 0; i < devices.size(); i++) { + device = devices.get(i); + propertiesList = DeviceManagerUtil.convertDevicePropertiesToMap(device.getProperties()); + + deviceObject = {}; + deviceObject[constants.DEVICE_IDENTIFIER] = + privateMethods.validateAndReturn(device.getDeviceIdentifier()); + deviceObject[constants.DEVICE_NAME] = + privateMethods.validateAndReturn(device.getName()); + deviceObject[constants.DEVICE_OWNERSHIP] = + privateMethods.validateAndReturn(device.getEnrolmentInfo().getOwnership()); + deviceObject[constants.DEVICE_OWNER] = + privateMethods.validateAndReturn(device.getEnrolmentInfo().getOwner()); + deviceObject[constants.DEVICE_TYPE] = + privateMethods.validateAndReturn(device.getType()); + deviceObject[constants.DEVICE_PROPERTIES] = {}; + if (device.getType() == constants.PLATFORM_IOS) { + deviceObject[constants.DEVICE_PROPERTIES][constants.DEVICE_MODEL] = + privateMethods.validateAndReturn(propertiesList.get(constants.DEVICE_PRODUCT)); + deviceObject[constants.DEVICE_PROPERTIES][constants.DEVICE_VENDOR] = constants.VENDOR_APPLE; + } else { + deviceObject[constants.DEVICE_PROPERTIES][constants.DEVICE_MODEL] = + privateMethods.validateAndReturn(propertiesList.get(constants.DEVICE_MODEL)); + deviceObject[constants.DEVICE_PROPERTIES][constants.DEVICE_VENDOR] = + privateMethods.validateAndReturn(propertiesList.get(constants.DEVICE_VENDOR)); + } + deviceObject[constants.DEVICE_PROPERTIES][constants.DEVICE_OS_VERSION] = + privateMethods.validateAndReturn(propertiesList.get(constants.DEVICE_OS_VERSION)); + + deviceList.push(deviceObject); + } + return deviceList; + } catch (e) { + throw e; + } finally { + utility.endTenantFlow(); + } + }; + /* @Deprecated */ @@ -216,36 +272,36 @@ deviceModule = function () { try { utility.startTenantFlow(carbonUser); - var url = devicemgtProps["httpsURL"] + constants.ADMIN_SERVICE_CONTEXT + "/devices/view?type=" + deviceType + "&id=" + deviceId; + var url = devicemgtProps["httpsURL"] + constants.ADMIN_SERVICE_CONTEXT + "/api/device-mgt/v1.0/devices/" + deviceType + "/" + deviceId; return serviceInvokers.XMLHttp.get( - url, function (responsePayload) { - var device = responsePayload.responseContent; - if (device) { - var propertiesList = device["properties"]; - var properties = {}; - 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"]; - deviceObject[constants["DEVICE_NAME"]] = device["name"]; - deviceObject[constants["DEVICE_OWNERSHIP"]] = device["enrolmentInfo"]["ownership"]; - deviceObject[constants["DEVICE_OWNER"]] = device["enrolmentInfo"]["owner"]; - deviceObject[constants["DEVICE_STATUS"]] = device["enrolmentInfo"]["status"]; - deviceObject[constants["DEVICE_TYPE"]] = device["type"]; - if (device["type"] == constants["PLATFORM_IOS"]) { - properties[constants["DEVICE_MODEL"]] = properties[constants["DEVICE_PRODUCT"]]; - delete properties[constants["DEVICE_PRODUCT"]]; - properties[constants["DEVICE_VENDOR"]] = constants["VENDOR_APPLE"]; - } - deviceObject[constants["DEVICE_PROPERTIES"]] = properties; - return deviceObject; + url, + function (backendResponse) { + var response = {}; + if (backendResponse.status == 200 && backendResponse.responseText) { + response["status"] = "success"; + var device = parse(backendResponse.responseText); + var propertiesList = device["properties"]; + var properties = {}; + for (var i = 0; i < propertiesList.length; i++) { + properties[propertiesList[i]["name"]] = + propertiesList[i]["value"]; } - }, - function (responsePayload) { - var response = {}; + var deviceObject = {}; + deviceObject[constants["DEVICE_IDENTIFIER"]] = device["deviceIdentifier"]; + deviceObject[constants["DEVICE_NAME"]] = device["name"]; + deviceObject[constants["DEVICE_OWNERSHIP"]] = device["enrolmentInfo"]["ownership"]; + deviceObject[constants["DEVICE_OWNER"]] = device["enrolmentInfo"]["owner"]; + deviceObject[constants["DEVICE_STATUS"]] = device["enrolmentInfo"]["status"]; + deviceObject[constants["DEVICE_TYPE"]] = device["type"]; + if (device["type"] == constants["PLATFORM_IOS"]) { + properties[constants["DEVICE_MODEL"]] = properties[constants["DEVICE_PRODUCT"]]; + delete properties[constants["DEVICE_PRODUCT"]]; + properties[constants["DEVICE_VENDOR"]] = constants["VENDOR_APPLE"]; + } + deviceObject[constants["DEVICE_PROPERTIES"]] = properties; + response["content"] = deviceObject; + return response; + } else { response["status"] = "error"; return response; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/invoker-request-wrapper.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/invoker-request-wrapper.js index a8fe2b0fc1..b653b4e163 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/invoker-request-wrapper.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/invoker-request-wrapper.js @@ -32,13 +32,11 @@ var invokerRequestWrapper = function () { var response = serviceInvokers.XMLHttp.get(url, function (responsePayload) { var response = {}; response.content = responsePayload["responseContent"]; - response.status = "success"; - return response; - }, - function (responsePayload) { - var response = {}; - response.content = responsePayload; - response.status = "error"; + if (responsePayload.status == 200) { + response.status = "success"; + } else { + response.status = "error"; + } return response; }); return response; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/policy.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/policy.js index a4ee8c1256..a82c8b4732 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/policy.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/policy.js @@ -31,73 +31,91 @@ policyModule = function () { var publicMethods = {}; var privateMethods = {}; - privateMethods.handleGetAllPoliciesError = function (responsePayload) { + privateMethods.handleGetAllPoliciesResponse = function (backendResponse) { var response = {}; - response.status = "error"; - /* responsePayload == "Scope validation failed" - Here the response.context("Scope validation failed") is used other then response.status(401). - Reason for this is IDP return 401 as the status in 4 different situations such as, - 1. UnAuthorized. - 2. Scope Validation Failed. - 3. Permission Denied. - 4. Access Token Expired. - 5. Access Token Invalid. - In these cases in order to identify the correct situation we have to compare the unique value from status and - context which is context. - */ - if (responsePayload == "Scope validation failed") { - response.content = "Permission Denied"; - } else { - response.content = responsePayload; - } - return response; - }; + if (backendResponse.status = 200) { + var isUpdated = false; + var policyListFromRestEndpoint = parse(backendResponse.responseText)["policies"]; + var policyListToView = []; + var i, policyObjectFromRestEndpoint, policyObjectToView; + for (i = 0; i < policyListFromRestEndpoint.length; i++) { + // get list object + policyObjectFromRestEndpoint = policyListFromRestEndpoint[i]; + // populate list object values to view-object + policyObjectToView = {}; + policyObjectToView["id"] = policyObjectFromRestEndpoint["id"]; + policyObjectToView["priorityId"] = policyObjectFromRestEndpoint["priorityId"]; + policyObjectToView["name"] = policyObjectFromRestEndpoint["policyName"]; + policyObjectToView["platform"] = policyObjectFromRestEndpoint["profile"]["deviceType"]["name"]; + policyObjectToView["ownershipType"] = policyObjectFromRestEndpoint["ownershipType"]; + + var assignedRoleCount = policyObjectFromRestEndpoint["roles"].length; + var assignedUserCount = policyObjectFromRestEndpoint["users"].length; + + if (assignedRoleCount == 0) { + policyObjectToView["roles"] = "None"; + } else if (assignedRoleCount == 1) { + policyObjectToView["roles"] = policyObjectFromRestEndpoint["roles"][0]; + } else if (assignedRoleCount > 1) { + policyObjectToView["roles"] = policyObjectFromRestEndpoint["roles"][0] + ", ..."; + } + + if (assignedUserCount == 0) { + policyObjectToView["users"] = "None"; + } else if (assignedUserCount == 1) { + policyObjectToView["users"] = policyObjectFromRestEndpoint["users"][0]; + } else if (assignedUserCount > 1) { + policyObjectToView["users"] = policyObjectFromRestEndpoint["users"][0] + ", ..."; + } - privateMethods.handleGetAllPoliciesSuccess = function (responsePayload) { - var isUpdated = false; - var policyListFromRestEndpoint = responsePayload["responseContent"]; - var policyListToView = []; - var i, policyObjectFromRestEndpoint, policyObjectToView; - for (i = 0; i < policyListFromRestEndpoint.length; i++) { - // get list object - policyObjectFromRestEndpoint = policyListFromRestEndpoint[i]; - // populate list object values to view-object - policyObjectToView = {}; - policyObjectToView["id"] = policyObjectFromRestEndpoint["id"]; - policyObjectToView["priorityId"] = policyObjectFromRestEndpoint["priorityId"]; - policyObjectToView["name"] = policyObjectFromRestEndpoint["policyName"]; - policyObjectToView["platform"] = policyObjectFromRestEndpoint["profile"]["deviceType"]["name"]; - policyObjectToView["icon"] = utility.getDeviceThumb(policyObjectToView["platform"]); - policyObjectToView["ownershipType"] = policyObjectFromRestEndpoint["ownershipType"]; - policyObjectToView["roles"] = privateMethods. - getElementsInAString(policyObjectFromRestEndpoint["roles"]); - policyObjectToView["users"] = privateMethods. - getElementsInAString(policyObjectFromRestEndpoint["users"]); - policyObjectToView["compliance"] = policyObjectFromRestEndpoint["compliance"]; + policyObjectToView["compliance"] = policyObjectFromRestEndpoint["compliance"]; - if (policyObjectFromRestEndpoint["active"] == true && policyObjectFromRestEndpoint["updated"] == true) { - policyObjectToView["status"] = "Active/Updated"; - isUpdated = true; - } else if (policyObjectFromRestEndpoint["active"] == true && - policyObjectFromRestEndpoint["updated"] == false) { - policyObjectToView["status"] = "Active"; - } else if (policyObjectFromRestEndpoint["active"] == false && - policyObjectFromRestEndpoint["updated"] == true) { - policyObjectToView["status"] = "Inactive/Updated"; - isUpdated = true; - } else if (policyObjectFromRestEndpoint["active"] == false && - policyObjectFromRestEndpoint["updated"] == false) { - policyObjectToView["status"] = "Inactive"; + if (policyObjectFromRestEndpoint["active"] == true && + policyObjectFromRestEndpoint["updated"] == true) { + policyObjectToView["status"] = "Active/Updated"; + isUpdated = true; + } else if (policyObjectFromRestEndpoint["active"] == true && + policyObjectFromRestEndpoint["updated"] == false) { + policyObjectToView["status"] = "Active"; + } else if (policyObjectFromRestEndpoint["active"] == false && + policyObjectFromRestEndpoint["updated"] == true) { + policyObjectToView["status"] = "Inactive/Updated"; + isUpdated = true; + } else if (policyObjectFromRestEndpoint["active"] == false && + policyObjectFromRestEndpoint["updated"] == false) { + policyObjectToView["status"] = "Inactive"; + } + // push view-objects to list + policyListToView.push(policyObjectToView); } - // push view-objects to list - policyListToView.push(policyObjectToView); + // generate response + response.updated = isUpdated; + response.status = "success"; + response.content = policyListToView; + + log.info(stringify(policyListToView)); + + return response; + } else { + response.status = "error"; + /* backendResponse.responseText == "Scope validation failed" + Here the response.context("Scope validation failed") is used other then response.status(401). + Reason for this is IDP return 401 as the status in 4 different situations such as, + 1. UnAuthorized. + 2. Scope Validation Failed. + 3. Permission Denied. + 4. Access Token Expired. + 5. Access Token Invalid. + In these cases in order to identify the correct situation we have to compare the unique value from status and + context which is context. + */ + if (backendResponse.responseText == "Scope validation failed") { + response.content = "Permission Denied"; + } else { + response.content = backendResponse.responseText; + } + return response; } - // generate response - var response = {}; - response.updated = isUpdated; - response.status = "success"; - response.content = policyListToView; - return response; }; publicMethods.addPolicy = function (policyName, deviceType, policyDefinition, policyDescription, @@ -155,9 +173,8 @@ policyModule = function () { throw constants["ERRORS"]["USER_NOT_FOUND"]; } try { - var url = devicemgtProps["httpsURL"] + constants.ADMIN_SERVICE_CONTEXT + "/policies"; - return serviceInvokers.XMLHttp. - get(url, privateMethods.handleGetAllPoliciesSuccess, privateMethods.handleGetAllPoliciesError); + var url = devicemgtProps["httpsURL"] + constants.ADMIN_SERVICE_CONTEXT + "/policies?offset=0&limit=100"; + return serviceInvokers.XMLHttp.get(url, privateMethods.handleGetAllPoliciesResponse); } catch (e) { throw e; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/user.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/user.js index b0eb98c554..24cb4c63bb 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/user.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/user.js @@ -66,22 +66,18 @@ var userModule = function () { */ privateMethods.callBackend = function (url, method) { if (constants.HTTP_GET == method) { - var response = serviceInvokers.XMLHttp.get(url, function (responsePayload) { - var response = {}; - response.content = responsePayload["responseContent"]; - if (responsePayload["responseContent"] == null && responsePayload != null) { - response.content = responsePayload; + return serviceInvokers.XMLHttp.get(url, + function (backendResponse) { + var response = {}; + response.content = backendResponse.responseText; + if (backendResponse.status == 200) { + response.status = "success"; + } else { + response.status = "error"; + } + return response; } - response.status = "success"; - return response; - }, - function (responsePayload) { - var response = {}; - response.content = responsePayload; - response.status = "error"; - return response; - }); - return response; + ); } else { log.error("Programming error : This method only support HTTP GET requests."); } @@ -382,9 +378,12 @@ var userModule = function () { } try { utility.startTenantFlow(carbonUser); - var url = devicemgtProps["httpsURL"] + constants.ADMIN_SERVICE_CONTEXT + "/users"; - return privateMethods.callBackend(url, constants.HTTP_GET); - + var url = devicemgtProps["httpsURL"] + constants.ADMIN_SERVICE_CONTEXT + "/users?offset=0&limit=100"; + var response = privateMethods.callBackend(url, constants["HTTP_GET"]); + if (response.status == "success") { + response.content = parse(response.content).users; + } + return response; } catch (e) { throw e; } finally { @@ -409,8 +408,10 @@ var userModule = function () { var carbonUser = privateMethods.getCarbonUser(); try { utility.startTenantFlow(carbonUser); - var url = devicemgtProps["httpsURL"] + constants.ADMIN_SERVICE_CONTEXT + "/users/view?username=" + username; - var response = privateMethods.callBackend(url, constants.HTTP_GET); + var url = devicemgtProps["httpsURL"] + constants.ADMIN_SERVICE_CONTEXT + "/users/" + + encodeURIComponent(username); + var response = privateMethods.callBackend(url, constants["HTTP_GET"]); + response["content"] = parse(response.content); response["userDomain"] = carbonUser.domain; return response; } catch (e) { @@ -420,17 +421,17 @@ var userModule = function () { } }; /** - * TODO: comment + * Returns a set of roles assigned to a particular user * @param username - * @returns {*} + * @returns {object} a response object with status and content on success. */ publicMethods.getRolesByUsername = function (username) { var carbonUser = privateMethods.getCarbonUser(); try { utility.startTenantFlow(carbonUser); - var url = devicemgtProps["httpsURL"] + constants.ADMIN_SERVICE_CONTEXT + "/users/roles?username=" + username; - var response = privateMethods.callBackend(url, constants.HTTP_GET); - return response; + var url = devicemgtProps["httpsURL"] + constants.ADMIN_SERVICE_CONTEXT + + "/users/" + + encodeURIComponent(username) + "/roles"; + return privateMethods.callBackend(url, constants["HTTP_GET"]); } catch (e) { throw e; } finally { @@ -464,6 +465,7 @@ var userModule = function () { */ /** * Get User Roles from user store (Internal roles not included). + * @returns {object} a response object with status and content on success. */ publicMethods.getRoles = function () { var carbonUser = session.get(constants["USER_SESSION_KEY"]); @@ -475,7 +477,11 @@ var userModule = function () { try { utility.startTenantFlow(carbonUser); var url = devicemgtProps["httpsURL"] + constants.ADMIN_SERVICE_CONTEXT + "/roles"; - return privateMethods.callBackend(url, constants.HTTP_GET); + var response = privateMethods.callBackend(url, constants["HTTP_GET"]); + if (response.status == "success") { + response.content = parse(response.content).roles; + } + return response; } catch (e) { throw e; } finally { @@ -488,8 +494,10 @@ var userModule = function () { */ /** * Get User Roles from user store (Internal roles not included). + * @returns {object} a response object with status and content on success. */ - publicMethods.getRolesByUserStore = function (userStore) { + publicMethods.getRolesByUserStore = function () { + var ROLE_LIMIT = devicemgtProps.pageSize; var carbonUser = session.get(constants["USER_SESSION_KEY"]); var utility = require('/app/modules/utility.js')["utility"]; if (!carbonUser) { @@ -498,8 +506,12 @@ var userModule = function () { } try { utility.startTenantFlow(carbonUser); - var url = devicemgtProps["httpsURL"] + constants.ADMIN_SERVICE_CONTEXT + "/roles/" + encodeURIComponent(userStore); - return privateMethods.callBackend(url, constants.HTTP_GET); + var url = devicemgtProps["httpsURL"] + constants.ADMIN_SERVICE_CONTEXT + "/roles?limit=" + ROLE_LIMIT; + var response = privateMethods.callBackend(url, constants["HTTP_GET"]); + if (response.status == "success") { + response.content = parse(response.content).roles; + } + return response; } catch (e) { throw e; } finally { @@ -520,7 +532,11 @@ var userModule = function () { try { utility.startTenantFlow(carbonUser); var url = devicemgtProps["httpsURL"] + constants.ADMIN_SERVICE_CONTEXT + "/devices/types"; - return privateMethods.callBackend(url, constants.HTTP_GET); + var response = privateMethods.callBackend(url, constants["HTTP_GET"]); + if (response.status == "success") { + response.content = parse(response.content); + } + return response; } catch (e) { throw e; } finally { @@ -542,8 +558,9 @@ var userModule = function () { } try { utility.startTenantFlow(carbonUser); - var url = devicemgtProps["httpsURL"] + constants.ADMIN_SERVICE_CONTEXT + "/roles/role?rolename=" + encodeURIComponent(roleName); - var response = privateMethods.callBackend(url, constants.HTTP_GET); + var url = devicemgtProps["httpsURL"] + constants.ADMIN_SERVICE_CONTEXT + "/roles/" + encodeURIComponent(roleName); + var response = privateMethods.callBackend(url, constants["HTTP_GET"]); + response.content = parse(response.content); return response; } catch (e) { throw e; @@ -683,6 +700,9 @@ var userModule = function () { if (publicMethods.isAuthorized("/permission/admin/device-mgt/admin/platform-configs/view")) { permissions["TENANT_CONFIGURATION"] = true; } + if (publicMethods.isAuthorized("/permission/admin/device-mgt/user/devices/list")) { + permissions["LIST_OWN_DEVICES"] = true; + } return permissions; };