fixing jaggery app register process

merge-requests/1/head
Rasika 10 years ago
parent d527d7697e
commit 6b02b4efcb

@ -30,71 +30,94 @@ var utility = require("/modules/utility.js").utility;
var result;
if (uriMatcher.match("/{context}/api/user/login/")) {
username = request.getParameter("username");
password = request.getParameter("password");
try {
userModule.login(username, password, function(user) {
if (log.isDebugEnabled()) {
log.debug("User Logged In : " + user);
}
utility.insertAppPermissions(userModule, "login");
response.sendRedirect(constants.WEB_APP_CONTEXT);
}, function() {
response.sendRedirect(dcProps.appContext + "login?#auth-failed");
});
} catch (e) {
log.error("Exception occurred while a user tried to login to DC", e);
response.sendRedirect(dcProps.appContext + "login?#error");
}
} else if (uriMatcher.match("/{context}/api/user/logout/")){
userModule.logout(function() {
response.sendRedirect(dcProps.appContext + "login");
});
username = request.getParameter("username");
password = request.getParameter("password");
try {
userModule.login(username, password, function (user) {
if (log.isDebugEnabled()) {
log.debug("User Logged In : " + user);
}
utility.insertAppPermissions(userModule, "login");
response.sendRedirect(constants.WEB_APP_CONTEXT);
}, function () {
response.sendRedirect(dcProps.appContext + "login?#auth-failed");
});
} catch (e) {
log.error("Exception occurred while a user tried to login to DC", e);
response.sendRedirect(dcProps.appContext + "login?#error");
}
} else if (uriMatcher.match("/{context}/api/user/logout/")) {
userModule.logout(function () {
response.sendRedirect(dcProps.appContext + "login");
});
} else if (uriMatcher.match("/{context}/api/users/register")) {
addUserFormData = request.getContent();
username = addUserFormData.username;
firstname = addUserFormData.firstname;
lastname = addUserFormData.lastname;
emailAddress = addUserFormData.emailAddress;
password = addUserFormData.password;
if (!addUserFormData.userRoles) {
userRoles = null;
} else {
userRoles = String(addUserFormData.userRoles).split(",");
}
try {
result = userModule.registerUser(username, firstname, lastname, emailAddress, password,
userRoles);
} catch (e) {
log.error("Exception occurred while trying to registering a new user to DC User Store", e);
// http status code 400 refers to - Bad request.
result = 400;
}
} else if (uriMatcher.match("/{context}/api/users/add")) {
if (userModule.isAuthorized("/permission/device-mgt/admin/users/add")) {
addUserFormData = request.getContent();
if (userModule.isAuthorized("/permission/device-mgt/admin/users/add")) {
addUserFormData = request.getContent();
username = addUserFormData.username;
firstname = addUserFormData.firstname;
lastname = addUserFormData.lastname;
emailAddress = addUserFormData.emailAddress;
username = addUserFormData.username;
firstname = addUserFormData.firstname;
lastname = addUserFormData.lastname;
emailAddress = addUserFormData.emailAddress;
if (!addUserFormData.userRoles) {
userRoles = null;
} else {
userRoles = String(addUserFormData.userRoles).split(",");
}
if (!addUserFormData.userRoles) {
userRoles = null;
} else {
userRoles = String(addUserFormData.userRoles).split(",");
}
try {
result = userModule.addUser(username, firstname, lastname, emailAddress, userRoles);
} catch (e) {
log.error("Exception occurred while trying to add a user to DC User Store", e);
// http status code 400 refers to - Bad request.
result = 400;
}
} else {
// http status code 403 refers to - forbidden.
result = 403;
}
try {
result = userModule.addUser(username, firstname, lastname, emailAddress, userRoles);
} catch (e) {
log.error("Exception occurred while trying to add a user to DC User Store", e);
// http status code 400 refers to - Bad request.
result = 400;
}
} else {
// http status code 403 refers to - forbidden.
result = 403;
}
} else if (uriMatcher.match("/{context}/api/users/{username}/remove")) {
if (userModule.isAuthorized("/permission/device-mgt/admin/users/remove")) {
elements = uriMatcher.elements();
username = elements.username;
try {
result = userModule.removeUser(username);
} catch (e) {
log.error("Exception occurred while trying to remove a user from DC User Store", e);
// http status code 400 refers to - Bad request.
result = 400;
}
} else {
// http status code 403 refers to - forbidden.
result = 403;
}
if (userModule.isAuthorized("/permission/device-mgt/admin/users/remove")) {
elements = uriMatcher.elements();
username = elements.username;
try {
result = userModule.removeUser(username);
} catch (e) {
log.error("Exception occurred while trying to remove a user from DC User Store", e);
// http status code 400 refers to - Bad request.
result = 400;
}
} else {
// http status code 403 refers to - forbidden.
result = 403;
}
}
// returning the result.
if (result) {
print(result);
print(result);
}
%>

