Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue on state authorization #81

Closed
jkeczan opened this issue Sep 4, 2015 · 18 comments
Closed

Issue on state authorization #81

jkeczan opened this issue Sep 4, 2015 · 18 comments

Comments

@jkeczan
Copy link

jkeczan commented Sep 4, 2015

When a state is found to be authorized, the code that allows the state to continue and broadcasts the success message.

However, we are getting an error '$$hashKey' not defined on state change. We traced it back to angular-permission line 49. In the source, you said this is a hacky fix that should be fixed in later versions on UI-router. Could this be the culprit?

Permission.defineRole('projectmanager', function(stateParams) {
       var deferred = $q.defer();

       $http.get('/api/managers').then(function(data) {
           var isPM = false;
           for (var i = 0; i < data.data.length; i++) {
               if (data.data[i].projectMgr == $rootScope.session.projectMgr) {
                   isPM = true;
                   break;
               }
           }
           if (isPM) {
               deferred.resolve();
           } else {
               deferred.reject();
           }
       }, function() {
           deferred.reject();
       });
       return deferred.promise;
   });
@crediblebytes
Copy link

I am also getting the same issue tracked down here:
if (!$rootScope.$broadcast('$stateChangeStart', toState, toParams, fromState, fromParams).defaultPrevented) {
Any workaround for the moment?

@RafaelVidaurre
Copy link
Owner

This error seems to have emerged out of nowhere, could it be the ui-router version?

What version are you guys using

@crediblebytes
Copy link

After further investigation I found this was only an issue when using both ui-router-extras and angular-permission. Seems like both libraries are catching the "stateChangeStart" event. I'm not exactly sure who is at fault here but platdesign made a fix for the ui-router-extras library. After implementing this one line fix the error went away. For those interested see this link.
Here are the versions I am currently using:

  • ui-router 0.2.15
  • ct-ui-router-extras 0.0.14
  • angular-permission 0.3.1

@jkeczan
Copy link
Author

jkeczan commented Sep 15, 2015

This would not be our problem as we are not using ct-ui-router-extras currently in our project.

  • express -> 4.1.1
  • express-ui0router -> 0.2.15

@crediblebytes
Copy link

Not sure if it is correct but according to what was said in that link the core of the problem is with angular-permission. I'll quote here what was said:

"angular-permission is also capturing the $stateChangeStart event and it has logic that prevents further propagation of as well as (re)transmitting the $stateChangeStart event."

...

  1. UI Router broadcast a $stateChangeStart
  2. UI-Router-Extras capture the $stateChangeStart event. All fine.
  3. angular permission capture the $stateChangeStart event, blocks propagation and then does a retransmit (i.e. broadcast)
  4. UI-Router-Extras capture the $stateChangeStart event again. Code breaks.

Might be worth at least looking into.

@christopherthielen
Copy link

Hi, ui-router-extras guy here. Extras has code which keeps track of every transitionTo call, then matches that up with the $stateChangeStart event. The problem here is that angular-permission is firing fake $stateChangeStart events, and that's causing the Extras code to bomb.

I can add some logic in Extras that will work around the problem, but these two features ($transition$ and Previous States) will be incompatible with angular-permission, as long as it relies on fake ui-router events.

Note that in 1.0 we've substantially improved the ui-router integration hooks making things like angular-permission much easier.

@RafaelVidaurre
Copy link
Owner

Will have a look at the 1.0 version, The fake state emission is the source
of many issues with angular-permission. If anyone figures out an
alternative way to do this without having to stop/re-emit stateChange
events that would help a bunch
On Fri, Oct 9, 2015 at 5:03 PM Chris Thielen notifications@github.com
wrote:

Hi, ui-router-extras guy here. Extras has code which keeps track of every
transitionTo call, then matches that up with the $stateChangeStart event.
The problem here is that angular-permission is firing fake
$stateChangeStart events, and that's causing the Extras code to bomb.

I can add some logic in Extras that will work around the problem, but
these two features ($transition$ and Previous States) will be incompatible
with angular-permission, as long as it relies on fake ui-router events.

Note that in 1.0 we've substantially improved the ui-router integration
hooks making things like angular-permission much easier.


Reply to this email directly or view it on GitHub
#81 (comment)
.

@christopherthielen
Copy link

@Narzerus 1.0 is close to an alpha release, but hasn't gotten much testing besides our own unit tests. Would love to get some feedback on it.

you'll do something like so:

// gets permissions config from a state
let getPermissions = (state) => state.permissions || state.data && state.data.permissions

app.run(($transitions) => {
  // When a transition starts, and getPermissions returns truthy for 
  // the "to" state, run the checkPermissionsHook
  $transitions.onStart({ to: getPermissions }, checkPermissionsHook);
}

// $transition$ has data about the current transition, including the "to state"
checkPermissionsHook.$inject = ['$transition$', '$state'];
function checkPermissionsHook($transition$, $state) {
  let to = $transition$.to();
  let permissions = getPermissions(to);
  let toParams = $transition$.params();

  // do $stateChangePermissionStart eventing here
  return Permission.authorize(permissions, toParams)
    .then(() => /* do $stateChangePermissionAccepted eventing here */)
    .catch((error) => {
      // do $stateChangePermissionDenied eventing
      if (permissions.redirectTo) {
        state = // get redirect to state
        var targetState = $state.targetState(state);
        // asynchronously returning a redirect from the hook
        return $transition$.redirect(targetState); 
      } else {
        return false; // cancels transition
      }
    });
}

Note that since this is pre-alpha, the API may change, especially around returning the redirect. I think the API is pretty close to done though.

@temka1234
Copy link

Is there a complete solution for ui-router-extras and angular-permission?

@christopherthielen can you help please with your previous code.

  • How to cancel transitions stack?
  • $transition$.redirect(targetState) make with $state.go ...?
  • $transitions.onStart is equals $rootScope.$on("$transitionStart", ...)? What does it mean in this context { to: getPermissions }
  • Is $previous functionality saved if i transition cancels?

@christopherthielen
Copy link

@temka1234 the code I wrote is for the currently unreleased feature-1.0 branch of ui-router

@masterspambot
Copy link
Collaborator

@christopherthielen 👍

@davincho
Copy link

Any updates on this issue? Would love to use angular-permission and ui-router-extras together.

@masterspambot
Copy link
Collaborator

Wwe can't solve it till release of ui-router 1.0.0. There will be changes in api that allow us to fix those issues. Sorry.

@temka1234
Copy link

Here simple plunker example of angular-permission with ui-router 1.0alpha http://plnkr.co/edit/AuScw1?p=preview. redirectTo option not working now, wait for this issue angular-ui/ui-router#2450

Changes in angular-permission: https://github.com/temka1234/angular-permission/tree/ui-router-1.0

@temka1234
Copy link

Create "ui-router-1.0" branch, fix tests. redirectTo option now worked.
@masterspambot, can you make review?

masterspambot added a commit that referenced this issue Feb 3, 2016
…of browser history and problem with ui-router-extras.

Either I am super smart or super dumb. But it works.
masterspambot added a commit that referenced this issue Feb 16, 2016
#81 Removed triggering $stateChangeSuccess. Solving issue with order …
@masterspambot
Copy link
Collaborator

@temka1234 The code itself looks great, but as long as ui-router guys don't make official release we can not rely on this code. But! But i got idea how to make it work with 0.2.x. I just need to refactor ParmissionMap and release 2.1.0 that will solve the history messing problem.

@RafaelVidaurre
Copy link
Owner

:0 awesome!

On Sun, Feb 28, 2016, 1:43 PM Błażej Krysiak notifications@github.com
wrote:

@temka1234 https://github.com/temka1234 The code itself looks great,
but as long as ui-router guys don't make official release we can not rely
on this code. But! But i got idea how to make it work in 0.2.x I just need
to refactor ParmissionMap and release 2.1.0 that will solve the history
messing problem.


Reply to this email directly or view it on GitHub
#81 (comment)
.

@masterspambot
Copy link
Collaborator

Solved in v2.0.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants