bahmniapps-commons
Version:
Common Modules carved out from bahmniapps.
486 lines (429 loc) • 17.8 kB
JavaScript
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory();
else if(typeof define === 'function' && define.amd)
define([], factory);
else {
var a = factory();
for(var i in a) (typeof exports === 'object' ? exports : root)[i] = a[i];
}
})(this, function() {
return /******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // identity function for calling harmony imports with the correct context
/******/ __webpack_require__.i = function(value) { return value; };
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, {
/******/ configurable: false,
/******/ enumerable: true,
/******/ get: getter
/******/ });
/******/ }
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 55);
/******/ })
/************************************************************************/
/******/ ({
/***/ 0:
/***/ (function(module, exports, __webpack_require__) {
;
angular.module('authentication').config(['$httpProvider', function ($httpProvider) {
var interceptor = ['$rootScope', '$q', function ($rootScope, $q) {
function success(response) {
return response;
}
function error(response) {
if (response.status === 401) {
$rootScope.$broadcast('event:auth-loginRequired');
}
return $q.reject(response);
}
return {
response: success,
responseError: error
};
}];
$httpProvider.interceptors.push(interceptor);
}]).run(['$rootScope', '$window', '$timeout', function ($rootScope, $window, $timeout) {
$rootScope.$on('event:auth-loginRequired', function () {
$timeout(function () {
$window.location = "../home/index.html#/login";
});
});
}]).service('sessionService', ['$rootScope', '$http', '$q', '$bahmniCookieStore', 'userService', function ($rootScope, $http, $q, $bahmniCookieStore, userService) {
var sessionResourcePath = Bahmni.Common.Constants.RESTWS_V1 + '/session?v=custom:(uuid)';
var getAuthFromServer = function (username, password, otp) {
var btoa = otp ? username + ':' + password + ':' + otp : username + ':' + password;
return $http.get(sessionResourcePath, {
headers: { 'Authorization': 'Basic ' + window.btoa(btoa) },
cache: false
});
};
this.resendOTP = function (username, password) {
var btoa = username + ':' + password;
return $http.get(sessionResourcePath + '&resendOTP=true', {
headers: { 'Authorization': 'Basic ' + window.btoa(btoa) },
cache: false
});
};
var createSession = function (username, password, otp) {
var deferrable = $q.defer();
destroySessionFromServer().success(function () {
getAuthFromServer(username, password, otp).then(function (response) {
if (response.status == 204) {
deferrable.resolve({ "firstFactAuthorization": true });
}
deferrable.resolve(response.data);
}, function (response) {
if (response.status == 401) {
deferrable.reject('LOGIN_LABEL_WRONG_OTP_MESSAGE_KEY');
} else if (response.status == 410) {
deferrable.reject('LOGIN_LABEL_OTP_EXPIRED');
} else if (response.status == 429) {
// Too many requests
deferrable.reject('LOGIN_LABEL_MAX_FAILED_ATTEMPTS');
}
deferrable.reject('LOGIN_LABEL_LOGIN_ERROR_MESSAGE_KEY');
});
}).error(function () {
deferrable.reject('LOGIN_LABEL_LOGIN_ERROR_MESSAGE_KEY');
});
return deferrable.promise;
};
var hasAnyActiveProvider = function (providers) {
return _.filter(providers, function (provider) {
return provider.retired == undefined || provider.retired == "false";
}).length > 0;
};
var self = this;
var destroySessionFromServer = function () {
return $http.delete(sessionResourcePath);
};
var sessionCleanup = function () {
delete $.cookie(Bahmni.Common.Constants.currentUser, null, { path: "/" });
delete $.cookie(Bahmni.Common.Constants.currentUser, null, { path: "/" });
delete $.cookie(Bahmni.Common.Constants.retrospectiveEntryEncounterDateCookieName, null, { path: "/" });
delete $.cookie(Bahmni.Common.Constants.grantProviderAccessDataCookieName, null, { path: "/" });
$rootScope.currentUser = undefined;
};
this.destroy = function () {
var deferrable = $q.defer();
destroySessionFromServer().then(function () {
sessionCleanup();
deferrable.resolve();
});
return deferrable.promise;
};
this.loginUser = function (username, password, location, otp) {
var deferrable = $q.defer();
createSession(username, password, otp).then(function (data) {
if (data.authenticated) {
$bahmniCookieStore.put(Bahmni.Common.Constants.currentUser, username, { path: '/', expires: 7 });
if (location != undefined) {
$bahmniCookieStore.remove(Bahmni.Common.Constants.locationCookieName);
$bahmniCookieStore.put(Bahmni.Common.Constants.locationCookieName, { name: location.display, uuid: location.uuid }, { path: '/', expires: 7 });
}
deferrable.resolve(data);
} else if (data.firstFactAuthorization) {
deferrable.resolve(data);
} else {
deferrable.reject('LOGIN_LABEL_LOGIN_ERROR_MESSAGE_KEY');
}
}, function (errorInfo) {
deferrable.reject(errorInfo);
});
return deferrable.promise;
};
this.get = function () {
return $http.get(sessionResourcePath, { cache: false });
};
this.loadCredentials = function () {
var deferrable = $q.defer();
var currentUser = $bahmniCookieStore.get(Bahmni.Common.Constants.currentUser);
if (!currentUser) {
this.destroy().finally(function () {
$rootScope.$broadcast('event:auth-loginRequired');
deferrable.reject("No User in session. Please login again.");
});
return deferrable.promise;
}
userService.getUser(currentUser).then(function (data) {
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.$broadcast('event:user-credentialsLoaded', data.results[0]);
deferrable.resolve(data.results[0]);
} else {
self.destroy();
deferrable.reject("YOU_HAVE_NOT_BEEN_SETUP_PROVIDER");
}
}, function () {
self.destroy();
deferrable.reject("COULD_NOT_GET_PROVIDER");
});
}, function () {
self.destroy();
deferrable.reject('Could not get roles for the current user.');
});
return deferrable.promise;
};
this.getLoginLocationUuid = function () {
return $bahmniCookieStore.get(Bahmni.Common.Constants.locationCookieName) ? $bahmniCookieStore.get(Bahmni.Common.Constants.locationCookieName).uuid : null;
};
this.changePassword = function (currentUserUuid, oldPassword, newPassword) {
return $http({
method: 'POST',
url: Bahmni.Common.Constants.passwordUrl,
data: {
"oldPassword": oldPassword,
"newPassword": newPassword
},
headers: { 'Content-Type': 'application/json' }
});
};
this.loadProviders = function (userInfo) {
return $http.get(Bahmni.Common.Constants.providerUrl, {
method: "GET",
params: {
user: userInfo.uuid
},
cache: false
}).success(function (data) {
var providerUuid = data.results.length > 0 ? data.results[0].uuid : undefined;
$rootScope.currentProvider = { uuid: providerUuid };
});
};
}]).factory('authenticator', ['$rootScope', '$q', '$window', 'sessionService', function ($rootScope, $q, $window, sessionService) {
var authenticateUser = function () {
var defer = $q.defer();
var sessionDetails = sessionService.get();
sessionDetails.then(function (response) {
if (response.data.authenticated) {
defer.resolve();
} else {
defer.reject('User not authenticated');
$rootScope.$broadcast('event:auth-loginRequired');
}
});
return defer.promise;
};
return {
authenticateUser: authenticateUser
};
}]).directive('logOut', ['sessionService', '$window', 'configurationService', 'auditLogService', function (sessionService, $window, configurationService, auditLogService) {
return {
link: function (scope, element) {
element.bind('click', function () {
scope.$apply(function () {
auditLogService.log(undefined, 'USER_LOGOUT_SUCCESS', undefined, 'MODULE_LABEL_LOGOUT_KEY').then(function () {
sessionService.destroy().then(function () {
$window.location = "../home/index.html#/login";
});
});
});
});
}
};
}]).directive('btnUserInfo', [function () {
return {
restrict: 'CA',
link: function (scope, elem) {
elem.bind('click', function (event) {
$(this).next().toggleClass('active');
event.stopPropagation();
});
$(document).find('body').bind('click', function () {
$(elem).next().removeClass('active');
});
}
};
}]);
/***/ }),
/***/ 1:
/***/ (function(module, exports, __webpack_require__) {
;
Bahmni.Auth.User = function (user) {
angular.extend(this, user);
this.userProperties = user.userProperties || {};
this.favouriteObsTemplates = this.userProperties.favouriteObsTemplates ? this.userProperties.favouriteObsTemplates.split("###") : [];
this.favouriteWards = this.userProperties.favouriteWards ? this.userProperties.favouriteWards.split("###") : [];
this.recentlyViewedPatients = this.userProperties.recentlyViewedPatients ? JSON.parse(this.userProperties.recentlyViewedPatients) : [];
this.toContract = function () {
var user = angular.copy(this);
user.userProperties.favouriteObsTemplates = this.favouriteObsTemplates.join("###");
user.userProperties.favouriteWards = this.favouriteWards.join("###");
user.userProperties.recentlyViewedPatients = JSON.stringify(this.recentlyViewedPatients);
delete user.favouriteObsTemplates;
delete user.favouriteWards;
delete user.recentlyViewedPatients;
return user;
};
this.addDefaultLocale = function (locale) {
this.userProperties['defaultLocale'] = locale;
};
this.addToRecentlyViewed = function (patient, maxPatients) {
if (!_.some(this.recentlyViewedPatients, { 'uuid': patient.uuid })) {
this.recentlyViewedPatients.unshift({
uuid: patient.uuid,
name: patient.name,
identifier: patient.identifier
});
if (_.size(this.recentlyViewedPatients) >= maxPatients) {
this.recentlyViewedPatients = _.take(this.recentlyViewedPatients, maxPatients);
}
}
};
this.isFavouriteObsTemplate = function (conceptName) {
return _.includes(this.favouriteObsTemplates, conceptName);
};
this.toggleFavoriteObsTemplate = function (conceptName) {
if (this.isFavouriteObsTemplate(conceptName)) {
this.favouriteObsTemplates = _.without(this.favouriteObsTemplates, conceptName);
} else {
this.favouriteObsTemplates.push(conceptName);
}
};
this.isFavouriteWard = function (wardName) {
return _.includes(this.favouriteWards, wardName);
};
this.toggleFavoriteWard = function (wardName) {
if (this.isFavouriteWard(wardName)) {
this.favouriteWards = _.without(this.favouriteWards, wardName);
} else {
this.favouriteWards.push(wardName);
}
};
};
/***/ }),
/***/ 2:
/***/ (function(module, exports, __webpack_require__) {
;
angular.module('authentication').service('userService', ['$rootScope', '$http', '$q', function ($rootScope, $http, $q) {
var getUserFromServer = function (userName) {
return $http.get(Bahmni.Common.Constants.userUrl, {
method: "GET",
params: {
username: userName,
v: "custom:(username,uuid,person:(uuid,),privileges:(name,retired),userProperties)"
},
cache: false
});
};
this.getUser = function (userName) {
var deferrable = $q.defer();
getUserFromServer(userName).success(function (data) {
deferrable.resolve(data);
}).error(function () {
deferrable.reject('Unable to get user data');
});
return deferrable.promise;
};
this.savePreferences = function () {
var deferrable = $q.defer();
var user = $rootScope.currentUser.toContract();
$http.post(Bahmni.Common.Constants.userUrl + "/" + user.uuid, { "uuid": user.uuid, "userProperties": user.userProperties }, {
withCredentials: true
}).then(function (response) {
$rootScope.currentUser.userProperties = response.data.userProperties;
deferrable.resolve();
});
return deferrable.promise;
};
var getProviderFromServer = function (uuid) {
return $http.get(Bahmni.Common.Constants.providerUrl, {
method: "GET",
params: {
user: uuid
},
cache: false
});
};
this.getProviderForUser = function (uuid) {
var deferrable = $q.defer();
getProviderFromServer(uuid).success(function (data) {
if (data.results.length > 0) {
var providerName = data.results[0].display.split("-")[1];
data.results[0].name = providerName ? providerName.trim() : providerName;
deferrable.resolve(data);
} else {
deferrable.reject("UNABLE_TO_GET_PROVIDER_DATA");
}
}).error(function () {
deferrable.reject("UNABLE_TO_GET_PROVIDER_DATA");
});
return deferrable.promise;
};
this.getPasswordPolicies = function () {
return $http.get(Bahmni.Common.Constants.passwordPolicyUrl, {
method: "GET",
withCredentials: true
});
};
}]);
/***/ }),
/***/ 55:
/***/ (function(module, exports, __webpack_require__) {
;
window.Bahmni = Bahmni || {};
Bahmni.Auth = Bahmni.Auth || {};
angular.module('authentication', ['ui.router']);
__webpack_require__(1);
__webpack_require__(2);
__webpack_require__(0);
/***/ })
/******/ });
});