src/permission-ui/permission-ui.js
function handleStateChangeStartEvent(event, toState, toParams, fromState, fromParams, options) {
    if (!isAuthorizationFinished()) {
      setStateAuthorizationStatus(true);
      setTransitionProperties();

      if (!PermTransitionEvents.areEventsDefaultPrevented()) {
        PermTransitionEvents.broadcastPermissionStartEvent();

        event.preventDefault();
        var statePermissionMap = new PermStatePermissionMap(PermTransitionProperties.toState);

        PermStateAuthorization
          .authorizeByPermissionMap(statePermissionMap)
          .then(function () {
            handleAuthorizedState();
          })
          .catch(function (rejectedPermission) {
            handleUnauthorizedState(rejectedPermission, statePermissionMap);
          })
          .finally(function () {
            setStateAuthorizationStatus(false);
          });
      } else {
        setStateAuthorizationStatus(false);
      }
    }


    /**
     * Updates values of `PermTransitionProperties` holder object
     * @method
     * @private
     */
    function setTransitionProperties() {
      PermTransitionProperties.toState = toState;
      PermTransitionProperties.toParams = toParams;
      PermTransitionProperties.fromState = fromState;
      PermTransitionProperties.fromParams = fromParams;
      PermTransitionProperties.options = options;
    }

    /**
     * Sets internal state `$$finishedAuthorization` variable to prevent looping
     * @method
     * @private
     *
     * @param status {boolean} When true authorization has been already preceded
     */
    function setStateAuthorizationStatus(status) {
      angular.extend(toState, {'$$isAuthorizationFinished': status});
    }

    /**
     * Checks if state has been already checked for authorization
     * @method
     * @private
     *
     * @returns {boolean}
     */
    function isAuthorizationFinished() {
      return toState.$$isAuthorizationFinished;
    }

    /**
     * Handles redirection for authorized access
     * @method
     * @private
     */
    function handleAuthorizedState() {
      PermTransitionEvents.broadcastPermissionAcceptedEvent();

      // Overwrite notify option to broadcast it later
      var transitionOptions = angular.extend({}, PermTransitionProperties.options, {notify: false, location: true});

      $state
        .go(PermTransitionProperties.toState.name, PermTransitionProperties.toParams, transitionOptions)
        .then(function () {
          PermTransitionEvents.broadcastStateChangeSuccessEvent();
        });
    }

    /**
     * Handles redirection for unauthorized access
     * @method
     * @private
     *
     * @param rejectedPermission {String} Rejected access right
     * @param statePermissionMap {permission.ui.PermPermissionMap} State permission map
     */
    function handleUnauthorizedState(rejectedPermission, statePermissionMap) {
      PermTransitionEvents.broadcastPermissionDeniedEvent();

      statePermissionMap
        .resolveRedirectState(rejectedPermission)
        .then(function (redirect) {
          $state.go(redirect.state, redirect.params, redirect.options);
        });
    }
  }
function handleStateChangeStartEvent(event, toState, toParams, fromState, fromParams, options) {
    if (!isAuthorizationFinished()) {
      setStateAuthorizationStatus(true);
      setTransitionProperties();

      if (!PermTransitionEvents.areEventsDefaultPrevented()) {
        PermTransitionEvents.broadcastPermissionStartEvent();

        event.preventDefault();
        var statePermissionMap = new PermStatePermissionMap(PermTransitionProperties.toState);

        PermStateAuthorization
          .authorizeByPermissionMap(statePermissionMap)
          .then(function () {
            handleAuthorizedState();
          })
          .catch(function (rejectedPermission) {
            handleUnauthorizedState(rejectedPermission, statePermissionMap);
          })
          .finally(function () {
            setStateAuthorizationStatus(false);
          });
      } else {
        setStateAuthorizationStatus(false);
      }
    }


    /**
     * Updates values of `PermTransitionProperties` holder object
     * @method
     * @private
     */
    function setTransitionProperties() {
      PermTransitionProperties.toState = toState;
      PermTransitionProperties.toParams = toParams;
      PermTransitionProperties.fromState = fromState;
      PermTransitionProperties.fromParams = fromParams;
      PermTransitionProperties.options = options;
    }

    /**
     * Sets internal state `$$finishedAuthorization` variable to prevent looping
     * @method
     * @private
     *
     * @param status {boolean} When true authorization has been already preceded
     */
    function setStateAuthorizationStatus(status) {
      angular.extend(toState, {'$$isAuthorizationFinished': status});
    }

    /**
     * Checks if state has been already checked for authorization
     * @method
     * @private
     *
     * @returns {boolean}
     */
    function isAuthorizationFinished() {
      return toState.$$isAuthorizationFinished;
    }

    /**
     * Handles redirection for authorized access
     * @method
     * @private
     */
    function handleAuthorizedState() {
      PermTransitionEvents.broadcastPermissionAcceptedEvent();

      // Overwrite notify option to broadcast it later
      var transitionOptions = angular.extend({}, PermTransitionProperties.options, {notify: false, location: true});

      $state
        .go(PermTransitionProperties.toState.name, PermTransitionProperties.toParams, transitionOptions)
        .then(function () {
          PermTransitionEvents.broadcastStateChangeSuccessEvent();
        });
    }

    /**
     * Handles redirection for unauthorized access
     * @method
     * @private
     *
     * @param rejectedPermission {String} Rejected access right
     * @param statePermissionMap {permission.ui.PermPermissionMap} State permission map
     */
    function handleUnauthorizedState(rejectedPermission, statePermissionMap) {
      PermTransitionEvents.broadcastPermissionDeniedEvent();

      statePermissionMap
        .resolveRedirectState(rejectedPermission)
        .then(function (redirect) {
          $state.go(redirect.state, redirect.params, redirect.options);
        });
    }
  }
