@@ -82,15 +82,15 @@ jQuery(document).ready(function () {
82
82
$ ( '#check-all' ) . prop ( "disabled" , disabled ) ;
83
83
$ ( '.data-check' ) . prop ( "disabled" , disabled ) ;
84
84
}
85
-
85
+
86
86
var previousStatus ;
87
87
function updateTable ( currentStatus ) {
88
88
var statusChanged = ( previousStatus != currentStatus ) ;
89
- if ( statusChanged && ! currentStatus ) {
90
- table . clear ( ) . draw ( ) ;
89
+ if ( statusChanged && ! currentStatus && table != undefined ) {
90
+ table . clear ( ) . draw ( ) ;
91
91
}
92
92
if ( statusChanged && currentStatus ) {
93
- reload_table ( ) ;
93
+ reload_table ( ) ;
94
94
}
95
95
previousStatus = currentStatus ;
96
96
}
@@ -357,8 +357,8 @@ jQuery(document).ready(function () {
357
357
function checkSecurityAndDrawTable ( ) {
358
358
var callback = {
359
359
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 ) ;
362
362
} ,
363
363
error : function ( XMLHttpRequest , textStatus , errorThrown ) {
364
364
drawTable ( false ) ;
@@ -375,8 +375,7 @@ jQuery(document).ready(function () {
375
375
}
376
376
377
377
// /Start ## Datatables ##################################################
378
- function drawTable ( isSecured ) {
379
- var currentUser = sessionStorage . getItem ( "currentUser" ) ;
378
+ function drawTable ( ldapEnabled ) {
380
379
table = $ ( '#table' ) . DataTable ( {
381
380
"responsive" : true ,
382
381
"autoWidth" : false ,
@@ -466,23 +465,27 @@ jQuery(document).ready(function () {
466
465
"title" : "Action" ,
467
466
"data" : null ,
468
467
"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>' ;
481
484
}
482
485
}
483
486
] ,
484
487
"initComplete" : function ( ) {
485
- if ( isSecured == false ) {
488
+ if ( ldapEnabled == false ) {
486
489
table . column ( 2 ) . visible ( false ) ;
487
490
}
488
491
}
@@ -495,6 +498,51 @@ jQuery(document).ready(function () {
495
498
table . responsive . recalc ( ) ;
496
499
} ) ;
497
500
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
+ }
498
546
// /Stop ## Datatables ##################################################
499
547
500
548
// /Start ## Add Subscription ########################################
@@ -672,11 +720,11 @@ jQuery(document).ready(function () {
672
720
createUploadWindow ( ) ;
673
721
}
674
722
} ) ;
675
- // /END ## upload_subscriptions #################################################
723
+ // /END ## upload_subscriptions #################################################
676
724
// /Start ## Reload Datatables ###########################################
677
725
function reload_table ( ) {
678
726
if ( table != undefined ) {
679
- table . ajax . reload ( null , false ) ; // reload datatable ajax
727
+ table . ajax . reload ( null , false ) ; // reload datatable ajax
680
728
}
681
729
}
682
730
// /Stop ## Reload Datatables ############################################
0 commit comments