我已根据文档在我的应用程序中定义了我的用户角色和权限。
https://github.com/Narzerus/angular-permission/wiki
有了这个,我可以使用ui指令或ui-router处理。
问:如何测试用户在控制器或服务中是否具有角色或权限?
(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
// }
};
}
})();
答案 0 :(得分:3)
@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 */ });
}
},
};