diff --git a/modules/distribution/src/repository/jaggeryapps/iotserver/api/group-api.jag b/modules/distribution/src/repository/jaggeryapps/iotserver/api/group-api.jag new file mode 100644 index 00000000..abb5b499 --- /dev/null +++ b/modules/distribution/src/repository/jaggeryapps/iotserver/api/group-api.jag @@ -0,0 +1,252 @@ +<% +/* + * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +var uri = request.getRequestURI(); +var uriMatcher = new URIMatcher(String(uri)); + +var log = new Log("api/group-api.jag"); + +var constants = require("/modules/constants.js"); +var dcProps = require('/config/dc-props.js').config(); + +var carbon = require('carbon'); +var carbonHttpsServletTransport = carbon.server.address('https'); +var deviceCloudService = carbonHttpsServletTransport + "/devicecloud/group_manager"; + +var user = session.get(constants.USER_SESSION_KEY); +if (!user) { + response.sendRedirect(dcProps.appContext + "login?#login-required"); + exit();//stop execution +} + +var result; +var endPoint; +var data; +var groupId; + +if (uriMatcher.match("/{context}/api/group/add")) { + + var group = request.getContent(); + var name = group["name"]; + var description = group["description"]; + //URL: POST https://localhost:9443/devicecloud/group_manager/group + endPoint = deviceCloudService + "/group"; + data = {"name": name, "username": user.username, "description": description}; + result = post(endPoint, data, "json"); + +} else if (uriMatcher.match("/{context}/api/group/id/{groupId}/update")) { + + groupId = uriMatcher.elements().groupId; + group = request.getContent(); + name = group["name"]; + description = group["description"]; + + //URL: PUT https://localhost:9443/devicecloud/group_manager/group/id/{groupId} + endPoint = deviceCloudService + "/group/id/" + groupId; + data = {"name": name, "username": user.username, "description": description}; + result = put(endPoint, data, "json"); + +} else if (uriMatcher.match("/{context}/api/group/id/{groupId}/remove")) { + + groupId = uriMatcher.elements().groupId; + + //URL: DELETE https://localhost:9443/devicecloud/group_manager/group/id/{groupId} + endPoint = deviceCloudService + "/group/id/" + groupId; + data = {"username": user.username}; + result = del(endPoint, data, "json"); + +} else if (uriMatcher.match("/{context}/api/group/id/{groupId}")) { + groupId = uriMatcher.elements().groupId; + + //URL: GET https://localhost:9443/devicecloud/group_manager/group/id/{groupId} + endPoint = deviceCloudService + "/group/id/" + groupId; + log.info(endPoint); + data = {"username": user.username}; + result = get(endPoint, data, "json"); + +} else if (uriMatcher.match("/{context}/api/group/name/{groupName}")) { + + //URL: GET https://localhost:9443/devicecloud/group_manager/group/name/{name} + endPoint = deviceCloudService + "/group/name/" + uriMatcher.elements().groupName; + data = {"username": user.username}; + result = get(endPoint, data, "json"); + +} else if (uriMatcher.match("/{context}/api/group/all")) { + + //URL: GET https://localhost:9443/devicecloud/group_manager/group/all + endPoint = deviceCloudService + "/group/all"; + data = {"username": user.username}; + result = get(endPoint, data, "json"); + +} else if (uriMatcher.match("/{context}/api/group/all/count")) { + + //URL: GET https://localhost:9443/devicecloud/group_manager/group/all/count + endPoint = deviceCloudService + "/group/all/count"; + data = {"username": user.username}; + result = get(endPoint, data, "json"); + +} else if (uriMatcher.match("/{context}/api/group/id/{groupId}/share")) { + + groupId = uriMatcher.elements().groupId; + var shareUser = request.getContent()["shareUser"]; + role = request.getContent()["role"]; + + //URL: PUT https://localhost:9443/devicecloud/group_manager/group/id/{groupId}/share + endPoint = deviceCloudService + "/group/id/" + groupId + "/share"; + data = {"username": user.username, "shareUser": shareUser, "role": role}; + result = post(endPoint, data, "json"); + +} else if (uriMatcher.match("/{context}/api/group/id/{groupId}/unshare")) { + + groupId = uriMatcher.elements().groupId; + var unShareUser = request.getContent()["unShareUser"]; + role = request.getContent()["role"]; + + //URL: DELETE https://localhost:9443/devicecloud/group_manager/group/id/{groupId}/unshare + endPoint = deviceCloudService + "/group/id/" + groupId + "/unshare"; + data = {"username": user.username, "unShareUser": unShareUser, "role": role}; + result = post(endPoint, data, "json"); + +} else if (uriMatcher.match("/{context}/api/group/id/{groupId}/role/add")) { + + groupId = uriMatcher.elements().groupId; + var permissions = request.getContent()["permissions"]; + role = request.getContent()["role"]; + + //URL: POST https://localhost:9443/devicecloud/group_manager/group/id/{groupId}/role + endPoint = deviceCloudService + "/group/id/" + groupId + "/role"; + data = {"username": user.username, "permissions": permissions, "role": role}; + result = post(endPoint, data, "json"); + +} else if (uriMatcher.match("/{context}/api/group/id/{groupId}/role/delete")) { + + groupId = uriMatcher.elements().groupId; + role = request.getContent()["role"]; + + //URL: DELETE https://localhost:9443/devicecloud/group_manager/group/id/{groupId}/role + endPoint = deviceCloudService + "/group/id/" + groupId + "/role"; + data = {"username": user.username, "role": role}; + result = del(endPoint, data, "json"); + +} else if (uriMatcher.match("/{context}/api/group/id/{groupId}/role/all")) { + + groupId = uriMatcher.elements().groupId; + + //URL: GET https://localhost:9443/devicecloud/group_manager/group/id/{groupId}/role/all + endPoint = deviceCloudService + "/group/id/" + groupId + "/role/all"; + data = {"username": user.username}; + result = get(endPoint, data, "json"); + +} else if (uriMatcher.match("/{context}/api/group/id/{groupId}/{userId}/role/all")) { + + groupId = uriMatcher.elements().groupId; + var userId = uriMatcher.elements().userId; + + //URL: GET https://localhost:9443/devicecloud/group_manager/group/id/{groupId}/{user}/role/all + endPoint = deviceCloudService + "/group/id/" + groupId + "/" + userId + "/role/all"; + data = {"username": user.username}; + result = get(endPoint, data, "json"); + +} else if (uriMatcher.match("/{context}/api/group/id/{groupId}/{userId}/rolemapping")) { + + groupId = uriMatcher.elements().groupId; + userId = uriMatcher.elements().userId; + + data = {"username": user.username}; + + //URL: GET https://localhost:9443/devicecloud/group_manager/group/id/{groupId}/role/all + endPoint = deviceCloudService + "/group/id/" + groupId + "/role/all"; + var allRoles = get(endPoint, data, "json").data; + + //URL: GET https://localhost:9443/devicecloud/group_manager/group/id/{groupId}/{user}/role/all + endPoint = deviceCloudService + "/group/id/" + groupId + "/" + userId + "/role/all"; + var userRolesObj = get(endPoint, data, "json"); + var userRoles = userRolesObj.data; + var roleMap = []; + for (var role in allRoles) { + var objRole = {"role": allRoles[role], "assigned": false}; + for (var usrRole in userRoles) { + if (allRoles[role] == userRoles[usrRole]) { + objRole.assigned = true; + break; + } + } + roleMap.push(objRole); + } + result = {}; + result.data = roleMap; + result.xhr = userRolesObj.xhr; + +} else if (uriMatcher.match("/{context}/api/group/id/{groupId}/{userId}/roleupdate")) { + + groupId = uriMatcher.elements().groupId; + userId = uriMatcher.elements().userId; + roleMap = request.getContent(); + + for (role in roleMap) { + if (roleMap[role].assigned == true) { + //URL: POST https://localhost:9443/devicecloud/group_manager/group/id/{groupId}/share + endPoint = deviceCloudService + "/group/id/" + groupId + "/share"; + data = {"username": user.username, "shareUser": userId, "role": roleMap[role].role}; + result = post(endPoint, data, "json"); + } else { + //URL: DELETE https://localhost:9443/devicecloud/group_manager/group/id/{groupId}/unshare + endPoint = deviceCloudService + "/group/id/" + groupId + "/unshare"; + data = {"username": user.username, "unShareUser": userId, "role": roleMap[role].role}; + result = post(endPoint, data, "json"); + } + } + +} else if (uriMatcher.match("/{context}/api/group/id/{groupId}/user/all")) { + + groupId = uriMatcher.elements().groupId; + + //URL: GET https://localhost:9443/devicecloud/group_manager/group/id/{groupId}/user/all + endPoint = deviceCloudService + "/group/id/" + groupId + "/user/all"; + data = {"username": user.username}; + result = get(endPoint, data, "json"); + +} else if (uriMatcher.match("/{context}/api/group/id/{groupId}/device/all")) { + + groupId = uriMatcher.elements().groupId; + + //URL: GET https://localhost:9443/devicecloud/group_manager/group/id/{groupId}/device/all + endPoint = deviceCloudService + "/group/id/" + groupId + "/device/all"; + data = {"username": user.username}; + result = get(endPoint, data, "json"); + +} else if (uriMatcher.match("/{context}/api/group/id/{groupId}/assign")) { + + groupId = uriMatcher.elements().groupId; + var deviceId = request.getContent()["deviceId"]; + var deviceType = request.getContent()["deviceType"]; + + //URL: GET https://localhost:9443/devicecloud/group_manager/group/id/{groupId}/device/assign + endPoint = deviceCloudService + "/group/id/" + groupId + "/device/assign"; + data = {"username": user.username, "deviceId": deviceId, "deviceType": deviceType}; + result = put(endPoint, data, "json"); + +} + +// returning the result. +if (result) { + response.status = result.xhr.status; + print(result.data); +} +%> diff --git a/modules/distribution/src/repository/jaggeryapps/iotserver/api/user-api.jag b/modules/distribution/src/repository/jaggeryapps/iotserver/api/user-api.jag index eacea91d..e965c1be 100644 --- a/modules/distribution/src/repository/jaggeryapps/iotserver/api/user-api.jag +++ b/modules/distribution/src/repository/jaggeryapps/iotserver/api/user-api.jag @@ -137,6 +137,8 @@ if (uriMatcher.match("/{context}/api/user/login/")) { result = 403; } +} else if (uriMatcher.match("/{context}/api/users")) { + result = userModule.getUsers(); } // returning the result. diff --git a/modules/distribution/src/repository/jaggeryapps/iotserver/jaggery.conf b/modules/distribution/src/repository/jaggeryapps/iotserver/jaggery.conf index 420fe87e..2771f3c3 100644 --- a/modules/distribution/src/repository/jaggeryapps/iotserver/jaggery.conf +++ b/modules/distribution/src/repository/jaggeryapps/iotserver/jaggery.conf @@ -19,6 +19,10 @@ "url": "/api/devices/*", "path": "/api/device-api.jag" }, + { + "url": "/api/group/*", + "path": "/api/group-api.jag" + }, { "url": "/api/operation/*", "path": "/api/operation-api.jag" diff --git a/modules/distribution/src/repository/jaggeryapps/iotserver/modules/utility.js b/modules/distribution/src/repository/jaggeryapps/iotserver/modules/utility.js index 6516b7a3..81f1bffd 100644 --- a/modules/distribution/src/repository/jaggeryapps/iotserver/modules/utility.js +++ b/modules/distribution/src/repository/jaggeryapps/iotserver/modules/utility.js @@ -56,6 +56,28 @@ utility = function () { name: "Perform Operation" }], "device-mgt/admin", type); + userModule.addPermissions([{key: "groups", name: "Groups"}], "device-mgt/admin", type); + userModule.addPermissions([{ + key: "groups/add_devices", + name: "Add Devices to Group" + }], "device-mgt/admin", type); + userModule.addPermissions([{ + key: "groups/remove_devices", + name: "Remove Devices from Group" + }], "device-mgt/admin", type); + userModule.addPermissions([{ + key: "groups/modify", + name: "Modify Group" + }], "device-mgt/admin", type); + userModule.addPermissions([{ + key: "groups/share", + name: "Change Group Sharing" + }], "device-mgt/admin", type); + userModule.addPermissions([{ + key: "groups/delete", + name: "Delete Group" + }], "device-mgt/admin", type); + userModule.addPermissions([{key: "users", name: "Users"}], "device-mgt/admin", type); userModule.addPermissions([{ key: "users/add", @@ -83,6 +105,20 @@ utility = function () { key: "devices/operation", name: "Perform Operation" }], "device-mgt/user", "init"); + + userModule.addPermissions([{key: "groups", name: "Groups"}], "device-mgt/user", type); + userModule.addPermissions([{ + key: "groups/monitor", + name: "Monitor Devices in Group" + }], "device-mgt/user", type); + userModule.addPermissions([{ + key: "groups/operation", + name: "Perform Device Operation" + }], "device-mgt/user", type); + userModule.addPermissions([{ + key: "groups/list", + name: "List Groups of User" + }], "device-mgt/user", type); }; return publicMethods; diff --git a/modules/distribution/src/repository/jaggeryapps/iotserver/pages/devices/index.hbs b/modules/distribution/src/repository/jaggeryapps/iotserver/pages/devices/index.hbs index ea9cd1a3..c2ec062f 100644 --- a/modules/distribution/src/repository/jaggeryapps/iotserver/pages/devices/index.hbs +++ b/modules/distribution/src/repository/jaggeryapps/iotserver/pages/devices/index.hbs @@ -5,7 +5,7 @@ {{/zone}} {{#zone "body"}} {{unit "appbar" link="device-mgt" title="My Devices"}} - {{unit "extended-search-box"}} + {{unit "extended-search-box" item="Device"}} {{unit "operation-mod"}}