<commit_before>/* eslint camelcase: "off" */

/** @ngInject */
export function authConfig ($httpProvider) {
  $httpProvider.interceptors.push(interceptor)

  /** @ngInject */
  function interceptor ($injector, $q, $window) {
    return {
      response: response,
      responseError: responseError
    }

    function response (res) {
      if (res.status === 401) {
        endSession()

        return $q.reject(res)
      }

      return $q.resolve(res)
    }

    function responseError (rej) {
      if (rej.status === 401) {
        endSession()

        return $q.reject(rej)
      }

      return $q.reject(rej)
    }

    function endSession () {
      var $state = $injector.get('$state')
      var Session = $injector.get('Session')

      if ($state.current.name !== 'login') {
        Session.destroy()
        $window.location.href = $state.href('login') + '?timeout'
      }
    }
  }
}

/** @ngInject */
export function authInit ($rootScope, $state, $log, Session, $sessionStorage, Language, $window, RBAC) {
  $rootScope.$on('$stateChangeStart', changeStart)
  $rootScope.$on('$stateChangeError', changeError)
  $rootScope.$on('$stateChangeSuccess', changeSuccess)

  $sessionStorage.$sync()  // needed when called right on reload
  if ($sessionStorage.token) {
    syncSession()
  }

  function changeStart (event, toState, toParams, _fromState, _fromParams) {
    if (angular.isDefined(toState.data)) {
      if (angular.isDefined(toState.data.authorization) && !toState.data.authorization) {
        event.preventDefault()
        $state.transitionTo('dashboard')

        return
      }
      if (!toState.data.requireUser) {
        return
      }
    }
    if (Session.active()) {
      return
    }

    // user is required and session not active - not going anywhere right away
    event.preventDefault()
    $sessionStorage.$sync()  // needed when called right on reload
    if (!$sessionStorage.token) {
      // no saved token, go directly to login
      $state.transitionTo('login')

      return
    }
    syncSession().then(rbacReloadOrLogin(toState, toParams)).catch(badUser)
  }

  function syncSession () {
    return new Promise(function (resolve, reject) {
      // trying saved token..
      Session.setAuthToken($sessionStorage.token)
      Session.setGroup($sessionStorage.miqGroup)

      Session.loadUser()
        .then(function (response) {
          if (angular.isDefined(response)) {
            Language.onReload(response)
            RBAC.setRole(response.identity.role)
          }
          resolve()
        })
        .catch(badUser)
    })
  }

  function rbacReloadOrLogin (toState, toParams) {
    return function () {
      if (RBAC.suiAuthorized()) {
        $state.go(toState, toParams)
      } else {
        Session.privilegesError = true
        $window.location.href = $state.href('login')
      }
    }
  }

  function badUser (error) {
    $log.error(__('Error retrieving user info'), [error])
    $window.location.href = $state.href('login')<commit_msg>Ensures that on page reload, ws is resysnced<commit_after>/* eslint camelcase: "off" */

/** @ngInject */
export function authConfig ($httpProvider) {
  $httpProvider.interceptors.push(interceptor)

  /** @ngInject */
  function interceptor ($injector, $q, $window) {
    return {
      response: response,
      responseError: responseError
    }

    function response (res) {
      if (res.status === 401) {
        endSession()

        return $q.reject(res)
      }

      return $q.resolve(res)
    }

    function responseError (rej) {
      if (rej.status === 401) {
        endSession()

        return $q.reject(rej)
      }

      return $q.reject(rej)
    }

    function endSession () {
      var $state = $injector.get('$state')
      var Session = $injector.get('Session')

      if ($state.current.name !== 'login') {
        Session.destroy()
        $window.location.href = $state.href('login') + '?timeout'
      }
    }
  }
}

/** @ngInject */
export function authInit ($rootScope, $state, $log, Session, $sessionStorage, Language, $window, RBAC, ActionCable) {
  $rootScope.$on('$stateChangeStart', changeStart)
  $rootScope.$on('$stateChangeError', changeError)
  $rootScope.$on('$stateChangeSuccess', changeSuccess)

  $sessionStorage.$sync()  // needed when called right on reload
  if ($sessionStorage.token) {
    syncSession()
  }

  function changeStart (event, toState, toParams, _fromState, _fromParams) {
    if (angular.isDefined(toState.data)) {
      if (angular.isDefined(toState.data.authorization) && !toState.data.authorization) {
        event.preventDefault()
        $state.transitionTo('dashboard')

        return
      }
      if (!toState.data.requireUser) {
        return
      }
    }
    if (Session.active()) {
      return
    }

    // user is required and session not active - not going anywhere right away
    event.preventDefault()
    $sessionStorage.$sync()  // needed when called right on reload
    if (!$sessionStorage.token) {
      // no saved token, go directly to login
      $state.transitionTo('login')

      return
    }
    syncSession()
    .then(rbacReloadOrLogin(toState, toParams))
    .then(() => {
      const cable = ActionCable.createConsumer('/ws/notifications')
      cable.subscriptions.create('NotificationChannel', {})
    })
    .catch(badUser)
  }

  function syncSession () {
    return new Promise(function (resolve, reject) {
      // trying saved token..
      Session.setAuthToken($sessionStorage.token)
      Session.setGroup($sessionStorage.miqGroup)

      Session.loadUser()
      .then(function (response) {
        if (angular.isDefined(response)) {
          Language.onReload(response)
          RBAC.setRole(response.identity.role)
        }
        resolve()
      })
      .catch(badUser)
    })
  }

  function rbacReloadOrLogin (toState, toParams) {
    return function () {
      if (RBAC.suiAuthorized()) {
        $state.go(toState, toParams)
      } else {
        Session.privilegesError = true
        $window.location.href = $state.href('login')
      }
    }
  }

  function badUser (error) {
    $log.error(__('Error retrieving user info'), [error])
    $window.location.href = $state.href('login')
{'fraction_non_alphanumeric': 0.13585996466998554, 'fraction_numerical': 0.0019270916974466034, 'mean_word_length': 2.6062536189924725, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1674838', 'n_tokens_mistral': 1918, 'n_tokens_neox': 1869, 'n_words': 386}