From 73aaac46b04740f7af1556b473b09b81a31be4cd Mon Sep 17 00:00:00 2001 From: Rasika Date: Mon, 18 May 2015 14:33:18 +0530 Subject: [PATCH] adding jaggery app --- .gitignore | 20 ++ modules/distribution/pom.xml | 4 + .../jaggeryapps/iot/api/user-api.jag | 116 +++++++ .../jaggeryapps/iot/config/config.json | 27 ++ .../repository/jaggeryapps/iot/config/init.js | 27 ++ .../repository/jaggeryapps/iot/jaggery.conf | 39 +++ .../repository/jaggeryapps/iot/lib/login.jag | 36 +++ .../repository/jaggeryapps/iot/lib/logout.jag | 37 +++ .../jaggeryapps/iot/modules/constants.js | 23 ++ .../repository/jaggeryapps/iot/modules/sso.js | 161 ++++++++++ .../jaggeryapps/iot/modules/user.js | 296 ++++++++++++++++++ .../jaggeryapps/iot/modules/utility.js | 41 +++ .../jaggeryapps/iot/pages/add-user.hbs | 9 + .../jaggeryapps/iot/pages/index.hbs | 19 ++ .../jaggeryapps/iot/pages/login.hbs | 7 + .../jaggeryapps/iot/pages/users.hbs | 9 + .../src/repository/jaggeryapps/iot/test.jag | 12 + .../jaggeryapps/iot/test/testExecutor.jag | 20 ++ .../repository/jaggeryapps/iot/tmp/.gitignore | 5 + .../iot/units/add-user/add-user.hbs | 56 ++++ .../iot/units/add-user/add-user.js | 26 ++ .../iot/units/add-user/add-user.json | 3 + .../iot/units/add-user/public/js/bottomJs.js | 91 ++++++ .../jaggeryapps/iot/units/appbar/appbar.hbs | 44 +++ .../jaggeryapps/iot/units/appbar/appbar.js | 16 + .../jaggeryapps/iot/units/appbar/appbar.json | 3 + 26 files changed, 1147 insertions(+) create mode 100644 modules/distribution/src/repository/jaggeryapps/iot/api/user-api.jag create mode 100644 modules/distribution/src/repository/jaggeryapps/iot/config/config.json create mode 100644 modules/distribution/src/repository/jaggeryapps/iot/config/init.js create mode 100755 modules/distribution/src/repository/jaggeryapps/iot/jaggery.conf create mode 100644 modules/distribution/src/repository/jaggeryapps/iot/lib/login.jag create mode 100644 modules/distribution/src/repository/jaggeryapps/iot/lib/logout.jag create mode 100644 modules/distribution/src/repository/jaggeryapps/iot/modules/constants.js create mode 100644 modules/distribution/src/repository/jaggeryapps/iot/modules/sso.js create mode 100644 modules/distribution/src/repository/jaggeryapps/iot/modules/user.js create mode 100644 modules/distribution/src/repository/jaggeryapps/iot/modules/utility.js create mode 100644 modules/distribution/src/repository/jaggeryapps/iot/pages/add-user.hbs create mode 100644 modules/distribution/src/repository/jaggeryapps/iot/pages/index.hbs create mode 100644 modules/distribution/src/repository/jaggeryapps/iot/pages/login.hbs create mode 100644 modules/distribution/src/repository/jaggeryapps/iot/pages/users.hbs create mode 100644 modules/distribution/src/repository/jaggeryapps/iot/test.jag create mode 100644 modules/distribution/src/repository/jaggeryapps/iot/test/testExecutor.jag create mode 100755 modules/distribution/src/repository/jaggeryapps/iot/tmp/.gitignore create mode 100644 modules/distribution/src/repository/jaggeryapps/iot/units/add-user/add-user.hbs create mode 100644 modules/distribution/src/repository/jaggeryapps/iot/units/add-user/add-user.js create mode 100644 modules/distribution/src/repository/jaggeryapps/iot/units/add-user/add-user.json create mode 100644 modules/distribution/src/repository/jaggeryapps/iot/units/add-user/public/js/bottomJs.js create mode 100755 modules/distribution/src/repository/jaggeryapps/iot/units/appbar/appbar.hbs create mode 100644 modules/distribution/src/repository/jaggeryapps/iot/units/appbar/appbar.js create mode 100755 modules/distribution/src/repository/jaggeryapps/iot/units/appbar/appbar.json diff --git a/.gitignore b/.gitignore index 32858aad..3a90e233 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,23 @@ # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* + +*.class + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.war +*.ear + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +# Ignore everything in this directory +target +.classpath +.settings +.project +*.iml +.idea diff --git a/modules/distribution/pom.xml b/modules/distribution/pom.xml index 9217f302..99047028 100644 --- a/modules/distribution/pom.xml +++ b/modules/distribution/pom.xml @@ -340,6 +340,10 @@ + + + + +
+ {{unit "operation-bar"}} + {{unit "device-listing"}} +
+ + + +{{/zone}} \ No newline at end of file diff --git a/modules/distribution/src/repository/jaggeryapps/iot/pages/login.hbs b/modules/distribution/src/repository/jaggeryapps/iot/pages/login.hbs new file mode 100644 index 00000000..fc1255bf --- /dev/null +++ b/modules/distribution/src/repository/jaggeryapps/iot/pages/login.hbs @@ -0,0 +1,7 @@ +{{layout "fluid"}} +{{#zone "title"}} + WSO2 Mobile Device Manager | Login +{{/zone}} +{{#zone "body"}} + {{unit "login"}} +{{/zone}} \ No newline at end of file diff --git a/modules/distribution/src/repository/jaggeryapps/iot/pages/users.hbs b/modules/distribution/src/repository/jaggeryapps/iot/pages/users.hbs new file mode 100644 index 00000000..b06fb457 --- /dev/null +++ b/modules/distribution/src/repository/jaggeryapps/iot/pages/users.hbs @@ -0,0 +1,9 @@ +{{authorized}} +{{layout "fluid"}} +{{#zone "title"}} + User Management +{{/zone}} +{{#zone "body"}} + {{unit "appbar"}} + {{unit "user-listing"}} +{{/zone}} \ No newline at end of file diff --git a/modules/distribution/src/repository/jaggeryapps/iot/test.jag b/modules/distribution/src/repository/jaggeryapps/iot/test.jag new file mode 100644 index 00000000..c0097b63 --- /dev/null +++ b/modules/distribution/src/repository/jaggeryapps/iot/test.jag @@ -0,0 +1,12 @@ +<% +var userModule = require("/modules/user.js").userModule; +userModule.addPermissions([{key: "device-mgt/", name: "Device Management"}], ""); +userModule.addPermissions([{key: "device-mgt/admin", name: "Device Management Admin"}], ""); +userModule.addPermissions([{key: "device-mgt/user", name: "Device Management User"}], ""); + +userModule.addPermissions([{key: "devices", name: "Device"}], "device-mgt/admin"); +userModule.addPermissions([{key: "devices", name: "Device"}], "device-mgt/user"); +userModule.addPermissions([{key: "devices/list", name: "List all Devices"}], "device-mgt/admin"); +userModule.addPermissions([{key: "devices/list", name: "List own Devices"}], "device-mgt/user"); +new Log().info(userModule.isAuthorized("/permission/device-mgt/admin/devices/list")); +%> \ No newline at end of file diff --git a/modules/distribution/src/repository/jaggeryapps/iot/test/testExecutor.jag b/modules/distribution/src/repository/jaggeryapps/iot/test/testExecutor.jag new file mode 100644 index 00000000..8701e7ba --- /dev/null +++ b/modules/distribution/src/repository/jaggeryapps/iot/test/testExecutor.jag @@ -0,0 +1,20 @@ +<% +/* + * 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. + */ +require("jaggery-test").test.run(); +%> \ No newline at end of file diff --git a/modules/distribution/src/repository/jaggeryapps/iot/tmp/.gitignore b/modules/distribution/src/repository/jaggeryapps/iot/tmp/.gitignore new file mode 100755 index 00000000..50e166d6 --- /dev/null +++ b/modules/distribution/src/repository/jaggeryapps/iot/tmp/.gitignore @@ -0,0 +1,5 @@ +# Ignore everything in this directory. +# they are auto generated, should not be committed. +* +# Except this file +!.gitignore \ No newline at end of file diff --git a/modules/distribution/src/repository/jaggeryapps/iot/units/add-user/add-user.hbs b/modules/distribution/src/repository/jaggeryapps/iot/units/add-user/add-user.hbs new file mode 100644 index 00000000..ac13eaa9 --- /dev/null +++ b/modules/distribution/src/repository/jaggeryapps/iot/units/add-user/add-user.hbs @@ -0,0 +1,56 @@ +{{#zone "main"}} + +
+
+ +
+
+

Add User

+ Please note that * sign represents required fields of data. +
+ +
+
+ +
+ +
+ +
+ +
+ +
+ +
+ +
+ +
+ +
+ +
+
+
+ +
+
+ +
+
+ +{{/zone}} +{{#zone "bottomJs"}} + +{{/zone}} + diff --git a/modules/distribution/src/repository/jaggeryapps/iot/units/add-user/add-user.js b/modules/distribution/src/repository/jaggeryapps/iot/units/add-user/add-user.js new file mode 100644 index 00000000..98dce470 --- /dev/null +++ b/modules/distribution/src/repository/jaggeryapps/iot/units/add-user/add-user.js @@ -0,0 +1,26 @@ +/** + * Returns the dynamic state to be populated by add-user page. + * + * @param context Object that gets updated with the dynamic state of this page to be presented + * @returns {*} A context object that returns the dynamic state of this page to be presented + */ +function onRequest(context) { + var carbon = require('carbon'); + var tenantId = carbon.server.tenantId(); + var url = carbon.server.address('https') + "/admin/services/"; + var server = new carbon.server.Server(url); + var userManager = new carbon.user.UserManager(server, tenantId); + + var allRoles = userManager.allRoles(); + var i = 0; + var filteredRoles = []; + while (allRoles[i]) { + if (allRoles[i] != "Internal/subscriber" && allRoles[i] != "Internal/everyone") { + filteredRoles.push(allRoles[i]); + } + i++; + } + + context.roles = filteredRoles; + return context; +} \ No newline at end of file diff --git a/modules/distribution/src/repository/jaggeryapps/iot/units/add-user/add-user.json b/modules/distribution/src/repository/jaggeryapps/iot/units/add-user/add-user.json new file mode 100644 index 00000000..de7c5829 --- /dev/null +++ b/modules/distribution/src/repository/jaggeryapps/iot/units/add-user/add-user.json @@ -0,0 +1,3 @@ +{ + "predicate": false +} \ No newline at end of file diff --git a/modules/distribution/src/repository/jaggeryapps/iot/units/add-user/public/js/bottomJs.js b/modules/distribution/src/repository/jaggeryapps/iot/units/add-user/public/js/bottomJs.js new file mode 100644 index 00000000..9075e2b9 --- /dev/null +++ b/modules/distribution/src/repository/jaggeryapps/iot/units/add-user/public/js/bottomJs.js @@ -0,0 +1,91 @@ +$( document ).ready(function() { + $("select.select2").select2({ + placeholder : "Select..." + }); + + $("select.select2[multiple=multiple]").select2({ + placeholder : "Select...", + tags : true + }); +}); + +/** + * Checks if an email address has the valid format or not. + * + * @param email Email address + * @returns {boolean} true if email has the valid format, otherwise false. + */ +var emailIsValid = function(email) { + var atPosition = email.indexOf("@"); + var dotPosition = email.lastIndexOf("."); + return !(atPosition < 1 || ( dotPosition - atPosition < 2 )); +}; + +/** + * Following click function would execute + * when a user clicks on "Add User" button + * on Add User page in WSO2 MDM Console. + */ +$("button#add-user-btn").click(function() { + var username = $("input#username").val(); + var firstname = $("input#firstname").val(); + var lastname = $("input#lastname").val(); + var emailAddress = $("input#email").val(); + var userRoles = $("select#roles").val(); + + if (!username) { + $(".wr-validation-summary p").text("Username is a required field. It cannot be empty."); + $(".wr-validation-summary").removeClass("hidden"); + } else if (!firstname) { + $(".wr-validation-summary p").text("Firstname is a required field. It cannot be empty."); + $(".wr-validation-summary").removeClass("hidden"); + } else if (!lastname) { + $(".wr-validation-summary p").text("Lastname is a required field. It cannot be empty."); + $(".wr-validation-summary").removeClass("hidden"); + } else if (!emailAddress) { + $(".wr-validation-summary p").text("Email is a required field. It cannot be empty."); + $(".wr-validation-summary").removeClass("hidden"); + } else if (!emailIsValid(emailAddress)) { + $(".wr-validation-summary p").text("Email is not valid. Please enter a correct email address."); + $(".wr-validation-summary").removeClass("hidden"); + } else { + var addUserFormData = {}; + addUserFormData.username = username; + addUserFormData.firstname = firstname; + addUserFormData.lastname = lastname; + addUserFormData.emailAddress = emailAddress; + addUserFormData.userRoles = userRoles; + + var addUserAPI = "/mdm/api/users/add"; + + $.ajax({ + type : "POST", + url : addUserAPI, + contentType : "application/json", + data : JSON.stringify(addUserFormData), + success : function(data) { + if (data == 201) { + $(".wr-validation-summary p").text("User (" + username + ") was added. " + + "An invitation mail will also be sent to this user to initiate a device enrollment."); + // Clearing user input fields. + $("input#username").val(""); + $("input#firstname").val(""); + $("input#lastname").val(""); + $("input#email").val(""); + $("select#roles").select2("val", ""); + } else if (data == 400) { + $(".wr-validation-summary p").text("Exception occurred at backend."); + } else if (data == 403) { + $(".wr-validation-summary p").text("Action was not permitted."); + } else if (data == 409) { + $(".wr-validation-summary p").text("Sorry, User already exists."); + } + $(".wr-validation-summary").removeClass("hidden"); + }, + error : function() { + $(".wr-validation-summary p").text("An unexpected error occurred."); + $(".wr-validation-summary").removeClass("hidden"); + } + }); + } +}); \ No newline at end of file diff --git a/modules/distribution/src/repository/jaggeryapps/iot/units/appbar/appbar.hbs b/modules/distribution/src/repository/jaggeryapps/iot/units/appbar/appbar.hbs new file mode 100755 index 00000000..7ad460e6 --- /dev/null +++ b/modules/distribution/src/repository/jaggeryapps/iot/units/appbar/appbar.hbs @@ -0,0 +1,44 @@ +{{#zone "main"}} + + + +{{/zone}} \ No newline at end of file diff --git a/modules/distribution/src/repository/jaggeryapps/iot/units/appbar/appbar.js b/modules/distribution/src/repository/jaggeryapps/iot/units/appbar/appbar.js new file mode 100644 index 00000000..6cfb2900 --- /dev/null +++ b/modules/distribution/src/repository/jaggeryapps/iot/units/appbar/appbar.js @@ -0,0 +1,16 @@ +function onRequest(context) { + var userModule = require("/modules/user.js").userModule; + var permissions = {}; + if (userModule.isAuthorized("/permission/device-mgt/admin/devices/list") || + userModule.isAuthorized("/permission/device-mgt/user/devices/list")) { + permissions.LIST_DEVICES = true; + } + if (userModule.isAuthorized("/permission/device-mgt/admin/users/list")) { + permissions.LIST_USERS = true; + } + if (userModule.isAuthorized("/permission/device-mgt/admin/users/add")) { + permissions.ADD_USER = true; + } + context.permissions = permissions; + return context; +} \ No newline at end of file diff --git a/modules/distribution/src/repository/jaggeryapps/iot/units/appbar/appbar.json b/modules/distribution/src/repository/jaggeryapps/iot/units/appbar/appbar.json new file mode 100755 index 00000000..5e32dcda --- /dev/null +++ b/modules/distribution/src/repository/jaggeryapps/iot/units/appbar/appbar.json @@ -0,0 +1,3 @@ +{ + "predicate": "true" +} \ No newline at end of file