我正在使用Meteor和alanning:我项目中用户角色的角色。我试图检查登录用户是否拥有管理员权限。
我已经尝试了所有方法。
该检查返回false。
我已经完成了alanning的README中的内容:角色,如下:
//server/init.js
var users = [
{name:"Normal User",email:"normal@ehnormal.com.br",roles:[]},
{name:"Usuario Secreto",email:"mauriciord@me.com",roles:['view-secrets']},
{name:"Usuario Gerenciador",email:"mauricioreattoduarte@gmail.com",roles:['manage-users']},
{name:"Mauricio",email:"mauricio@thcm.com.br",roles:['admin']}
];
_.each(users, function (user) {
var id;
id = Accounts.createUser({
email: user.email,
password: "apple1",
profile: { name: user.name }
});
if (user.roles.length > 0) {
// Need _id of existing user record so this call must come
// after `Accounts.createUser` or `Accounts.onCreate`
// Roles.addUsersToRoles(id, user.roles, 'default-group');
Meteor.users.update({_id: id}, {$set:{'emails.0.verified': true}});
console.log("usuario criado");
Roles.addUsersToRoles(id, user.roles, 'default-group');
//Roles.addUsersToRoles(id, user.roles);
}
});
在README中也是这样说的:
//server/publish.js
Meteor.publish(null, function (){
return Meteor.roles.find({})
});
但是当我尝试检查routes.js中用户的权限时:请参阅我的存储库中的here。
// lib/routes.js
var logado = FlowRouter.group({
name: 'logadoRoutes',
// se não estiver logado vai para /login
triggersEnter: [function(context, redirect) {
console.log('logado grupo');
if(!Meteor.userId()) {
FlowRouter.go('login');
} else {
return true;
}
}]
});
var admin = logado.group({
prefix: '/admin',
name: 'adminRoutes',
triggersEnter: [function(context, redirect) {
var loggedInUser = Meteor.userId();
console.log('verificando admin ...');
if (Roles.userIsInRole(loggedInUser, ['view-secrets', 'admin'], "default-group")) {
return true;
console.log('é admin - rotas');
}
console.log('não é admin - rotas');
throw new Meteor.Error(403, "Acesso Negado");
}]
});
这是我的存储库:https://github.com/mauriciord/thomasicamargo
套餐:https://github.com/mauriciord/thomasicamargo/blob/master/.meteor/packages
meteor-base # Packages every Meteor app needs to have
mobile-experience # Packages for a great mobile UX
mongo # The database Meteor supports right now
blaze-html-templates # Compile .html files into Meteor Blaze views
session # Client-side reactive dictionary for your app
jquery # Helpful client-side library
tracker # Meteor's client-side reactive programming library
standard-minifiers # JS/CSS minifiers run for production mode
es5-shim # ECMAScript 5 compatibility for older browsers.
ecmascript # Enable ECMAScript2015+ syntax in app code
kadira:flow-router
kadira:blaze-layout
erasaur:meteor-lodash
stolinski:stylus-multi
fortawesome:fontawesome
spiderable
fastclick
raix:handlebar-helpers
aldeed:collection2
aldeed:autoform
accounts-ui
accounts-password
matb33:bootstrap-glyphicons
zimme:active-route
gwendall:auth-client-callbacks
meteortoys:allthings
datariot:ganalytics
check
twbs:bootstrap
less
arillo:flow-router-helpers
alanning:roles
更新:我使用isInRole
(ifInRole
)更改了布局,现在它正在运行。
我试图在routes.js上验证条件,只需按“输入”即可。在导航栏的URL上。 localhost:3000 / admin,但是当我通过锚链接这条路线时,条件工作100%哈哈。在条件方面,我做了这个:
if (Roles.subscription.ready() && Roles.userIsInRole(loggedInUser, ['admin'], 'default-group')) {
return true;
console.log('é admin - rotas');
}
我不知道为什么,我是MeteorJS的新人。