Merge pull request #24 from charithag/master

Add user registration UIs and Shabir's fix
Ruwan 9 years ago
commit 5ce4eef12c

@ -16,23 +16,7 @@
* under the License.
*/
package org.wso2.carbon.device.mgt.iot.analytics.statistics;/*
*Copyright (c) 2005-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.
*/
package org.wso2.carbon.device.mgt.iot.analytics.statistics;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

@ -104,7 +104,7 @@ public class IotDeviceManagementServiceComponent {
IotDeviceManagementDAOUtil
.setupIotDeviceManagementSchema(
IotDeviceManagementDAOFactory.getDataSourceMap
().get(pluginType));
().get(pluginType), pluginType);
}
} catch (IotDeviceMgtPluginException e) {
log.error(

@ -70,10 +70,10 @@ public class IotDeviceManagementDAOUtil {
*
* @param dataSource Iot data source
*/
public static void setupIotDeviceManagementSchema(DataSource dataSource) throws
public static void setupIotDeviceManagementSchema(DataSource dataSource, String pluginType) throws
IotDeviceMgtPluginException {
IotDeviceManagementSchemaInitializer initializer =
new IotDeviceManagementSchemaInitializer(dataSource);
new IotDeviceManagementSchemaInitializer(dataSource, pluginType);
log.info("Initializing iot device management repository database schema");
try {
initializer.createRegistryDatabase();

@ -40,18 +40,26 @@ public final class IotDeviceManagementSchemaInitializer extends DatabaseCreator
CarbonUtils.getCarbonHome() + File.separator + "dbscripts" + File.separator
+ IotPluginConstants.IOT_DB_SCRIPTS_FOLDER +
File.separator + "plugins" + File.separator;
private String pluginType;
public IotDeviceManagementSchemaInitializer(DataSource dataSource) {
public String getPluginType() {
return pluginType;
}
public IotDeviceManagementSchemaInitializer(DataSource dataSource, String pType) {
super(dataSource);
this.pluginType = pType;
}
protected String getDbScriptLocation(String databaseType) {
String scriptName = databaseType + ".sql";
String scriptLocation = setupSQLScriptBaseLocation + this.getPluginType() + File.separator + scriptName;
if (log.isDebugEnabled()) {
log.debug("Loading database script from :" + scriptName);
log.debug("Loading database script from :" + scriptLocation);
}
//TODO:test
return setupSQLScriptBaseLocation.replaceFirst("DBTYPE", databaseType) + scriptName;
// return setupSQLScriptBaseLocation.replaceFirst("DBTYPE", databaseType) + scriptName;
return scriptLocation;
}
}

@ -1,6 +0,0 @@
{{authenticated}}
{{unit "cdmf.unit.ui.title" pageTitle="Platform Configuration"}}
{{#zone "content"}}
{{unit "cdmf.unit.platform.configuration"}}
{{/zone}}

@ -0,0 +1,5 @@
{{unit "cdmf.unit.ui.title" pageTitle="Register"}}
{{#zone "content"}}
{{unit "iot.unit.user.register"}}
{{/zone}}

@ -0,0 +1,145 @@
var emailIsValid = function (email) {
var atPosition = email.indexOf("@");
var dotPosition = email.lastIndexOf(".");
return !(atPosition < 1 || ( dotPosition - atPosition < 2 ));
};
var validatePassword = function (psswd, conPass) {
var error = "";
var illegalChars = /[\W_]/; // allow only letters and numbers
if ((psswd.length < 5) || (psswd.length > 15)) {
error = "The password is of wrong length. Should be between 5 and 15 characters. \n";
$('.wr-validation-summary strong').text(error);
$('.wr-validation-summary').removeClass("hidden");
return false;
} else if (illegalChars.test(psswd)) {
error = "The password contains illegal characters.\n";
$('.wr-validation-summary strong').text(error);
$('.wr-validation-summary').removeClass("hidden");
return false;
} else if ((psswd.search(/[a-zA-Z]+/) == -1) || (psswd.search(/[0-9]+/) == -1)) {
error = "The password must contain at least one numeral and one character.\n";
$('.wr-validation-summary strong').text(error);
$('.wr-validation-summary').removeClass("hidden");
return false;
} else if (psswd != conPass) {
error = "The password and confirm-password should match.\n";
$('.wr-validation-summary strong').text(error);
$('.wr-validation-summary').removeClass("hidden");
// return false;
} else {
return true;
}
};
$(function () {
$("button#add-user-btn").click(function () {
//e.preventDefault();
var username = $("input#user_name").val();
var firstname = $("input#first_name").val();
var lastname = $("input#last_name").val();
var emailAddress = $("input#email").val();
var password = $("input#password").val();
var passwordConfirm = $("input#password_confirmation").val();
// var userRoles = $("select#roles").val();
if (!firstname) {
$('.wr-validation-summary strong').text("Firstname is a required field. It cannot be empty.");
$('.wr-validation-summary').removeClass("hidden");
return false;
} else if (!lastname) {
$('.wr-validation-summary strong').text("Lastname is a required field. It cannot be empty.");
$('.wr-validation-summary').removeClass("hidden");
return false;
} else if (!username) {
$('span.wr-validation-summary strong').text("Username is a required field. It cannot be empty.");
$('.wr-validation-summary').removeClass("hidden");
return false;
} else if (!emailAddress) {
$('.wr-validation-summary strong').text("Email is a required field. It cannot be empty.");
$('.wr-validation-summary').removeClass("hidden");
return false;
} else if (!emailIsValid(emailAddress)) {
$('.wr-validation-summary strong').text("Email is not valid. Please enter a correct email address.");
$('.wr-validation-summary').removeClass("hidden");
return false;
} else if (!password) {
$('.wr-validation-summary strong').text("Please enter a user login password.");
$('.wr-validation-summary').removeClass("hidden");
return false;
} else if (!passwordConfirm) {
$('.wr-validation-summary strong').text("Please re-type password");
$('.wr-validation-summary').removeClass("hidden");
return false;
} else if (!validatePassword(password, passwordConfirm)) {
return false;
} else if (!$('#t_and_c').is(':checked')) {
$('.wr-validation-summary strong').text("Please accept our terms and conditions");
$('.wr-validation-summary').removeClass("hidden");
} else {
var addUserFormData = {};
addUserFormData.username = username;
addUserFormData.firstname = firstname;
addUserFormData.lastname = lastname;
addUserFormData.emailAddress = emailAddress;
addUserFormData.password = password;
addUserFormData.userRoles = null;
var context = $(".form-login-box").attr("action");
var addUserAPI = context + "/api/user/register";
$.ajax({
type: 'POST',
url: addUserAPI,
contentType: 'application/json',
data: JSON.stringify(addUserFormData),
success: function (data) {
if (data == 200) {
$('.wr-validation-summary strong').text("Successfully Submitted.");
$('.wr-validation-summary strong').removeClass("label-danger");
$('.wr-validation-summary strong').addClass("label-success");
} else if (data == 201) {
$('.wr-validation-summary strong').text("User created. You will be redirected to login page");
$('.wr-validation-summary strong').removeClass("label-danger");
$('.wr-validation-summary strong').addClass("label-success");
setTimeout(function () {
window.location = context + "/login";
}, 2000);
} else if (data == 400) {
$('.wr-validation-summary strong').text("Exception at backend.");
$('.wr-validation-summary strong').removeClass("label-danger");
$('.wr-validation-summary strong').addClass("label-warning");
} else if (data == 403) {
$('.wr-validation-summary strong').text("Action not permitted.");
} else if (data == 409) {
$('.wr-validation-summary strong').text("User exists.");
$('.wr-validation-summary strong').removeClass("label-default");
$('.wr-validation-summary strong').addClass("label-success");
}
$('.wr-validation-summary').removeClass("hidden");
$('#password').val('');
$('#password_confirmation').val('');
// return true;
},
error: function (err) {
$('.wr-validation-summary strong').text("An unexpected error occurred.");
$('.wr-validation-summary').removeClass("hidden");
return false;
}
});
}
return false;
});
});

@ -0,0 +1,65 @@
<div class="container col-md-12 col-lg-6 col-centered wr-content wr-login col-centered sign-panel">
<p class="page-sub-title">Register</p>
<p>Create new account on IoT Server</p>
<!-- validation -->
<span class="wr-validation-summary hidden center-block col-centered">
<strong class="label label-danger col-centered"></strong>
</span>
<hr/>
<form method="POST" class="form-login-box" action="{{@app.context}}">
<label class="wr-input-label">First Name</label>
<div class="wr-input-control">
<input type="text right" id="first_name" placeholder="First Name">
</div>
<label class="wr-input-label">Last Name</label>
<div class="wr-input-control">
<input type="text right" id="last_name" placeholder="Last Name">
</div>
<label class="wr-input-label">Username</label>
<div class="wr-input-control">
<input type="text right" id="user_name" placeholder="Username ">
</div>
<label class="wr-input-label">Email</label>
<div class="wr-input-control">
<input type="text right" id="email" placeholder="Email">
</div>
<label class="wr-input-label">Password</label>
<div class="wr-input-control">
<input type="password" id="password" placeholder="Password">
</div>
<label class="wr-input-label">Confirm Password</label>
<div class="wr-input-control">
<input type="password" id="password_confirmation" placeholder="Confirm Password">
</div>
<div class="wr-input-label">
<input type="checkbox" name="Agree" id="t_and_c" value="Bike"> <span class="italic">
I Agree by Clicking register, you agree to the Terms and Conditions set out by this site</span>
</div>
<div class="wr-input-control">
<button class="wr-btn" id="add-user-btn">&nbsp;&nbsp;&nbsp;&nbsp;Register&nbsp;&nbsp;&nbsp;&nbsp;</button>
<button class="wr-btn" onclick="document.location.href='{{@app.context}}/login';">&nbsp;&nbsp;&nbsp;&nbsp;Cancel&nbsp;&nbsp;&nbsp;&nbsp;</button>
</div>
</form>
</div>
{{#zone "bottomLoginJs"}}
{{js "js/validate-register.js"}}
{{/zone}}

@ -0,0 +1,50 @@
function post(path, method) {
method = method || "post"; // Set method to post by default if not specified.
form.submit();
}
$( document ).ready(function() {
var currentHash = window.location.hash,
submitPath = $("#signInForm").attr("action");
if(currentHash=="#auth-failed") {
$('.wr-validation-summary p').text("Sorry!, Please make sure to enter correct username and password");
$('.wr-validation-summary').removeClass("hidden");
}else if(currentHash=="#error"){
$('.wr-validation-summary p').text("Sorry!, Error occured");
$('.wr-validation-summary').removeClass("hidden");
}
$('#signIn').click(function(){
var username = $("input#username").val(),
password = $("input#password").val();
if (!username) {
$('.wr-validation-summary p').text("Sorry!, Username cannot be empty.");
$('.wr-validation-summary').removeClass("hidden");
} else if (!password){
$('.wr-validation-summary p').text("Sorry!, Password cannot be empty.");
$('.wr-validation-summary').removeClass("hidden");
} else {
post(submitPath,"POST");
}
});
});
function submitLoginForm() {
var submitPath = $("#signInForm").attr("action");
$('form input').keypress(function() {
if(e.which == 10 || e.which == 13) {
if (!username) {
alert()
$('.wr-validation-summary p').text("Sorry!, Username cannot be empty.");
$('.wr-validation-summary').removeClass("hidden");
} else if (!password){
$('.wr-validation-summary p').text("Sorry!, Password cannot be empty.");
$('.wr-validation-summary').removeClass("hidden");
} else {
post(submitPath,"POST");
}
}
});
}

@ -0,0 +1,44 @@
<div class="container col-md-12 col-lg-3 col-centered wr-content wr-login col-centered">
<p class="page-sub-title">Login</p>
<hr/>
<!-- validation -->
<span class="wr-validation-summary hidden">
<p>Login failed. Check your credentials and try again.</p>
</span>
<div class="panel-body">
<form id="signInForm" method="POST"
class="{{defineZone "signInForm-class" scope="protected"}}"
action="{{@app.context}}/api/user/login">
<div class="wr-input-control">
<label class="wr-input-label">Username *</label>
<input type="text" name="username" class="form-control" placeholder="User Name"
id="username" autofocus="autofocus" />
</div>
<div class="wr-input-control">
<label class="wr-input-label">Password *</label>
<input type="password" name="password" class="form-control"
id="password" placeholder="Password" />
</div>
<div class="wr-input-control wr-btn-grp">
<button class="wr-btn btn-download-agent" id="signIn">&nbsp;&nbsp;&nbsp;&nbsp;Login&nbsp;&nbsp;&nbsp;&nbsp;</button>
</div>
<div class="form-group">
{{#if showRememberMe}}
<span class="pull-left">
<input type="checkbox" name="rememberMe" value="remember"> Remember me
</span>
{{/if}}
{{#if showCreateAccount}}
<a id="createAccount"
href="{{@app.context}}/register"
class="pull-right create-account">Create an account</a>
{{/if}}
</div>
</form>
</div>
</div>
</div>
{{#zone "bottomLoginJs"}}
{{js "js/login.js" }}
{{/zone}}
Loading…
Cancel
Save