Skip to content

Commit 98c5f9f

Browse files
author
Anders Breid
authored
Solve bug with undefined users (#208)
* Solve bug with undefined users - Validation weather user may or may not edit/delete subscription is made in seperate function. - Valitation of usernames is made to enusre they are defined and not null. * Update subscription buttons, cleanup code fix faulty bootstrap icons for buttons, rearange buttons, grouped always on buttons.
1 parent 01c809f commit 98c5f9f

File tree

4 files changed

+84
-36
lines changed

4 files changed

+84
-36
lines changed

src/main/resources/static/js/common.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ function doIfUserLoggedIn(user) {
224224
$("#ldapUserName").text(user);
225225
$("#loginBlock").hide();
226226
$("#logoutBlock").show();
227-
$(".show_if_authorized").prop('disabled', false);
227+
$(".show_if_authorized").prop('disabled', false);
228228
}
229229

230230
function doIfUserLoggedOut() {
@@ -246,8 +246,8 @@ function doIfSecurityOff() {
246246
function checkBackendSecured() {
247247
var callback = {
248248
success: function (responseData, textStatus) {
249-
var isSecured = JSON.parse(ko.toJSON(responseData)).security;
250-
if (isSecured == true) {
249+
var ldapEnabled = JSON.parse(ko.toJSON(responseData)).security;
250+
if (ldapEnabled == true) {
251251
checkLoggedInUser();
252252
} else {
253253
doIfSecurityOff();

src/main/resources/static/js/login.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ jQuery(document).ready(function () {
77
},
88
success: function (responseData, textStatus) {
99
var currentUser = localStorage.getItem("currentUser");
10-
var isSecured = responseData.security;
11-
if (isSecured == false || (isSecured == true && currentUser != null)) {
10+
var ldapEnabled = responseData.security;
11+
if (ldapEnabled == false || (ldapEnabled == true && currentUser != null)) {
1212
router.navigate('subscriptions');
1313
}
1414
},

src/main/resources/static/js/subscription.js

+71-23
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,15 @@ jQuery(document).ready(function () {
8282
$('#check-all').prop("disabled", disabled);
8383
$('.data-check').prop("disabled", disabled);
8484
}
85-
85+
8686
var previousStatus;
8787
function updateTable(currentStatus) {
8888
var statusChanged = (previousStatus != currentStatus);
89-
if(statusChanged && !currentStatus){
90-
table.clear().draw();
89+
if(statusChanged && !currentStatus && table != undefined){
90+
table.clear().draw();
9191
}
9292
if(statusChanged && currentStatus){
93-
reload_table();
93+
reload_table();
9494
}
9595
previousStatus = currentStatus;
9696
}
@@ -357,8 +357,8 @@ jQuery(document).ready(function () {
357357
function checkSecurityAndDrawTable() {
358358
var callback = {
359359
success: function (responseData, textStatus) {
360-
isSecured = JSON.parse(ko.toJSON(responseData)).security;
361-
drawTable(isSecured);
360+
var ldapEnabled = JSON.parse(ko.toJSON(responseData)).security;
361+
drawTable(ldapEnabled);
362362
},
363363
error: function (XMLHttpRequest, textStatus, errorThrown) {
364364
drawTable(false);
@@ -375,8 +375,7 @@ jQuery(document).ready(function () {
375375
}
376376

377377
// /Start ## Datatables ##################################################
378-
function drawTable(isSecured) {
379-
var currentUser = sessionStorage.getItem("currentUser");
378+
function drawTable(ldapEnabled) {
380379
table = $('#table').DataTable({
381380
"responsive": true,
382381
"autoWidth": false,
@@ -466,23 +465,27 @@ jQuery(document).ready(function () {
466465
"title": "Action",
467466
"data": null,
468467
"render": function (data, type, row, meta) {
469-
var unsecureEditAll = isSecured == false;
470-
var securedEditOwn = row.ldapUserName == currentUser && row.ldapUserName != null;
471-
var allUserEditNoOwner = row.ldapUserName.length == 0 && currentUser != undefined;
472-
var disableButton = unsecureEditAll || securedEditOwn || allUserEditNoOwner;
473-
474-
var disablingText = "";
475-
if(disableButton == false){
476-
disablingText = " disabled";
477-
}
478-
return '<button id="view-' + data.subscriptionName + '" class="btn btn-sm btn-success view_record table-btn">View</button> '
479-
+ '<button id="edit-' + data.subscriptionName + '" class="btn btn-sm btn-primary edit_record table-btn"' + disablingText + '>Edit</button> '
480-
+ '<button id="delete-' + data.subscriptionName + '" class="btn btn-sm btn-danger delete_record table-btn"' + disablingText + '>Delete</button>';
468+
if (data == undefined || row == undefined) {
469+
window.logMessages("Error: Subscription data is not defined");
470+
return ''
471+
}
472+
subscriptionOwner = row.ldapUserName
473+
subscriptionName = data.subscriptionName
474+
475+
disableEditDeleteButtons = isEditAndDeleteButtonsDisabled(subscriptionOwner, subscriptionName, ldapEnabled)
476+
var disablingText = "";
477+
if(disableEditDeleteButtons == true){
478+
disablingText = " disabled";
479+
}
480+
481+
return '<button id="view-' + subscriptionName + '" class="btn btn-sm btn-success view_record table-btn">View</button> '
482+
+ '<button id="edit-' + subscriptionName + '" class="btn btn-sm btn-primary edit_record table-btn"' + disablingText + '>Edit</button> '
483+
+ '<button id="delete-' + subscriptionName + '" class="btn btn-sm btn-danger delete_record table-btn"' + disablingText + '>Delete</button>';
481484
}
482485
}
483486
],
484487
"initComplete": function () {
485-
if (isSecured == false) {
488+
if (ldapEnabled == false) {
486489
table.column(2).visible(false);
487490
}
488491
}
@@ -495,6 +498,51 @@ jQuery(document).ready(function () {
495498
table.responsive.recalc();
496499
});
497500

501+
function isEditAndDeleteButtonsDisabled(subscriptionOwner, subscriptionName, ldapEnabled) {
502+
if (ldapEnabled == false) {
503+
// LDAP is NOT activated
504+
return false
505+
}
506+
507+
// Check if subscriptionOwner is defined
508+
var isSubscriptionOwnerDefined = isUserNameDefined(subscriptionOwner)
509+
if (isSubscriptionOwnerDefined == false) {
510+
// LDAP is NOT activated or is anonymous subscription
511+
return false
512+
}
513+
514+
// Check if current user is logged in
515+
var currentUser = sessionStorage.getItem("currentUser");
516+
var isCurrentUserLoggedIn = isUserNameDefined(currentUser)
517+
if (isCurrentUserLoggedIn == false) {
518+
// Current user is not logged in
519+
console.log("User must be logged in to edit subscription: '" + subscriptionName + "'." )
520+
return true
521+
}
522+
523+
var isUserSubscriptionOwner = subscriptionOwner == currentUser
524+
if (isUserSubscriptionOwner == false) {
525+
// Back end is secured, but current user is not owner to subscription
526+
console.log("User is not owner of subscription: '" + subscriptionName + "'." )
527+
return true
528+
}
529+
530+
return false
531+
}
532+
533+
function isUserNameDefined(username) {
534+
var isDefined = false
535+
var userNameIsString = isString(username)
536+
if (userNameIsString == true) {
537+
isDefined = username.length != 0
538+
}
539+
return isDefined
540+
}
541+
542+
function isString(value) {
543+
var isString = typeof value === 'string' || value instanceof String
544+
return isString
545+
}
498546
// /Stop ## Datatables ##################################################
499547

500548
// /Start ## Add Subscription ########################################
@@ -672,11 +720,11 @@ jQuery(document).ready(function () {
672720
createUploadWindow();
673721
}
674722
});
675-
// /END ## upload_subscriptions #################################################
723+
// /END ## upload_subscriptions #################################################
676724
// /Start ## Reload Datatables ###########################################
677725
function reload_table() {
678726
if(table != undefined) {
679-
table.ajax.reload(null, false); // reload datatable ajax
727+
table.ajax.reload(null, false); // reload datatable ajax
680728
}
681729
}
682730
// /Stop ## Reload Datatables ############################################

src/main/resources/templates/subscription.html

+8-8
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,19 @@ <h3 id="subData">Subscription Data</h3>
2626
</div>
2727
<div id="subButtons" class="pb-1 col-12 hidden_by_default">
2828
<button id="addSubscription" class="mt-1 btn btn-success add_subscription show_if_authorized">
29-
<i class="glyphicon glyphicon-plus"></i> Add Subscription
29+
<i class="fa fa-fw fa-plus"></i> Add Subscription
3030
</button>
31-
<button id="reloadButton" class="mt-1 btn btn-default table_reload">
32-
<i class="glyphicon glyphicon-refresh"></i> Reload
31+
<button id="uploadSubscription" class="mt-1 btn btn-success upload_subscriptions show_if_authorized">
32+
<i class="fa fa-fw fa-upload"></i>Upload Subscriptions
3333
</button>
3434
<button id="bulkDelete" class="mt-1 btn btn-danger bulk_delete show_if_authorized">
35-
<i class="glyphicon glyphicon-trash"></i> Bulk Delete
35+
<i class="fa fa-fw fa-trash"></i> Bulk Delete
3636
</button>
37-
<button id="getTemplateButton" class="mt-1 btn btn-primary get_subscription_template">
38-
<i class="glyphicon glyphicon-download"></i> Get Template
37+
<button id="reloadButton" class="mt-1 btn btn-info table_reload">
38+
<i class="fa fa-fw fa-refresh"></i> Reload
3939
</button>
40-
<button id="uploadSubscription" class="mt-1 btn btn-info upload_subscriptions show_if_authorized">
41-
<i class="glyphicon glyphicon-upload"></i>Upload Subscriptions
40+
<button id="getTemplateButton" class="mt-1 btn btn-info get_subscription_template">
41+
<i class="fa fa-fw fa-download"></i> Get Template
4242
</button>
4343
<input class="hide" type='file' id='upload_sub' name='file' />
4444
</div>

0 commit comments

Comments
 (0)