loopbackjs - 在环回中创建超级用户管理员
问题描述
我仍在深入研究环回,请提供帮助。我有以下模型:admin
=> 扩展内置User
模型,student
=> 扩展内置User
模型和 coz 的其他模型。
简短的:
我不希望经过身份验证的实例student
能够访问端点Student GET /students
。我不希望Student
能够访问有关所有Student
(s) 的信息。所以,我提出了 admin,它应该能够通过 Role 的实现来访问端点 GET /students。我希望管理员能够访问所有端点
常设:
在 script.js 中
module.exports = function(app) {
const User = app.models.admin;
const Role = app.models.Role;
const RoleMapping = app.models.RoleMapping;
Role.find({ name: 'admin' }, function(err, results) {
if (err) {
throw err;
}
if (results.length < 1) {
// now Role creation...
User.create([{name:'Felix Olonde',username:"felix",email: 'felix@gmail.com', password: 'felix123',phone_number:3127287656,dob:1988-03-04,state: 'LA'
}
], function(err, users) {
if (err) throw err;
console.log('Created user:', users);
//create the admin role
Role.create({
name: 'admin'
}, function(err, role) {
if (err) throw err;
console.log('Created role:', role);
role.principals.create({
principalType: RoleMapping.USER,
principalId: users[0].id
}, function(err, principal) {
if (err) throw err;
console.log('Created principal:', principal);
});
});
});
}
});
}
学生.json
"acls": [
{
"accessType": "*",
"principalType": "ROLE",
"principalId": "$everyone",
"permission": "DENY"
},
{
"accessType": "*",
"principalType": "ROLE",
"principalId": "$owner",//student to access their own information
"permission": "ALLOW"
},
{
"accessType": "EXECUTE",
"principalType": "ROLE",
"principalId": "admin",
"permission": "ALLOW",
"property": "find"
}
],
管理员.json
"acls": [
{
"accessType": "*",
"principalType": "ROLE",
"principalId": "$owner",
"permission": "ALLOW"
}
],
问题
登录在数据库中成功创建的管理员后(我使用托管在 mLab 中的 mongo),当我尝试让所有学生使用资源管理器时,我不断收到 401 ..“需要授权”
目标
我只希望管理员能够真正完全控制 API 端点。即管理员应该能够获得所有学生等。
解决方案
在大多数情况下,一个好的设计是让一个类 Member(或 Customer 或任何你喜欢的)扩展内置的 User 模型。
基本上,您注册两个成员,创建角色 admin 并提升两个成员之一的 admin。此处的示例: 多用户角色环回
输入的 aclsstudent.json
是正确的,并且可以正常工作。
如果你真的想有不同的模型来扩展内置的用户,在官方文档中有很好的解释多用户模型的访问控制:
推荐阅读
- angularjs - AngularJS ng-keyup 触发了两次
- android - FrameLayout 中的旋转图像未正确旋转
- mysql - 如何使用 LIKE 关键字和在 laravel 查询生成器中传递变量来制作自定义 orderBy 或 DB::raw(FIELD())
- python - 从列表列表中提取具有数字元素的列表
- javascript - 创建脚本标签shopify时缺少参数错误
- python - 自动将破折号添加到 Line Edit PyQt5
- highcharts - 优化 Highcharts 中饼图的大小
- android - 为什么这个 ConstraintLayout 中的 0dp 和 wrap_content 一样?
- jquery - 部分搜索 JSON
- php - 使用 PHP 和 YITH 批量编辑从 Regex 中排除一个单词