{"size": 11936, "ext": "js", "max_stars_repo_path": "src/services/login-service.js", "max_stars_repo_name": "slavayssiere/front_crf", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/services/login-service.js", "max_issues_repo_name": "slavayssiere/front_crf", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/services/login-service.js", "max_forks_repo_name": "slavayssiere/front_crf", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 39.9197324415, "max_line_length": 155, "alphanum_fraction": 0.5924932976}
angular.module('loginService', ['ui.router'])
  .provider('loginService', function () {
    var userToken = localStorage.getItem('userToken'),
      errorState = 'app.error',
      logoutState = 'app.logout',
      loginState = 'app.home';

    this.$get = function ($rootScope, $http, $q, $state, $log, $auth) {

      /**
       * Low-level, private functions.
       */
      var setTokens = function () {
        if (!wrappedService.F5_ST && !wrappedService.LastMRH_Session && !wrappedService.MRHSession && !wrappedService.SAML && !wrappedService.JSESSIONID) {
          localStorage.removeItem('F5_ST');
          localStorage.removeItem('LastMRH_Session');
          localStorage.removeItem('MRHSession');
          localStorage.removeItem('SAML');
          localStorage.removeItem('JSESSIONID');

          delete $http.defaults.headers.common['F5-ST'];
          delete $http.defaults.headers.common['LastMRH-Session'];
          delete $http.defaults.headers.common['MRHSession'];
          delete $http.defaults.headers.common['SAML'];
          delete $http.defaults.headers.common['JSESSIONID'];

          wrappedService.inLocalStorage = false;
        } else {
          localStorage.setItem('F5_ST', wrappedService.F5_ST);
          localStorage.setItem('LastMRH_Session', wrappedService.LastMRH_Session);
          localStorage.setItem('MRHSession', wrappedService.MRHSession);
          localStorage.setItem('SAML', wrappedService.SAML);
          localStorage.setItem('JSESSIONID', wrappedService.JSESSIONID);

          $http.defaults.headers.common['F5-ST'] = wrappedService.F5_ST;
          $http.defaults.headers.common['LastMRH-Session'] = wrappedService.LastMRH_Session;
          $http.defaults.headers.common['MRHSession'] = wrappedService.MRHSession;
          $http.defaults.headers.common['SAML'] = wrappedService.SAML;
          $http.defaults.headers.common['JSESSIONID'] = wrappedService.JSESSIONID;

          wrappedService.inLocalStorage = true;
        }

        return true;
      };

      var getTokens = function () {

        wrappedService.F5_ST = localStorage.getItem('F5_ST');
        wrappedService.LastMRH_Session = localStorage.getItem('LastMRH_Session');
        wrappedService.MRHSession = localStorage.getItem('MRHSession');
        wrappedService.SAML = localStorage.getItem('SAML');
        wrappedService.JSESSIONID = localStorage.getItem('JSESSIONID');

        $http.defaults.headers.common['F5-ST'] = wrappedService.F5_ST;
        $http.defaults.headers.common['LastMRH-Session'] = wrappedService.LastMRH_Session;
        $http.defaults.headers.common['MRHSession'] = wrappedService.MRHSession;
        $http.defaults.headers.common['SAML'] = wrappedService.SAML;
        $http.defaults.headers.common['JSESSIONID'] = wrappedService.JSESSIONID;

        if (!wrappedService.F5_ST && !wrappedService.LastMRH_Session && !wrappedService.MRHSession && !wrappedService.SAML && !wrappedService.JSESSIONID) {
          return false;
        }
        else {
          return true;
        }
      };

      var getLoginData = function () {

        wrappedService.userRole = userRoles.public;
        wrappedService.isLogged = false;
        wrappedService.doneLoading = true;

        if (!getTokens()) {
          wrappedService.inLocalStorage = false;
        }
        else {
          wrappedService.inLocalStorage = true;
        }
      };

      var managePermissions = function () {
        // Register routing function.
        $rootScope.$on('$stateChangeStart', function (event, to, toParams, from, fromParams) {
          /**
           * $stateChangeStart is a synchronous check to the accessLevels property
           * if it's not set, it will setup a pendingStateChange and will let
           * the grandfather resolve do his job.
           *
           * In short:
           * If accessLevels is still undefined, it let the user change the state.
           * Grandfather.resolve will either let the user in or reject the promise later!
           */
          if (wrappedService.userRole === null) {
            wrappedService.doneLoading = false;
            wrappedService.pendingStateChange = {
              to: to,
              toParams: toParams
            };
            return;
          }

          // if the state has undefined accessLevel, anyone can access it.
          // NOTE: if `wrappedService.userRole === undefined` means the service still doesn't know the user role,
          // we need to rely on grandfather resolve, so we let the stateChange success, for now.
          if (to.accessLevel === undefined || to.accessLevel.bitMask & wrappedService.userRole.bitMask) {
            angular.noop(); // requested state can be transitioned to.
          } else {
            event.preventDefault();
            $rootScope.$emit('$statePermissionError');
            $state.go(errorState, { error: 'unauthorized' }, { location: false, inherit: false });
          }
        });

        /**
         * Gets triggered when a resolve isn't fulfilled
         * NOTE: when the user doesn't have required permissions for a state, this event
         *       it's not triggered.
         *
         * In order to redirect to the desired state, the $http status code gets parsed.
         * If it's an HTTP code (ex: 403), could be prefixed with a string (ex: resolvename403),
         * to handle same status codes for different resolve(s).
         * This is defined inside $state.redirectMap.
         */
        $rootScope.$on('$stateChangeError', function (event, to, toParams, from, fromParams, error) {
          /**
           * This is a very clever way to implement failure redirection.
           * You can use the value of redirectMap, based on the value of the rejection
           * So you can setup DIFFERENT redirections based on different promise errors.
           */
          var errorObj, redirectObj;
          // in case the promise given to resolve function is an $http request
          // the error is a object containing the error and additional informations
          error = (typeof error === 'object') ? error.status.toString() : error;
          // in case of a random 4xx/5xx status code from server, user gets loggedout
          // otherwise it *might* forever loop (look call diagram)
          if (/^[45]\d{2}$/.test(error)) {
            wrappedService.logoutUser();
          }
          /**
           * Generic redirect handling.
           * If a state transition has been prevented and it's not one of the 2 above errors, means it's a
           * custom error in your application.
           *
           * redirectMap should be defined in the $state(s) that can generate transition errors.
           */
          if (angular.isDefined(to.redirectMap) && angular.isDefined(to.redirectMap[error])) {
            if (typeof to.redirectMap[error] === 'string') {
              return $state.go(to.redirectMap[error], { error: error }, { location: false, inherit: false });
            } else if (typeof to.redirectMap[error] === 'object') {
              redirectObj = to.redirectMap[error];
              return $state.go(redirectObj.state, { error: redirectObj.prefix + error }, { location: false, inherit: false });
            }
          }
          return $state.go(errorState, { error: error }, { location: false, inherit: false });
        });
      };

      /**
       * High level, public methods
       */
      var wrappedService = {
        loginHandler: function (user, status, headers, config) {
          /**
           * Custom logic to manually set userRole goes here
           *
           * Commented example shows an userObj coming with a 'completed'
           * property defining if the user has completed his registration process,
           * validating his/her email or not.
           *
           * EXAMPLE:
           * if (user.hasValidatedEmail) {
           *   wrappedService.userRole = userRoles.registered;
           * } else {
           *   wrappedService.userRole = userRoles.invalidEmail;
           *   $state.go('app.nagscreen');
           * }
           */

          // setup token
          wrappedService.F5_ST = user.F5_ST;
          wrappedService.MRHSession = user.MRHSession;
          wrappedService.LastMRH_Session = user.LastMRH_Session;
          wrappedService.SAML = user.SAML;
          wrappedService.JSESSIONID = user.JSESSIONID;
          setTokens();
          // update user
          angular.extend(wrappedService.user, user);
          // flag true on isLogged
          wrappedService.isLogged = true;

          //$log.info("set nivol as google id", user.utilisateur.id);
          ga('set', 'userId', user.utilisateur.id);

          // update userRole
          switch (user.role) {
            case "admin":
              wrappedService.userRole = userRoles.admin;
              break;

            case "dlus":
              wrappedService.userRole = userRoles.dlus;
              break;

            case "dlaf":
              wrappedService.userRole = userRoles.dlaf;
              break;

            default:
              wrappedService.userRole = userRoles.user;
          }
          wrappedService.isInTeamFormat = user.isInTeamFormat;

          $state.go(loginState);

          return user;
        },
        loginUser: function (httpPromise) {
          httpPromise.success(this.loginHandler);
        },
        logoutUser: function (httpPromise) {
          /**
           * De-registers the userToken remotely
           * then clears the loginService as it was on startup
           */

          this.F5_ST = null;
          this.MRHSession = null;
          this.LastMRH_Session = null;
          this.JSESSIONID = null;
          this.SAML = null;
          setTokens();

          this.userRole = userRoles.public;
          this.isInTeamFormat = false;
          this.user = {};
          this.isLogged = false;

          $auth.logout();
          $state.go(logoutState);
        },
        resolvePendingState: function (httpPromise) {
          var checkUser = $q.defer(),
            self = this,
            pendingState = self.pendingStateChange;

          // When the $http is done, we register the http result into loginHandler, `data` parameter goes into loginService.loginHandler
          httpPromise.success(self.loginHandler);

          httpPromise.then(
            function success(httpObj) {
              self.doneLoading = true;
              // duplicated logic from $stateChangeStart, slightly different, now we surely have the userRole informations.
              if (pendingState.to.accessLevel === undefined || pendingState.to.accessLevel.bitMask & self.userRole.bitMask) {
                checkUser.resolve();
              } else {
                checkUser.reject('unauthorized');
              }
            },
            function reject(httpObj) {
              checkUser.reject(httpObj.status.toString());
            }
          );
          /**
           * I setted up the state change inside the promises success/error,
           * so i can safely assign pendingStateChange back to null.
           */
          self.pendingStateChange = null;
          return checkUser.promise;
        },
        /**
         * Public properties
         */
        userRole: null,
        user: {},
        isLogged: null,
        inLocalStorage: null,
        pendingStateChange: null,
        doneLoading: null,
        gw_token: null,
        gw_refresh: null,
        gw_expire: null,
        isInTeamFormat: false,

        F5_ST: localStorage.getItem('F5_ST'),
        LastMRH_Session: localStorage.getItem('LastMRH_Session'),
        MRHSession: localStorage.getItem('MRHSession'),
        JSESSIONID: localStorage.getItem('JSESSIONID'),
        SAML: localStorage.getItem('SAML')
      };

      getLoginData();
      managePermissions();

      return wrappedService;
    };
  });