@ -24,7 +24,7 @@ var getScope = function (unit,configs) {
};
onRequestCb = script.onRequest;
cbResult = onRequestCb(templateConfigs);
log.info("passing configs to unit "+unit+" configs: "+stringify(templateConfigs));
log.debug("passing configs to unit "+unit+" configs: "+stringify(templateConfigs));
//If the execution does not yield an object we will print
//a warning as the unit author may have forgotten to return a data object
if(cbResult===undefined){
@ -157,7 +157,7 @@ Handlebars.registerHelper('unit', function (unitName,options) {
//TODO warn when unspecified decencies are included.
fuseState.zoneStack.push('main');
var template = fuse.getFile(baseUnit, '', '.hbs');
log.info('[' + requestId + '] including "' + baseUnit + '"'+" with configs "+stringify(templateConfigs));
log.debug('[' + requestId + '] including "' + baseUnit + '"'+" with configs "+stringify(templateConfigs));
var result = new Handlebars.SafeString(Handlebars.compileFile(template)(getScope(baseUnit,templateConfigs)));
fuseState.zoneStack.pop();
return result;

@ -56,6 +56,47 @@ userModule = function () {
}
};
/**
* Register user to dc-user-store.
*
* @param username Username of the user
* @param firstname First name of the user
* @param lastname Last name of the user
* @param emailAddress Email address of the user
* @param password Password of the user
* @param userRoles Roles assigned to the user
*
* @returns {number} HTTP Status code 201 if succeeded, 409 if user already exists
*/
publicMethods.registerUser = function (username, firstname, lastname, emailAddress, password, userRoles) {
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);
try {
if (userManager.userExists(username)) {
if (log.isDebugEnabled()) {
log.debug("A user with name '" + username + "' already exists.");
}
// http status code 409 refers to - conflict.
return 409;
} else {
var defaultUserClaims = privateMethods.buildDefaultUserClaims(firstname, lastname, emailAddress);
userManager.addUser(username, password, userRoles, defaultUserClaims, "default");
if (log.isDebugEnabled()) {
log.debug("A new user with name '" + username + "' was created.");
}
// http status code 201 refers to - created.
return 201;
}
} catch (e) {
throw e;
}
};
/**
* Add user to dc-user-store.
*

@ -132,7 +132,7 @@
<div class="col-lg-12 center padding-top-double padding-bottom-double">
<button id="add-user-btn" class="blue-action-btn uppercase margin"> Submit</button>
<button class="black-btn uppercase margin " > Cancel</button>
<button class="black-btn uppercase margin cancel-btn" > Cancel</button>
</div>
</div>
</div>
@ -145,15 +145,19 @@
Username :<br> <input name="username" type="text" value="">
</div>
<div class="col-lg-3 padding ">
Password: <br> <input name="password" type="text" value="">
Password: <br> <input name="password" type="password" value="">
</div>
<div class="col-lg-6 padding-top-double padding-bottom-double padding-left-none">
<button class="blue-action-btn uppercase margin" onclick="document.location.href='home.html';"> Login</button>
<button class="black-btn uppercase margin " > Cancel</button>
<button class="black-btn uppercase margin cancel-btn" > Cancel</button>
</div>
</div>
</form>
</div>
</header>
{{/zone}}
{{#zone "bottomJs"}}
<script src="{{self.publicURL}}/scripts/validate-register.js"></script>
{{/zone}}

@ -0,0 +1,140 @@
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 addUserAPI = "/iot/api/users/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.");
$('.wr-validation-summary strong').removeClass("label-danger");
$('.wr-validation-summary strong').addClass("label-success");
$(".sign-panel").delay(2000).slideUp();
} 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 () {
$('.wr-validation-summary strong').text("An unexpected error occurred.");
$('.wr-validation-summary').removeClass("hidden");
// return false;
}
});
}
});
});

@ -1,143 +0,0 @@
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 addUserAPI = "/login/users/registration";
$.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.");
$('.wr-validation-summary strong').removeClass("label-danger");
$('.wr-validation-summary strong').addClass("label-success");
} 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(){
$('.wr-validation-summary strong').text("An unexpected error occurred.");
$('.wr-validation-summary').removeClass("hidden");
// return false;
}
});
}
});
});

@ -69,7 +69,3 @@
</div>
{{/zone}}
{{#zone "bottomJs"}}
<script src="{{self.publicURL}}/scripts/validate-register.js"></script>
{{/zone}}

@ -35,4 +35,9 @@ input[type="checkbox"], input[type="radio"] {
line-height: normal;
margin-top: -3px;
width: 30px;
}
.navbar-default .navbar-nav>li>a:focus,
.navbar-default .navbar-nav>li>a:hover {
color: #747474;
outline: 0;
}

@ -255,7 +255,7 @@ h5{ font-size:30px;font-weight:100; line-height:Auto; font-style: normal; color:
.black{color:#000;}
.grey{color:#333;}
.white{color:#FFF;}
.blue{color: #00979c;}
.blue{color: #00979c;} .blue:hover{color: #03c5cb;}
.bold{font-weight:bold;}
.no-underline{text-decoration:none;}

@ -5,4 +5,8 @@ $(document).ready(function(){
$(container).slideToggle("fast");
$(container).siblings('div.sign-panel').slideUp("fast");
});
$(".cancel-btn").click(function(){
$(".sign-panel").slideUp();
});
});

@ -1,143 +0,0 @@
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 (!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 (!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 (!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 addUserAPI = "/login/users/registration";
$.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.");
$('.wr-validation-summary strong').removeClass("label-danger");
$('.wr-validation-summary strong').addClass("label-success");
} 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(){
$('.wr-validation-summary strong').text("An unexpected error occurred.");
$('.wr-validation-summary').removeClass("hidden");
// return false;
}
});
}
});
// });
Loading…
Cancel
Save