Fix the policy create wizard ui issues

4.x.x
kamidu 8 years ago
parent 9510554f67
commit 160d8c2feb

@ -1,24 +1,21 @@
/* /*
* 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
* in compliance with the License. * in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* 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,118 +28,90 @@ 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" var isUpdated = false;
Here the response.context("Scope validation failed") is used other then response.status(401). var policyListFromRestEndpoint = parse(backendResponse.responseText)["policies"];
Reason for this is IDP return 401 as the status in 4 different situations such as,
1. UnAuthorized. var policyListToView = [];
2. Scope Validation Failed. var i, policyObjectFromRestEndpoint, policyObjectToView;
3. Permission Denied. for (i = 0; i < policyListFromRestEndpoint.length; i++) {
4. Access Token Expired. // get list object
5. Access Token Invalid. policyObjectFromRestEndpoint = policyListFromRestEndpoint[i];
In these cases in order to identify the correct situation we have to compare the unique value from status and // populate list object values to view-object
context which is context. policyObjectToView = {};
*/ policyObjectToView["id"] = policyObjectFromRestEndpoint["id"];
if (responsePayload == "Scope validation failed") { policyObjectToView["priorityId"] = policyObjectFromRestEndpoint["priorityId"];
response.content = "Permission Denied"; policyObjectToView["name"] = policyObjectFromRestEndpoint["policyName"];
} else { policyObjectToView["platform"] = policyObjectFromRestEndpoint["profile"]["deviceType"];
response.content = responsePayload; policyObjectToView["ownershipType"] = policyObjectFromRestEndpoint["ownershipType"];
}
return response; 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] + ", ...";
}
privateMethods.handleGetAllPoliciesSuccess = function (responsePayload) { if (assignedUserCount == 0) {
var isUpdated = false; policyObjectToView["users"] = "None";
var policyListFromRestEndpoint = responsePayload["responseContent"]; } else if (assignedUserCount == 1) {
var policyListToView = []; policyObjectToView["users"] = policyObjectFromRestEndpoint["users"][0];
var i, policyObjectFromRestEndpoint, policyObjectToView; } else if (assignedUserCount > 1) {
for (i = 0; i < policyListFromRestEndpoint.length; i++) { policyObjectToView["users"] = policyObjectFromRestEndpoint["users"][0] + ", ...";
// 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"];
if (policyObjectFromRestEndpoint["active"] == true && policyObjectFromRestEndpoint["updated"] == true) { policyObjectToView["compliance"] = policyObjectFromRestEndpoint["compliance"];
policyObjectToView["status"] = "Active/Updated";
isUpdated = true; if (policyObjectFromRestEndpoint["active"] == true &&
} else if (policyObjectFromRestEndpoint["active"] == true && policyObjectFromRestEndpoint["updated"] == true) {
policyObjectFromRestEndpoint["updated"] == false) { policyObjectToView["status"] = "Active/Updated";
policyObjectToView["status"] = "Active"; isUpdated = true;
} else if (policyObjectFromRestEndpoint["active"] == false && } else if (policyObjectFromRestEndpoint["active"] == true &&
policyObjectFromRestEndpoint["updated"] == true) { policyObjectFromRestEndpoint["updated"] == false) {
policyObjectToView["status"] = "Inactive/Updated"; policyObjectToView["status"] = "Active";
isUpdated = true; } else if (policyObjectFromRestEndpoint["active"] == false &&
} else if (policyObjectFromRestEndpoint["active"] == false && policyObjectFromRestEndpoint["updated"] == true) {
policyObjectFromRestEndpoint["updated"] == false) { policyObjectToView["status"] = "Inactive/Updated";
policyObjectToView["status"] = "Inactive"; 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 // generate response
policyListToView.push(policyObjectToView); response.updated = isUpdated;
} response.status = "success";
// generate response response.content = policyListToView;
var response = {};
response.updated = isUpdated;
response.status = "success";
response.content = policyListToView;
return response;
};
publicMethods.addPolicy = function (policyName, deviceType, policyDefinition, policyDescription, return response;
deviceId) { } else {
var carbonUser = session.get(constants["USER_SESSION_KEY"]); response.status = "error";
if (!carbonUser) { /* backendResponse.responseText == "Scope validation failed"
log.error("User object was not found in the session"); Here the response.context("Scope validation failed") is used other then response.status(401).
throw constants["ERRORS"]["USER_NOT_FOUND"]; Reason for this is IDP return 401 as the status in 4 different situations such as,
} 1. UnAuthorized.
if (policyName && deviceType) { 2. Scope Validation Failed.
var queName = "WSO2IoTServer/" + carbonUser.username + "/" + deviceType; 3. Permission Denied.
var deviceQueName; 4. Access Token Expired.
if (deviceId) { 5. Access Token Invalid.
deviceQueName = queName + "/" + deviceId; In these cases in order to identify the correct situation we have to compare the unique value from status and
privateMethods.publish(deviceQueName, policyName, deviceType, policyDefinition); context which is context.
*/
if (backendResponse.responseText == "Scope validation failed") {
response.content = "Permission Denied";
} else { } else {
var deviceManagementService = utility.getDeviceManagementService(); response.content = backendResponse.responseText;
var devices = deviceManagementService.getDevicesOfUser(carbonUser.username);
var device;
for (var i = 0; i < devices.size(); i++) {
device = devices.get(i);
deviceId = device.getDeviceIdentifier();
deviceQueName = queName + "/" + deviceId;
privateMethods.publish(deviceQueName, policyName, deviceType, policyDefinition);
}
} }
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