IOTS-296: Adding group selection capability when adding a policy

This commit introduces the device group select capabilty and saving the policy when creating and editing policies.
revert-70aa11f8
Madawa Soysa 8 years ago
parent a4ca74c6c0
commit c61cd484a0

@ -21,6 +21,7 @@ package org.wso2.carbon.device.mgt.jaxrs.beans;
import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.policy.mgt.common.DeviceGroupWrapper;
import javax.validation.constraints.Size; import javax.validation.constraints.Size;
import java.util.List; import java.util.List;
@ -75,6 +76,10 @@ public class PolicyWrapper {
required = true) required = true)
private List<String> users; private List<String> users;
@ApiModelProperty(name = "deviceGroups", value = "Lists out the groups on whose devices the policy is enforced",
required = true)
private List<DeviceGroupWrapper> deviceGroups;
public Profile getProfile() { public Profile getProfile() {
return profile; return profile;
} }
@ -147,4 +152,12 @@ public class PolicyWrapper {
this.users = users; this.users = users;
} }
public List<DeviceGroupWrapper> getDeviceGroups() {
return deviceGroups;
}
public void setDeviceGroups(List<DeviceGroupWrapper> deviceGroups) {
this.deviceGroups = deviceGroups;
}
} }

@ -122,6 +122,7 @@ public class PolicyManagementServiceImpl implements PolicyManagementService {
policy.setRoles(policyWrapper.getRoles()); policy.setRoles(policyWrapper.getRoles());
policy.setUsers(policyWrapper.getUsers()); policy.setUsers(policyWrapper.getUsers());
policy.setCompliance(policyWrapper.getCompliance()); policy.setCompliance(policyWrapper.getCompliance());
policy.setDeviceGroups(policyWrapper.getDeviceGroups());
//TODO iterates the device identifiers to create the object. need to implement a proper DAO layer here. //TODO iterates the device identifiers to create the object. need to implement a proper DAO layer here.
List<Device> devices = new ArrayList<Device>(); List<Device> devices = new ArrayList<Device>();
List<DeviceIdentifier> deviceIdentifiers = policyWrapper.getDeviceIdentifiers(); List<DeviceIdentifier> deviceIdentifiers = policyWrapper.getDeviceIdentifiers();

@ -79,4 +79,30 @@ var groupModule = {};
); );
}; };
groupModule.getGroups = function () {
var permissions = userModule.getUIPermissions();
if (permissions.LIST_ALL_GROUPS) {
endPoint = deviceServiceEndpoint + "/admin/groups";
} else if (permissions.LIST_GROUPS) {
endPoint = deviceServiceEndpoint + "/groups";
} else {
log.error("Access denied for user: " + carbonUser.username);
return -1;
}
return serviceInvokers.XMLHttp.get(
endPoint, function (responsePayload) {
var data = JSON.parse(responsePayload.responseText);
if(data) {
return data.deviceGroups;
} else {
return [];
}
},
function (responsePayload) {
log.error(responsePayload);
return -1;
}
);
};
}(groupModule)); }(groupModule));

