Skip to content
This repository has been archived by the owner on Aug 11, 2022. It is now read-only.

[question] acl: protect a route #56

Closed
arivasvera opened this issue Jul 13, 2016 · 5 comments
Closed

[question] acl: protect a route #56

arivasvera opened this issue Jul 13, 2016 · 5 comments

Comments

@arivasvera
Copy link

can someone help me to use this https://github.com/mikemclin/angular-acl#protect-a-route to protect ui-routes?

@silverbux
Copy link
Owner

Hi @andresrivas1506 , its already implemented by default, on protected routes you simply add
data { auth: true } example: routes.config.js#L36

and to show / hide elements use vm.can() nav-sidebar.component.html#L28 which is referenced on its controller nav-sidebar.component.js#L6

@arivasvera
Copy link
Author

Hello @silverbux thanks for your time. Yes, its already implemented, but only using data { auth: true } and showing/hiding things in template. If you go, for example, to url http://myapp/#/user-lists from url bar, you can see the html.

I want to protect the route using permissions:

.when('/content', {
      resolve : {
        'acl' : ['$q', 'AclService', function($q, AclService){
          if(AclService.can('view_content')){
            // Has proper permissions
            return true;
          } else {
            // Does not have permission
            return $q.reject('Unauthorized');
          }
        }]
      }
    });

Sorry for my bad english

@arivasvera
Copy link
Author

arivasvera commented Jul 13, 2016

I can solve with this:

$rootScope.$on('$stateChangeError', function (event, toState, toParams, fromState, fromParams, rejection) {
    if (rejection === 'Unauthorized') {
      $state.go('app.landing');
    }
  });
  • my protected route
.state('app.somerute', {
      url: '/some-route',
      resolve : {
        'acl' : ['$q', 'AclService', function($q, AclService){
          if(AclService.can('some_permission')){
            // Has proper permissions
            return true;
          } else {
            // Does not have permission
            return $q.reject('Unauthorized');
          }
        }]
      },
      data: {
        auth: true
      },
      views: {
        'main@app': {
          templateUrl: getView('someview')
        }
      }
    })

Thank you @silverbux

@silverbux
Copy link
Owner

Coolness, or try this approach as well.

data: {
   auth: true,
   access: 'manage.user'
}

then you could add a function on your /angular/run/routes.run.js and call AclService from there.

@arivasvera
Copy link
Author

very nice that approach, thanks

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

No branches or pull requests

2 participants