116 lines
4.7 KiB
JavaScript
116 lines
4.7 KiB
JavaScript
"use strict";
|
|
(function () {
|
|
'use strict';
|
|
angular.module('securityModule')
|
|
.factory('authModel', ['authService', 'session', '$window', '$q', '$rootScope', 'AUTH_EVENTS', 'systemAlertService', '$filter',
|
|
function (authService, session, $window, $q, $rootScope, AUTH_EVENTS, systemAlertService, $filter) {
|
|
var authModel = {};
|
|
/**
|
|
* Login user
|
|
*
|
|
* @param userId
|
|
* @param password
|
|
* @returns {*}
|
|
*/
|
|
authModel.login = function (userId, password) {
|
|
var requestParams = { userId: encodeURIComponent(userId) };
|
|
var loginData = {
|
|
password: password,
|
|
appName: 'Galileo',
|
|
appVersion: '22.1.06.000',
|
|
apiVersion: 22100600,
|
|
locale: window.myitsmLocale,
|
|
deviceToken: 'dummyToken',
|
|
os: $window.navigator.userAgent,
|
|
model: 'Web Client'
|
|
};
|
|
if (authService.isSSOEnabled()) {
|
|
requestParams.userId = null;
|
|
delete loginData.password;
|
|
}
|
|
return authService.login(requestParams, loginData);
|
|
};
|
|
/**
|
|
* Logout current user
|
|
*
|
|
* @returns {$promise|*}
|
|
*/
|
|
authModel.logout = function () {
|
|
return authService.logout();
|
|
};
|
|
authModel.isSSOEnabled = function () {
|
|
return authService.isSSOEnabled();
|
|
};
|
|
authModel.getMidtierUrl = function () {
|
|
return authService.getMidtierUrl();
|
|
};
|
|
authModel.isAuthenticated = function () {
|
|
return authService.isAuthenticated();
|
|
};
|
|
authModel.isAuthorized = function (authorizedRoles) {
|
|
if (!authorizedRoles) { //access property is not specified for this current state, not authorized
|
|
return false;
|
|
}
|
|
if (_.isArray(authorizedRoles) && authorizedRoles.length === 0) { //access property is [], free access
|
|
return true;
|
|
}
|
|
for (var i = 0; i < authorizedRoles.length; i++) { //validate access roles with user roles
|
|
if (authService.isAuthorized(authorizedRoles[i])) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
};
|
|
authModel.checkSessionStatus = function () {
|
|
console.log('authModel.checkSessionStatus');
|
|
return authService.serverState().then(function () {
|
|
return authService.sessionStatus().then(function () {
|
|
if (session.alive) {
|
|
$rootScope.$broadcast(AUTH_EVENTS.SESSION_ACTIVE);
|
|
}
|
|
else {
|
|
if (authService.isSSOEnabled()) {
|
|
return authModel.login().catch(handleSSOLoginFailed);
|
|
}
|
|
else {
|
|
$rootScope.$broadcast(AUTH_EVENTS.NOT_AUTHENTICATED); //show login screen
|
|
return $q.reject('NOT_AUTHENTICATED');
|
|
}
|
|
}
|
|
});
|
|
});
|
|
};
|
|
authModel.deRegister = function () {
|
|
return authService.deRegister();
|
|
};
|
|
function handleSSOLoginFailed(error) {
|
|
if (error.status === 401) {
|
|
var modal = systemAlertService.modal({
|
|
type: 'error',
|
|
title: $filter('i18n')('error'),
|
|
text: $filter('i18n')('user.loginError.401'),
|
|
buttons: [
|
|
{
|
|
text: $filter('i18n')('common.labels.ok'),
|
|
data: true
|
|
}
|
|
]
|
|
});
|
|
modal.result.then(function () {
|
|
authService.logout();
|
|
});
|
|
}
|
|
// beckend fix is pending to show proper error message
|
|
if (error.status === 500 || error.status === 403) {
|
|
systemAlertService.modal({
|
|
type: 'error',
|
|
title: $filter('i18n')('error'),
|
|
text: $filter('i18n')('error.unauthorized')
|
|
});
|
|
}
|
|
}
|
|
return authModel;
|
|
}
|
|
]);
|
|
}());
|