@ -1,5 +1,7 @@
{{#zone "content"}} {{#zone "content"}}
{{#if isAuthorized}} {{#if isAuthorized}}
<span id="logged-in-user" class="hidden" data-username="{{user.username}}" data-domain="{{user.domain}}"
data-tenant-id="{{user.tenantId}}"></span>
<div class="row"> <div class="row">
<div class="col-md-12"> <div class="col-md-12">
@ -188,6 +190,26 @@
</div> </div>
</div> </div>
{{/if}} {{/if}}
{{#if isAuthorizedViewGroups}}
<label class="wr-input-label">
Select Groups
</label>
<div id="groups-select-field" class="select-users">
<div class="wr-input-control">
<div class="cus-col-50">
<!--suppress HtmlFormInputWithoutLabel -->
<select id="groups-input" class="form-control select2"
multiple="multiple">
<option value="NONE" selected>NONE</option>
{{#each groups}}
<option>{{this.name}}</option>
{{/each}}
</select>
</div>
<br class="c-both"/>
</div>
</div>
{{/if}}
<br> <br>
<label class="wr-input-label" title=""> <label class="wr-input-label" title="">
Set an action upon non-compliance Set an action upon non-compliance

@ -23,12 +23,15 @@ function onRequest(context) {
var utility = require("/app/modules/utility.js").utility; var utility = require("/app/modules/utility.js").utility;
var userModule = require("/app/modules/business-controllers/user.js")["userModule"]; var userModule = require("/app/modules/business-controllers/user.js")["userModule"];
var deviceModule = require("/app/modules/business-controllers/device.js")["deviceModule"]; var deviceModule = require("/app/modules/business-controllers/device.js")["deviceModule"];
var groupModule = require("/app/modules/business-controllers/group.js")["groupModule"];
var types = {}; var types = {};
types.isAuthorized = userModule.isAuthorized("/permission/admin/device-mgt/policies/manage"); types.isAuthorized = userModule.isAuthorized("/permission/admin/device-mgt/policies/manage");
types.isAuthorizedViewUsers = userModule.isAuthorized("/permission/admin/device-mgt/roles/view"); types.isAuthorizedViewUsers = userModule.isAuthorized("/permission/admin/device-mgt/roles/view");
types.isAuthorizedViewRoles = userModule.isAuthorized("/permission/admin/device-mgt/users/view"); types.isAuthorizedViewRoles = userModule.isAuthorized("/permission/admin/device-mgt/users/view");
types.isAuthorizedViewGroups = userModule.isAuthorized("/permission/admin/device-mgt/groups/view");
types["types"] = []; types["types"] = [];
var typesListResponse = deviceModule.getDeviceTypes(); var typesListResponse = deviceModule.getDeviceTypes();
if (typesListResponse["status"] == "success") { if (typesListResponse["status"] == "success") {
for (var type in typesListResponse["content"]["deviceTypes"]) { for (var type in typesListResponse["content"]["deviceTypes"]) {
@ -48,5 +51,14 @@ function onRequest(context) {
} }
} }
} }
var user = userModule.getCarbonUser();
types["user"] = {username: user.username, domain: user.domain, tenantId: user.tenantId};
var roles = userModule.getRoles();
if (roles["status"] == "success") {
types["roles"] = roles["content"];
}
types["groups"] = groupModule.getGroups();
return types; return types;
} }

@ -59,6 +59,35 @@ var disableInlineError = function (inputField, errorMsg, errorSign) {
} }
}; };
function loadGroups(callback) {
invokerUtil.get(
"/api/device-mgt/v1.0/groups",
function (data) {
data = JSON.parse(data);
callback(data.deviceGroups);
});
}
var createDeviceGroupWrapper = function (selectedGroups) {
var groupObjects = [];
loadGroups(function (deviceGroups) {
var tenantId = $("#logged-in-user").data("tenant-id");
for (var index in deviceGroups) {
if(deviceGroups.hasOwnProperty(index)) {
var deviceGroupWrapper = {};
if (selectedGroups.indexOf(deviceGroups[index].name) > -1) {
deviceGroupWrapper.id = deviceGroups[index].id;
deviceGroupWrapper.name = deviceGroups[index].name;
deviceGroupWrapper.owner = deviceGroups[index].owner;
deviceGroupWrapper.tenantId = tenantId;
groupObjects.push(deviceGroupWrapper);
}
}
}
});
return groupObjects;
};
/** /**
*clear inline validation messages. *clear inline validation messages.
*/ */
@ -161,6 +190,11 @@ stepForwardFrom["policy-criteria"] = function () {
} }
} }
}); });
policy["selectedGroups"] = $("#groups-input").val();
if (policy["selectedGroups"].length > 1 || policy["selectedGroups"][0] !== "NONE") {
policy["selectedGroups"] = createDeviceGroupWrapper(policy["selectedGroups"]);
}
policy["selectedNonCompliantAction"] = $("#action-input").find(":selected").data("action"); policy["selectedNonCompliantAction"] = $("#action-input").find(":selected").data("action");
policy["selectedOwnership"] = $("#ownership-input").val(); policy["selectedOwnership"] = $("#ownership-input").val();
//updating next-page wizard title with selected platform //updating next-page wizard title with selected platform
@ -347,6 +381,10 @@ var savePolicy = function (policy, isActive, serviceURL) {
payload["roles"] = []; payload["roles"] = [];
} }
if(policy["selectedGroups"]) {
payload["deviceGroups"] = policy["selectedGroups"];
}
invokerUtil.post( invokerUtil.post(
serviceURL, serviceURL,
payload, payload,
@ -461,6 +499,16 @@ $(document).ready(function () {
} }
}); });
$("#groups-input").select2({
"tags": false
}).on("select2:select", function (e) {
if (e.params.data.id == "NONE") {
$(this).val("NONE").trigger("change");
} else {
$("option[value=NONE]", this).prop("selected", false).parent().trigger("change");
}
});
//Policy wizard stepper //Policy wizard stepper
$(".wizard-stepper").click(function () { $(".wizard-stepper").click(function () {
// button clicked here can be either a continue button or a back button. // button clicked here can be either a continue button or a back button.

@ -1,5 +1,7 @@
{{#zone "content"}} {{#zone "content"}}
{{#if isAuthorized }} {{#if isAuthorized }}
<span id="logged-in-user" class="hidden" data-username="{{user.username}}" data-domain="{{user.domain}}"
data-tenant-id="{{user.tenantId}}"></span>
<div class="row"> <div class="row">
<div class="col-md-12"> <div class="col-md-12">
@ -135,46 +137,72 @@
<br class="c-both"/> <br class="c-both"/>
</div> </div>
<div class="wr-input-control"> <div class="wr-input-control">
<label class="wr-input-control radio light"> {{#if isAuthorizedViewRoles}}
<input id="user-roles-radio-btn" type="radio" name="select-users-radio-btn" <label class="wr-input-control radio light">
class="select-users-radio" checked/> <input id="user-roles-radio-btn" type="radio"
<span class="helper">Set user role(s)</span> name="select-users-radio-btn"
</label> class="select-users-radio" checked/>
<label class="wr-input-control radio light" rel="assetfilter"> <span class="helper">Set user role(s)</span>
<input id="users-radio-btn" type="radio" name="select-users-radio-btn" </label>
class="select-users-radio"/> {{/if}}
<span class="helper">Set user(s)</span> {{#if isAuthorizedViewUsers}}
</label> <label class="wr-input-control radio light" rel="assetfilter">
<input id="users-radio-btn" type="radio" name="select-users-radio-btn"
class="select-users-radio"/>
<span class="helper">Set user(s)</span>
</label>
{{/if}}
</div> </div>
<div id="user-roles-select-field" class="select-users"> {{#if isAuthorizedViewRoles}}
<div class="wr-input-control"> <div id="user-roles-select-field" class="select-users">
<div class="cus-col-50"> <div class="wr-input-control">
<select id="user-roles-input" class="form-control select2" <div class="cus-col-50">
multiple="multiple"> <!--suppress HtmlFormInputWithoutLabel -->
<option value="ANY" selected>ANY</option> <select id="user-roles-input" class="form-control select2"
{{#each roles}} multiple="multiple">
<option>{{this}}</option> <option value="ANY" selected>ANY</option>
{{/each}} {{#each roles}}
</select> <option>{{this}}</option>
{{/each}}
</select>
</div>
<br class="c-both"/>
</div> </div>
<br class="c-both"/>
</div> </div>
</div> {{/if}}
<div id="users-select-field" class="select-users"> {{#if isAuthorizedViewUsers}}
<div class="wr-input-control"> <div id="users-select-field" class="select-users">
<div class="cus-col-50"> <div class="wr-input-control">
<select id="users-input" class="form-control select2" <div class="cus-col-50">
multiple="multiple"> <!--suppress HtmlFormInputWithoutLabel -->
<option value="ANY" selected>ANY</option> <select id="users-input" class="form-control select2"
{{#each users}} multiple="multiple">
<option>{{username}}</option> </select>
{{/each}} </div>
</select> <br class="c-both"/>
</div> </div>
<br class="c-both"/>
</div> </div>
</div> {{/if}}
<br> {{#if isAuthorizedViewGroups}}
<label class="wr-input-label">
Select Groups
</label>
<div id="groups-select-field" class="select-users">
<div class="wr-input-control">
<div class="cus-col-50">
<!--suppress HtmlFormInputWithoutLabel -->
<select id="groups-input" class="form-control select2"
multiple="multiple">
<option value="NONE" selected>NONE</option>
{{#each groups}}
<option>{{this.name}}</option>
{{/each}}
</select>
</div>
<br class="c-both"/>
</div>
</div>
{{/if}}
<label class="wr-input-label" title=""> <label class="wr-input-label" title="">
Set an action upon non-compliance Set an action upon non-compliance
</label> </label>

@ -18,9 +18,9 @@
function onRequest(context) { function onRequest(context) {
var log = new Log("policy-view-edit-unit backend js"); var log = new Log("policy-view-edit-unit backend js");
log.debug("calling policy-view-edit-unit");
var userModule = require("/app/modules/business-controllers/user.js")["userModule"]; var userModule = require("/app/modules/business-controllers/user.js")["userModule"];
var groupModule = require("/app/modules/business-controllers/group.js")["groupModule"];
var rolesResult = userModule.getRoles(); var rolesResult = userModule.getRoles();
if (rolesResult.status == "success") { if (rolesResult.status == "success") {
@ -32,9 +32,15 @@ function onRequest(context) {
context.users = usersResult.content; context.users = usersResult.content;
} }
context["groups"] = groupModule.getGroups();
var user = userModule.getCarbonUser();
context["user"] = {username: user.username, domain: user.domain, tenantId: user.tenantId};
context.isAuthorized = userModule.isAuthorized("/permission/admin/device-mgt/policies/manage"); context.isAuthorized = userModule.isAuthorized("/permission/admin/device-mgt/policies/manage");
context.isAuthorizedViewUsers = userModule.isAuthorized("/permission/admin/device-mgt/roles/view"); context.isAuthorizedViewUsers = userModule.isAuthorized("/permission/admin/device-mgt/roles/view");
context.isAuthorizedViewRoles = userModule.isAuthorized("/permission/admin/device-mgt/users/view"); context.isAuthorizedViewRoles = userModule.isAuthorized("/permission/admin/device-mgt/users/view");
context.isAuthorizedViewGroups = userModule.isAuthorized("/permission/admin/device-mgt/groups/view");
return context; return context;
} }

@ -63,6 +63,35 @@ var disableInlineError = function (inputField, errorMsg, errorSign) {
} }
}; };
function loadGroups(callback) {
invokerUtil.get(
"/api/device-mgt/v1.0/groups",
function (data) {
data = JSON.parse(data);
callback(data.deviceGroups);
});
}
var createDeviceGroupWrapper = function (selectedGroups) {
var groupObjects = [];
loadGroups(function (deviceGroups) {
var tenantId = $("#logged-in-user").data("tenant-id");
for (var index in deviceGroups) {
if(deviceGroups.hasOwnProperty(index)) {
var deviceGroupWrapper = {};
if (selectedGroups.indexOf(deviceGroups[index].name) > -1) {
deviceGroupWrapper.id = deviceGroups[index].id;
deviceGroupWrapper.name = deviceGroups[index].name;
deviceGroupWrapper.owner = deviceGroups[index].owner;
deviceGroupWrapper.tenantId = tenantId;
groupObjects.push(deviceGroupWrapper);
}
}
}
});
return groupObjects;
};
/** /**
*clear inline validation messages. *clear inline validation messages.
*/ */
@ -96,12 +125,14 @@ skipStep["policy-platform"] = function (policyPayloadObj) {
var userRoleInput = $("#user-roles-input"); var userRoleInput = $("#user-roles-input");
var ownershipInput = $("#ownership-input"); var ownershipInput = $("#ownership-input");
var userInput = $("#users-input"); var userInput = $("#users-input");
var groupsInput = $("#groups-input");
var actionInput = $("#action-input"); var actionInput = $("#action-input");
var policyNameInput = $("#policy-name-input"); var policyNameInput = $("#policy-name-input");
var policyDescriptionInput = $("#policy-description-input"); var policyDescriptionInput = $("#policy-description-input");
currentlyEffected["roles"] = policyPayloadObj.roles; currentlyEffected["roles"] = policyPayloadObj.roles;
currentlyEffected["users"] = policyPayloadObj.users; currentlyEffected["users"] = policyPayloadObj.users;
currentlyEffected["groups"] = policyPayloadObj.deviceGroups;
if (currentlyEffected["roles"].length > 0) { if (currentlyEffected["roles"].length > 0) {
$("#user-roles-radio-btn").prop("checked", true); $("#user-roles-radio-btn").prop("checked", true);
@ -114,6 +145,10 @@ skipStep["policy-platform"] = function (policyPayloadObj) {
$("#user-roles-select-field").hide(); $("#user-roles-select-field").hide();
userInput.val(currentlyEffected["users"]).trigger("change"); userInput.val(currentlyEffected["users"]).trigger("change");
} }
if(currentlyEffected["groups"].length > 0) {
groupsInput.val(currentlyEffected["groups"]).trigger("change");
}
ownershipInput.val(policyPayloadObj.ownershipType); ownershipInput.val(policyPayloadObj.ownershipType);
actionInput.val(policyPayloadObj.compliance); actionInput.val(policyPayloadObj.compliance);
@ -186,6 +221,11 @@ stepForwardFrom["policy-criteria"] = function () {
} }
} }
}); });
policy["selectedGroups"] = $("#groups-input").val();
if (policy["selectedGroups"].length > 1 || policy["selectedGroups"][0] !== "NONE") {
policy["selectedGroups"] = createDeviceGroupWrapper(policy["selectedGroups"]);
}
policy["selectedNonCompliantAction"] = $("#action-input").find(":selected").data("action"); policy["selectedNonCompliantAction"] = $("#action-input").find(":selected").data("action");
policy["selectedOwnership"] = $("#ownership-input").val(); policy["selectedOwnership"] = $("#ownership-input").val();
// updating next-page wizard title with selected platform // updating next-page wizard title with selected platform
@ -382,6 +422,10 @@ var updatePolicy = function (policy, state) {
payload["roles"] = []; payload["roles"] = [];
} }
if(policy["selectedGroups"]) {
payload["deviceGroups"] = policy["selectedGroups"];
}
var serviceURL = "/api/device-mgt/v1.0/policies/" + getParameterByName("id"); var serviceURL = "/api/device-mgt/v1.0/policies/" + getParameterByName("id");
invokerUtil.put( invokerUtil.put(
serviceURL, serviceURL,
@ -476,6 +520,16 @@ $(document).ready(function () {
} }
}); });
$("#groups-input").select2({
"tags": false
}).on("select2:select", function (e) {
if (e.params.data.id == "NONE") {
$(this).val("NONE").trigger("change");
} else {
$("option[value=NONE]", this).prop("selected", false).parent().trigger("change");
}
});
$("#users-input").select2({ $("#users-input").select2({
"tags": false "tags": false
}).on("select2:select", function (e) { }).on("select2:select", function (e) {

Loading…
Cancel
Save