diff --git a/ui/app/clinical/common/controllers/patientListHeaderController.js b/ui/app/clinical/common/controllers/patientListHeaderController.js index c757a9c0fb..39a696df19 100644 --- a/ui/app/clinical/common/controllers/patientListHeaderController.js +++ b/ui/app/clinical/common/controllers/patientListHeaderController.js @@ -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(); diff --git a/ui/app/common/auth/authentication.js b/ui/app/common/auth/authentication.js index 55cb2f5a0a..af1ae84434 100644 --- a/ui/app/common/auth/authentication.js +++ b/ui/app/common/auth/authentication.js @@ -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 { diff --git a/ui/app/common/auth/userService.js b/ui/app/common/auth/userService.js index dc357bc04c..eee047fa84 100644 --- a/ui/app/common/auth/userService.js +++ b/ui/app/common/auth/userService.js @@ -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 }); diff --git a/ui/app/home/app.js b/ui/app/home/app.js index bb0b33b480..8635881f2b 100644 --- a/ui/app/home/app.js +++ b/ui/app/home/app.js @@ -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', diff --git a/ui/app/home/controllers/dashboardController.js b/ui/app/home/controllers/dashboardController.js index e2da637591..398d6b608c 100644 --- a/ui/app/home/controllers/dashboardController.js +++ b/ui/app/home/controllers/dashboardController.js @@ -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) { @@ -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 () { diff --git a/ui/app/home/controllers/loginController.js b/ui/app/home/controllers/loginController.js index a3b2f6004f..9d649ff9e1 100644 --- a/ui/app/home/controllers/loginController.js +++ b/ui/app/home/controllers/loginController.js @@ -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"; @@ -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(); @@ -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) { @@ -211,7 +229,12 @@ angular.module('bahmni.home') } }); } else { - $location.url(landingPagePath); + if (checkIfUserHasProviderAttributes()) { + saveUserAssignedLocationsToLocalStorage(); + } else { + localStorage.removeItem("loginLocations"); + } + $state.go('loginLocation', {}); } } ); diff --git a/ui/app/home/controllers/loginLocationController.js b/ui/app/home/controllers/loginLocationController.js new file mode 100644 index 0000000000..bfd1bb45a4 --- /dev/null +++ b/ui/app/home/controllers/loginLocationController.js @@ -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); + } + ); + }; + }]); diff --git a/ui/app/home/index.html b/ui/app/home/index.html index 69def74123..8f3f372cb2 100644 --- a/ui/app/home/index.html +++ b/ui/app/home/index.html @@ -99,6 +99,7 @@ + diff --git a/ui/app/home/views/login.html b/ui/app/home/views/login.html index dba56c3751..09dd7ab299 100644 --- a/ui/app/home/views/login.html +++ b/ui/app/home/views/login.html @@ -53,20 +53,6 @@ -