"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: '9.1.07.000', apiVersion: 9010700, 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.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 $q.all([authService.serverState(), authService.sessionStatus()]) .then(function () { if (session.alive) { $rootScope.$broadcast(AUTH_EVENTS.SESSION_ACTIVE); } else { if (authService.isSSOEnabled()) { 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(); }); } } return authModel; } ]); }());