我正在使用角度1.5+,角度流星1.3.10和角色 并且想禁止非管理员角色去管理路径...怎么做?
export default angular.module(name, [
angularMeteor,
'angular-meteor.auth'
]).component(name, {
templateUrl: `imports/ui/components/${name}/${name}.html`,
controllerAs: name,
controller: Admin
})
.config(config);
function config($stateProvider) {
'ngInject';
$stateProvider
.state('admin', {
url: '/admin',
template: '<admin></admin>',
resolve: {
user: ($q) => {
let user = Meteor.user();
console.log(Meteor.user());
console.log(Meteor.userId());
// let user = $auth.currentUser;
var user1 = Meteor.users.findOne({ _id: Meteor.userId() });
console.log(user);
console.log(user1);
if (Meteor.userId() === null) {
console.log("null user");
return $q.reject();
} else if (Roles.userIsInRole(user, ["admin"])) {
console.log("admin user");
return $q.resolve();
}
else {
console.log("reject");
return $q.reject();
}
}
}
});
}
在这里,我无法让用户检查userInRole ...我只能在这里获得用户Meteor.userId()
我做错了什么?
答案 0 :(得分:1)
我也使用了alanning角色,我解决了这个问题,希望对你有用。
angular.module("app").run(function ($rootScope, $state, toastr) {
$rootScope.$on('$stateChangeError', function (event, toState, toParams, fromState, fromParams, error) {
switch(error) {
case "AUTH_REQUIRED":
$state.go('anon.login');
break;
case "FORBIDDEN":
//$state.go('root.home');
break;
case "UNAUTHORIZED":
toastr.error("Acceso Denegado");
toastr.error("No tiene permiso para ver esta opción");
break;
default:
$state.go('internal-client-error');
}
});
$rootScope.$on('$stateChangeStart', function(next, current) {
NProgress.set(0.2);
});
$rootScope.$on('$stateChangeSuccess', function(next, current) {
NProgress.set(1.0);
});
});
.state('root.view', {
url: '/view/:param1/:param2/:param3',
templateUrl: 'client/view.html',
controller: 'ViewCtrl as vc',
resolve: {
"currentUser": ["$meteor", "toastr", function($meteor, toastr){
return $meteor.requireValidUser(function(user) {
if(user.roles[0] == "rolUser1" || user.roles[0] == "rolUser2"){
return true;
}else{
return 'UNAUTHORIZED';
}
});
}]
}
})