Fix the policy create wizard ui issues

merge-requests/7/head
kamidu 8 years ago
parent 9510554f67
commit 160d8c2feb

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
* *
* WSO2 Inc. licenses this file to you under the Apache License, * WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except * Version 2.0 (the "License"); you may not use this file except
@ -10,15 +10,12 @@
* *
* Unless required by applicable law or agreed to in writing, * Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an * software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* KIND, either express or implied. See the License for the * either express or implied. See the License for the
* specific language governing permissions and limitations * specific language governing permissions and limitations
* under the License. * under the License.
*/ */
/*
@Refactored
*/
var policyModule; var policyModule;
policyModule = function () { policyModule = function () {
var log = new Log("/app/modules/business-controllers/policy.js"); var log = new Log("/app/modules/business-controllers/policy.js");
@ -31,31 +28,12 @@ policyModule = function () {
var publicMethods = {}; var publicMethods = {};
var privateMethods = {}; var privateMethods = {};
privateMethods.handleGetAllPoliciesError = function (responsePayload) { privateMethods.handleGetAllPoliciesResponse = function (backendResponse) {
var response = {}; var response = {};
response.status = "error"; if (backendResponse.status == 200 && backendResponse.responseText) {
/* 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;
};
privateMethods.handleGetAllPoliciesSuccess = function (responsePayload) {
var isUpdated = false; var isUpdated = false;
var policyListFromRestEndpoint = responsePayload["responseContent"]; var policyListFromRestEndpoint = parse(backendResponse.responseText)["policies"];
var policyListToView = []; var policyListToView = [];
var i, policyObjectFromRestEndpoint, policyObjectToView; var i, policyObjectFromRestEndpoint, policyObjectToView;
for (i = 0; i < policyListFromRestEndpoint.length; i++) { for (i = 0; i < policyListFromRestEndpoint.length; i++) {
@ -66,16 +44,32 @@ policyModule = function () {
policyObjectToView["id"] = policyObjectFromRestEndpoint["id"]; policyObjectToView["id"] = policyObjectFromRestEndpoint["id"];
policyObjectToView["priorityId"] = policyObjectFromRestEndpoint["priorityId"]; policyObjectToView["priorityId"] = policyObjectFromRestEndpoint["priorityId"];
policyObjectToView["name"] = policyObjectFromRestEndpoint["policyName"]; policyObjectToView["name"] = policyObjectFromRestEndpoint["policyName"];
policyObjectToView["platform"] = policyObjectFromRestEndpoint["profile"]["deviceType"]["name"]; policyObjectToView["platform"] = policyObjectFromRestEndpoint["profile"]["deviceType"];
policyObjectToView["icon"] = utility.getDeviceThumb(policyObjectToView["platform"]);
policyObjectToView["ownershipType"] = policyObjectFromRestEndpoint["ownershipType"]; policyObjectToView["ownershipType"] = policyObjectFromRestEndpoint["ownershipType"];
policyObjectToView["roles"] = privateMethods.
getElementsInAString(policyObjectFromRestEndpoint["roles"]); var assignedRoleCount = policyObjectFromRestEndpoint["roles"].length;
policyObjectToView["users"] = privateMethods. var assignedUserCount = policyObjectFromRestEndpoint["users"].length;
getElementsInAString(policyObjectFromRestEndpoint["users"]);
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] + ", ...";
}
policyObjectToView["compliance"] = policyObjectFromRestEndpoint["compliance"]; policyObjectToView["compliance"] = policyObjectFromRestEndpoint["compliance"];
if (policyObjectFromRestEndpoint["active"] == true && policyObjectFromRestEndpoint["updated"] == true) { if (policyObjectFromRestEndpoint["active"] == true &&
policyObjectFromRestEndpoint["updated"] == true) {
policyObjectToView["status"] = "Active/Updated"; policyObjectToView["status"] = "Active/Updated";
isUpdated = true; isUpdated = true;
} else if (policyObjectFromRestEndpoint["active"] == true && } else if (policyObjectFromRestEndpoint["active"] == true &&
@ -93,56 +87,31 @@ policyModule = function () {
policyListToView.push(policyObjectToView); policyListToView.push(policyObjectToView);
} }
// generate response // generate response
var response = {};
response.updated = isUpdated; response.updated = isUpdated;
response.status = "success"; response.status = "success";
response.content = policyListToView; response.content = policyListToView;
return response;
};
publicMethods.addPolicy = function (policyName, deviceType, policyDefinition, policyDescription, return response;
deviceId) {
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"];
}
if (policyName && deviceType) {
var queName = "WSO2IoTServer/" + carbonUser.username + "/" + deviceType;
var deviceQueName;
if (deviceId) {
deviceQueName = queName + "/" + deviceId;
privateMethods.publish(deviceQueName, policyName, deviceType, policyDefinition);
} else { } else {
var deviceManagementService = utility.getDeviceManagementService(); response.status = "error";
var devices = deviceManagementService.getDevicesOfUser(carbonUser.username); /* backendResponse.responseText == "Scope validation failed"
var device; Here the response.context("Scope validation failed") is used other then response.status(401).
for (var i = 0; i < devices.size(); i++) { Reason for this is IDP return 401 as the status in 4 different situations such as,
device = devices.get(i); 1. UnAuthorized.
deviceId = device.getDeviceIdentifier(); 2. Scope Validation Failed.
deviceQueName = queName + "/" + deviceId; 3. Permission Denied.
privateMethods.publish(deviceQueName, policyName, deviceType, policyDefinition); 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 true; return response;
} }
return false;
};
privateMethods.publish = function (queName, policyName, deviceType, policyDefinition) {
var configurationService = utility.getConfigurationService();
var mqttEndPointDeviceConfig = configurationService.getControlQueue(constants.MQTT_QUEUE_CONFIG_NAME);
var mqttBrokerURL = mqttEndPointDeviceConfig.getServerURL();
var mqttBrokerPort = mqttEndPointDeviceConfig.getPort();
var mqttQueueEndpoint = mqttBrokerURL + ":" + mqttBrokerPort;
var mqttsenderClass = Packages.org.wso2.carbon.device.mgt.iot.mqtt.PolicyPush;
var mqttsender = new mqttsenderClass();
var policyPayload = "POLICY:" + policyDefinition;
var result = mqttsender.pushToMQTT(queName, policyPayload, mqttQueueEndpoint, "MQTT_Agent");
mqttsender = null;
return result;
}; };
/* /*
@ -155,9 +124,9 @@ policyModule = function () {
throw constants["ERRORS"]["USER_NOT_FOUND"]; throw constants["ERRORS"]["USER_NOT_FOUND"];
} }
try { try {
var url = devicemgtProps["httpsURL"] + constants.ADMIN_SERVICE_CONTEXT + "/policies"; var url = devicemgtProps["httpsURL"] + devicemgtProps["backendRestEndpoints"]["deviceMgt"] +
return serviceInvokers.XMLHttp. "/policies?offset=0&limit=100";
get(url, privateMethods.handleGetAllPoliciesSuccess, privateMethods.handleGetAllPoliciesError); return serviceInvokers.XMLHttp.get(url, privateMethods.handleGetAllPoliciesResponse);
} catch (e) { } catch (e) {
throw e; throw e;
} }
@ -178,97 +147,5 @@ policyModule = function () {
return elementsInAString; return elementsInAString;
}; };
/*
@Deprecated
*/
publicMethods.getProfiles = 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 policyManagementService = utility.getPolicyManagementService();
var policyAdminPoint = policyManagementService.getPAP();
var profiles = policyAdminPoint.getProfiles();
var profileList = [];
var i, profile, profileObject;
for (i = 0; i < profiles.size(); i++) {
profile = profiles.get(i);
profileObject = {};
profileObject.name = profile.getProfileName();
profileObject.id = profile.getProfileId();
profileList.push(profileObject);
}
return profileList;
} catch (e) {
throw e;
} finally {
utility.endTenantFlow();
}
};
/*
@Deprecated
*/
publicMethods.updatePolicyPriorities = function (payload) {
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 {
utility.startTenantFlow(carbonUser);
var policyManagementService = utility.getPolicyManagementService();
var policyAdminPoint = policyManagementService.getPAP();
var policyCount = payload.length;
var policyList = new java.util.ArrayList();
var i, policyObject;
for (i = 0; i < policyCount; i++) {
policyObject = new Policy();
policyObject.setId(payload[i].id);
policyObject.setPriorityId(payload[i].priority);
policyList.add(policyObject);
}
policyAdminPoint.updatePolicyPriorities(policyList);
} catch (e) {
throw e;
} finally {
utility.endTenantFlow();
}
};
/*
@Deprecated
*/
publicMethods.deletePolicy = function (policyId) {
var isDeleted;
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 policyManagementService = utility.getPolicyManagementService();
var policyAdminPoint = policyManagementService.getPAP();
isDeleted = policyAdminPoint.deletePolicy(policyId);
if (isDeleted) {
// http status code 200 refers to - success.
return 200;
} else {
// http status code 409 refers to - conflict.
return 409;
}
} catch (e) {
throw e;
} finally {
utility.endTenantFlow();
}
};
return publicMethods; return publicMethods;
}(); }();

@ -28,7 +28,9 @@ function onRequest(context) {
var typesListResponse = userModule.getPlatforms(); var typesListResponse = userModule.getPlatforms();
if (typesListResponse["status"] == "success") { if (typesListResponse["status"] == "success") {
for (var type in typesListResponse["content"]) { for (var type in typesListResponse["content"]) {
var deviceType = typesListResponse["content"][type]["name"]; var content = {};
var deviceType = typesListResponse["content"]["deviceTypes"][type];
content["name"] = deviceType;
var configs = utility.getDeviceTypeConfig(deviceType); var configs = utility.getDeviceTypeConfig(deviceType);
var deviceTypeLabel = deviceType; var deviceTypeLabel = deviceType;
if (configs && configs[DTYPE_CONF_DEVICE_TYPE_KEY][DTYPE_CONF_DEVICE_TYPE_LABEL_KEY]) { if (configs && configs[DTYPE_CONF_DEVICE_TYPE_KEY][DTYPE_CONF_DEVICE_TYPE_LABEL_KEY]) {
@ -36,9 +38,9 @@ function onRequest(context) {
} }
var policyWizard = new File("/app/units/" + utility.getTenantedDeviceUnitName(deviceType, "policy-wizard")); var policyWizard = new File("/app/units/" + utility.getTenantedDeviceUnitName(deviceType, "policy-wizard"));
if(policyWizard.isExists()){ if(policyWizard.isExists()){
typesListResponse["content"][type]["icon"] = utility.getDeviceThumb(deviceType); content["icon"] = utility.getDeviceThumb(deviceType);
typesListResponse["content"][type]["label"] = deviceTypeLabel; content["label"] = deviceTypeLabel;
types["types"].push(typesListResponse["content"][type]); types["types"].push(content);
} }
} }
} }

Loading…
Cancel
Save