Skip to content

Commit c6ec741

Browse files
author
Anders Breid
authored
Fix ldap check (#212)
* Fix bug now working in explorer, better handling of ldap satus - HTML code fixed invalid end tags - Website now works in explorer - handling of ldap status is made in common.js and may be accessed via getters and setters * fix undefined security when switching back-end in test * Add errormessage when failure to login, getter - setter for logged in user - When user fails to login a red message should appear indicating what was wrong - Always on buttons for subscriptions got a darker blue, the light blue color looked like they where disabled always - Getter and setter for logged in user has been added, making it easier to get username when needed - String/username validation is made through a function in common making it easier to validate is a username/string exist
1 parent ab04d81 commit c6ec741

File tree

10 files changed

+202
-177
lines changed

10 files changed

+202
-177
lines changed

src/functionaltest/java/com/ericsson/ei/frontend/TestSwitchBackend.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ public static void setUpMocks() throws IOException {
9393
mockClient2.when(request().withMethod("GET").withPath("/auth/checkStatus"))
9494
.respond(response().withStatusCode(200).withBody(""));
9595

96+
mockClient2.when(request().withMethod("GET").withPath("/auth"))
97+
.respond(response().withStatusCode(200).withBody("{\"security\":false}"));
98+
9699
String newInstanceSubscriptionResponse = getJSONStringFromFile(NEW_INSTANCE_SUBSCRIPTION_RESPONSE_FILEPATH);
97100
mockClient2.when(request().withMethod("GET").withPath("/subscriptions"))
98101
.respond(response().withStatusCode(200).withBody(newInstanceSubscriptionResponse));
@@ -101,6 +104,12 @@ public static void setUpMocks() throws IOException {
101104
DEFAULT_INSTANCE_SUBSCRIPTION_RESPONSE_FILEPATH);
102105
mockClient1.when(request().withMethod("GET").withPath("/subscriptions"))
103106
.respond(response().withStatusCode(200).withBody(defaultInstanceSubscriptionResponse));
107+
108+
mockClient1.when(request().withMethod("GET").withPath("/auth/checkStatus"))
109+
.respond(response().withStatusCode(200).withBody(""));
110+
111+
mockClient1.when(request().withMethod("GET").withPath("/auth"))
112+
.respond(response().withStatusCode(200).withBody("{\"security\":false}"));
104113
}
105114

106115
@AfterClass

src/main/resources/application.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ ei.eiffel.documentation.urls={ "EI Frontend GitHub": "https://github.com/eiffel-
3232
#### LOGGING #########
3333
logging.level.root: INFO
3434
logging.level.org.springframework.web: INFO
35-
logging.level.com.ericsson.ei: INFO
35+
logging.level.com.ericsson.ei: INFO

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

Lines changed: 86 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,47 @@ var router = new Navigo(null, true, '#');
22
var frontendServiceUrl = $('#frontendServiceUrl').text();
33
var frontendServiceBackEndPath = "/backend";
44
var timerInterval;
5+
var ldapEnabled = true;
6+
7+
// Start ## getters and setters
8+
9+
function isLdapEnabled(){
10+
return Boolean(ldapEnabled);
11+
}
12+
13+
function setLdapEnabled(value){
14+
ldapEnabled = Boolean(value);
15+
}
16+
17+
function getCurrentUser() {
18+
return sessionStorage.getItem("currentUser");
19+
}
20+
21+
function setCurrentUser(user) {
22+
sessionStorage.removeItem("currentUser");
23+
sessionStorage.setItem("currentUser", user);
24+
}
25+
26+
// End ## getters and setters
27+
28+
function stringContainsSubstring(string, substring) {
29+
var isSubstring = string.indexOf(substring) !== -1;
30+
return isSubstring;
31+
}
532

633
function addBackendParameter(url) {
34+
var parameterKey = "backendname";
35+
736
if (!sessionStorage.selectedActive) {
837
return url;
938
}
10-
var delimiter = "";
11-
var parameterKey = "backendname";
1239

13-
if (url.includes("?")) {
40+
var delimiter = "?";
41+
if (stringContainsSubstring(url, delimiter)) {
42+
// url has delimeter ?, then delimeter should be &
1443
delimiter = "&";
15-
} else {
16-
delimiter = "?";
1744
}
45+
1846
url = url + delimiter + parameterKey + "=" + sessionStorage.selectedActive;
1947
return url;
2048
}
@@ -32,7 +60,7 @@ AjaxHttpSender.prototype.sendAjax = function (contextPath, type, data, callback,
3260
if (!dataType) {
3361
dataType = "json";
3462
}
35-
url = addBackendParameter(frontendServiceUrl+contextPath)
63+
url = addBackendParameter(frontendServiceUrl+contextPath);
3664
$.ajax({
3765
url: url,
3866
type: type,
@@ -61,9 +89,10 @@ AjaxHttpSender.prototype.sendAjax = function (contextPath, type, data, callback,
6189
}
6290
}
6391
});
64-
}
92+
};
6593
// /Stop ## Global AJAX Sender function ##################################
6694

95+
// Start ## Common functions ##
6796
function formatUrl(host, port, useHttps, contextPath) {
6897
var protocol = "http";
6998
if (useHttps) {
@@ -87,6 +116,21 @@ function formatUrl(host, port, useHttps, contextPath) {
87116
return protocol + "://" + host + port + contextPath;
88117
}
89118

119+
function isString(value) {
120+
var isString = typeof value === 'string' || value instanceof String;
121+
return isString;
122+
}
123+
124+
function isStringDefined(value) {
125+
var isDefined = false;
126+
if (isString(value)) {
127+
isDefined = value.length != 0;
128+
}
129+
return isDefined;
130+
}
131+
132+
// /Stop ## Common functions ##################################
133+
90134
// Start ## Routing ##
91135
var routes = {};
92136
routes["subscriptions"] = function () {
@@ -164,20 +208,19 @@ function updateBackEndInstanceList() {
164208
});
165209
}
166210

167-
function singleInstanceModel(name, host, port, contextPath, https, active) {
168-
this.name = ko.observable(name),
169-
this.host = ko.observable(host),
170-
this.port = ko.observable(port),
171-
this.contextPath = ko.observable(contextPath),
172-
this.https = ko.observable(https),
173-
this.active = ko.observable(active),
174-
this.information = name.toUpperCase() + " - " + host + " " + port + "/" + contextPath;
211+
function singleInstanceModel(name, host, port, contextPath, https, active, defaultBackend) {
212+
this.name = ko.observable(name);
213+
this.host = ko.observable(host);
214+
this.port = ko.observable(port);
215+
this.contextPath = ko.observable(contextPath);
216+
this.https = ko.observable(https);
217+
this.active = ko.observable(active);
218+
this.defaultBackend = ko.observable(defaultBackend);
219+
this.information = name.toUpperCase() + " - " + host + " " + port + "/" + contextPath;
175220
}
176221

177-
function viewModel(backendInstanceData) {
178-
var self = this;
179-
self.instances = ko.observableArray();
180-
var jsonBackendInstanceData = JSON.parse(ko.toJSON(backendInstanceData));
222+
function getInstanceModels(jsonBackendInstanceData) {
223+
instanceModels = [];
181224

182225
for (var i = 0; i < jsonBackendInstanceData.length; i++) {
183226
var instanceData = jsonBackendInstanceData[i];
@@ -187,9 +230,10 @@ function viewModel(backendInstanceData) {
187230
var port = instanceData.port;
188231
var https = instanceData.https;
189232
var contextPath = instanceData.contextPath;
233+
var defaultBackend = instanceData.defaultBackend;
190234

191235
var thisInstanceShouldBeSelectedAsActive =
192-
instanceData.defaultBackend == true && !sessionStorage.selectedActive ||
236+
defaultBackend == true && !sessionStorage.selectedActive ||
193237
sessionStorage.selectedActive && sessionStorage.selectedActive == name;
194238

195239
if (thisInstanceShouldBeSelectedAsActive) {
@@ -198,27 +242,38 @@ function viewModel(backendInstanceData) {
198242
}
199243

200244
sessionStorage.setItem(name, formatUrl(host, port, https, contextPath));
201-
var singleInstance = new singleInstanceModel(name, host, port, contextPath, https, isActive);
202-
self.instances.push(singleInstance);
245+
var singleInstance = new singleInstanceModel(name, host, port, contextPath, https, isActive, defaultBackend);
246+
instanceModels.push(singleInstance);
203247
}
248+
return instanceModels;
249+
}
250+
251+
function viewModel(backendInstanceData) {
252+
var self = this;
204253

254+
var jsonBackendInstanceData = JSON.parse(ko.toJSON(backendInstanceData));
255+
var instanceModels = getInstanceModels(jsonBackendInstanceData);
205256
self.selectedActive = ko.observable(sessionStorage.selectedActive);
206257

258+
self.instances = ko.observableArray();
259+
instanceModels.forEach(function (instanceModel) {
260+
self.instances.push(instanceModel);
261+
});
262+
207263
self.onChange = function () {
208264
if (typeof self.selectedActive() !== "undefined") {
209265
sessionStorage.selectedActive = self.selectedActive();
210266
location.reload();
211267
} else {
212268
$.jGrowl("Please choose backend instance", { sticky: false, theme: 'Error' });
213269
}
214-
}
270+
};
215271
}
216272
// End ## Load Back end list ##
217273

218274
// Start ## Login and Security ##
219275
function doIfUserLoggedIn(user) {
220-
sessionStorage.removeItem("currentUser");
221-
sessionStorage.setItem("currentUser", user);
276+
setCurrentUser(user);
222277
$("#userItem").show();
223278
$("#userItem").addClass("user-login");
224279
$("#ldapUserName").text(user);
@@ -228,7 +283,7 @@ function doIfUserLoggedIn(user) {
228283
}
229284

230285
function doIfUserLoggedOut() {
231-
sessionStorage.removeItem("currentUser");
286+
setCurrentUser("");
232287
$("#userItem").show();
233288
$("#userItem").removeClass("user-login");
234289
$("#ldapUserName").text("Guest");
@@ -246,8 +301,10 @@ function doIfSecurityOff() {
246301
function checkBackendSecured() {
247302
var callback = {
248303
success: function (responseData, textStatus) {
249-
var ldapEnabled = JSON.parse(ko.toJSON(responseData)).security;
250-
if (ldapEnabled == true) {
304+
var response = JSON.parse(ko.toJSON(responseData));
305+
var ldapStatus = response.security;
306+
setLdapEnabled(ldapStatus);
307+
if (isLdapEnabled()) {
251308
checkLoggedInUser();
252309
} else {
253310
doIfSecurityOff();
@@ -277,9 +334,6 @@ function checkLoggedInUser() {
277334
ajaxHttpSender.sendAjax(contextPath, "GET", null, callback);
278335
}
279336

280-
function getCurrentUserInSession() {
281-
return sessionStorage.getItem("currentUser");
282-
}
283337
// End ## Login and Security ##
284338

285339
// Start ## Status Indicator ##
@@ -293,7 +347,7 @@ var statusType = {
293347
var statusText = {
294348
backend_down: "<strong>Back end is down!</strong> Wait for it go up or switch to another back end before continuing!",
295349
test_rules_disabled: "<strong>Test Rule service is disabled!</strong> To enable it set the backend property [testaggregated.enabled] as [true]"
296-
}
350+
};
297351

298352
function addStatusIndicator(statusType, statusText) {
299353
var statusIndicator = $(".content")[0].previousElementSibling;

src/main/resources/static/js/errorMessages.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,54 +13,54 @@ function viewModel(data) {
1313
for (var i = 0; i < json.length; i++) {
1414
self.addErrorMessage(json[i].message);
1515
}
16-
}
16+
};
1717
self.addErrorMessage = function (data) {
1818
var model = new messageModel(data);
1919
self.errorMessages.push(model);
20-
}
20+
};
2121
self.removeErrorMessage = function (index) {
2222
var length = self.errorMessages.length;
2323
var realIndex = length - 1 - index;
2424
self.errorMessages.splice(realIndex, 1);
2525
self.mergeErrorMessages();
26-
}
26+
};
2727
self.removeAllErrorMessages = function () {
2828
self.errorMessages([]);
2929
sessionStorage.setItem('ei.errorMessages', '[]');
3030
sessionStorage.setItem('ei.errorMessagesNew', '[]');
31-
}
31+
};
3232
self.storeErrorMessage = function (data) {
33-
storedNew.push({ "message": data })
33+
storedNew.push({ "message": data });
3434
sessionStorage.setItem('ei.errorMessagesNew', JSON.stringify(storedNew));
3535
self.updateNewMessagesLength();
36-
}
36+
};
3737
self.mergeErrorMessages = function () {
3838
storedOld = ko.toJS(self.errorMessages);
3939
storedNew = [];
4040
sessionStorage.setItem('ei.errorMessages', JSON.stringify(storedOld));
4141
sessionStorage.setItem('ei.errorMessagesNew', JSON.stringify(storedNew));
4242
self.updateNewMessagesLength();
43-
}
43+
};
4444
self.updateNewMessagesLength = function () {
4545
self.newMessagesLength(storedNew.length);
46-
}
46+
};
4747
self.expandMessage = function (event) {
4848
if (event.target.classList.contains("expand")) {
4949
event.target.classList.remove("expand");
5050
} else {
5151
self.resetExpandMessage();
5252
event.target.classList.add("expand");
5353
}
54-
}
54+
};
5555
self.resetExpandMessage = function () {
5656
$(".alert-message").removeClass("expand");
57-
}
57+
};
5858
self.stopPropagation = function () {
5959
$('.alert-menu').on('click', function (event) {
6060
event.stopPropagation();
6161
event.preventDefault();
6262
});
63-
}
63+
};
6464
}
6565
var vm = new viewModel();
6666
vm.init();

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

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,13 @@
11
jQuery(document).ready(function () {
2+
function rerouteToSubscription() {
3+
checkBackendSecured();
4+
if ( !isLdapEnabled() || isStringDefined(getCurrentUser()) ) {
5+
console.log("Ldap not enabled or user already logged in, this page should not be accessable!");
6+
navigateToRoute('subscriptions');
7+
}
28

3-
function checkBackendSecured() {
4-
var callback = {
5-
beforeSend: function () {
6-
},
7-
success: function (responseData, textStatus) {
8-
var currentUser = getCurrentUserInSession();
9-
var ldapEnabled = responseData.security;
10-
if (ldapEnabled == false || (ldapEnabled == true && currentUser != null)) {
11-
navigateToRoute('subscriptions');
12-
}
13-
},
14-
error: function (responseData) {
15-
navigateToRoute('subscriptions');
16-
},
17-
complete: function () {
18-
}
19-
};
20-
var ajaxHttpSender = new AjaxHttpSender();
21-
var contextPath = "/auth";
22-
ajaxHttpSender.sendAjax(contextPath, "GET", null, callback);
239
}
24-
25-
checkBackendSecured();
10+
rerouteToSubscription();
2611

2712
// /Start ## Knockout ####################################################
2813
function loginModel() {
@@ -40,10 +25,12 @@ jQuery(document).ready(function () {
4025
var token = window.btoa(dataJSON.ldapUserName + ":" + dataJSON.password);
4126
sendLoginRequest(token);
4227
}
43-
}
28+
};
4429
}
4530

4631
function sendLoginRequest(token) {
32+
$('#loginError').hide();
33+
$('#loginError').removeClass("is-invalid");
4734
var callback = {
4835
beforeSend: function (XMLHttpRequest) {
4936
XMLHttpRequest.setRequestHeader("Authorization", "Basic " + token);
@@ -55,14 +42,24 @@ jQuery(document).ready(function () {
5542
navigateToRoute('subscriptions');
5643
},
5744
error: function (XMLHttpRequest, textStatus, errorThrown) {
58-
window.logMessages("Bad credentials");
45+
if (XMLHttpRequest.status == 401) {
46+
window.logMessages("Bad credentials");
47+
$('#loginError').text("Invalid username and/or password!");
48+
$('#loginError').addClass("is-invalid");
49+
$('#loginError').show();
50+
} else {
51+
window.logMessages("Unknown login error");
52+
$('#loginError').text("Unknown login error!");
53+
$('#loginError').addClass("is-invalid");
54+
$('#loginError').show();
55+
}
5956
},
6057
complete: function () {
6158
}
6259
};
6360
var ajaxHttpSender = new AjaxHttpSender();
6461
var contextPath = "/auth/login";
65-
ajaxHttpSender.sendAjax(contextPath, "GET", token, callback);
62+
ajaxHttpSender.sendAjax(contextPath, "GET", "", callback);
6663
}
6764

6865
var observableObject = $("#viewModelDOMObject")[0];

0 commit comments

Comments
 (0)