RafaelVidaurre / angular-permission

Simple route authorization via roles/permissions
MIT License
1.13k stars 212 forks source link

Using angular-permission in controller #422

Closed wal0x closed 7 years ago

wal0x commented 7 years ago

Im struggling to find a way to use angular-permission from within controllers, I saw your example here

But I got Unknown provider: PermissionProvider <- Permission <- MyCtrl Are authorize still valid methods, other wize how can I use permissions inside a controller.

Thanks

masterspambot commented 7 years ago

You can use for example Autorization service for your needs. However I would not recommend wrapping permission logic around controller.

https://github.com/Narzerus/angular-permission/blob/master/src/permission/authorization/Authorization.js#L22

wal0x commented 7 years ago

@masterspambot when I open a page I want to send an http request only if the user is logged as an Admin, if not it's another http request, how can I do that if not in the controller ? and why is not recommended to do so ? Thanks

namelivia commented 7 years ago

I'm also experiencing a similar problem where I think this is needed, in this particular case I am creating a navigation menu and it is built dynamically based on the route information. I would like to use the permission function when initializing the menu to check if I should insert a link or not.

Somehow I would like to check the already defined function for the route permission in order to evaluate this without writing it more than once, I don't see why this would not be recommended or if there is any better way of solving this kind of problems.

By the way, @wal0x I solved my problem by taking a look at how permission-ng does.

Assuming you already have the permission name you want to check defined in a variable called permissionName, you can do:

var PermissionMap = new PermPermissionMap({
    only: permissionName
}); 
var authorizationResult = PermAuthorization.authorizeByPermissionMap(PermissionMap);

authorizationResult
  .then(function () {
    //Do whatever you want when the permission is ok
  })
  .catch(function (rejectedPermission) {
    //Do whatever you want when the permission is not ok
  });
bogdanlupuro commented 6 years ago

Is there a reason why PermAuthorization.authorizeByPermissionMap is not documented in the wiki? I mean is it something internal which could change at any point or are we confident we can use it without braking changes at regular releases?

morteza-gho commented 6 years ago

@namelivia Hello. I want to use array of permissions, But it doesn't work when I get array of permissions from server of cache!

var permissionsArr = localstorageService.get('user').permissions; //  ← doesn't work with this

var PermissionMap = new PermPermissionMap({
    only: permissionsArr 
}); 

var authorizationResult = PermAuthorization.authorizeByPermissionMap(PermissionMap);

authorizationResult
  .then(function () {
    //Do whatever you want when the permission is ok
  })
  .catch(function (rejectedPermission) {
    //Do whatever you want when the permission is not ok
  });