function handleStateChangeStartEvent ( event , toState , toParams , fromState , fromParams , options ) { if ( ! isAuthorizationFinished ( ) ) { setStateAuthorizationStatus ( true ) ; setTransitionProperties ( ) ; if ( ! PermTransitionEvents . areEventsDefaultPrevented ( ) ) { PermTransitionEvents . broadcastPermissionStartEvent ( ) ; event . preventDefault ( ) ; var statePermissionMap = new PermStatePermissionMap ( PermTransitionProperties . toState ) ; PermStateAuthorization . authorizeByPermissionMap ( statePermissionMap ) . then ( function ( ) { handleAuthorizedState ( ) ; } ) . catch ( function ( rejectedPermission ) { handleUnauthorizedState ( rejectedPermission , statePermissionMap ) ; } ) . finally ( function ( ) { setStateAuthorizationStatus ( false ) ; } ) ; } else { setStateAuthorizationStatus ( false ) ; } } /**
     * Updates values of `PermTransitionProperties` holder object
     * @method
     * @private
     */ function setTransitionProperties ( ) { PermTransitionProperties . toState = toState ; PermTransitionProperties . toParams = toParams ; PermTransitionProperties . fromState = fromState ; PermTransitionProperties . fromParams = fromParams ; PermTransitionProperties . options = options ; } /**
     * Sets internal state `$$finishedAuthorization` variable to prevent looping
     * @method
     * @private
     *
     * @param status {boolean} When true authorization has been already preceded
     */ function setStateAuthorizationStatus ( status ) { angular . extend ( toState , { '$$isAuthorizationFinished' : status } ) ; } /**
     * Checks if state has been already checked for authorization
     * @method
     * @private
     *
     * @returns {boolean}
     */ function isAuthorizationFinished ( ) { return toState . $$isAuthorizationFinished ; } /**
     * Handles redirection for authorized access
     * @method
     * @private
     */ function handleAuthorizedState ( ) { PermTransitionEvents . broadcastPermissionAcceptedEvent ( ) ; // Overwrite notify option to broadcast it later var transitionOptions = angular . extend ( { } , PermTransitionProperties . options , { notify : false , location : true } ) ; $state . go ( PermTransitionProperties . toState . name , PermTransitionProperties . toParams , transitionOptions ) . then ( function ( ) { PermTransitionEvents . broadcastStateChangeSuccessEvent ( ) ; } ) ; } /**
     * Handles redirection for unauthorized access
     * @method
     * @private
     *
     * @param rejectedPermission {String} Rejected access right
     * @param statePermissionMap {permission.ui.PermPermissionMap} State permission map
     */ function handleUnauthorizedState ( rejectedPermission , statePermissionMap ) { PermTransitionEvents . broadcastPermissionDeniedEvent ( ) ; statePermissionMap . resolveRedirectState ( rejectedPermission ) . then ( function ( redirect ) { $state . go ( redirect . state , redirect . params , redirect . options ) ; } ) ; } }
State transition event interceptor
State transition event interceptor