gpt4 book ai didi

javascript - Angular-meteor 授权 - 在加载页面之前检查用户 Angular 色

转载 作者:行者123 更新时间:2023-12-03 06:35:06 24 4
gpt4 key购买 nike

我正在使用 Angular 1.5+、Angular-Meteor 1.3.10 和 alanning:roles并想禁止非管理员 Angular 色进入管理员路径...如何做到这一点?

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()我做错了什么?

screen

最佳答案

我也在使用 alanning Angular 色,我解决了问题,希望对你有用。

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';
}
});
}]
}
})

关于javascript - Angular-meteor 授权 - 在加载页面之前检查用户 Angular 色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38267493/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com