silverbux / laravel-angular-admin

Laravel + Angularjs + Bootstrap + AdminLTE binded by Gulp workflow Admin Dashboard Boilerplate / Starter.
http://silverbux.github.io/laravel-angular-admin/
MIT License
923 stars 414 forks source link

[question] acl: protect a route #56

Closed arivasvera closed 8 years ago

arivasvera commented 8 years ago

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

silverbux commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

I can solve with this:

$rootScope.$on('$stateChangeError', function (event, toState, toParams, fromState, fromParams, rejection) {
    if (rejection === 'Unauthorized') {
      $state.go('app.landing');
    }
  });
.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 commented 8 years ago

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 commented 8 years ago

very nice that approach, thanks