loginCtrl.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. app.controller('loginCtrl',
  2. function ($scope,$http,$cookies, $location,$timeout, utilsSvc, userService, UserManager) {
  3. $scope.user = {username:'', password:'', userProfile:'', userDescription: ''};
  4. //$scope.UserManager = new UserManager();
  5. $scope.login = function() {
  6. userService.clearFilter();
  7. try {
  8. UserManager.Login($scope.user).$promise.then(function(data) {
  9. $cookies.put('user', JSON.stringify(data));
  10. $scope.clearLocalStorage();
  11. $location.path('/');
  12. }, function(error) {
  13. $cookies.remove('user');
  14. if (error.status=404)
  15. utilsSvc.show("Autenticazione fallita");
  16. else if (error.status=401)
  17. utilsSvc.show("E' necessario effettuare l'autenticazione");
  18. else
  19. utilsSvc.show("Errore " + error.status+ " durante la comunicazione con il server: " + error.statusText);
  20. });
  21. } catch(Ex) {
  22. Console.log(Ex);
  23. }
  24. }
  25. $scope.clearLocalStorage = function(){
  26. localStorage.removeItem('filter');
  27. localStorage.removeItem('filterDistinte');
  28. localStorage.removeItem('filterDistinteAttivo');
  29. localStorage.removeItem('filterDistinteSelectedIndex');
  30. }
  31. $scope.checkCredential = function () {
  32. userService.login($scope.user, $location)
  33. .success(function(response) {
  34. if(response != null && response!="") {
  35. console.log("login successful.");
  36. // todo: setta il cookie di autenticazione
  37. $cookies.put("user", JSON.stringify(response));
  38. var path = $location.$$protocol+'://'+$location.$$host+':'+$location.$$port+'/sicura';
  39. window.location = path;
  40. //$location.path('/');
  41. } else {
  42. console.log("login failed.");
  43. $cookies.remove("user");
  44. utilsSvc.showMessage('Nome utente o password errati');
  45. ; }
  46. })
  47. .error(function(err) {
  48. console.log("Error occurred during login.");
  49. utilsSvc.showMessage('Nome utente o password errati');
  50. });
  51. }
  52. $scope.getGroups = function(){
  53. if ($scope.user.username != '' && $scope.user.password != '') {
  54. UserManager.AutenticationGroups($scope.user,
  55. function(data){
  56. console.log(data);
  57. $scope.profiles = data;
  58. }
  59. );
  60. }
  61. }
  62. $scope.init = function() {
  63. $timeout(function() {
  64. // Fix for password field not updating label correctly in Chrome
  65. // document.querySelector("input[type=password]").focus();
  66. // document.querySelector("input[type=password]").value = " ";
  67. // document.querySelector("input[type=password]").value = "";
  68. // document.querySelector("input[type=password]").blur();
  69. // document.querySelector("#username").select();
  70. // document.querySelector("#username").focus();
  71. },0,false);
  72. UserManager.GetUserInfo().
  73. $promise.then(function(data){
  74. $scope.user.username = data.login;
  75. $scope.user.password = '*';
  76. $scope.profiles = data.gruppi;
  77. });
  78. }
  79. $scope.init();
  80. }
  81. );