adding policies

merge-requests/1/head
Rasika Perera 9 years ago
parent 2b9ebf1e2f
commit 43a756d851

@ -30,44 +30,31 @@ var result;
if (uriMatcher.match("/{context}/api/policies/update")) { if (uriMatcher.match("/{context}/api/policies/update")) {
payload = request.getContent(); payload = request.getContent();
policyModule.updatePolicyPriorities(payload); policyModule.updatePolicyPriorities(payload);
} else if (uriMatcher.match("/{context}/api/policies/{id}/delete")) {
elements = uriMatcher.elements();
policyId = elements.id;
try {
result = policyModule.deletePolicy(policyId);
} catch (e) {
log.error("Exception occurred while trying to delete policy for id:" + policyId, e);
// http status code 500 refers to - Internal Server Error.
result = 500;
}
}else if (uriMatcher.match("/{context}/api/policies/update")) {
payload = request.getContent();
policyModule.updatePolicyPriorities(payload);
} else if (uriMatcher.match("/{context}/api/policies/add")) { } else if (uriMatcher.match("/{context}/api/policies/add")) {
var content = request.getContent(); var content = request.getContent();
var id = content.policyName; var id = content.policyName;
var policyDefinition = content.profile.policyDefinition; var policyDefinition = content.profile.policyDefinition;
var deviceType = content.profile.deviceType.id; var policyDescription = content.profile.policyDescription;
var deviceType = content.profile.deviceType.name;
log.info("@@@@@ Policy Declaration : " + stringify(content)); log.info("@@@@@ Policy Declaration : " + stringify(content));
try { try {
result = policyModule.addPolicy(id,deviceType,policyDefinition); result = policyModule.addPolicy(id, deviceType, policyDefinition, policyDescription);
} catch (e) { } catch (e) {
log.error("Exception occurred while trying to add new policy under name:" + id, e); log.error("Exception occurred while trying to add new policy under name:" + id, e);
// http status code 500 refers to - Internal Server Error. // http status code 500 refers to - Internal Server Error.
result = 500; result = 500;
} }
} else if (uriMatcher.match("/{context}/api/policies/get")) { } else if (uriMatcher.match("/{context}/api/policies/{deviceType}/{policyName}/remove")) {
var id = request.getParameter("policyName"); elements = uriMatcher.elements();
var deviceType = request.getParameter("deviceTypeId"); var id = elements.policyName;
var deviceType = elements.deviceType;
log.info("@@@@@ Policy Declaration : "+stringify(content));
try { try {
result = policyModule.getPolicy(id,deviceType); result = policyModule.removePolicy(id, deviceType);
} catch (e) { } catch (e) {
log.error("Exception occurred while trying to add new policy under name:" + id, e); log.error("Exception occurred while trying to remove policy under name:" + id, e);
// http status code 500 refers to - Internal Server Error. // http status code 500 refers to - Internal Server Error.
result = 500; result = 500;
} }

@ -21,6 +21,7 @@ var WEB_APP_CONTEXT = "/iotserver";
var USER_SESSION_KEY = "USER"; var USER_SESSION_KEY = "USER";
var UNSPECIFIED = "Unspecified"; var UNSPECIFIED = "Unspecified";
var DEVICES_UNIT_PATH="/units/"; var DEVICES_UNIT_PATH="/units/";
var POLICY_REGISTRY_PATH="/_system/governance/policy_declarations/";
var DEVICE_IDENTIFIER = "deviceIdentifier"; var DEVICE_IDENTIFIER = "deviceIdentifier";
var DEVICE_NAME = "name"; var DEVICE_NAME = "name";

@ -26,7 +26,7 @@ policyModule = function () {
var publicMethods = {}; var publicMethods = {};
var privateMethods = {}; var privateMethods = {};
publicMethods.addPolicy = function (name, deviceType, policyDefinition) { publicMethods.addPolicy = function (name, deviceType, policyDefinition, policyDescription) {
var carbonModule = require("carbon"); var carbonModule = require("carbon");
var carbonServer = application.get("carbonServer"); var carbonServer = application.get("carbonServer");
var options = {system: true}; var options = {system: true};
@ -35,7 +35,8 @@ policyModule = function () {
resource = { resource = {
name: name, name: name,
mediaType: 'text/plain', mediaType: 'text/plain',
content : policyDefinition content: policyDefinition,
description: policyDescription
}; };
if (carbonUser) { if (carbonUser) {
@ -43,8 +44,9 @@ policyModule = function () {
var registry = new carbonModule.registry.Registry(carbonServer, options); var registry = new carbonModule.registry.Registry(carbonServer, options);
log.info("########### Policy name : " + name); log.info("########### Policy name : " + name);
log.info("########### Policy type : " + deviceType); log.info("########### Policy type : " + deviceType);
log.info("########### Policy Declarationsss : "+policyDefinition); log.info("########### Policy Declaration : " + policyDefinition);
registry.put("/_system/governance/policy_declarations/" + deviceType + "/" + name, resource); log.info("########### Policy policyDescription: " + policyDescription);
registry.put(constants.POLICY_REGISTRY_PATH + deviceType + "/" + name, resource);
} }
var mqttsenderClass = Packages.org.wso2.device.mgt.mqtt.policy.push.MqttPush; var mqttsenderClass = Packages.org.wso2.device.mgt.mqtt.policy.push.MqttPush;
@ -61,59 +63,83 @@ policyModule = function () {
var options = {system: true}; var options = {system: true};
var carbonUser = session.get(constants.USER_SESSION_KEY); var carbonUser = session.get(constants.USER_SESSION_KEY);
var policies = [];
if (carbonUser) { if (carbonUser) {
options.tenantId = carbonUser.tenantId; options.tenantId = carbonUser.tenantId;
var registry = new carbonModule.registry.Registry(carbonServer, options); var registry = new carbonModule.registry.Registry(carbonServer, options);
log.info(registry.get("/_system/governance/policy_declarations/firealarm/")); var allPolicies = registry.get(constants.POLICY_REGISTRY_PATH);
}
if (allPolicies) {
//TODO-This method returns includes dummy policy data
//loop through all device types
var policies = []; for (var i = 0; i < allPolicies.content.length; i++) {
log.info("Policies for device types: " + allPolicies.content);
log.info("");
var deviceType = allPolicies.content[i].replace(constants.POLICY_REGISTRY_PATH, "");
log.info("##### deviceType:"+deviceType);
var deviceTypePolicies = registry.get(allPolicies.content[i]);
//loop through policies
for (var j = 0; j < deviceTypePolicies.content.length; j++) {
log.info("Policies:" + deviceTypePolicies.content);
var deviceTypePolicy = registry.get(deviceTypePolicies.content[j]);
log.info(deviceTypePolicy);
var policyObj = { var policyObj = {
"id":1, // Identifier of the policy. "id": deviceTypePolicy.uuid, // Identifier of the policy.
"priorityId":1, // Priority of the policies. This will be used only for simple evaluation. //"priorityId": 1, // Priority of the policies. This will be used only for simple evaluation.
"profile":{}, // Profile //"profile": {}, // Profile
"policyName":"Turn off light", // Name of the policy. "policyName": deviceTypePolicy.name, // Name of the policy.
"generic":true, // If true, this should be applied to all related device. "updated": deviceTypePolicy.updated.time,
"roles":{}, // Roles which this policy should be applied. "deviceType":deviceType
"ownershipType":{}, // Ownership type (COPE, BYOD, CPE) //"generic": true, // If true, this should be applied to all related device.
"devices":{}, // Individual devices this policy should be applied //"roles": {}, // Roles which this policy should be applied.
"users":{}, // Individual users this policy should be applied //"ownershipType": {}, // Ownership type (COPE, BYOD, CPE)
"Compliance":{}, //"devices": {}, // Individual devices this policy should be applied
"policyCriterias":{}, //"users": {}, // Individual users this policy should be applied
"startTime":283468236, // Start time to apply the policy. //"Compliance": {},
"endTime":283468236, // After this time policy will not be applied //"policyCriterias": {},
"startDate":"", // Start date to apply the policy //"startTime": 283468236, // Start time to apply the policy.
"endDate":"", // After this date policy will not be applied. //"endTime": 283468236, // After this time policy will not be applied
"tenantId":-1234, //"startDate": "", // Start date to apply the policy
"profileId":1 //"endDate": "", // After this date policy will not be applied.
//"tenantId": -1234,
//"profileId": 1
}; };
policies.push(policyObj); policies.push(policyObj);
log.info("");
}//end of policy loop
log.info("");
}//end of device type policy loop
}
}
return policies;
policyObj = {
"id":2, // Identifier of the policy.
"priorityId":1, // Priority of the policies. This will be used only for simple evaluation.
"profile":{}, // Profile
"policyName":"Turn on Buzzer", // Name of the policy.
"generic":false, // If true, this should be applied to all related device.
"roles":{}, // Roles which this policy should be applied.
"ownershipType":{}, // Ownership type (COPE, BYOD, CPE)
"devices":{}, // Individual devices this policy should be applied
"users":{}, // Individual users this policy should be applied
"Compliance":{},
"policyCriterias":{},
"startTime":283468236, // Start time to apply the policy.
"endTime":283468236, // After this time policy will not be applied
"startDate":"", // Start date to apply the policy
"endDate":"", // After this date policy will not be applied.
"tenantId":-1234,
"profileId":2
}; };
policies.push(policyObj); publicMethods.removePolicy = function (name, deviceType) {
return policies; var carbonModule = require("carbon");
var carbonServer = application.get("carbonServer");
var options = {system: true};
var carbonUser = session.get(constants.USER_SESSION_KEY);
var bool = false;
if (carbonUser) {
options.tenantId = carbonUser.tenantId;
var registry = new carbonModule.registry.Registry(carbonServer, options);
log.info("########### Policy name : " + name);
log.info("########### Policy type : " + deviceType);
try {
registry.remove(constants.POLICY_REGISTRY_PATH + deviceType + "/" + name);
bool = true;
}catch(err){
log.error("Error while trying to remove policy :" + name);
}
}
return bool;
}; };
return publicMethods; return publicMethods;

@ -68,7 +68,7 @@
<h1 class="wr-title">Policy creation is successful</h1> <h1 class="wr-title">Policy creation is successful</h1>
Please click <b>"Finish"</b> to complete the process and go back to the policy list Please click <b>"Finish"</b> to complete the process and go back to the policy list
<hr /> <hr />
<button class="wr-btn wizard-stepper" data-current="policy-message" data-direct="/mdm/policies/" onclick="window.location.href=''"> <button class="wr-btn wizard-stepper" data-current="policy-message" data-direct="/iotserver/policies/" onclick="window.location.href='/iotserver/policies/'">
&nbsp;&nbsp;&nbsp;&nbsp;Finish&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;Finish&nbsp;&nbsp;&nbsp;&nbsp;
</button> </button>
@ -92,7 +92,7 @@
</label> </label>
<div class="wr-input-control"> <div class="wr-input-control">
<div class="cus-col-50"> <div class="cus-col-50">
<input id="policy-name-input" type="text" value="" placeholder="input text"/> <input id="policy-name-input" type="text" value="" placeholder="Policy name here" required/>
</div> </div>
<br class="c-both" /> <br class="c-both" />
</div> </div>
@ -103,7 +103,7 @@
</label> </label>
<div class="wr-input-control"> <div class="wr-input-control">
<div class="cus-col-50"> <div class="cus-col-50">
<textarea id="policy-description-input" placeholder="description"></textarea> <textarea id="policy-description-input" placeholder="Policy description here" required></textarea>
</div> </div>
<br class="c-both" /> <br class="c-both" />
</div> </div>
@ -134,7 +134,7 @@
</label> </label>
<div class="wr-input-control"> <div class="wr-input-control">
<div class="cus-col-100"> <div class="cus-col-100">
<textarea id="policy-definition-input" placeholder="Enter the policy" style="width: 100%;"></textarea> <textarea id="policy-definition-input" placeholder="Enter the policy" style="width: 100%;" required></textarea>
</div> </div>
<br class="c-both" /> <br class="c-both" />
</div> </div>

@ -57,9 +57,11 @@ function savePolicy(){
profile: { profile: {
profileName: policy.policyName, profileName: policy.policyName,
deviceType: { deviceType: {
id: policy.devicetypeId id: policy.devicetypeId,
name: policy.devicetype
}, },
policyDefinition: policy.policyDefinition policyDefinition: policy.policyDefinition,
policyDescription: policy.policyDescription
} }
}; };
@ -102,8 +104,6 @@ $(document).ready(function(){
savePolicy(); savePolicy();
}; };
stepperRegistry['policy-profile'] = function (actionButton){ stepperRegistry['policy-profile'] = function (actionButton){
var deviceType = policy.devicetype;
console.log(window.queryEditor.getValue());
policy.policyDefinition = window.queryEditor.getValue(); policy.policyDefinition = window.queryEditor.getValue();
}; };
stepperRegistry['policy-devicetype'] = function (actionButton){ stepperRegistry['policy-devicetype'] = function (actionButton){

@ -4,7 +4,9 @@
<!-- content --> <!-- content -->
<div class="wr-content"> <div class="wr-content">
<p class="page-sub-title">Policies</p> <p class="page-sub-title">Policies</p>
<p>{{listPolicyStatus}}</p> <p>{{listPolicyStatus}}</p>
<div class="wr-list-group wr-sortable"> <div class="wr-list-group wr-sortable">
{{#if policies}} {{#if policies}}
{{#each policies}} {{#each policies}}
@ -13,7 +15,8 @@
<i class="wr-list-icon fw fw-user"></i> <i class="wr-list-icon fw fw-user"></i>
<div class="wr-list-desc"> <div class="wr-list-desc">
<h3 class="wr-list-name">{{policyName}}</h3> <h3 class="wr-list-name">{{policyName}}</h3>
<span class="wr-list-username">{{policyName}}</span> <span class="wr-list-username">{{deviceType}} | Last Updated: <span
class="formatDate">{{updated}}</span></span>
</div> </div>
<span class="list-group-item-actions"> <span class="list-group-item-actions">
<!--a href="#" class="invite-user-link cu-btn-inner" data-policyname="{{policyName}}"> <!--a href="#" class="invite-user-link cu-btn-inner" data-policyname="{{policyName}}">
@ -23,7 +26,10 @@
</span> </span>
Invite Invite
</a--> </a-->
<a href="#" class="remove-user-link cu-btn-inner" data-policyname="{{policyName}}"> <a href="#" class="remove-policy-link cu-btn-inner"
data-devicetype="{{deviceType}}"
data-policyname="{{policyName}}"
data-policyuuid="{{id}}">
<span class="fw-stack"> <span class="fw-stack">
<i class="fw fw-ring fw-stack-2x"></i> <i class="fw fw-ring fw-stack-2x"></i>
<i class="fw fw-delete fw-stack-1x"></i> <i class="fw fw-delete fw-stack-1x"></i>
@ -39,6 +45,7 @@
<div class="container-fluid wr-content-alt"> <div class="container-fluid wr-content-alt">
<div class="ctrl-info-panel col-md-6 col-centered"> <div class="ctrl-info-panel col-md-6 col-centered">
<h2>You don't have any Policies added at the moment.</h2> <h2>You don't have any Policies added at the moment.</h2>
<p> <p>
</a><a href="/iotserver/policies/add-policy" class="cu-btn"> </a><a href="/iotserver/policies/add-policy" class="cu-btn">
<span class="fw-stack"> <span class="fw-stack">
@ -59,6 +66,107 @@
</div> </div>
<div id="remove-policy-modal-content" class="hide">
<div class="content">
<div class="row">
<div class="col-lg-5 col-md-6 col-centered">
<h3>Do you really want to remove this policy from IoT Server?</h3>
<div class="buttons">
<a href="#" id="remove-policy-yes-link" class="btn-operations">
&nbsp;&nbsp;&nbsp;&nbsp;Yes&nbsp;&nbsp;&nbsp;&nbsp;
</a>
&nbsp;&nbsp;
<a href="#" id="remove-policy-cancel-link" class="btn-operations">
&nbsp;&nbsp;&nbsp;&nbsp;Cancel&nbsp;&nbsp;&nbsp;&nbsp;
</a>
</div>
</div>
</div>
</div>
</div>
<div id="remove-policy-200-content" class="hide">
<div class="content">
<div class="row">
<div class="col-lg-5 col-md-6 col-centered">
<h3>Policy was successfully removed.</h3>
<div class="buttons">
<a href="#" id="remove-policy-200-link" class="btn-operations">
&nbsp;&nbsp;&nbsp;&nbsp;Ok&nbsp;&nbsp;&nbsp;&nbsp;
</a>
</div>
</div>
</div>
</div>
</div>
<div id="remove-policy-400-content" class="hide">
<div class="content">
<div class="row">
<div class="col-lg-5 col-md-6 col-centered">
<h3>Exception at backend. Try Later.</h3>
<div class="buttons">
<a href="#" id="remove-policy-400-link" class="btn-operations">
&nbsp;&nbsp;&nbsp;&nbsp;Ok&nbsp;&nbsp;&nbsp;&nbsp;
</a>
</div>
</div>
</div>
</div>
</div>
<div id="remove-policy-403-content" class="hide">
<div class="content">
<div class="row">
<div class="col-lg-5 col-md-6 col-centered">
<h3>Action not permitted.</h3>
<div class="buttons">
<a href="#" id="remove-policy-403-link" class="btn-operations">
&nbsp;&nbsp;&nbsp;&nbsp;Ok&nbsp;&nbsp;&nbsp;&nbsp;
</a>
</div>
</div>
</div>
</div>
</div>
<div id="remove-policy-409-content" class="hide">
<div class="content">
<div class="row">
<div class="col-lg-5 col-md-6 col-centered">
<h3>Policy does not exist.</h3>
<div class="buttons">
<a href="#" id="remove-policy-409-link" class="btn-operations">
&nbsp;&nbsp;&nbsp;&nbsp;Ok&nbsp;&nbsp;&nbsp;&nbsp;
</a>
</div>
</div>
</div>
</div>
</div>
<div id="remove-policy-unexpected-error-content" class="hide">
<div class="content">
<div class="row">
<div class="col-lg-5 col-md-6 col-centered">
<h3>Policy does not exist.</h3>
<div class="buttons">
<a href="#" id="remove-policy-unexpected-error-link" class="btn-operations">
&nbsp;&nbsp;&nbsp;&nbsp;Ok&nbsp;&nbsp;&nbsp;&nbsp;
</a>
</div>
</div>
</div>
</div>
</div>
{{/zone}} {{/zone}}
{{#zone "common-navigation"}} {{#zone "common-navigation"}}

@ -2,6 +2,7 @@ function onRequest(context) {
// var log = new Log("policy-listing"); // var log = new Log("policy-listing");
var policyModule = require("/modules/policy.js").policyModule; var policyModule = require("/modules/policy.js").policyModule;
var allPolicies = policyModule.getPolicies(); var allPolicies = policyModule.getPolicies();
//log.info((allPolicies));
if (!allPolicies || allPolicies.length == 0) { if (!allPolicies || allPolicies.length == 0) {
context.policies = []; context.policies = [];
context.listPolicyStatus = "Oops, Sorry, No other Policies found."; context.listPolicyStatus = "Oops, Sorry, No other Policies found.";
@ -10,6 +11,7 @@ function onRequest(context) {
for (i = 0; i < allPolicies.length; i++) { for (i = 0; i < allPolicies.length; i++) {
filteredPoliciesList.push(allPolicies[i]); filteredPoliciesList.push(allPolicies[i]);
} }
//log.info(filteredPoliciesList.length);
context.policies = filteredPoliciesList; context.policies = filteredPoliciesList;
context.listPolicyStatus = "Total number of Policies found : " + filteredPoliciesList.length; context.listPolicyStatus = "Total number of Policies found : " + filteredPoliciesList.length;
} }

@ -42,90 +42,74 @@ function hidePopup() {
$(modalPopup).hide(); $(modalPopup).hide();
} }
/** $(document).ready(function () {
* Following click function would execute formatDates();
* when a user clicks on "Invite" link
* on User Management page in WSO2 MDM Console.
*/
$("a.invite-user-link").click(function () {
var username = $(this).data("username");
var inviteUserAPI = "/iotserver/api/users/" + username + "/invite";
$(modalPopupContent).html($('#invite-user-modal-content').html());
showPopup();
$("a#invite-user-yes-link").click(function () {
invokerUtil.get(
inviteUserAPI,
function () {
$(modalPopupContent).html($('#invite-user-success-content').html());
$("a#invite-user-success-link").click(function () {
hidePopup();
});
},
function () {
$(modalPopupContent).html($('#invite-user-error-content').html());
$("a#invite-user-error-link").click(function () {
hidePopup();
});
}
);
}); });
$("a#invite-user-cancel-link").click(function () { function formatDates() {
hidePopup(); $(".formatDate").each(function () {
}); var timeStamp = $(this).html();
$(this).html(new Date(parseFloat(timeStamp)).toUTCString());
}); });
}
/** /**
* Following click function would execute * Following click function would execute
* when a user clicks on "Remove" link * when a user clicks on "Remove" link
* on User Management page in WSO2 MDM Console. * on User Management page in WSO2 MDM Console.
*/ */
$("a.remove-user-link").click(function () { $("a.remove-policy-link").click(function () {
var username = $(this).data("username"); var deviceType = $(this).data("devicetype");
var removeUserAPI = "/iotserver/api/users/" + username + "/remove"; var policyName = $(this).data("policyname");
var policyUUID = $(this).data("policyuuid");
///{context}/api/policies/{deviceType}/{policyName}/remove
var removePolicyAPI = "/iotserver/api/policies/" + deviceType + "/" + policyName + "/remove";
$(modalPopupContent).html($('#remove-user-modal-content').html()); $(modalPopupContent).html($('#remove-policy-modal-content').html());
showPopup(); showPopup();
$("a#remove-user-yes-link").click(function () { $("a#remove-policy-yes-link").click(function () {
invokerUtil.get( invokerUtil.get(
removeUserAPI, removePolicyAPI,
function (data) { function (data) {
if (data == 200) { if (data == 200 || data == "true") {
$("#" + username).addClass("hide"); $(modalPopupContent).html($('#remove-policy-200-content').html());
$(modalPopupContent).html($('#remove-user-200-content').html()); $('#' + policyUUID).remove();
$("a#remove-user-200-link").click(function () { $("a#remove-policy-200-link").click(function () {
hidePopup(); hidePopup();
}); });
} else if (data == 400) { } else if (data == 400) {
$(modalPopupContent).html($('#remove-user-400-content').html()); $(modalPopupContent).html($('#remove-policy-400-content').html());
$("a#remove-user-400-link").click(function () { $("a#remove-policy-400-link").click(function () {
hidePopup(); hidePopup();
}); });
} else if (data == 403) { } else if (data == 403) {
$(modalPopupContent).html($('#remove-user-403-content').html()); $(modalPopupContent).html($('#remove-policy-403-content').html());
$("a#remove-user-403-link").click(function () { $("a#remove-policy-403-link").click(function () {
hidePopup();
});
} else if (data == 409 || data == "false") {
$(modalPopupContent).html($('#remove-policy-409-content').html());
$("a#remove-policy-409-link").click(function () {
hidePopup(); hidePopup();
}); });
} else if (data == 409) { } else if (data == 500) {
$(modalPopupContent).html($('#remove-user-409-content').html()); $(modalPopupContent).html($('#remove-policy-unexpected-content').html());
$("a#remove-user-409-link").click(function () { $("a#remove-policy-unexpected-link").click(function () {
hidePopup(); hidePopup();
}); });
} }
}, },
function () { function () {
$(modalPopupContent).html($('#remove-user-unexpected-error-content').html()); $(modalPopupContent).html($('#remove-policy-unexpected-error-content').html());
$("a#remove-user-unexpected-error-link").click(function () { $("a#remove-policy-unexpected-error-link").click(function () {
hidePopup(); hidePopup();
}); });
} }
); );
}); });
$("a#remove-user-cancel-link").click(function () { $("a#remove-policy-cancel-link").click(function () {
hidePopup(); hidePopup();
}); });
}); });
Loading…
Cancel
Save