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.ApiModelProperty;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.policy.mgt.common.DeviceGroupWrapper;
import javax.validation.constraints.Size;
import java.util.List;
@ -75,6 +76,10 @@ public class PolicyWrapper {
required = true)
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() {
return profile;
}
@ -147,4 +152,12 @@ public class PolicyWrapper {
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.setUsers(policyWrapper.getUsers());
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.
List<Device> devices = new ArrayList<Device>();
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));

@ -1,5 +1,7 @@
{{#zone "content"}}
{{#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="col-md-12">
@ -188,6 +190,26 @@
</div>
</div>
{{/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>
<label class="wr-input-label" title="">
Set an action upon non-compliance

@ -23,12 +23,15 @@ function onRequest(context) {
var utility = require("/app/modules/utility.js").utility;
var userModule = require("/app/modules/business-controllers/user.js")["userModule"];
var deviceModule = require("/app/modules/business-controllers/device.js")["deviceModule"];
var groupModule = require("/app/modules/business-controllers/group.js")["groupModule"];
var types = {};
types.isAuthorized = userModule.isAuthorized("/permission/admin/device-mgt/policies/manage");
types.isAuthorizedViewUsers = userModule.isAuthorized("/permission/admin/device-mgt/roles/view");
types.isAuthorizedViewRoles = userModule.isAuthorized("/permission/admin/device-mgt/users/view");
types.isAuthorizedViewGroups = userModule.isAuthorized("/permission/admin/device-mgt/groups/view");
types["types"] = [];
var typesListResponse = deviceModule.getDeviceTypes();
if (typesListResponse["status"] == "success") {
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;
}

@ -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.
*/
@ -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["selectedOwnership"] = $("#ownership-input").val();
//updating next-page wizard title with selected platform
@ -347,6 +381,10 @@ var savePolicy = function (policy, isActive, serviceURL) {
payload["roles"] = [];
}
if(policy["selectedGroups"]) {
payload["deviceGroups"] = policy["selectedGroups"];
}
invokerUtil.post(
serviceURL,
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
$(".wizard-stepper").click(function () {
// button clicked here can be either a continue button or a back button.

@ -1,5 +1,7 @@
{{#zone "content"}}
{{#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="col-md-12">
@ -135,20 +137,27 @@
<br class="c-both"/>
</div>
<div class="wr-input-control">
{{#if isAuthorizedViewRoles}}
<label class="wr-input-control radio light">
<input id="user-roles-radio-btn" type="radio" name="select-users-radio-btn"
<input id="user-roles-radio-btn" type="radio"
name="select-users-radio-btn"
class="select-users-radio" checked/>
<span class="helper">Set user role(s)</span>
</label>
{{/if}}
{{#if isAuthorizedViewUsers}}
<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>
{{#if isAuthorizedViewRoles}}
<div id="user-roles-select-field" class="select-users">
<div class="wr-input-control">
<div class="cus-col-50">
<!--suppress HtmlFormInputWithoutLabel -->
<select id="user-roles-input" class="form-control select2"
multiple="multiple">
<option value="ANY" selected>ANY</option>
@ -160,21 +169,40 @@
<br class="c-both"/>
</div>
</div>
{{/if}}
{{#if isAuthorizedViewUsers}}
<div id="users-select-field" class="select-users">
<div class="wr-input-control">
<div class="cus-col-50">
<!--suppress HtmlFormInputWithoutLabel -->
<select id="users-input" class="form-control select2"
multiple="multiple">
<option value="ANY" selected>ANY</option>
{{#each users}}
<option>{{username}}</option>
</select>
</div>
<br class="c-both"/>
</div>
</div>
{{/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>
<br>
{{/if}}
<label class="wr-input-label" title="">
Set an action upon non-compliance
</label>

@ -18,9 +18,9 @@
function onRequest(context) {
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 groupModule = require("/app/modules/business-controllers/group.js")["groupModule"];
var rolesResult = userModule.getRoles();
if (rolesResult.status == "success") {
@ -32,9 +32,15 @@ function onRequest(context) {
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.isAuthorizedViewUsers = userModule.isAuthorized("/permission/admin/device-mgt/roles/view");
context.isAuthorizedViewRoles = userModule.isAuthorized("/permission/admin/device-mgt/users/view");
context.isAuthorizedViewGroups = userModule.isAuthorized("/permission/admin/device-mgt/groups/view");
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.
*/
@ -96,12 +125,14 @@ skipStep["policy-platform"] = function (policyPayloadObj) {
var userRoleInput = $("#user-roles-input");
var ownershipInput = $("#ownership-input");
var userInput = $("#users-input");
var groupsInput = $("#groups-input");
var actionInput = $("#action-input");
var policyNameInput = $("#policy-name-input");
var policyDescriptionInput = $("#policy-description-input");
currentlyEffected["roles"] = policyPayloadObj.roles;
currentlyEffected["users"] = policyPayloadObj.users;
currentlyEffected["groups"] = policyPayloadObj.deviceGroups;
if (currentlyEffected["roles"].length > 0) {
$("#user-roles-radio-btn").prop("checked", true);
@ -115,6 +146,10 @@ skipStep["policy-platform"] = function (policyPayloadObj) {
userInput.val(currentlyEffected["users"]).trigger("change");
}
if(currentlyEffected["groups"].length > 0) {
groupsInput.val(currentlyEffected["groups"]).trigger("change");
}
ownershipInput.val(policyPayloadObj.ownershipType);
actionInput.val(policyPayloadObj.compliance);
policyNameInput.val(policyPayloadObj["policyName"]);
@ -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["selectedOwnership"] = $("#ownership-input").val();
// updating next-page wizard title with selected platform
@ -382,6 +422,10 @@ var updatePolicy = function (policy, state) {
payload["roles"] = [];
}
if(policy["selectedGroups"]) {
payload["deviceGroups"] = policy["selectedGroups"];
}
var serviceURL = "/api/device-mgt/v1.0/policies/" + getParameterByName("id");
invokerUtil.put(
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({
"tags": false
}).on("select2:select", function (e) {

Loading…
Cancel
Save