我正在使用最新版本的环回,我现在想开始使用acl。我的应用程序有一个项目模型,应该是tontge团队成员,相对所有者和管理员。当然,我希望有多个项目的成员可以成为多个项目的团队成员......所有者可以拥有多个项目,管理员应该能够看到所有内容并完成所有事情。
Loopback似乎有可能定义它。
你会怎么做?
由于
答案 0 :(得分:0)
首先,在启动脚本中定义静态角色 eq:TeamMeamber,所有者,管理员等
请参阅此示例https://loopback.io/doc/en/lb3/Defining-and-using-roles.html#static-roles
//create the admin role
Role.create({
name: 'admin'
}, function(err, role) {
});
然后在您的角色的启动脚本中注册RoleResolver。这是确定要求的角色的逻辑,角色是否适用。
来自上述链接的示例。
Role.registerResolver('teamMember', function(role, context, cb) {
// Q: Is the current request accessing a Project?
if (context.modelName !== 'project') {
// A: No. This role is only for projects: callback with FALSE
return process.nextTick(() => cb(null, false));
}
//Q: Is the user logged in? (there will be an accessToken with an ID if so)
var userId = context.accessToken.userId;
if (!userId) {
//A: No, user is NOT logged in: callback with FALSE
return process.nextTick(() => cb(null, false));
}
// Q: Is the current logged-in user associated with this Project?
// Step 1: lookup the requested project
context.model.findById(context.modelId, function(err, project) {
// A: The datastore produced an error! Pass error to callback
if(err) return cb(err);
// A: There's no project by this ID! Pass error to callback
if(!project) return cb(new Error("Project not found"));
// Step 2: check if User is part of the Team associated with this Project
// (using count() because we only want to know if such a record exists)
var Team = app.models.Team;
Team.count({
ownerId: project.ownerId,
memberId: userId
}, function(err, count) {
// A: The datastore produced an error! Pass error to callback
if (err) return cb(err);
if(count > 0){
// A: YES. At least one Team associated with this User AND Project
// callback with TRUE, user is role:`teamMember`
return cb(null, true);
}
else{
// A: NO, User is not in this Project's Team
// callback with FALSE, user is NOT role:`teamMember`
return cb(null, false);
}
});
});
});
然后在你的模型中,按名称使用这些角色,
{
"accessType": "READ",
"principalType": "ROLE",
"principalId": "admin",
"permission": "ALLOW",
"property": "findById"
}
基本上,