2017-02-28 32 views
2

我已根據文檔在我的應用程序中定義了我的用戶角色和權限。angular-permission:測試用戶具有控制器的角色或權限

https://github.com/Narzerus/angular-permission/wiki

有了這個,我能夠使用UI指令或UI路由器處理。


問:如何測試用戶是否具有控制器或服務中的角色或權限?

(function() { 
    'use strict'; 

    angular 
    .module('app') 
    .component('userDetail', component()); 

    /** @ngInject */ 
    function component() { 
    return { 
     restrict: 'E', 
     bindings: { 
     user: '<' 
     }, 
     templateUrl: 'app/components/users/user-detail/user-detail.html', 
     transclude: true, 
     controller: Controller 
    } 
    } 

    /** @ngInject */ 
    function Controller($log) { 
    var ctrl = this; 
    ctrl.$onInit = function() { 
     $log.log('How to test for user permission?') 
     // Something like... 
     // if(PermPermission.hasPermission('createUser')) { 
     // do something 
     // } 
    }; 
    } 
})(); 
+1

請考慮提供[最小,完整,可驗證的示例](HTTP如果可能的話,或者至少發佈你迄今嘗試過的(代碼)/你正在嘗試做什麼(更具體)。這太寬IMO – lealceldeiro

+0

@AsielLealCeldeiro,在這種情況下,我會不同意。根據演示https://cdn.rawgit.com/Narzerus/angular-permission/0d502cb4/test/e2e/permission-ui/assets/demo.html – Blowsie

回答

2

@blowsie這是我是怎麼做的,但感覺很沉重:

export default { 
 
    template: '<div></div>', 
 
    controller: class test { 
 
    /* @ngInject */ 
 
    constructor(PermPermissionStore) { 
 
     PermPermissionStore.definePermission('truePermission',() => true); 
 
     PermPermissionStore.definePermission('falsePermission',() => false); 
 

 
     const truePerm = PermPermissionStore.getPermissionDefinition('truePermission'); 
 
     const falsePerm = PermPermissionStore.getPermissionDefinition('falsePermission'); 
 

 
     truePerm.validatePermission() 
 
     .then((res) => { /* you go there */ }) 
 
     .catch((e) => { /* never go there */ }); 
 
     falsePerm.validatePermission() 
 
     .then((res) => { /* never go there */ }) 
 
     .catch((e) => { /* you go there */ }); 
 
    } 
 
    }, 
 
};

相關問題