Skip to content

Commit

Permalink
BAH-3037 | Allow users to login onto assigned login locations (#589)
Browse files Browse the repository at this point in the history
* Introduced screen for login location. WIP

* minor fixes for getting provider attributes

* fixed null pointer exception if last login location is null

* Display only those locations which is assigned to the provider

* Stored assigned locations in local storage and use items from there to display location in dashboard and clinical dashboard header

* #BAH-3037 | Rename. assignedLocations to loginLocations | [Riya Kumari]

* #BAH-3037 | Test. Login location controller | [Riya Kumari]

* BAH-3037 | Undo. Karma to run in Firefox from Chrome | [Riya Kumari]

* BAH-3037 | Refactor. Remove unused locale key | [Riya Kumari]

* #BAH-3037 | Remove unnecessary comments and refactor variable name | [Riya Kumari]

* #BAH-3037 | Add. Params for getProviderFromServer to get  attributes | [Riya Kumari]

* #BAH-3037 | Reformatting. es-Lint formatter rules | [Riya Kumari]

* #BAH-3037 | Reformatting. es-Lint formatter rules | [Riya Kumari]

* #BAH-3037 | Reformatting. es-Lint formatter rules | [Riya Kumari]

* #BAH-3037 | Reformatting. es-Lint formatter rules | [Riya Kumari]

* #BAH-3037 | Reformatting. es-Lint formatter rules | [Riya Kumari]

* BAH-3037 | Refactor. English to french transalation in fr locale file for two keys  | [Riya Kumari]

* BAH-3037 | Fix. Login location screen to show assigned locations after refresh also  | [Riya Kumari]

---------

Co-authored-by: Angshuman Sarkar <[email protected]>
  • Loading branch information
riyaTw and angshu authored Jul 12, 2023
1 parent 4361531 commit de864db
Show file tree
Hide file tree
Showing 15 changed files with 435 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,14 @@ angular.module('bahmni.clinical')
$scope.date = retrospectiveDate ? new Date(retrospectiveDate) : new Date($scope.maxStartDate);
$scope.encounterProvider = getCurrentProvider();
selectedProvider = getCurrentProvider();

const loginLocations = localStorage.getItem("loginLocations");
if (loginLocations) {
$scope.locations = JSON.parse(loginLocations);
return;
}
return locationService.getAllByTag("Login Location").then(function (response) {
$scope.locations = response.data.results;
$scope.selectedLocationUuid = getCurrentCookieLocation().uuid;
}
);
});
};

return init();
Expand Down
3 changes: 2 additions & 1 deletion ui/app/common/auth/authentication.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ angular.module('authentication')
userService.getProviderForUser(data.results[0].uuid).then(function (providers) {
if (!_.isEmpty(providers.results) && hasAnyActiveProvider(providers.results)) {
$rootScope.currentUser = new Bahmni.Auth.User(data.results[0]);
$rootScope.currentUser.currentLocation = $bahmniCookieStore.get(Bahmni.Common.Constants.locationCookieName).name;
$rootScope.currentUser.provider = providers.results[0];
$rootScope.currentUser.currentLocation = null;
$rootScope.$broadcast('event:user-credentialsLoaded', data.results[0]);
deferrable.resolve(data.results[0]);
} else {
Expand Down
3 changes: 2 additions & 1 deletion ui/app/common/auth/userService.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ angular.module('authentication')
return $http.get(Bahmni.Common.Constants.providerUrl, {
method: "GET",
params: {
user: uuid
user: uuid,
v: 'custom:(uuid,display,attributes)'
},
cache: false
});
Expand Down
10 changes: 10 additions & 0 deletions ui/app/home/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ angular.module('bahmni.home', ['ui.router', 'httpErrorInterceptor', 'bahmni.comm
}
}
})
.state('loginLocation', {
url: '/loginLocation',
controller: 'LoginLocationController',
templateUrl: 'views/loginLocation.html',
resolve: {
initialData: function (loginInitialization) {
return loginInitialization();
}
}
})
.state('errorLog', {
url: '/errorLog',
controller: 'ErrorLogController',
Expand Down
27 changes: 23 additions & 4 deletions ui/app/home/controllers/dashboardController.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,26 @@ angular.module('bahmni.home')
return $bahmniCookieStore.get(Bahmni.Common.Constants.locationCookieName) ? $bahmniCookieStore.get(Bahmni.Common.Constants.locationCookieName) : null;
};

var setCurrentLoginLocationForUser = function () {
const currentLoginLocation = getCurrentLocation();
if (currentLoginLocation) {
$scope.selectedLocationUuid = getCurrentLocation().uuid;
} else {
$scope.selectedLocationUuid = null;
}
};

var init = function () {
const loginLocations = localStorage.getItem("loginLocations");
if (loginLocations) {
$scope.locations = JSON.parse(loginLocations);
setCurrentLoginLocationForUser();
return;
}
return locationService.getAllByTag("Login Location").then(function (response) {
$scope.locations = response.data.results;
$scope.selectedLocationUuid = getCurrentLocation().uuid;
}
);
setCurrentLoginLocationForUser();
});
};

