Skip to content

Commit 408ae51

Browse files
CRUD buttons are visible only for logged in users (#23)
1 parent 1ad1498 commit 408ae51

File tree

4 files changed

+231
-180
lines changed

4 files changed

+231
-180
lines changed

src/main/java/com/ericsson/ei/frontend/EIRequestsController.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public void setUseSecureHttp(boolean useSecureHttp) {
9393
*
9494
*/
9595
@CrossOrigin
96-
@RequestMapping(value = "/auth/*", method = RequestMethod.GET)
96+
@RequestMapping(value = "/auth/login", method = RequestMethod.GET)
9797
public ResponseEntity<String> getAuthRequests(Model model, HttpServletRequest request) {
9898
String eiBackendAddressSuffix = request.getServletPath();
9999
String newRequestUrl = getEIBackendSubscriptionAddress() + eiBackendAddressSuffix;
@@ -123,8 +123,8 @@ public ResponseEntity<String> getAuthRequests(Model model, HttpServletRequest re
123123
*
124124
*/
125125
@CrossOrigin
126-
@RequestMapping(value = { "/subscriptions", "/subscriptions/*", "/information",
127-
"/download/subscriptiontemplate" }, method = RequestMethod.GET)
126+
@RequestMapping(value = { "/subscriptions", "/subscriptions/*", "/information", "/auth",
127+
"/auth/checkStatus", "/auth/logout", "/download/subscriptiontemplate" }, method = RequestMethod.GET)
128128
public ResponseEntity<String> getRequests(Model model, HttpServletRequest request) {
129129
String eiBackendAddressSuffix = request.getServletPath();
130130
String newRequestUrl = getEIBackendSubscriptionAddress() + eiBackendAddressSuffix;
@@ -236,7 +236,7 @@ private String getEIBackendSubscriptionAddress() {
236236

237237
private ResponseEntity<String> getResponse(HttpRequestBase request) {
238238
String jsonContent = "";
239-
int statusCode = 0;
239+
int statusCode = 102;
240240
try (CloseableHttpResponse eiResponse = client.execute(request)) {
241241
InputStream inStream = eiResponse.getEntity().getContent();
242242
BufferedReader bufReader = new BufferedReader(new InputStreamReader(inStream, "UTF-8"));

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

Lines changed: 40 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,6 @@
11

22
jQuery(document).ready(function() {
33

4-
// /Start ## Global AJAX Sender function ##################################
5-
var AjaxHttpSender = function () {};
6-
7-
AjaxHttpSender.prototype.sendAjax = function (url, type, token, callback) {
8-
$.ajax({
9-
url : url,
10-
type : type,
11-
contentType : 'application/json; charset=utf-8',
12-
cache: false,
13-
beforeSend : function (request) {
14-
callback.beforeSend(request, token);
15-
},
16-
error : function (XMLHttpRequest, textStatus, errorThrown) {
17-
callback.error(XMLHttpRequest, errorThrown);
18-
},
19-
success : function (responseData, textStatus) {
20-
callback.success(responseData);
21-
},
22-
complete : function (XMLHttpRequest, textStatus) {
23-
callback.complete();
24-
}
25-
});
26-
}
27-
// /Stop ## Global AJAX Sender function ##################################
28-
29-
// /Start ## Cookies functions ###########################################
30-
function setCookie(name, value) {
31-
var expiry = new Date(new Date().getTime() + 1800 * 1000); // plus 30 min
32-
if(window.location.protocol == "https:") {
33-
document.cookie = name + "=" + escape(value) + "; path=/; expires=" + expiry.toGMTString() + "; secure; HttpOnly";
34-
} else {
35-
document.cookie = name + "=" + escape(value) + "; path=/; expires=" + expiry.toGMTString();
36-
}
37-
}
38-
39-
function getCookie(name) {
40-
var re = new RegExp(name + "=([^;]+)");
41-
var value = re.exec(document.cookie);
42-
return (value != null) ? unescape(value[1]) : null;
43-
}
44-
// /Stop ## Cookies functions ############################################
45-
464
// /Start ## Knockout ####################################################
475
function loginModel() {
486
this.userState = {
@@ -52,28 +10,6 @@ jQuery(document).ready(function() {
5210
this.remember = ko.observable(false);
5311

5412
this.login = function(userState, remember) {
55-
var callback = {
56-
beforeSend : function (xhr, data) {
57-
xhr.setRequestHeader("Authorization", "Basic " + token);
58-
},
59-
success : function (data) {
60-
var currentUser = JSON.parse(ko.toJSON(data)).user;
61-
$.jGrowl("Welcome " + currentUser, {
62-
sticky : false,
63-
theme : 'Notify'
64-
});
65-
doIfUserLoggedIn(currentUser);
66-
$("#mainFrame").load("subscriptionpage.html");
67-
},
68-
error : function (XMLHttpRequest, errorThrown) {
69-
$.jGrowl("Bad credentials", {
70-
sticky : false,
71-
theme : 'Error'
72-
});
73-
},
74-
complete : function () {}
75-
};
76-
7713
var dataJSON = ko.toJSON(userState);
7814
if(JSON.parse(dataJSON).username == "" || JSON.parse(dataJSON).password == "") {
7915
$.jGrowl("Username and password fields cannot be empty", {
@@ -82,12 +18,33 @@ jQuery(document).ready(function() {
8218
});
8319
} else {
8420
var token = window.btoa(JSON.parse(dataJSON).username + ":" + JSON.parse(dataJSON).password);
85-
var ajaxHttpSender = new AjaxHttpSender();
86-
ajaxHttpSender.sendAjax("/auth/login", "GET", token, callback);
21+
sendLoginRequest("/auth/login", "GET", token);
8722
}
8823
}
8924
}
9025

26+
function sendLoginRequest (url, type, token) {
27+
$.ajax({
28+
url : url,
29+
type : type,
30+
contentType : 'application/json; charset=utf-8',
31+
cache: false,
32+
beforeSend : function (request) {
33+
request.setRequestHeader("Authorization", "Basic " + token);
34+
},
35+
error : function (request, textStatus, errorThrown) {
36+
$.jGrowl("Bad credentials", { sticky : false, theme : 'Error' });
37+
},
38+
success : function (responseData, textStatus) {
39+
var currentUser = JSON.parse(ko.toJSON(responseData)).user;
40+
$.jGrowl("Welcome " + currentUser, { sticky : false, theme : 'Notify' });
41+
doIfUserLoggedIn(currentUser);
42+
$("#mainFrame").load("subscriptionpage.html");
43+
},
44+
complete : function (request, textStatus) { }
45+
});
46+
}
47+
9148
function doIfUserLoggedIn(name) {
9249
localStorage.removeItem("currentUser");
9350
localStorage.setItem("currentUser", name);
@@ -102,4 +59,21 @@ jQuery(document).ready(function() {
10259
ko.applyBindings(model, observableObject);
10360
// /Stop ## Knockout #####################################################
10461

62+
// /Start ## Cookies functions ###########################################
63+
function setCookie(name, value) {
64+
var expiry = new Date(new Date().getTime() + 1800 * 1000); // plus 30 min
65+
if(window.location.protocol == "https:") {
66+
document.cookie = name + "=" + escape(value) + "; path=/; expires=" + expiry.toGMTString() + "; secure; HttpOnly";
67+
} else {
68+
document.cookie = name + "=" + escape(value) + "; path=/; expires=" + expiry.toGMTString();
69+
}
70+
}
71+
72+
function getCookie(name) {
73+
var re = new RegExp(name + "=([^;]+)");
74+
var value = re.exec(document.cookie);
75+
return (value != null) ? unescape(value[1]) : null;
76+
}
77+
// /Stop ## Cookies functions ############################################
78+
10579
});

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

Lines changed: 71 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -40,34 +40,33 @@ jQuery(document).ready(function() {
4040
}
4141
// /Stop ## Global AJAX Sender function ##################################
4242

43-
var checkEiBackend = false;
4443
// Check EI Backend Server Status ########################################
45-
function checkEiBackendServer() {
44+
var backendStatus = false;
45+
function checkBackendStatus() {
4646
var EIConnBtn = document.getElementById("btnEIConnection");
4747
if (EIConnBtn == null) {
4848
return;
4949
}
5050
var red="#ff0000";
5151
var green="#00ff00";
5252
$.ajax({
53-
url: "/subscriptions/testDummySubscription",
54-
contentType : 'application/json; charset=utf-8',
53+
url: "/auth/checkStatus",
54+
contentType: 'application/json; charset=utf-8',
5555
type: 'GET',
56-
error : function (XMLHttpRequest, textStatus, errorThrown) {
57-
doIfUserLoggedOut();
56+
error: function (XMLHttpRequest) {
5857
if(XMLHttpRequest.status == 401) {
58+
doIfUserLoggedOut();
5959
EIConnBtn.style.background = green;
60-
checkEiBackend = true;
60+
backendStatus = true;
6161
} else {
6262
EIConnBtn.style.background = red;
63-
checkEiBackend = false;
63+
backendStatus = false;
6464
}
6565
},
66-
success : function (data, textStatus, xhr) {
66+
success: function () {
6767
EIConnBtn.style.background = green;
68-
checkEiBackend = true;
69-
},
70-
complete: function (XMLHttpRequest, textStatus) { }
68+
backendStatus = true;
69+
}
7170
});
7271
}
7372

@@ -76,25 +75,26 @@ jQuery(document).ready(function() {
7675
if(currentUser != "") {
7776
$("#userName").text(currentUser);
7877
$("#logoutBlock").show();
78+
$(".show_if_authorized").show();
7979
}
8080
}
81-
8281
function doIfUserLoggedOut() {
8382
localStorage.removeItem("currentUser");
8483
$("#userName").text("Guest");
8584
$("#loginBlock").show();
8685
$("#logoutBlock").hide();
86+
$(".show_if_authorized").hide();
8787
}
8888

8989
// Check if EI Backend Server is online every X seconds
90-
window.setInterval(function(){ checkEiBackendServer(); }, 15000);
90+
window.setInterval(function(){ checkBackendStatus(); }, 15000);
9191

9292
// Check if EI Backend Server is online when Status Connection button is pressed.
9393
$('.container').on( 'click', 'button.btnEIConnectionStatus', function (event) {
9494
event.stopPropagation();
9595
event.preventDefault();
9696

97-
checkEiBackendServer();
97+
checkBackendStatus();
9898
});
9999
// END OF EI Backend Server check #########################################
100100

@@ -251,16 +251,33 @@ jQuery(document).ready(function() {
251251

252252

253253

254-
};// var SubscriptionViewModel = function(){
254+
};
255+
256+
// Start to check is backend secured
257+
var isSecured = false;
258+
$.ajax({
259+
url: "/auth",
260+
contentType : 'application/json; charset=utf-8',
261+
type: 'GET',
262+
error: function () {},
263+
success: function (data) {
264+
isSecured = JSON.parse(ko.toJSON(data)).security;
265+
if(isSecured == true) {
266+
doIfUserLoggedIn();
267+
}
268+
},
269+
complete: function () {
270+
checkBackendStatus();
271+
}
272+
});
273+
// Finish to check is backend secured
255274

256-
checkEiBackendServer();
257-
doIfUserLoggedIn();
258275
// Cleanup old ViewModel and Knockout Obeservables from previous page load.
259276
var observableObject = $('#ViewModelDOMObject')[0];
260277
ko.cleanNode(observableObject);
261278
// Apply bindings
262279
var vm = new SubscriptionViewModel();
263-
ko.applyBindings(vm, document.getElementById("ViewModelDOMObject"));
280+
ko.applyBindings(vm, observableObject);
264281

265282

266283
// /Stop ## Knockout #####################################################
@@ -270,17 +287,19 @@ jQuery(document).ready(function() {
270287

271288

272289
// /Start ## Datatables ##################################################
290+
var currentUser = localStorage.getItem("currentUser");
273291
table = $('#table').DataTable({
274-
275292
"processing": true, //Feature control the processing indicator.
276293
"serverSide": false, //Feature control DataTables' server-side processing mode.
277294
"fixedHeader": true,
278295
"order": [], //Initial no order.
296+
"searching": true,
279297
// Load data for the table's content from an Ajax source
280298
"ajax": {
281299
"url": frontendServiceUrl + "/subscriptions",
282300
"type": "GET",
283-
"dataSrc": "" // Flat structure from EI backend REST API
301+
"dataSrc": "", // Flat structure from EI backend REST API
302+
"error": function () {}
284303
},
285304
//Set column definition initialisation properties.
286305
"columnDefs": [
@@ -294,19 +313,26 @@ jQuery(document).ready(function() {
294313
}
295314
},
296315
{
297-
"targets": [ 1 ],
316+
"targets": [ 1 ],
317+
"orderable": true,
318+
"title": "UserName",
319+
"data": "userName",
320+
"defaultContent": ""
321+
},
322+
{
323+
"targets": [ 2 ],
298324
"orderable": true,
299325
"title": "SubscriptionName",
300326
"data": "subscriptionName"
301327
},
302328
{
303-
"targets": [ 2 ],
329+
"targets": [ 3 ],
304330
"orderable": true,
305331
"title": "Type",
306332
"data": "aggregationtype"
307333
},
308334
{
309-
"targets": [ 3 ],
335+
"targets": [ 4 ],
310336
"orderable": true,
311337
"title": "Date",
312338
"data": "created",
@@ -315,32 +341,47 @@ jQuery(document).ready(function() {
315341
}
316342
},
317343
{
318-
"targets": [ 4 ],
344+
"targets": [ 5 ],
319345
"orderable": true,
320346
"title": "NotificationType",
321347
"data": "notificationType"
322348
},
323349
{
324-
"targets": [ 5 ],
350+
"targets": [ 6 ],
325351
"orderable": true,
326352
"title": "NotificationMeta",
327353
"data": "notificationMeta"
328354
},
329355
{
330-
"targets": [ 6 ],
356+
"targets": [ 7 ],
331357
"orderable": true,
332358
"title": "Repeat",
333359
"data": "repeat"
334360
},
335361
{
336-
"targets": [ 7 ], //last column
362+
"targets": [ 8 ], //last column
337363
"orderable": false,
338364
"title": "Action",
339365
"data": null,
340366
"width":"150px",
341-
"defaultContent": '<button data-toggle="tooltip" title="Edit subscription" class="btn btn-sm btn-primary edit_record">Edit</button><button data-toggle="tooltip" title="Delete subscription from EI" class="btn btn-sm btn-danger delete_record">Delete</button>'
342-
},
367+
"render": function ( data, type, row, meta ) {
368+
if(isSecured == true && row.userName == currentUser && row.userName != null) {
369+
return '<button data-toggle="tooltip" title="Edit subscription" class="btn btn-sm btn-primary edit_record">Edit</button> '
370+
+ '<button data-toggle="tooltip" title="Delete subscription from EI" class="btn btn-sm btn-danger delete_record">Delete</button>';
371+
} else if(isSecured == false) {
372+
return '<button data-toggle="tooltip" title="Edit subscription" class="btn btn-sm btn-primary edit_record">Edit</button> '
373+
+ '<button data-toggle="tooltip" title="Delete subscription from EI" class="btn btn-sm btn-danger delete_record">Delete</button>';
374+
} else {
375+
return '';
376+
}
377+
}
378+
}
343379
],
380+
"initComplete": function () {
381+
if(isSecured == false) {
382+
table.column(1).visible(false);
383+
}
384+
}
344385
});
345386
// /Stop ## Datatables ##################################################
346387

0 commit comments

Comments
 (0)