"use strict"; (function () { 'use strict'; angular.module('myitsmApp') .controller('LoginController', ['$rootScope', '$scope', '$cookies', 'authModel', 'events', 'utilityFunctions', '$location', function ($rootScope, $scope, $cookies, authModel, events, utilityFunctions, $location) { var state = { loginPending: false }, errorMessageType = '', errorMessageTypeEnforcePwdConstraint = '', isUsernameOrPasswordEmpty = function () { var form = $scope.loginForm; if (!form.username) { errorMessageType = 'usernameRequired'; } if (!form.password) { errorMessageType = 'passwordRequired'; } if (!form.username && !form.password) { errorMessageType = 'usernameAndPasswordRequired'; } return errorMessageType; }; $scope.state = state; $scope.loginForm = { username: '', password: '', accessibility: $cookies.get('accessibility') === 'true' ? true : false, action: function () { var that = this; errorMessageType = null; errorMessageTypeEnforcePwdConstraint = null; state.loginPending = true; if (isUsernameOrPasswordEmpty()) { $scope.errorMessageType = errorMessageType; state.loginPending = false; return; } $scope.sendMessageToPwaDomain({ username: that.username, password: that.password, isLoginCredential: true }); authModel.login(that.username, that.password).then(function () { var expDate = new Date(); expDate.setDate(expDate.getDate() + 365); if ($location.protocol() === "https") { $cookies.put('accessibility', that.accessibility, { expires: expDate, httpOnly: true, secure: true }); } else { $cookies.put('accessibility', that.accessibility, { expires: expDate, httpOnly: true }); } }, function (error) { if (error) { if (error.status === 403 && error.data.error === 'MOBILITY_ERROR_SESSION_EXPIRED') { errorMessageType = '403'; } else if (error.status === 401) { that.username = ''; errorMessageType = '401'; } else if (error.data.error === 'MOBILITY_ERROR_LOGIN' && error.data.errorCode == '1006' && error.data.detailMessage.indexOf('(8102)') >= 0) { // jshint ignore:line errorMessageTypeEnforcePwdConstraint = error.data.detailMessage; } else if (error.data.error === 'MOBILITY_ERROR_LOGIN' && error.data.errorCode == '1006' && error.data.ARConnectionProblem) { // jshint ignore:line errorMessageType = '90'; } else if (error.data.error === 'MOBILITY_ERROR_LOGIN' && error.data.errorCode == '1006') { // jshint ignore:line errorMessageType = '1006'; } else { errorMessageType = 'unknown'; } } sessionStorage.removeItem('calendarFilterState'); sessionStorage.removeItem('calendarFilterView'); sessionStorage.removeItem('calendarFilterDate'); $scope.errorMessageType = errorMessageType; $scope.errorMessageTypeEnforcePwdConstraint = errorMessageTypeEnforcePwdConstraint; state.loginPending = false; that.password = ''; }); } }; $scope.showLoginHelp = function () { return false; }; $scope.loginBgrClass = backgroundChange(); /** * Utility function which randomly picks background-image for login screen * */ function backgroundChange() { var imagesAmount = 4; //Same amount of CSS classes named "bgr-"{{#image}} should exist in _login.scss var randomId = Math.floor(Math.random() * imagesAmount) + 1; return 'bgr-' + randomId; } } ]); }());