var getLocationFor = function (uuid) {
Expand All @@ -33,7 +47,12 @@ angular.module('bahmni.home')
};

$scope.isCurrentLocation = function (location) {
return getCurrentLocation().uuid === location.uuid;
const currentLocation = getCurrentLocation();
if (currentLocation) {
return getCurrentLocation().uuid === location.uuid;
} else {
return false;
}
};

$scope.onLocationChange = function () {
Expand Down
51 changes: 37 additions & 14 deletions ui/app/home/controllers/loginController.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

angular.module('bahmni.home')
.controller('LoginController', ['$rootScope', '$scope', '$window', '$location', 'sessionService', 'initialData', 'spinner', '$q', '$stateParams', '$bahmniCookieStore', 'localeService', '$translate', 'userService', 'auditLogService',
function ($rootScope, $scope, $window, $location, sessionService, initialData, spinner, $q, $stateParams, $bahmniCookieStore, localeService, $translate, userService, auditLogService) {
.controller('LoginController', ['$rootScope', '$scope', '$window', '$location', 'sessionService', 'initialData', 'spinner', '$q', '$stateParams', '$bahmniCookieStore', 'localeService', '$translate', 'userService', 'auditLogService', '$state',
function ($rootScope, $scope, $window, $location, sessionService, initialData, spinner, $q, $stateParams, $bahmniCookieStore, localeService, $translate, userService, auditLogService, $state) {
var redirectUrl = $location.search()['from'];
var landingPagePath = "/dashboard";
var loginPagePath = "/login";
Expand Down Expand Up @@ -118,6 +118,26 @@ angular.module('bahmni.home')
$scope.loginInfo.currentLocation = getLastLoggedinLocation();
};

var checkIfUserHasProviderAttributes = function () {
return $rootScope.currentUser.provider && $rootScope.currentUser.provider.attributes && $rootScope.currentUser.provider.attributes.length > 0;
};

var saveUserAssignedLocationsToLocalStorage = function () {
var userAssignedLocations = $rootScope.currentUser.provider.attributes
.filter(function (attribute) {
return attribute.attributeType.display === "Login Locations";
})
.map(function (attribute) {
return { display: attribute.value.name, uuid: attribute.value.uuid };
});

if (userAssignedLocations.length > 0) {
localStorage.setItem("loginLocations", JSON.stringify(userAssignedLocations));
} else {
localStorage.removeItem("loginLocations");
}
};

$scope.login = function () {
$scope.errorMessageTranslateKey = null;
var deferrable = $q.defer();
Expand All @@ -140,20 +160,18 @@ angular.module('bahmni.home')
deferrable.resolve(data);
return;
}
sessionService.updateSession($scope.loginInfo.currentLocation, null).then(function () {
sessionService.loadCredentials().then(function () {
onSuccessfulAuthentication();
$rootScope.currentUser.addDefaultLocale($scope.selectedLocale);
userService.savePreferences().then(
sessionService.loadCredentials().then(function () {
onSuccessfulAuthentication();
$rootScope.currentUser.addDefaultLocale($scope.selectedLocale);
userService.savePreferences().then(
function () { deferrable.resolve(); },
function (error) { deferrable.reject(error); }
);
logAuditForLoginAttempts("USER_LOGIN_SUCCESS");
}, function (error) {
$scope.errorMessageTranslateKey = error;
deferrable.reject(error);
logAuditForLoginAttempts("USER_LOGIN_FAILED", true);
});
logAuditForLoginAttempts("USER_LOGIN_SUCCESS");
}, function (error) {
$scope.errorMessageTranslateKey = error;
deferrable.reject(error);
logAuditForLoginAttempts("USER_LOGIN_FAILED", true);
});
},
function (error) {
Expand Down Expand Up @@ -211,7 +229,12 @@ angular.module('bahmni.home')
}
});
} else {
$location.url(landingPagePath);
if (checkIfUserHasProviderAttributes()) {
saveUserAssignedLocationsToLocalStorage();
} else {
localStorage.removeItem("loginLocations");
}
$state.go('loginLocation', {});
}
}
);
Expand Down
145 changes: 145 additions & 0 deletions ui/app/home/controllers/loginLocationController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
'use strict';

angular.module('bahmni.home')
.controller('LoginLocationController', ['$rootScope', '$scope', '$window', '$location', 'sessionService', 'initialData', 'spinner', '$q', '$stateParams', '$bahmniCookieStore', 'localeService', '$translate', 'userService', 'auditLogService',
function ($rootScope, $scope, $window, $location, sessionService, initialData, spinner, $q, $stateParams, $bahmniCookieStore, localeService, $translate, userService, auditLogService) {
var redirectUrl = $location.search()['from'];
var landingPagePath = "/dashboard";
var loginPagePath = "/login";
const LOGIN_LOCATIONS = "Login Locations";
$scope.loginInfo = {};
var localeLanguages = [];

var getLocalTimeZone = function () {
var currentLocalTime = new Date().toString();
var localTimeZoneList = currentLocalTime.split(" ");
var localTimeZone = localTimeZoneList[localTimeZoneList.length - 1];
localTimeZone = localTimeZone.substring(1, localTimeZone.length - 1);
return localTimeZone;
};

var userLoginLocations = function () {
var loginLocations = localStorage.getItem("loginLocations");
return loginLocations ? JSON.parse(loginLocations) : [];
};

var identifyLoginLocations = function (allLocations) {
var loginLocations = userLoginLocations();
if (loginLocations.length === 0) {
return allLocations;
}
return loginLocations;
};

$scope.locations = identifyLoginLocations(initialData.locations);

var findLanguageByLocale = function (localeCode) {
return _.find(localeLanguages, function (localeLanguage) {
return localeLanguage.code == localeCode;
});
};

var logAuditForLoginAttempts = function (eventType, isFailedEvent) {
if ($scope.loginInfo.username) {
var messageParams = isFailedEvent ? {userName: $scope.loginInfo.username} : undefined;
auditLogService.log(undefined, eventType, messageParams, 'MODULE_LABEL_LOGIN_KEY');
}
};

var promise = localeService.allowedLocalesList();
localeService.serverDateTime().then(function (response) {
var serverTime = response.data.date;
var offset = response.data.offset;
var localTime = new Date().toLocaleString();
var localtimeZone = getLocalTimeZone();
var localeTimeZone = localTime + " " + localtimeZone;
$scope.timeZoneObject = { serverTime: serverTime, localeTimeZone: localeTimeZone};
if (offset && !new Date().toString().includes(offset)) {
$scope.warning = "Warning";
$scope.warningMessage = "WARNING_SERVER_TIME_ZONE_MISMATCH";
}
});

localeService.getLoginText().then(function (response) {
$scope.logo = response.data.loginPage.logo;
$scope.bottomLogos = response.data.loginPage.bottomLogos;
$scope.headerText = response.data.loginPage.showHeaderText;
$scope.titleText = response.data.loginPage.showTitleText;
$scope.helpLink = response.data.helpLink.url;
});

localeService.getLocalesLangs().then(function (response) {
localeLanguages = response.data.locales;
}).finally(function () {
promise.then(function (response) {
var localeList = response.data.replace(/\s+/g, '').split(',');
$scope.locales = [];
_.forEach(localeList, function (locale) {
var localeLanguage = findLanguageByLocale(locale);
if (_.isUndefined(localeLanguage)) {
$scope.locales.push({"code": locale, "nativeName": locale});
} else {
$scope.locales.push(localeLanguage);
}
});
$scope.selectedLocale = $translate.use() ? $translate.use() : $scope.locales[0].code;
});
});

var getLoginLocationUuid = function () { return $bahmniCookieStore.get(Bahmni.Common.Constants.locationCookieName) ? $bahmniCookieStore.get(Bahmni.Common.Constants.locationCookieName).uuid : null; };

var getLastLoggedinLocation = function () {
return _.find(initialData.locations, function (location) {
return location.uuid === getLoginLocationUuid();
});
};

$scope.loginInfo.currentLocation = getLastLoggedinLocation();

if ($stateParams.showLoginMessage) {
$scope.errorMessageTranslateKey = "LOGIN_LABEL_LOGIN_ERROR_MESSAGE_KEY";
}

var redirectToLandingPageIfAlreadyAuthenticated = function () {
sessionService.get().then(function (data) {
if (data.authenticated) {
$location.path(landingPagePath);
}
});
};

if ($location.path() === loginPagePath) {
redirectToLandingPageIfAlreadyAuthenticated();
}
var onSuccessfulAuthentication = function () {
$scope.loginInfo.currentLocation = getLastLoggedinLocation();
$rootScope.$broadcast('event:auth-loggedin');
};

$scope.updateSessionLocation = function () {
$scope.errorMessageTranslateKey = null;
var deferrable = $q.defer();

sessionService.updateSession($scope.loginInfo.currentLocation, null).then(function () {
sessionService.loadCredentials().then(function () {
onSuccessfulAuthentication();
userService.savePreferences().then(
function () { deferrable.resolve(); },
function (error) { deferrable.reject(error); }
);
logAuditForLoginAttempts("USER_LOGIN_LOCATION_SUCCESS");
}, function (error) {
$scope.errorMessageTranslateKey = error;
deferrable.reject(error);
logAuditForLoginAttempts("USER_LOGIN_LOCATION_FAILED", true);
});
});

spinner.forPromise(deferrable.promise).then(
function (data) {
if (data) return;
$location.url(landingPagePath);
}
);
};
}]);
1 change: 1 addition & 0 deletions ui/app/home/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
<script src="initialization.js"></script>
<script src="loginInitialization.js"></script>
<script src="controllers/loginController.js"></script>
<script src="controllers/loginLocationController.js"></script>
<script src="controllers/dashboardController.js"></script>
<script src="controllers/changePasswordController.js"></script>
<script src="controllers/errorLogController.js"></script>
Expand Down
14 changes: 0 additions & 14 deletions ui/app/home/views/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,6 @@
</div>
</div>

<div class="form-field">
<div class="field-attribute">
<label for="location">
{{'LOGIN_LABEL_LOCATION_KEY' | translate}} <span class="asterick">*</span>
</label>
</div>
<div class="field-value">
<select id="location" ng-model="loginInfo.currentLocation"
ng-options="item as (item.display | translate) for item in locations | orderBy:'display'" required>
<option value="">{{'LOGIN_LABEL_SELECT_LOCATION_KEY' | translate}}</option>
</select>
</div>
</div>

<div class="form-footer">
<button type="submit" class="confirm">{{'LOGIN_LABEL_LOGIN_KEY' | translate}}</button>
</div>
Expand Down
Loading

0 comments on commit de864db

Please sign in to comment.