Angularjs strong环回如何为新用户分配静态角色
我克隆了一个Angularjs strong环回如何为新用户分配静态角色,angularjs,authorization,roles,loopbackjs,strongloop,Angularjs,Authorization,Roles,Loopbackjs,Strongloop,我克隆了一个https://github.com/beeman/loopback-angular-admin 我已经使用环回资源管理器创建了几个新角色,但是如何为我创建的用户分配角色呢 我有一个用户模型,它在环回中从用户模型扩展而来 模型文件是这样的- { "name": "user", "plural": "users", "base": "User", "idInjection": true, "options": { "validateUpsert": true
https://github.com/beeman/loopback-angular-admin
我已经使用环回资源管理器创建了几个新角色,但是如何为我创建的用户分配角色呢
我有一个用户模型,它在环回中从用户模型扩展而来
模型文件是这样的-
{
"name": "user",
"plural": "users",
"base": "User",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {},
"validations": [],
"relations": {
"accessTokens": {
"type": "hasMany",
"model": "accessToken",
"foreignKey": "userId"
},
"identities": {
"type": "hasMany",
"model": "userIdentity",
"foreignKey": "userId"
},
"credentials": {
"type": "hasMany",
"model": "userCredential",
"foreignKey": "userId"
},
"roles": {
"type": "hasMany",
"model": "Role",
"foreignKey": "principalId",
"through": "RoleMapping"
}
},
"acls": [
{
"accessType": "*",
"principalType": "ROLE",
"principalId": "admin",
"permission": "ALLOW"
},
{
"accessType": "READ",
"principalType": "ROLE",
"principalId": "$unauthenticated",
"permission": "DENY"
},
{
"accessType": "READ",
"principalType": "ROLE",
"principalId": "$authenticated",
"permission": "ALLOW"
},
{
"accessType": "*",
"principalType": "ROLE",
"principalId": "$owner",
"permission": "ALLOW"
}
],
"methods": {}
}
我的user.js就像-
module.exports = function (user) {
// Set the username to the users email address by default.
user.observe('before save', function setDefaultUsername(ctx, next) {
if (ctx.instance) {
if(ctx.isNewInstance) {
ctx.instance.username = ctx.instance.email;
}
ctx.instance.status = 'created';
ctx.instance.created = Date.now();
}
next();
});
};
现在,我想根据我从客户端传递的属性
ctx.instance.type
为用户分配角色和主体假设您已经在角色表中创建了一组有限的角色,请使用after save钩子为刚创建的用户分配一个特定角色:
User.observe('after save', function setRoleMapping(ctx, next) {
if (ctx.instance) {
if(ctx.isNewInstance) {
var RoleMapping = User.app.models.RoleMapping;
// var roleId = based on type lookup or static?
RoleMapping.create({
principalType: "USER",
principalId: ctx.instance.id,
roleId: roleId
}, function(err, roleMapping) {
if (err) {return console.log(err);}
// success stuff
}):
}
}
next();
});
代码没有经过测试,只是一个大概的想法。您不能使用before save钩子,因为您不知道角色映射表中principalId要使用的用户ID
更新:包括按传入类型查找角色的版本:
user.observe('after save', function setRoleMapping(ctx, next) {
if (ctx.instance) {
if(ctx.isNewInstance) {
// look up role based on type
//
Role.find({where: {name: ctx.instance.type}}, function(err, role) {
if (err) {return console.log(err);}
RoleMapping.create({
principalType: "USER",
principalId: ctx.instance.id,
roleId: role.id
}, function(err, roleMapping) {
if (err) {return console.log(err);}
console.log('User assigned RoleID ' + role.id + ' (' + ctx.instance.type + ')');
}):
});
}
}
next();
});
查询文档在这里:那么如何通过基于
ctx.instance.type
value查询数据库模型来获取角色ID取决于您如何根据type值确定角色。有多少角色?角色名称只是类型名称吗?是的,它只是类型名称,除了admin之外,还有大约6个角色,但只是为了学习如何查询角色或任何模型,我想知道如何做到这一点,首先查询数据库,然后从找到的角色中附加角色ID,在我的答案中添加一个带有角色查找的版本。但是这里要小心,如果传入的类型不在列表中,您将没有role.id
来创建角色映射条目,因此您需要添加逻辑来处理错误的类型值。使用观察者还增加了需要继续执行next()
的复杂性,并且您还可以传递错误next(err)
,使它们冒泡(但是您必须在每个回调上下文中放入多个next(err)
,或者使用带有单个catch()
的承诺。