From 0fab418187126dce9e5b21995ff35ec2afa2c1fb Mon Sep 17 00:00:00 2001 From: Rasika Perera Date: Tue, 13 Sep 2016 19:43:43 +0530 Subject: [PATCH 1/2] Fixing invalid device count and group count --- .../devicemgt/app/modules/business-controllers/device.js | 8 ++++---- .../devicemgt/app/pages/cdmf.page.dashboard/dashboard.js | 6 ++++-- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/business-controllers/device.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/business-controllers/device.js index b970b04cf9d..6c7dbcc1742 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/business-controllers/device.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/business-controllers/device.js @@ -298,20 +298,20 @@ deviceModule = function () { var url; if (uiPermissions.LIST_DEVICES) { url = devicemgtProps["httpsURL"] + - devicemgtProps["backendRestEndpoints"]["deviceMgt"] + "/devices/count"; + devicemgtProps["backendRestEndpoints"]["deviceMgt"] + "/devices?offset=0&limit=1"; } else if (uiPermissions.LIST_OWN_DEVICES) { url = devicemgtProps["httpsURL"] + devicemgtProps["backendRestEndpoints"]["deviceMgt"] + - "/devices/user/" + carbonUser.username + "/count"; + "/devices?offset=0&limit=1&user=" + carbonUser.username; } else { log.error("Access denied for user: " + carbonUser.username); return -1; } return serviceInvokers.XMLHttp.get( url, function (responsePayload) { - return responsePayload; + return parse(responsePayload["responseText"])["count"]; }, function (responsePayload) { - log.error(responsePayload); + log.error(responsePayload["responseText"]); return -1; } ); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.dashboard/dashboard.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.dashboard/dashboard.js index 06ac10ba3ab..7fcd099e189 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.dashboard/dashboard.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.dashboard/dashboard.js @@ -36,10 +36,12 @@ function onRequest() { page.permissions = permissions; page.enrollmentURL = devicemgtProps.enrollmentURL; page.device_count = deviceModule.getDevicesCount(); - page.group_count = groupModule.getGroupCount(); + //TODO: Enable Group Management Service API on CDMF + //page.group_count = groupModule.getGroupCount(); + page.group_count = -1; page.user_count = userModule.getUsers()["content"].length; page.policy_count = policyModule.getAllPolicies()["content"].length; page.role_count = userModule.getRoles()["content"].length; return page; -} +} \ No newline at end of file From 1d7546916c3f030db659045275e30891290be3ec Mon Sep 17 00:00:00 2001 From: Rasika Perera Date: Wed, 14 Sep 2016 07:09:05 +0530 Subject: [PATCH 2/2] Changes to the policiesCount, rolesCount, usersCount methods --- .../modules/business-controllers/policy.js | 34 ++++++- .../app/modules/business-controllers/user.js | 93 +++++++++++++++---- .../pages/cdmf.page.dashboard/dashboard.js | 6 +- 3 files changed, 109 insertions(+), 24 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/business-controllers/policy.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/business-controllers/policy.js index bf3a9daba3a..792d1243ff1 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/business-controllers/policy.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/business-controllers/policy.js @@ -79,14 +79,14 @@ policyModule = function () { policyObjectToView["status"] = "Active/Updated"; isUpdated = true; } else if (policyObjectFromRestEndpoint["active"] == true && - policyObjectFromRestEndpoint["updated"] == false) { + policyObjectFromRestEndpoint["updated"] == false) { policyObjectToView["status"] = "Active"; } else if (policyObjectFromRestEndpoint["active"] == false && - policyObjectFromRestEndpoint["updated"] == true) { + policyObjectFromRestEndpoint["updated"] == true) { policyObjectToView["status"] = "Inactive/Updated"; isUpdated = true; } else if (policyObjectFromRestEndpoint["active"] == false && - policyObjectFromRestEndpoint["updated"] == false) { + policyObjectFromRestEndpoint["updated"] == false) { policyObjectToView["status"] = "Inactive"; } // push view-objects to list @@ -131,13 +131,39 @@ policyModule = function () { } try { var url = devicemgtProps["httpsURL"] + devicemgtProps["backendRestEndpoints"]["deviceMgt"] + - "/policies?offset=0&limit=100"; + "/policies?offset=0&limit=100"; return serviceInvokers.XMLHttp.get(url, privateMethods.handleGetAllPoliciesResponse); } catch (e) { throw e; } }; + /* + Get policies count from backend services. + */ + publicMethods.getPoliciesCount = function () { + var carbonUser = session.get(constants["USER_SESSION_KEY"]); + if (!carbonUser) { + log.error("User object was not found in the session"); + throw constants["ERRORS"]["USER_NOT_FOUND"]; + } + try { + var url = devicemgtProps["httpsURL"] + devicemgtProps["backendRestEndpoints"]["deviceMgt"] + + "/policies?offset=0&limit=1"; + return serviceInvokers.XMLHttp.get( + url, function (responsePayload) { + return parse(responsePayload["responseText"])["count"]; + }, + function (responsePayload) { + log.error(responsePayload["responseText"]); + return -1; + } + ); + } catch (e) { + throw e; + } + }; + /* @Updated - used by getAllPolicies */ diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/business-controllers/user.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/business-controllers/user.js index 222db658b82..43e30b530c3 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/business-controllers/user.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/business-controllers/user.js @@ -59,17 +59,17 @@ var userModule = function () { privateMethods.callBackend = function (url, method) { if (constants["HTTP_GET"] == method) { return serviceInvokers.XMLHttp.get(url, - function (backendResponse) { - var response = {}; - response.content = backendResponse.responseText; - if (backendResponse.status == 200) { - response.status = "success"; - } else if (backendResponse.status == 400 || backendResponse.status == 401 || - backendResponse.status == 404 || backendResponse.status == 500) { - response.status = "error"; - } - return response; - } + function (backendResponse) { + var response = {}; + response.content = backendResponse.responseText; + if (backendResponse.status == 200) { + response.status = "success"; + } else if (backendResponse.status == 400 || backendResponse.status == 401 || + backendResponse.status == 404 || backendResponse.status == 500) { + response.status = "error"; + } + return response; + } ); } else { log.error("Runtime error : This method only support HTTP GET requests."); @@ -141,6 +141,35 @@ var userModule = function () { } }; + /* + Get users count from backend services. + */ + publicMethods.getUsersCount = function () { + var carbonUser = session.get(constants["USER_SESSION_KEY"]); + var utility = require("/app/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 url = devicemgtProps["httpsURL"] + devicemgtProps["backendRestEndpoints"]["deviceMgt"] + "/users?offset=0&limit=1"; + return serviceInvokers.XMLHttp.get( + url, function (responsePayload) { + return parse(responsePayload["responseText"])["count"]; + }, + function (responsePayload) { + log.error(responsePayload["responseText"]); + return -1; + } + ); + } catch (e) { + throw e; + } finally { + utility.endTenantFlow(); + } + }; + /** * Return a User object from the backend by calling the JAX-RS * @param username @@ -151,7 +180,7 @@ var userModule = function () { try { utility.startTenantFlow(carbonUser); var url = devicemgtProps["httpsURL"] + devicemgtProps["backendRestEndpoints"]["deviceMgt"] + "/users/" + - encodeURIComponent(username); + encodeURIComponent(username); var response = privateMethods.callBackend(url, constants["HTTP_GET"]); response["content"] = parse(response.content); response["userDomain"] = carbonUser.domain; @@ -173,7 +202,7 @@ var userModule = function () { try { utility.startTenantFlow(carbonUser); var url = devicemgtProps["httpsURL"] + devicemgtProps["backendRestEndpoints"]["deviceMgt"] + "/users/" + - encodeURIComponent(username) + "/roles"; + encodeURIComponent(username) + "/roles"; var response = privateMethods.callBackend(url, constants["HTTP_GET"]); if (response.status == "success") { response.content = parse(response.content).roles; @@ -223,7 +252,7 @@ var userModule = function () { try { utility.startTenantFlow(carbonUser); var url = devicemgtProps["httpsURL"] + devicemgtProps["backendRestEndpoints"]["deviceMgt"] + - "/roles?offset=0&limit=100"; + "/roles?offset=0&limit=100"; var response = privateMethods.callBackend(url, constants["HTTP_GET"]); if (response.status == "success") { response.content = parse(response.content).roles; @@ -236,6 +265,36 @@ var userModule = function () { } }; + /** + * Get User Roles count from user store (Internal roles not included). + */ + publicMethods.getRolesCount = function () { + var carbonUser = session.get(constants["USER_SESSION_KEY"]); + var utility = require("/app/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 url = devicemgtProps["httpsURL"] + devicemgtProps["backendRestEndpoints"]["deviceMgt"] + + "/roles?offset=0&limit=1"; + return serviceInvokers.XMLHttp.get( + url, function (responsePayload) { + return parse(responsePayload["responseText"])["count"]; + }, + function (responsePayload) { + log.error(responsePayload["responseText"]); + return -1; + } + ); + } catch (e) { + throw e; + } finally { + utility.endTenantFlow(); + } + }; + /* @Updated */ @@ -253,7 +312,7 @@ var userModule = function () { try { utility.startTenantFlow(carbonUser); var url = devicemgtProps["httpsURL"] + devicemgtProps["backendRestEndpoints"]["deviceMgt"] + - "/roles?user-store=" + userStore + "&limit=100"; + "/roles?user-store=" + userStore + "&limit=100"; var response = privateMethods.callBackend(url, constants["HTTP_GET"]); if (response.status == "success") { response.content = parse(response.content).roles; @@ -270,7 +329,7 @@ var userModule = function () { * Get Platforms. * @deprecated moved this device module under getDeviceTypes. */ - //TODO Move this piece of logic out of user.js to somewhere else appropriate. + //TODO Move this piece of logic out of user.js to somewhere else appropriate. publicMethods.getPlatforms = function () { var carbonUser = session.get(constants["USER_SESSION_KEY"]); var utility = require("/app/modules/utility.js")["utility"]; @@ -306,7 +365,7 @@ var userModule = function () { try { utility.startTenantFlow(carbonUser); var url = devicemgtProps["httpsURL"] + devicemgtProps["backendRestEndpoints"]["deviceMgt"] + - "/roles/" + encodeURIComponent(roleName); + "/roles/" + encodeURIComponent(roleName); var response = privateMethods.callBackend(url, constants["HTTP_GET"]); if (response.status == "success") { response.content = parse(response.content); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.dashboard/dashboard.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.dashboard/dashboard.js index 7fcd099e189..c5682c0c5b8 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.dashboard/dashboard.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.dashboard/dashboard.js @@ -39,9 +39,9 @@ function onRequest() { //TODO: Enable Group Management Service API on CDMF //page.group_count = groupModule.getGroupCount(); page.group_count = -1; - page.user_count = userModule.getUsers()["content"].length; - page.policy_count = policyModule.getAllPolicies()["content"].length; - page.role_count = userModule.getRoles()["content"].length; + page.user_count = userModule.getUsersCount(); + page.policy_count = policyModule.getPoliciesCount(); + page.role_count = userModule.getRolesCount(); return page; } \ No newline at end of file