Strongloop环回如何给新用户分配静态角色
Strongloop loopback how to assign static roles to new users
我克隆了一个 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
为用户分配角色和委托人
假设您已经在角色 table 中创建了一组有限的角色,请使用保存后挂钩为 just-created 用户分配特定角色:
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 挂钩,因为您不知道要在 RoleMapping table.
中用于 principalId 的用户 ID
UPDATE:版本包括按传入的类型查找角色:
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();
});
查询文档在这里:https://docs.strongloop.com/display/public/LB/Querying+data
我克隆了一个 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
为用户分配角色和委托人
假设您已经在角色 table 中创建了一组有限的角色,请使用保存后挂钩为 just-created 用户分配特定角色:
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 挂钩,因为您不知道要在 RoleMapping table.
中用于 principalId 的用户 IDUPDATE:版本包括按传入的类型查找角色:
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();
});
查询文档在这里:https://docs.strongloop.com/display/public/LB/Querying+data